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
40 changes: 29 additions & 11 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ export async function createPr(app: Probot, ctx: Context<any>, config: Config, u
const owner = getRepoOwnerLogin(ctx);
const repo = getRepoName(ctx);
const base = getPrTargetBranch(ctx, config);
const title = getIssueTitle(ctx);
const issueTitle = getIssueTitle(ctx);
const issueLabels = getIssueLabels(ctx);
const prTitle = createPrTitle(config, issueTitle, issueLabels);
const draft = config.openDraftPR;
const logContext: any = {owner, repo, branchName, base, title, draft, username};
const logContext: any = {owner, repo, branchName, base, prTitle, draft, username};
try {
const baseHeadSha = await getBranchHeadSha(ctx, base);
logContext.baseHeadSha = baseHeadSha;
Expand All @@ -276,8 +278,16 @@ export async function createPr(app: Probot, ctx: Context<any>, config: Config, u
logContext.emptyCommitResponse = await createEmptyCommit(ctx, branchName, getCommitText(ctx, config), String(branchHeadSha));
}
const {data: pr} = await ctx.octokit.pulls.create(
{owner, repo, head: branchName, base, title, body: await getPrBody(app, ctx, config), draft: draft})
app.log.info(`${draft ? 'Created draft' : 'Created'} pull request ${pr.number} for branch ${branchName}`)
{
owner,
repo,
head: branchName,
base,
title: prTitle,
body: await getPrBody(app, ctx, config),
draft: draft
});
app.log.info(`${draft ? 'Created draft' : 'Created'} pull request ${pr.number} for branch ${branchName}`);
await copyIssueAttributesToPr(app, ctx, config, pr);
} catch (e: any) {
app.log.info(`Could not create PR (${e.message})`);
Expand Down Expand Up @@ -459,15 +469,23 @@ async function queryProjectIdsForIssue(ctx: Context<any>) {
return result
}

export function createPrTitle(config: Config, issueTitle: string, labels: Array<string>) {
if (config.conventionalPrTitles) {
const conventionalPrefix = getConventionalPrTitlePrefix(config, labels);
return conventionalPrefix + ' ' + issueTitle;
} else {
return issueTitle;
}
}

export async function updatePrTitle(app: Probot, ctx: Context<any>, config: Config, pr: any, issueTitle: string, labels: Array<string>) {
const owner = getRepoOwnerLogin(ctx)
const repo = getRepoName(ctx)
const pullNumber = pr.number
const conventionalPrefix = getConventionalPrTitlePrefix(config, labels)
const updatedTitle = conventionalPrefix + ' ' + issueTitle
const updatedTitle = createPrTitle(config, issueTitle, labels);
if (updatedTitle !== pr.title) {
app.log.info(`Updating prefix for PR #${pullNumber} in ${owner}/${repo} to: ${conventionalPrefix}`)
await ctx.octokit.pulls.update({owner: owner, repo: repo, pull_number: pullNumber, title: updatedTitle})
const owner = getRepoOwnerLogin(ctx);
const repo = getRepoName(ctx);
const pullNumber = pr.number;
app.log.info(`Updating title for PR #${pullNumber} in ${owner}/${repo} to: ${updatedTitle}`);
await ctx.octokit.pulls.update({owner: owner, repo: repo, pull_number: pullNumber, title: updatedTitle});
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ test('get default branch', () => {

expect(getDefaultBranch(config)).toBe('main');
})

test('get PR title prefix for issue label', () => {
const config = getDefaultConfig();

Expand Down
28 changes: 22 additions & 6 deletions tests/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as github from "../src/github";
import {createPrTitle} from "../src/github";
import {formatAsExpandingMarkdown} from "../src/utils";
import {getDefaultConfig} from "../src/entities/Config";
import {getDefaultContext, initNock, initProbot} from "./test-helpers";
Expand Down Expand Up @@ -261,7 +262,7 @@ test('create (draft) PR', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
draft: false,
base: 'master',
base: 'main',
head: 'issue-1',
body: 'closes #1',
title: 'Hello world'
Expand All @@ -276,7 +277,7 @@ test('create (draft) PR', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
draft: true,
base: 'master',
base: 'main',
head: 'issue-1',
body: 'closes #1',
title: 'Hello world'
Expand All @@ -299,7 +300,7 @@ test('copy Issue description into PR', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
head: 'issue-1',
base: 'master',
base: 'main',
title: 'Hello world',
body: formatAsExpandingMarkdown('Original issue description', 'This is the description') + '\ncloses #1',
draft: false
Expand All @@ -321,7 +322,7 @@ test('Do not copy undefined Issue description into PR', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
draft: false,
base: 'master',
base: 'main',
head: 'issue-1',
body: 'closes #1',
title: 'Hello world'
Expand All @@ -346,7 +347,7 @@ test('copy pull-request template into PR', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
head: 'issue-1',
base: 'master',
base: 'main',
title: 'Hello world',
body: 'file content' + '\ncloses #1',
draft: false
Expand All @@ -371,7 +372,7 @@ test('pull-request template does not exist', async () => {
owner: 'robvanderleek',
repo: 'create-issue-branch',
head: 'issue-1',
base: 'master',
base: 'main',
title: 'Hello world',
body: 'closes #1',
draft: false
Expand Down Expand Up @@ -490,3 +491,18 @@ test('empty commit with skip CI text', async () => {

expect(capturedCommitMessage).toBe('Create PR for #1\n[skip ci]')
})


test('get PR title prefix for issue label', () => {
const config = getDefaultConfig();

expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('Test Issue...');

config.conventionalPrTitles = true;

expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('fix: 🐛 Test Issue...');

config.conventionalLabels = {fix: {bug: ':ambulance:'}};

expect(createPrTitle(config, 'Test Issue...', ['bug'])).toBe('fix: :ambulance: Test Issue...');
})
36 changes: 18 additions & 18 deletions tests/probot-chatops.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
commentCreatedWithLabelsPayload,
initNock,
initProbot,
nockCommentCreated,
nockCreateComment,
nockCommit,
nockCommitTreeSha,
nockConfig,
Expand All @@ -31,7 +31,7 @@ beforeEach(() => {

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

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

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

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

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

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

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

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

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

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

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

test('open a pull request when a chatops command is given', async () => {
nockNonExistingBranch('issue-1-Test_issue')
nockExistingBranch('master', '12345678')
nockExistingBranch('master', '12345678')
nockExistingBranch('main', '12345678')
nockExistingBranch('main', '12345678')
nockConfig('mode: chatops\nopenPR: true')
nockCreateBranch()
nockCommentCreated()
nockCreateComment()
nockExistingBranch('issue-1-Test_issue', '87654321')
nockCommitTreeSha('87654321', '12344321')
nockCommit()
Expand All @@ -285,7 +285,7 @@ test('open a pull request when a chatops command is given', async () => {

test('open a pull request but do not create a branch for issue with release label', async () => {
nockNonExistingBranch('issue-1-Test_issue')
nockExistingBranch('master', '12345678')
nockExistingBranch('main', '12345678')
nockExistingBranch('release', '98765432')
let config = ''
config += 'mode: chatops\n'
Expand All @@ -296,7 +296,7 @@ test('open a pull request but do not create a branch for issue with release labe
config += ' prTarget: release\n'
config += ' skipBranch: true\n'
nockConfig(config)
nockCommentCreated()
nockCreateComment()
nockExistingBranch('develop', '87654321')
nockCommitTreeSha('87654321', '12344321')
nockCommit()
Expand All @@ -308,8 +308,8 @@ test('open a pull request but do not create a branch for issue with release labe

test('open a pull request, copy labels and assignee from issue', async () => {
nockNonExistingBranch('issue-1-Test_issue')
nockExistingBranch('master', '12345678')
nockExistingBranch('master', '12345678')
nockExistingBranch('main', '12345678')
nockExistingBranch('main', '12345678')
nockConfig(`
mode: chatops
openPR: true
Expand All @@ -330,9 +330,9 @@ test('open a pull request, copy labels and assignee from issue', async () => {

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

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