Skip to content

Commit d45e8bf

Browse files
fix: 🐛 Conventional Pull Requests style isn’t working (#1104)
1 parent 5784a01 commit d45e8bf

13 files changed

+175
-105
lines changed

src/github.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,11 @@ export async function createPr(app: Probot, ctx: Context<any>, config: Config, u
263263
const owner = getRepoOwnerLogin(ctx);
264264
const repo = getRepoName(ctx);
265265
const base = getPrTargetBranch(ctx, config);
266-
const title = getIssueTitle(ctx);
266+
const issueTitle = getIssueTitle(ctx);
267+
const issueLabels = getIssueLabels(ctx);
268+
const prTitle = createPrTitle(config, issueTitle, issueLabels);
267269
const draft = config.openDraftPR;
268-
const logContext: any = {owner, repo, branchName, base, title, draft, username};
270+
const logContext: any = {owner, repo, branchName, base, prTitle, draft, username};
269271
try {
270272
const baseHeadSha = await getBranchHeadSha(ctx, base);
271273
logContext.baseHeadSha = baseHeadSha;
@@ -276,8 +278,16 @@ export async function createPr(app: Probot, ctx: Context<any>, config: Config, u
276278
logContext.emptyCommitResponse = await createEmptyCommit(ctx, branchName, getCommitText(ctx, config), String(branchHeadSha));
277279
}
278280
const {data: pr} = await ctx.octokit.pulls.create(
279-
{owner, repo, head: branchName, base, title, body: await getPrBody(app, ctx, config), draft: draft})
280-
app.log.info(`${draft ? 'Created draft' : 'Created'} pull request ${pr.number} for branch ${branchName}`)
281+
{
282+
owner,
283+
repo,
284+
head: branchName,
285+
base,
286+
title: prTitle,
287+
body: await getPrBody(app, ctx, config),
288+
draft: draft
289+
});
290+
app.log.info(`${draft ? 'Created draft' : 'Created'} pull request ${pr.number} for branch ${branchName}`);
281291
await copyIssueAttributesToPr(app, ctx, config, pr);
282292
} catch (e: any) {
283293
app.log.info(`Could not create PR (${e.message})`);
@@ -459,15 +469,23 @@ async function queryProjectIdsForIssue(ctx: Context<any>) {
459469
return result
460470
}
461471

472+
export function createPrTitle(config: Config, issueTitle: string, labels: Array<string>) {
473+
if (config.conventionalPrTitles) {
474+
const conventionalPrefix = getConventionalPrTitlePrefix(config, labels);
475+
return conventionalPrefix + ' ' + issueTitle;
476+
} else {
477+
return issueTitle;
478+
}
479+
}
480+
462481
export async function updatePrTitle(app: Probot, ctx: Context<any>, config: Config, pr: any, issueTitle: string, labels: Array<string>) {
463-
const owner = getRepoOwnerLogin(ctx)
464-
const repo = getRepoName(ctx)
465-
const pullNumber = pr.number
466-
const conventionalPrefix = getConventionalPrTitlePrefix(config, labels)
467-
const updatedTitle = conventionalPrefix + ' ' + issueTitle
482+
const updatedTitle = createPrTitle(config, issueTitle, labels);
468483
if (updatedTitle !== pr.title) {
469-
app.log.info(`Updating prefix for PR #${pullNumber} in ${owner}/${repo} to: ${conventionalPrefix}`)
470-
await ctx.octokit.pulls.update({owner: owner, repo: repo, pull_number: pullNumber, title: updatedTitle})
484+
const owner = getRepoOwnerLogin(ctx);
485+
const repo = getRepoName(ctx);
486+
const pullNumber = pr.number;
487+
app.log.info(`Updating title for PR #${pullNumber} in ${owner}/${repo} to: ${updatedTitle}`);
488+
await ctx.octokit.pulls.update({owner: owner, repo: repo, pull_number: pullNumber, title: updatedTitle});
471489
}
472490
}
473491

tests/config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ test('get default branch', () => {
132132

133133
expect(getDefaultBranch(config)).toBe('main');
134134
})
135+
135136
test('get PR title prefix for issue label', () => {
136137
const config = getDefaultConfig();
137138

tests/github.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as github from "../src/github";
2+
import {createPrTitle} from "../src/github";
23
import {formatAsExpandingMarkdown} from "../src/utils";
34
import {getDefaultConfig} from "../src/entities/Config";
45
import {getDefaultContext, initNock, initProbot} from "./test-helpers";
@@ -261,7 +262,7 @@ test('create (draft) PR', async () => {
261262
owner: 'robvanderleek',
262263
repo: 'create-issue-branch',
263264
draft: false,
264-
base: 'master',
265+
base: 'main',
265266
head: 'issue-1',
266267
body: 'closes #1',
267268
title: 'Hello world'
@@ -276,7 +277,7 @@ test('create (draft) PR', async () => {
276277
owner: 'robvanderleek',
277278
repo: 'create-issue-branch',
278279
draft: true,
279-
base: 'master',
280+
base: 'main',
280281
head: 'issue-1',
281282
body: 'closes #1',
282283
title: 'Hello world'
@@ -299,7 +300,7 @@ test('copy Issue description into PR', async () => {
299300
owner: 'robvanderleek',
300301
repo: 'create-issue-branch',
301302
head: 'issue-1',
302-
base: 'master',
303+
base: 'main',
303304
title: 'Hello world',
304305
body: formatAsExpandingMarkdown('Original issue description', 'This is the description') + '\ncloses #1',
305306
draft: false
@@ -321,7 +322,7 @@ test('Do not copy undefined Issue description into PR', async () => {
321322
owner: 'robvanderleek',
322323
repo: 'create-issue-branch',
323324
draft: false,
324-
base: 'master',
325+
base: 'main',
325326
head: 'issue-1',
326327
body: 'closes #1',
327328
title: 'Hello world'
@@ -346,7 +347,7 @@ test('copy pull-request template into PR', async () => {
346347
owner: 'robvanderleek',
347348
repo: 'create-issue-branch',
348349
head: 'issue-1',
349-
base: 'master',
350+
base: 'main',
350351
title: 'Hello world',
351352
body: 'file content' + '\ncloses #1',
352353
draft: false
@@ -371,7 +372,7 @@ test('pull-request template does not exist', async () => {
371372
owner: 'robvanderleek',
372373
repo: 'create-issue-branch',
373374
head: 'issue-1',
374-
base: 'master',
375+
base: 'main',
375376
title: 'Hello world',
376377
body: 'closes #1',
377378
draft: false
@@ -490,3 +491,18 @@ test('empty commit with skip CI text', async () => {
490491

491492
expect(capturedCommitMessage).toBe('Create PR for #1\n[skip ci]')
492493
})
494+
495+
496+
test('get PR title prefix for issue label', () => {
497+
const config = getDefaultConfig();
498+
499+
expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('Test Issue...');
500+
501+
config.conventionalPrTitles = true;
502+
503+
expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('fix: 🐛 Test Issue...');
504+
505+
config.conventionalLabels = {fix: {bug: ':ambulance:'}};
506+
507+
expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('fix: :ambulance: Test Issue...');
508+
})

tests/probot-chatops.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
commentCreatedWithLabelsPayload,
77
initNock,
88
initProbot,
9-
nockCommentCreated,
9+
nockCreateComment,
1010
nockCommit,
1111
nockCommitTreeSha,
1212
nockConfig,
@@ -31,7 +31,7 @@ beforeEach(() => {
3131

3232
test('creates a branch when a chatops command is given', async () => {
3333
nockNonExistingBranch('issue-1-Test_issue')
34-
nockExistingBranch('master', '12345678')
34+
nockExistingBranch('main', '12345678')
3535
nockConfig('mode: chatops')
3636
let createEndpointCalled = false
3737
let body = ''
@@ -58,7 +58,7 @@ test('creates a branch when a chatops command is given', async () => {
5858

5959
test('creates a branch when a chatops command is given when issue is created', async () => {
6060
nockNonExistingBranch('issue-1-Test_issue')
61-
nockExistingBranch('master', '12345678')
61+
nockExistingBranch('main', '12345678')
6262
nockConfig('mode: chatops')
6363
let createEndpointCalled = false
6464
let body = ''
@@ -85,7 +85,7 @@ test('creates a branch when a chatops command is given when issue is created', a
8585

8686
test('creates a branch when mode is immediate and an issue is created', async () => {
8787
nockNonExistingBranch('issue-1-Test_issue')
88-
nockExistingBranch('master', '12345678')
88+
nockExistingBranch('main', '12345678')
8989
nockConfig('mode: immediate')
9090
let createEndpointCalled = false
9191

@@ -108,7 +108,7 @@ test('creates a branch when mode is immediate and an issue is created', async ()
108108

109109
test('create branch anyway when a chatops command is given and mode is not chatops', async () => {
110110
nockNonExistingBranch('issue-1-Test_issue')
111-
nockExistingBranch('master', '12345678')
111+
nockExistingBranch('main', '12345678')
112112
nockConfig('mode: auto')
113113
let createEndpointCalled = false
114114

@@ -131,7 +131,7 @@ test('create branch anyway when a chatops command is given and mode is not chato
131131

132132
test('creates a branch when a chatops command is given, no comment', async () => {
133133
nockNonExistingBranch('issue-1-Test_issue')
134-
nockExistingBranch('master', '12345678')
134+
nockExistingBranch('main', '12345678')
135135
nockConfig('mode: chatops\nsilent: true')
136136
let createEndpointCalled = false
137137

@@ -149,7 +149,7 @@ test('creates a branch when a chatops command is given, no comment', async () =>
149149

150150
test('do not create a branch for issue labels that are configured to be skipped', async () => {
151151
nockNonExistingBranch('issue-1-Test_issue')
152-
nockExistingBranch('master', '12345678')
152+
nockExistingBranch('main', '12345678')
153153
const ymlConfig = `mode: chatops\nbranches:
154154
- label: question
155155
skip: true`
@@ -188,7 +188,7 @@ test('ignore chatops command if not at start of line', async () => {
188188

189189
test('chatops command with title argument', async () => {
190190
nockNonExistingBranch('issue-1-Simple_NPE_fix')
191-
nockExistingBranch('master', '12345678')
191+
nockExistingBranch('main', '12345678')
192192
nockExistingBranch('issue-1-Test_issue', '87654321')
193193
nockConfig('mode: chatops\nexperimental:\n branchNameArgument: true')
194194
let createEndpointCalled = false
@@ -218,7 +218,7 @@ test('chatops command with title argument', async () => {
218218

219219
test('chatops command with title argument and custom branch name', async () => {
220220
nockNonExistingBranch('1-foo-Simple_NPE_fix')
221-
nockExistingBranch('master', '12345678')
221+
nockExistingBranch('main', '12345678')
222222
nockExistingBranch('issue-1-Test_issue', '87654321')
223223
nockConfig( // eslint-disable-next-line no-template-curly-in-string
224224
'branchName: \'${issue.number}-foo-${issue.title}\'\nmode: chatops\nexperimental:\n branchNameArgument: true')
@@ -269,11 +269,11 @@ test('warn about existing branches', async () => {
269269

270270
test('open a pull request when a chatops command is given', async () => {
271271
nockNonExistingBranch('issue-1-Test_issue')
272-
nockExistingBranch('master', '12345678')
273-
nockExistingBranch('master', '12345678')
272+
nockExistingBranch('main', '12345678')
273+
nockExistingBranch('main', '12345678')
274274
nockConfig('mode: chatops\nopenPR: true')
275275
nockCreateBranch()
276-
nockCommentCreated()
276+
nockCreateComment()
277277
nockExistingBranch('issue-1-Test_issue', '87654321')
278278
nockCommitTreeSha('87654321', '12344321')
279279
nockCommit()
@@ -285,7 +285,7 @@ test('open a pull request when a chatops command is given', async () => {
285285

286286
test('open a pull request but do not create a branch for issue with release label', async () => {
287287
nockNonExistingBranch('issue-1-Test_issue')
288-
nockExistingBranch('master', '12345678')
288+
nockExistingBranch('main', '12345678')
289289
nockExistingBranch('release', '98765432')
290290
let config = ''
291291
config += 'mode: chatops\n'
@@ -296,7 +296,7 @@ test('open a pull request but do not create a branch for issue with release labe
296296
config += ' prTarget: release\n'
297297
config += ' skipBranch: true\n'
298298
nockConfig(config)
299-
nockCommentCreated()
299+
nockCreateComment()
300300
nockExistingBranch('develop', '87654321')
301301
nockCommitTreeSha('87654321', '12344321')
302302
nockCommit()
@@ -308,8 +308,8 @@ test('open a pull request but do not create a branch for issue with release labe
308308

309309
test('open a pull request, copy labels and assignee from issue', async () => {
310310
nockNonExistingBranch('issue-1-Test_issue')
311-
nockExistingBranch('master', '12345678')
312-
nockExistingBranch('master', '12345678')
311+
nockExistingBranch('main', '12345678')
312+
nockExistingBranch('main', '12345678')
313313
nockConfig(`
314314
mode: chatops
315315
openPR: true
@@ -330,9 +330,9 @@ test('open a pull request, copy labels and assignee from issue', async () => {
330330

331331
test('do not open a pull request when the branch already exists', async () => {
332332
nockExistingBranch('issue-1-Test_issue', '87654321')
333-
nockExistingBranch('master', '12345678')
333+
nockExistingBranch('main', '12345678')
334334
nockConfig('mode: chatops\nopenPR: true')
335-
nockCommentCreated()
335+
nockCreateComment()
336336

337337
await probot.receive({id: '', name: 'issue_comment', payload: commentCreatedPayload as any})
338338
})

0 commit comments

Comments
 (0)