From 865b9820ee8ceef3ac1cd6bde2addf6968d91456 Mon Sep 17 00:00:00 2001 From: sinchang Date: Thu, 17 Jan 2019 11:51:20 +0800 Subject: [PATCH] refactor --- src/Repository/index.js | 90 ++++++++++++------- src/processIssueComment.js | 27 ++++-- .../__snapshots__/index.test.js.snap | 18 ++-- test/Repository/index.test.js | 6 +- test/__snapshots__/index-e2e.test.js.snap | 32 +++---- test/index-e2e.test.js | 49 ++++++++-- 6 files changed, 143 insertions(+), 79 deletions(-) diff --git a/src/Repository/index.js b/src/Repository/index.js index 7cd90f3b..69c2774b 100644 --- a/src/Repository/index.js +++ b/src/Repository/index.js @@ -1,16 +1,25 @@ -const { ResourceNotFoundError } = require('../utils/errors') +const { + ResourceNotFoundError, + AllContributorBotError, +} = require('../utils/errors') class Repository { - constructor({ repo, owner, github }) { + constructor({ repo, owner, github, defaultBranch }) { this.github = github this.repo = repo this.owner = owner + this.defaultBranch = defaultBranch + this.basedBranch } getFullname() { return `${this.owner}/${this.repo}` } + setBasedBranch(branchName) { + this.basedBranch = branchName + } + async getFile(filePath) { // https://octokit.github.io/rest.js/#api-Repos-getContents let file @@ -19,6 +28,7 @@ class Repository { owner: this.owner, repo: this.repo, path: filePath, + ref: this.basedBranch, }) } catch (error) { if (error.code === 404) { @@ -61,25 +71,33 @@ class Repository { return multipleFilesByPath } - async getHeadRef(defaultBranch) { + async getHeadRef(branchName) { const result = await this.github.git.getRef({ owner: this.owner, repo: this.repo, - ref: `heads/${defaultBranch}`, + ref: `heads/${branchName}`, }) return result.data.object.sha } - async createBranch({ branchName, defaultBranch }) { - const fromSha = await this.getHeadRef(defaultBranch) + async createBranch(branchName) { + const fromSha = await this.getHeadRef(this.defaultBranch) - // https://octokit.github.io/rest.js/#api-Git-createRef - await this.github.git.createRef({ - owner: this.owner, - repo: this.repo, - ref: `refs/heads/${branchName}`, - sha: fromSha, - }) + try { + // https://octokit.github.io/rest.js/#api-Git-createRef + await this.github.git.createRef({ + owner: this.owner, + repo: this.repo, + ref: `refs/heads/${branchName}`, + sha: fromSha, + }) + + return false + } catch (error) { + // branch already exists + if (error.code === 422) return true + throw error + } } async updateFile({ filePath, content, branchName, originalSha }) { @@ -140,40 +158,44 @@ class Repository { await Promise.all(createOrUpdateFilesMultiple) } - async createPullRequest({ title, body, branchName, defaultBranch }) { - const result = await this.github.pulls.create({ - owner: this.owner, - repo: this.repo, - title, - body, - head: branchName, - base: defaultBranch, - maintainer_can_modify: true, - }) - return result.data.html_url + async createPullRequest({ title, body, branchName }) { + try { + const result = await this.github.pulls.create({ + owner: this.owner, + repo: this.repo, + title, + body, + head: branchName, + base: this.defaultBranch, + maintainer_can_modify: true, + }) + return { pullRequestURL: result.data.html_url, result: true } + } catch (error) { + // pull request is already open + if (error.code === 422) return { pullRequestURL: '', result: false } + throw error + } } - async createPullRequestFromFiles({ - title, - body, - filesByPath, - branchName, - defaultBranch, - }) { - await this.createBranch({ branchName, defaultBranch }) + async createPullRequestFromFiles({ title, body, filesByPath, branchName }) { + if (this.basedBranch === this.defaultBranch) + this.createBranch(branchName) await this.createOrUpdateFiles({ filesByPath, branchName, }) - const pullRequestURL = await this.createPullRequest({ + const { pullRequestURL, result } = await this.createPullRequest({ title, body, branchName, - defaultBranch, }) + // TODO + if (!result) + throw new AllContributorBotError('Pull request is already open') + return pullRequestURL } } diff --git a/src/processIssueComment.js b/src/processIssueComment.js index 342dd19c..861b87cc 100644 --- a/src/processIssueComment.js +++ b/src/processIssueComment.js @@ -20,7 +20,6 @@ async function processAddContributor({ optionsConfig, who, contributions, - defaultBranch, }) { const { name, avatar_url, profile } = await getUserDetails({ github: context.github, @@ -56,7 +55,6 @@ async function processAddContributor({ )}.\n\nThis was requested by ${commentReply.replyingToWho()} [in this comment](${commentReply.replyingToWhere()})`, filesByPath: filesByPathToUpdate, branchName: `all-contributors/add-${who}`, - defaultBranch, }) commentReply.reply( @@ -65,14 +63,29 @@ async function processAddContributor({ } async function processIssueComment({ context, commentReply }) { + const commentBody = context.payload.comment.body + const { who, action, contributions } = parseComment(commentBody) + const branchName = `all-contributors/add-${who}` + const defaultBranch = context.payload.repository.default_branch const repository = new Repository({ ...context.repo(), github: context.github, + defaultBranch, }) + + try { + await repository.getHeadRef(branchName) + repository.setBasedBranch(branchName) + } catch (error) { + if (error.code !== 404) throw error + repository.setBasedBranch(defaultBranch) + } + const optionsConfig = new OptionsConfig({ repository, commentReply, }) + try { await optionsConfig.fetch() } catch (error) { @@ -83,18 +96,14 @@ async function processIssueComment({ context, commentReply }) { } } - const commentBody = context.payload.comment.body - const parsedComment = parseComment(commentBody) - - if (parsedComment.action === 'add') { + if (action === 'add') { await processAddContributor({ context, commentReply, repository, optionsConfig, - who: parsedComment.who, - contributions: parsedComment.contributions, - defaultBranch: context.payload.repository.default_branch, + who, + contributions, }) return } diff --git a/test/Repository/__snapshots__/index.test.js.snap b/test/Repository/__snapshots__/index.test.js.snap index af5260da..9b096401 100644 --- a/test/Repository/__snapshots__/index.test.js.snap +++ b/test/Repository/__snapshots__/index.test.js.snap @@ -9,13 +9,6 @@ Object { `; exports[`Repository createPullRequest with files 1`] = ` -Object { - "ref": "refs/heads/all-contributors/add-jakebolam", - "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", -} -`; - -exports[`Repository createPullRequest with files 2`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "eyJ0ZXN0IjoidGVzdCBjb250ZW50In0=", @@ -24,7 +17,7 @@ Object { } `; -exports[`Repository createPullRequest with files 3`] = ` +exports[`Repository createPullRequest with files 2`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "VXBkYXRlZCBsaXN0", @@ -33,7 +26,7 @@ Object { } `; -exports[`Repository createPullRequest with files 4`] = ` +exports[`Repository createPullRequest with files 3`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "VXBkYXRlZCBsaXN0IGluIG5lc3RlZCBmb2xkZXI=", @@ -42,6 +35,13 @@ Object { } `; +exports[`Repository createPullRequest with files 4`] = ` +Object { + "ref": "refs/heads/all-contributors/add-jakebolam", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", +} +`; + exports[`Repository createPullRequest with files 5`] = ` Object { "base": "master", diff --git a/test/Repository/index.test.js b/test/Repository/index.test.js index 7b366c02..e76a9836 100644 --- a/test/Repository/index.test.js +++ b/test/Repository/index.test.js @@ -13,6 +13,7 @@ describe('Repository', () => { repo: 'all-contributors-bot', owner: 'all-contributors', github: mockGithub, + defaultBranch: 'master', }) const verifyBody = body => { @@ -36,9 +37,11 @@ describe('Repository', () => { }) test('createPullRequest with files', async () => { + const basedBranch = 'master' + repository.setBasedBranch(basedBranch) nock('https://api.github.com') .get( - `/repos/all-contributors/all-contributors-bot/git/refs/heads/master`, + `/repos/all-contributors/all-contributors-bot/git/refs/heads/${basedBranch}`, ) .reply(200, gitGetRefdata) @@ -95,7 +98,6 @@ describe('Repository', () => { }, }, branchName: 'all-contributors/add-jakebolam', - defaultBranch: 'master', }) expect(pullRequestNumber).toEqual( 'https://github.com/all-contributors/all-contributors-bot/pull/1347', diff --git a/test/__snapshots__/index-e2e.test.js.snap b/test/__snapshots__/index-e2e.test.js.snap index 8fd7d46f..a49a6482 100644 --- a/test/__snapshots__/index-e2e.test.js.snap +++ b/test/__snapshots__/index-e2e.test.js.snap @@ -25,13 +25,6 @@ Could not find the user jakebolam on github.", `; exports[`All Contributors app - End to end Happy path, add correct new contributor 1`] = ` -Object { - "ref": "refs/heads/all-contributors/add-jakebolam", - "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", -} -`; - -exports[`All Contributors app - End to end Happy path, add correct new contributor 2`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "IyBBbGxDb250cmlidXRvcnNCb3QKQSBib3QgZm9yIGF1dG9tYXRpY2FsbHkgYWRkaW5nIGFsbC1jb250cmlidXRvcnMuIPCfpJYKClshW0J1aWxkXShodHRwczovL2ltZy5zaGllbGRzLmlvL2NpcmNsZWNpL3Byb2plY3QvZ2l0aHViL2FsbC1jb250cmlidXRvcnMvYW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hhbmtzIGdvZXMgdG8gdGhlc2Ugd29uZGVyZnVsIHBlb3BsZSAoW2Vtb2ppIGtleV0oaHR0cHM6Ly9naXRodWIuY29tL2FsbC1jb250cmlidXRvcnMvYWxsLWNvbnRyaWJ1dG9ycyNlbW9qaS1rZXkpKToKCjwhLS0gQUxMLUNPTlRSSUJVVE9SUy1MSVNUOlNUQVJUIC0gRG8gbm90IHJlbW92ZSBvciBtb2RpZnkgdGhpcyBzZWN0aW9uIC0tPgo8IS0tIHByZXR0aWVyLWlnbm9yZSAtLT4KfCBbPGltZyBzcmM9Imh0dHBzOi8vYXZhdGFyczIuZ2l0aHVidXNlcmNvbnRlbnQuY29tL3UvMzUzNDIzNj92PTQiIHdpZHRoPSIxMDBweDsiIGFsdD0iSmFrZSBCb2xhbSIvPjxiciAvPjxzdWI+PGI+SmFrZSBCb2xhbTwvYj48L3N1Yj5dKGh0dHBzOi8vamFrZWJvbGFtLmNvbSk8YnIgLz5b8J+Su10oaHR0cHM6Ly9naXRodWIuY29tL2FsbC1jb250cmlidHVvcnMvYm90L2NvbW1pdHM/YXV0aG9yPWpha2Vib2xhbSAiQ29kZSIpIFvwn5qHXSgjaW5mcmEtamFrZWJvbGFtICJJbmZyYXN0cnVjdHVyZSAoSG9zdGluZywgQnVpbGQtVG9vbHMsIGV0YykiKSB8CnwgOi0tLTogfAo8IS0tIEFMTC1DT05UUklCVVRPUlMtTElTVDpFTkQgLS0+CgpUaGlzIHByb2plY3QgZm9sbG93cyB0aGUgW2FsbC1jb250cmlidXRvcnNdKGh0dHBzOi8vZ2l0aHViLmNvbS9hbGwtY29udHJpYnV0b3JzL2FsbC1jb250cmlidXRvcnMpIHNwZWNpZmljYXRpb24uIENvbnRyaWJ1dGlvbnMgb2YgYW55IGtpbmQgd2VsY29tZQoKIyMgTElDRU5TRQoKW01JVF0oTElDRU5TRSkK", @@ -40,7 +33,7 @@ Object { } `; -exports[`All Contributors app - End to end Happy path, add correct new contributor 3`] = ` +exports[`All Contributors app - End to end Happy path, add correct new contributor 2`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "ewogICJwcm9qZWN0TmFtZSI6ICJib3QiLAogICJwcm9qZWN0T3duZXIiOiAiYWxsLWNvbnRyaWJ0dW9ycyIsCiAgInJlcG9UeXBlIjogImdpdGh1YiIsCiAgInJlcG9Ib3N0IjogImh0dHBzOi8vZ2l0aHViLmNvbSIsCiAgImZpbGVzIjogWwogICAgIlJFQURNRS5tZCIKICBdLAogICJpbWFnZVNpemUiOiAxMDAsCiAgImNvbW1pdCI6IGZhbHNlLAogICJjb250cmlidXRvcnMiOiBbCiAgICB7CiAgICAgICJsb2dpbiI6ICJqYWtlYm9sYW0iLAogICAgICAibmFtZSI6ICJKYWtlIEJvbGFtIiwKICAgICAgImF2YXRhcl91cmwiOiAiaHR0cHM6Ly9hdmF0YXJzMi5naXRodWJ1c2VyY29udGVudC5jb20vdS8zNTM0MjM2P3Y9NCIsCiAgICAgICJwcm9maWxlIjogImh0dHBzOi8vamFrZWJvbGFtLmNvbSIsCiAgICAgICJjb250cmlidXRpb25zIjogWwogICAgICAgICJjb2RlIiwKICAgICAgICAiaW5mcmEiCiAgICAgIF0KICAgIH0KICBdLAogICJjb250cmlidXRvcnNQZXJMaW5lIjogNwp9Cg==", @@ -49,6 +42,13 @@ Object { } `; +exports[`All Contributors app - End to end Happy path, add correct new contributor 3`] = ` +Object { + "ref": "refs/heads/all-contributors/add-jakebolam", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", +} +`; + exports[`All Contributors app - End to end Happy path, add correct new contributor 4`] = ` Object { "base": "master", @@ -70,13 +70,6 @@ I've put up [a pull request](https://github.com/all-contributors/all-contributor `; exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 1`] = ` -Object { - "ref": "refs/heads/all-contributors/add-jakebolam", - "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", -} -`; - -exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first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c10oaHR0cHM6Ly9naXRodWIuY29tL2FsbC1jb250cmlidXRvcnMvYWxsLWNvbnRyaWJ1dG9ycykgc3BlY2lmaWNhdGlvbi4gQ29udHJpYnV0aW9ucyBvZiBhbnkga2luZCB3ZWxjb21lCgojIyBMSUNFTlNFCgpbTUlUXShMSUNFTlNFKQo=", @@ -85,7 +78,7 @@ Object { } `; -exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 3`] = ` +exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 2`] = ` Object { "branch": "all-contributors/add-jakebolam", "content": "ewogICJwcm9qZWN0TmFtZSI6ICJhbGwtY29udHJpYnV0b3JzLWJvdCIsCiAgInByb2plY3RPd25lciI6ICJhbGwtY29udHJpYnV0b3JzIiwKICAicmVwb1R5cGUiOiAiZ2l0aHViIiwKICAicmVwb0hvc3QiOiAiaHR0cHM6Ly9naXRodWIuY29tIiwKICAiZmlsZXMiOiBbCiAgICAiUkVBRE1FLm1kIgogIF0sCiAgImltYWdlU2l6ZSI6IDEwMCwKICAiY29tbWl0IjogZmFsc2UsCiAgImNvbnRyaWJ1dG9ycyI6IFsKICAgIHsKICAgICAgImxvZ2luIjogImpha2Vib2xhbSIsCiAgICAgICJuYW1lIjogIkpha2UgQm9sYW0iLAogICAgICAiYXZhdGFyX3VybCI6ICJodHRwczovL2F2YXRhcnMyLmdpdGh1YnVzZXJjb250ZW50LmNvbS91LzM1MzQyMzY/dj00IiwKICAgICAgInByb2ZpbGUiOiAiaHR0cHM6Ly9qYWtlYm9sYW0uY29tIiwKICAgICAgImNvbnRyaWJ1dGlvbnMiOiBbCiAgICAgICAgImNvZGUiLAogICAgICAgICJkb2MiLAogICAgICAgICJpbmZyYSIKICAgICAgXQogICAgfQogIF0sCiAgImNvbnRyaWJ1dG9yc1BlckxpbmUiOiA3Cn0K", @@ -93,6 +86,13 @@ Object { } `; +exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 3`] = ` +Object { + "ref": "refs/heads/all-contributors/add-jakebolam", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", +} +`; + exports[`All Contributors app - End to end Happy path, add correct new contributor, no allcontributors file (repo needs init first) 4`] = ` Object { "base": "master", diff --git a/test/index-e2e.test.js b/test/index-e2e.test.js index b3bfc97a..21b558d1 100644 --- a/test/index-e2e.test.js +++ b/test/index-e2e.test.js @@ -31,14 +31,19 @@ describe('All Contributors app - End to end', () => { test('Happy path, add correct new contributor', async () => { jest.setTimeout(10000) - nock('https://api.github.com') .post('/app/installations/11111/access_tokens') .reply(200, { token: 'test' }) nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc', + `/repos/all-contributors/all-contributors-bot/git/refs/heads/all-contributors/add-jakebolam`, + ) + .reply(404, gitGetRefdata) + + nock('https://api.github.com') + .get( + '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master', ) .reply(200, reposGetContentsAllContributorsRCdata) @@ -48,7 +53,7 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/README.md', + '/repos/all-contributors/all-contributors-bot/contents/README.md?ref=master', ) .reply(200, reposGetContentsREADMEMDdata) @@ -115,7 +120,13 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc', + `/repos/all-contributors/all-contributors-bot/git/refs/heads/all-contributors/add-jakebolam`, + ) + .reply(404, gitGetRefdata) + + nock('https://api.github.com') + .get( + '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master', ) .reply(404) @@ -125,7 +136,7 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/README.md', + '/repos/all-contributors/all-contributors-bot/contents/README.md?ref=master', ) .reply(200, reposGetContentsREADMEMDdata) @@ -184,13 +195,21 @@ describe('All Contributors app - End to end', () => { }) test('Fail path, no readme file (configuration error)', async () => { + jest.setTimeout(10000) + nock('https://api.github.com') .post('/app/installations/11111/access_tokens') .reply(200, { token: 'test' }) nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc', + `/repos/all-contributors/all-contributors-bot/git/refs/heads/all-contributors/add-jakebolam`, + ) + .reply(404, gitGetRefdata) + + nock('https://api.github.com') + .get( + '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master', ) .reply(200, reposGetContentsAllContributorsRCdata) @@ -200,7 +219,7 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/README.md', + '/repos/all-contributors/all-contributors-bot/contents/README.md?ref=master', ) .reply(404) @@ -224,7 +243,13 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc', + `/repos/all-contributors/all-contributors-bot/git/refs/heads/all-contributors/add-jakebolam`, + ) + .reply(404, gitGetRefdata) + + nock('https://api.github.com') + .get( + '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master', ) .reply(200, reposGetContentsAllContributorsRCdata) @@ -254,7 +279,13 @@ describe('All Contributors app - End to end', () => { nock('https://api.github.com') .get( - '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc', + `/repos/all-contributors/all-contributors-bot/git/refs/heads/all-contributors/add-jakebolam`, + ) + .reply(404, gitGetRefdata) + + nock('https://api.github.com') + .get( + '/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master', ) .reply(500)