Skip to content

Commit

Permalink
Dcwither/option update (#20)
Browse files Browse the repository at this point in the history
* added ignore_identical input for cleaner PRs

* feature(reviews): add ability to update previous reviews
  • Loading branch information
dcwither authored Jun 21, 2020
1 parent 7c23fb6 commit 0203845
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 69 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"jest/globals": true
},
"rules": {
"no-console": "off"
"no-console": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error"
]
}
}
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ inputs:
skip_step:
required: false
description: 'which step to skip, either "install" or "build"'
update_review:
required: false
description: "will update the previous review if the status hasn't changed"
default: false
runs:
using: 'node12'
main: 'dist/index.js'
60 changes: 51 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2005,13 +2005,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = __webpack_require__(470);
const github_1 = __webpack_require__(469);
const core_1 = __webpack_require__(470);
const SizeLimit_1 = __importDefault(__webpack_require__(617));
const Term_1 = __importDefault(__webpack_require__(733));
// @ts-ignore
const markdown_table_1 = __importDefault(__webpack_require__(366));
const Term_1 = __importDefault(__webpack_require__(733));
const SizeLimit_1 = __importDefault(__webpack_require__(617));
const SIZE_LIMIT_URL = "https://github.com/ai/size-limit";
const SIZE_LIMIT_HEADING = `## [size-limit](${SIZE_LIMIT_URL}) report`;
const stateToEventMapping = {
COMMENTED: "COMMENT",
CHANGES_REQUESTED: "REQUEST_CHANGES"
};
function fetchPreviousReview(octokit, repo, pr) {
return __awaiter(this, void 0, void 0, function* () {
const reviews = yield octokit.paginate(
// TODO: replace with octokit.pulls.listReviews when upgraded to v17
"GET /repos/:owner/:repo/pulls/:pull_number/reviews", Object.assign(Object.assign({}, repo), {
// eslint-disable-next-line camelcase
pull_number: pr.number }));
const sizeLimitReviews = reviews.filter(review => review.body.startsWith(SIZE_LIMIT_HEADING));
return sizeLimitReviews.length > 0
? sizeLimitReviews[sizeLimitReviews.length - 1]
: null;
});
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
Expand All @@ -2021,6 +2039,7 @@ function run() {
throw new Error("No PR found. Only pull_request workflows are supported.");
}
const token = core_1.getInput("github_token");
const updateReview = core_1.getInput("updateReview");
const skipStep = core_1.getInput("skip_step");
const buildScript = core_1.getInput("build_script");
const octokit = new github_1.GitHub(token);
Expand All @@ -2040,14 +2059,37 @@ function run() {
}
const event = status > 0 ? "REQUEST_CHANGES" : "COMMENT";
const body = [
`## [size-limit](${SIZE_LIMIT_URL}) report`,
SIZE_LIMIT_HEADING,
markdown_table_1.default(limit.formatResults(base, current))
].join("\r\n");
let previousReview = null;
let isReviewStateChanged = true;
try {
octokit.pulls.createReview(Object.assign(Object.assign({}, repo), {
// eslint-disable-next-line camelcase
pull_number: pr.number, event,
body }));
previousReview = yield fetchPreviousReview(octokit, repo, pr);
isReviewStateChanged =
!previousReview || stateToEventMapping[previousReview.state] !== event;
if (!isReviewStateChanged && previousReview.body === body) {
// The last review was the exact same, so we shouldn't repeat
return;
}
}
catch (error) {
console.log("Failed to compare against previous reviews");
}
try {
if (updateReview && !isReviewStateChanged) {
yield octokit.pulls.updateReview(Object.assign(Object.assign({}, repo), {
// eslint-disable-next-line camelcase
pull_number: pr.number,
// eslint-disable-next-line camelcase
review_id: previousReview.id, body }));
}
else {
yield octokit.pulls.createReview(Object.assign(Object.assign({}, repo), {
// eslint-disable-next-line camelcase
pull_number: pr.number, event,
body }));
}
}
catch (error) {
console.log("Error creating PR review. This can happen for PR's originating from a fork without write permissions.");
Expand Down Expand Up @@ -2650,7 +2692,7 @@ exports.getUserAgent = getUserAgent;
/***/ 215:
/***/ (function(module) {

module.exports = {"_args":[["@octokit/[email protected]","/Users/andres.alvarez/Projects/size-limit-action"]],"_from":"@octokit/[email protected]","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{},"_requested":{"type":"version","registry":true,"raw":"@octokit/[email protected]","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/Users/andres.alvarez/Projects/size-limit-action","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};
module.exports = {"_args":[["@octokit/[email protected]","/Users/devinwitherspoon/Developer/size-limit-action"]],"_from":"@octokit/[email protected]","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{},"_requested":{"type":"version","registry":true,"raw":"@octokit/[email protected]","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"16.43.1","saveSpec":null,"fetchSpec":"16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_spec":"16.43.1","_where":"/Users/devinwitherspoon/Developer/size-limit-action","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};

/***/ }),

Expand Down
48 changes: 0 additions & 48 deletions src/SizeLimit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,4 @@ describe("SizeLimit", () => {
["dist/index.js", "98.53 KB (-9.92% 🔽)"]
]);
});

test("should format size-limit with new section", () => {
const limit = new SizeLimit();
const base = {
"dist/index.js": {
name: "dist/index.js",
size: 110894
}
};
const current = {
"dist/index.js": {
name: "dist/index.js",
size: 100894
},
"dist/new.js": {
name: "dist/new.js",
size: 100894
}
};

expect(limit.formatResults(base, current)).toEqual([
SizeLimit.SIZE_RESULTS_HEADER,
["dist/index.js", "98.53 KB (-9.92% 🔽)"],
["dist/new.js", "98.53 KB (+100% 🔺)"]
]);
});

test("should format size-limit with deleted section", () => {
const limit = new SizeLimit();
const base = {
"dist/index.js": {
name: "dist/index.js",
size: 110894
}
};
const current = {
"dist/new.js": {
name: "dist/new.js",
size: 100894
}
};

expect(limit.formatResults(base, current)).toEqual([
SizeLimit.SIZE_RESULTS_HEADER,
["dist/index.js", "0 B (-100%)"],
["dist/new.js", "98.53 KB (+100% 🔺)"]
]);
});
});
80 changes: 69 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
import { GitHub, context } from "@actions/github";
import { getInput, setFailed } from "@actions/core";
import { context, GitHub } from "@actions/github";

import { Octokit } from "@octokit/rest";
import SizeLimit from "./SizeLimit";
import Term from "./Term";
// @ts-ignore
import table from "markdown-table";
import Term from "./Term";
import SizeLimit from "./SizeLimit";

const SIZE_LIMIT_URL = "https://github.com/ai/size-limit";
const SIZE_LIMIT_HEADING = `## [size-limit](${SIZE_LIMIT_URL}) report`;

const stateToEventMapping: { [key: string]: any } = {
COMMENTED: "COMMENT",
CHANGES_REQUESTED: "REQUEST_CHANGES"
};

async function fetchPreviousReview(
octokit: GitHub,
repo: { owner: string; repo: string },
pr: { number: number }
) {
const reviews: Octokit.PullsListReviewsResponse = await octokit.paginate(
// TODO: replace with octokit.pulls.listReviews when upgraded to v17
"GET /repos/:owner/:repo/pulls/:pull_number/reviews",
{
...repo,
// eslint-disable-next-line camelcase
pull_number: pr.number
}
);

const sizeLimitReviews = reviews.filter(review =>
review.body.startsWith(SIZE_LIMIT_HEADING)
);
return sizeLimitReviews.length > 0
? sizeLimitReviews[sizeLimitReviews.length - 1]
: null;
}

async function run() {
try {
Expand All @@ -19,6 +50,7 @@ async function run() {
}

const token = getInput("github_token");
const updateReview = getInput("updateReview");
const skipStep = getInput("skip_step");
const buildScript = getInput("build_script");
const octokit = new GitHub(token);
Expand Down Expand Up @@ -51,18 +83,44 @@ async function run() {

const event = status > 0 ? "REQUEST_CHANGES" : "COMMENT";
const body = [
`## [size-limit](${SIZE_LIMIT_URL}) report`,
SIZE_LIMIT_HEADING,
table(limit.formatResults(base, current))
].join("\r\n");

let previousReview: Octokit.PullsListReviewsResponseItem | null = null;
let isReviewStateChanged = true;
try {
previousReview = await fetchPreviousReview(octokit, repo, pr);
isReviewStateChanged =
!previousReview || stateToEventMapping[previousReview.state] !== event;

if (!isReviewStateChanged && previousReview.body === body) {
// The last review was the exact same, so we shouldn't repeat
return;
}
} catch (error) {
console.log("Failed to compare against previous reviews");
}

try {
octokit.pulls.createReview({
...repo,
// eslint-disable-next-line camelcase
pull_number: pr.number,
event,
body
});
if (updateReview && !isReviewStateChanged) {
await octokit.pulls.updateReview({
...repo,
// eslint-disable-next-line camelcase
pull_number: pr.number,
// eslint-disable-next-line camelcase
review_id: previousReview.id,
body
});
} else {
await octokit.pulls.createReview({
...repo,
// eslint-disable-next-line camelcase
pull_number: pr.number,
event,
body
});
}
} catch (error) {
console.log(
"Error creating PR review. This can happen for PR's originating from a fork without write permissions."
Expand Down

0 comments on commit 0203845

Please sign in to comment.