diff --git a/.gitignore b/.gitignore index e94e32b..82bfc13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.tgz +coverage/ node_modules/ diff --git a/src/GitHubClient.js b/src/GitHubClient.js index ac0af2a..5cc9cbd 100644 --- a/src/GitHubClient.js +++ b/src/GitHubClient.js @@ -83,13 +83,24 @@ 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", @@ -97,19 +108,15 @@ function makeReview(diff, makeClient = makeGitHubClient) { 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 = { diff --git a/tests/suggest.test.js b/tests/suggest.test.js index 708422e..9c0688e 100644 --- a/tests/suggest.test.js +++ b/tests/suggest.test.js @@ -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(