Skip to content

Commit a366295

Browse files
Fix commit message in createPR (#343)
* Fix commit message in createPR Currently, draft and non-draft PRs will get the same commit message 'Create draft PR'. This commit fixes that so that non-draft PRs get the message 'Create PR' * Extend test and small cosmetic change Co-authored-by: Rob van der Leek <[email protected]>
1 parent 5d18921 commit a366295

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/github.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,18 @@ async function createPR (app, ctx, config, username, branchName) {
207207
const title = context.getIssueTitle(ctx)
208208
const issueNumber = context.getIssueNumber(ctx)
209209
const draft = Config.shouldOpenDraftPR(config)
210+
const draftText = draft ? 'draft ' : ''
210211
try {
211212
const commitSha = await getBranchHeadSha(ctx, branchName)
212213
const treeSha = await getCommitTreeSha(ctx, commitSha)
213-
const emptyCommitSha = await createCommit(ctx, commitSha, treeSha, username, 'Create draft PR')
214+
const emptyCommitSha = await createCommit(ctx, commitSha, treeSha, username, `Create ${draftText}PR`)
214215
await updateReference(ctx, branchName, emptyCommitSha)
215216
await ctx.octokit.pulls.create(
216217
{ owner, repo, head: branchName, base, title, body: `closes #${issueNumber}`, draft: draft })
217218
app.log(`Pull request created for branch ${branchName}`)
218219
} catch (e) {
219220
app.log(`Could not create draft PR (${e.message})`)
220-
await addComment(ctx, config, `Could not create draft PR (${e.message})`)
221+
await addComment(ctx, config, `Could not create ${draftText}PR (${e.message})`)
221222
}
222223
}
223224

tests/github.test.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,13 @@ test('log branch create errors with error level', async () => {
181181
expect(createComment).toBeCalled()
182182
})
183183

184-
test('create PR', async () => {
184+
test('create (draft) PR', async () => {
185185
const createPR = jest.fn()
186+
let capturedCommitMessage = ''
187+
const createCommit = ({ message }) => {
188+
capturedCommitMessage = message
189+
return ({ data: { sha: 'abcd1234' } })
190+
}
186191
const ctx = {
187192
payload: {
188193
repository: {
@@ -198,16 +203,13 @@ test('create PR', async () => {
198203
create: createPR
199204
}, //
200205
git: {
201-
getCommit: () => ({ data: { tree: { sha: '1234abcd' } } }),
202-
createCommit: () => ({ data: { sha: 'abcd1234' } }),
203-
updateRef: () => {}
206+
getCommit: () => ({ data: { tree: { sha: '1234abcd' } } }), createCommit: createCommit, updateRef: () => {}
204207
}
205208
}, //
206209
issue: () => {}
207210
}
208211

209-
await github.createPR({ log: (msg) => { console.log(msg) } }, ctx, { silent: false }, 'robvanderleek', 'issue-1')
210-
212+
await github.createPR({ log: () => { } }, ctx, { silent: false }, 'robvanderleek', 'issue-1')
211213
expect(createPR).toHaveBeenCalledWith({
212214
owner: 'robvanderleek',
213215
repo: 'create-issue-branch',
@@ -217,4 +219,17 @@ test('create PR', async () => {
217219
body: 'closes #1',
218220
title: 'Hello world'
219221
})
222+
expect(capturedCommitMessage).toBe('Create PR')
223+
await github.createPR({ log: () => { } }, ctx, { silent: false, openDraftPR: true },
224+
'robvanderleek', 'issue-1')
225+
expect(createPR).toHaveBeenCalledWith({
226+
owner: 'robvanderleek',
227+
repo: 'create-issue-branch',
228+
draft: true,
229+
base: undefined,
230+
head: 'issue-1',
231+
body: 'closes #1',
232+
title: 'Hello world'
233+
})
234+
expect(capturedCommitMessage).toBe('Create draft PR')
220235
})

0 commit comments

Comments
 (0)