Skip to content

Commit

Permalink
feat: create pull request on the default branches (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinchang authored and jakebolam committed Jan 16, 2019
1 parent bc42436 commit b03b402
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand All @@ -165,6 +171,7 @@ class Repository {
title,
body,
branchName,
defaultBranch,
})

return pullRequestURL
Expand Down
3 changes: 3 additions & 0 deletions src/processIssueComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function processAddContributor({
optionsConfig,
who,
contributions,
defaultBranch,
}) {
const { name, avatar_url, profile } = await getUserDetails({
github: context.github,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -92,6 +94,7 @@ async function processIssueComment({ context, commentReply }) {
optionsConfig,
who: parsedComment.who,
contributions: parsedComment.contributions,
defaultBranch: context.payload.repository.default_branch,
})
return
}
Expand Down
1 change: 1 addition & 0 deletions test/Repository/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit b03b402

Please # to comment.