diff --git a/src/github.js b/src/github.js index d200030a..d21feb8d 100644 --- a/src/github.js +++ b/src/github.js @@ -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})`) } } diff --git a/tests/github.test.js b/tests/github.test.js index 856b265a..5a2c10fc 100644 --- a/tests/github.test.js +++ b/tests/github.test.js @@ -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: { @@ -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', @@ -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') })