Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,18 @@ async function createPR (app, ctx, config, username, branchName) {
const title = context.getIssueTitle(ctx)
const issueNumber = context.getIssueNumber(ctx)
const draft = Config.shouldOpenDraftPR(config)
const draftText = draft ? 'draft ' : ''
try {
const commitSha = await getBranchHeadSha(ctx, branchName)
const treeSha = await getCommitTreeSha(ctx, commitSha)
const emptyCommitSha = await createCommit(ctx, commitSha, treeSha, username, 'Create draft PR')
const emptyCommitSha = await createCommit(ctx, commitSha, treeSha, username, `Create ${draftText}PR`)
await updateReference(ctx, branchName, emptyCommitSha)
await ctx.octokit.pulls.create(
{ owner, repo, head: branchName, base, title, body: `closes #${issueNumber}`, draft: draft })
app.log(`Pull request created for branch ${branchName}`)
} catch (e) {
app.log(`Could not create draft PR (${e.message})`)
await addComment(ctx, config, `Could not create draft PR (${e.message})`)
await addComment(ctx, config, `Could not create ${draftText}PR (${e.message})`)
}
}

Expand Down
27 changes: 21 additions & 6 deletions tests/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ test('log branch create errors with error level', async () => {
expect(createComment).toBeCalled()
})

test('create PR', async () => {
test('create (draft) PR', async () => {
const createPR = jest.fn()
let capturedCommitMessage = ''
const createCommit = ({ message }) => {
capturedCommitMessage = message
return ({ data: { sha: 'abcd1234' } })
}
const ctx = {
payload: {
repository: {
Expand All @@ -198,16 +203,13 @@ test('create PR', async () => {
create: createPR
}, //
git: {
getCommit: () => ({ data: { tree: { sha: '1234abcd' } } }),
createCommit: () => ({ data: { sha: 'abcd1234' } }),
updateRef: () => {}
getCommit: () => ({ data: { tree: { sha: '1234abcd' } } }), createCommit: createCommit, updateRef: () => {}
}
}, //
issue: () => {}
}

await github.createPR({ log: (msg) => { console.log(msg) } }, ctx, { silent: false }, 'robvanderleek', 'issue-1')

await github.createPR({ log: () => { } }, ctx, { silent: false }, 'robvanderleek', 'issue-1')
expect(createPR).toHaveBeenCalledWith({
owner: 'robvanderleek',
repo: 'create-issue-branch',
Expand All @@ -217,4 +219,17 @@ test('create PR', async () => {
body: 'closes #1',
title: 'Hello world'
})
expect(capturedCommitMessage).toBe('Create PR')
await github.createPR({ log: () => { } }, ctx, { silent: false, openDraftPR: true },
'robvanderleek', 'issue-1')
expect(createPR).toHaveBeenCalledWith({
owner: 'robvanderleek',
repo: 'create-issue-branch',
draft: true,
base: undefined,
head: 'issue-1',
body: 'closes #1',
title: 'Hello world'
})
expect(capturedCommitMessage).toBe('Create draft PR')
})