Skip to content

Commit

Permalink
fix: Skip invalid diffs (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Jul 23, 2020
1 parent 73d7c1c commit 828ca61
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.tgz
coverage/
node_modules/
39 changes: 23 additions & 16 deletions src/GitHubClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,40 @@ function makeReview(diff, makeClient = makeGitHubClient) {
const parse = require("parse-diff");
const files = parse(diff);
if (files.length <= 0) {
return process.exit(0);
return Promise.resolve();
}

const { GITHUB_REPOSITORY, GITHUB_TOKEN } = process.env;

const octokit = makeClient({ auth: GITHUB_TOKEN });
const comments = files.reduce((comments, file) => {
const { chunks, to } = file;
if (chunks.length === 0) {
return comments;
}
return chunks.reduce((comments, chunk) => {
comments.push(makeComment(to, chunk));
return comments;
}, comments);
}, []);
if (comments.length === 0) {
return Promise.resolve();
}

const { GITHUB_REPOSITORY, GITHUB_TOKEN } = process.env;
const [owner, repo] = GITHUB_REPOSITORY.split("/");
const review = {
accept: "application/vnd.github.comfort-fade-preview+json",
owner,
repo,
pull_number: makeClient === makeGitHubClient ? getPullRequestNumber() : 0,
event: "COMMENT",
comments: files.reduce((comments, file) => {
const { chunks, to } = file;
return chunks.reduce((comments, chunk) => {
comments.push(makeComment(to, chunk));
return comments;
}, comments);
}, []),
comments,
};
octokit.pulls.createReview(review).catch((e) => {
console.error(e);
console.dir(review, undefined, { depth: null });
return process.exit(1);
});
return makeClient({ auth: GITHUB_TOKEN })
.pulls.createReview(review)
.catch((e) => {
console.error(e);
console.dir(review, undefined, { depth: null });
return process.exit(1);
});
}

module.exports = {
Expand Down
17 changes: 17 additions & 0 deletions tests/suggest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ describe("suggest", () => {
env.GITHUB_REPOSITORY = env.GITHUB_REPOSITORY || "tido64/suggestion-bot";
});

test("skips invalid diffs", async () => {
let payload = undefined;
const createReview = (review) => {
payload = review;
return Promise.resolve();
};

await makeReview("", makeMockClient(createReview));
expect(payload).toBeUndefined();

await makeReview(
"diff --git a/src/Graphics/TextureAllocator.gl.h b/src/Graphics/TextureAllocator.gl.h",
makeMockClient(createReview)
);
expect(payload).toBeUndefined();
});

test("supports unified diffs", async () => {
let payload = undefined;
await makeReview(
Expand Down

0 comments on commit 828ca61

Please sign in to comment.