From b03b4029a06b9183eb6c8b92f727c436023c2f50 Mon Sep 17 00:00:00 2001 From: Jeff Wen Date: Wed, 16 Jan 2019 13:32:06 +0800 Subject: [PATCH] feat: create pull request on the default branches (#46) --- src/Repository/index.js | 23 +++++++++++++++-------- src/processIssueComment.js | 3 +++ test/Repository/index.test.js | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Repository/index.js b/src/Repository/index.js index 124a8d28..7cd90f3b 100644 --- a/src/Repository/index.js +++ b/src/Repository/index.js @@ -61,17 +61,17 @@ class Repository { return multipleFilesByPath } - async getHeadRef() { + async getHeadRef(defaultBranch) { const result = await this.github.git.getRef({ owner: this.owner, repo: this.repo, - ref: `heads/master`, + ref: `heads/${defaultBranch}`, }) return result.data.object.sha } - async createBranch(branchName) { - const fromSha = await this.getHeadRef() + async createBranch({ branchName, defaultBranch }) { + const fromSha = await this.getHeadRef(defaultBranch) // https://octokit.github.io/rest.js/#api-Git-createRef await this.github.git.createRef({ @@ -140,21 +140,27 @@ class Repository { await Promise.all(createOrUpdateFilesMultiple) } - async createPullRequest({ title, body, branchName }) { + async createPullRequest({ title, body, branchName, defaultBranch }) { const result = await this.github.pulls.create({ owner: this.owner, repo: this.repo, title, body, head: branchName, - base: 'master', + base: defaultBranch, maintainer_can_modify: true, }) return result.data.html_url } - async createPullRequestFromFiles({ title, body, filesByPath, branchName }) { - await this.createBranch(branchName) + async createPullRequestFromFiles({ + title, + body, + filesByPath, + branchName, + defaultBranch, + }) { + await this.createBranch({ branchName, defaultBranch }) await this.createOrUpdateFiles({ filesByPath, @@ -165,6 +171,7 @@ class Repository { title, body, branchName, + defaultBranch, }) return pullRequestURL diff --git a/src/processIssueComment.js b/src/processIssueComment.js index 6ceb841c..342dd19c 100644 --- a/src/processIssueComment.js +++ b/src/processIssueComment.js @@ -20,6 +20,7 @@ async function processAddContributor({ optionsConfig, who, contributions, + defaultBranch, }) { const { name, avatar_url, profile } = await getUserDetails({ github: context.github, @@ -55,6 +56,7 @@ 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( @@ -92,6 +94,7 @@ async function processIssueComment({ context, commentReply }) { optionsConfig, who: parsedComment.who, contributions: parsedComment.contributions, + defaultBranch: context.payload.repository.default_branch, }) return } diff --git a/test/Repository/index.test.js b/test/Repository/index.test.js index ae548ccc..7b366c02 100644 --- a/test/Repository/index.test.js +++ b/test/Repository/index.test.js @@ -95,6 +95,7 @@ describe('Repository', () => { }, }, branchName: 'all-contributors/add-jakebolam', + defaultBranch: 'master', }) expect(pullRequestNumber).toEqual( 'https://github.com/all-contributors/all-contributors-bot/pull/1347',