Skip to content

Commit

Permalink
Undo changes of #20 in favor of #25
Browse files Browse the repository at this point in the history
  • Loading branch information
andresz1 committed Jun 21, 2020
1 parent 0203845 commit d8dfd12
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 78 deletions.
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"jest/globals": true
},
"rules": {
"no-console": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error"
]
"no-console": "off"
}
}
4 changes: 0 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ 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'
48 changes: 48 additions & 0 deletions src/SizeLimit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,52 @@ 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: 11 additions & 69 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
import { GitHub, context } from "@actions/github";
import { getInput, setFailed } from "@actions/core";

import { Octokit } from "@octokit/rest";
import SizeLimit from "./SizeLimit";
import Term from "./Term";
import { context, GitHub } from "@actions/github";
// @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 @@ -50,7 +19,6 @@ 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 @@ -83,44 +51,18 @@ async function run() {

const event = status > 0 ? "REQUEST_CHANGES" : "COMMENT";
const body = [
SIZE_LIMIT_HEADING,
`## [size-limit](${SIZE_LIMIT_URL}) report`,
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 {
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
});
}
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 d8dfd12

Please sign in to comment.