Skip to content

Commit

Permalink
Add prDescription cli argument (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Apr 29, 2019
1 parent 0556820 commit 536bdad
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export function getOptionsFromCliArgs(
description: 'Backport to multiple branches',
type: 'boolean'
})
.option('prDescription', {
default: configOptions.prDescription,
description: 'Description to be added to pull request',
type: 'string'
})
.option('sha', {
description: 'Commit sha to backport',
type: 'string',
Expand Down Expand Up @@ -73,6 +78,7 @@ export function getOptionsFromCliArgs(
multiple: cliArgs.multiple,
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
prDescription: cliArgs.prDescription,
sha: cliArgs.sha,
upstream: cliArgs.upstream,
username: cliArgs.username
Expand Down
1 change: 1 addition & 0 deletions src/options/config/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getGlobalConfigPath, getReposPath } from '../../services/env';
interface GlobalConfig {
username?: string;
accessToken?: string;
prDescription?: string;

// the following are overwritable by project config:
all?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function validateOptions({
multiple,
multipleBranches,
multipleCommits,
prDescription,
sha,
upstream,
username
Expand Down Expand Up @@ -83,6 +84,7 @@ export function validateOptions({
multiple,
multipleBranches,
multipleCommits,
prDescription,
sha,
upstream,
username
Expand Down
23 changes: 17 additions & 6 deletions src/steps/doBackportVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function doBackportVersions(
commits: Commit[],
branches: string[],
username: string,
labels: string[]
labels: string[],
prDescription: string | undefined
) {
return sequentially(branches, async branch => {
try {
Expand All @@ -32,7 +33,8 @@ export function doBackportVersions(
commits,
branch,
username,
labels
labels,
prDescription
);
log(`View pull request: ${pullRequest.html_url}`);
} catch (e) {
Expand All @@ -52,7 +54,8 @@ export async function doBackportVersion(
commits: Commit[],
baseBranch: string,
username: string,
labels: string[] = []
labels: string[] = [],
prDescription: string | undefined
) {
const featureBranch = getFeatureBranchName(baseBranch, commits);
const refValues = commits.map(commit => getReferenceLong(commit)).join(', ');
Expand Down Expand Up @@ -84,7 +87,12 @@ export async function doBackportVersion(
);

return withSpinner({ text: 'Creating pull request' }, async () => {
const payload = getPullRequestPayload(baseBranch, commits, username);
const payload = getPullRequestPayload(
baseBranch,
commits,
username,
prDescription
);
const pullRequest = await createPullRequest(owner, repoName, payload);
if (labels.length > 0) {
await addLabelsToPullRequest(owner, repoName, pullRequest.number, labels);
Expand Down Expand Up @@ -178,7 +186,8 @@ function getPullRequestTitle(baseBranch: string, commits: Commit[]) {
export function getPullRequestPayload(
baseBranch: string,
commits: Commit[],
username: string
username: string,
prDescription: string | undefined
) {
const featureBranch = getFeatureBranchName(baseBranch, commits);
const commitRefs = commits
Expand All @@ -188,9 +197,11 @@ export function getPullRequestPayload(
})
.join('\n');

const bodySuffix = prDescription ? `\n\n${prDescription}` : '';

return {
title: getPullRequestTitle(baseBranch, commits),
body: `Backports the following commits to ${baseBranch}:\n${commitRefs}`,
body: `Backports the following commits to ${baseBranch}:\n${commitRefs}${bodySuffix}`,
head: `${username}:${featureBranch}`,
base: `${baseBranch}`
};
Expand Down
3 changes: 2 additions & 1 deletion src/steps/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export async function initSteps(options: BackportOptions) {
commits,
branches,
options.username,
options.labels
options.labels,
options.prDescription
);
}
1 change: 1 addition & 0 deletions test/options/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const validOptions = {
multiple: false,
multipleBranches: true,
multipleCommits: false,
prDescription: undefined,
sha: undefined,
upstream: 'elastic/kibana',
username: 'sqren'
Expand Down
2 changes: 1 addition & 1 deletion test/steps/__snapshots__/steps.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Object {
"patch": Array [],
"post": Array [
Object {
"data": "{\\"title\\":\\"[6.2] myCommitMessage\\",\\"body\\":\\"Backports the following commits to 6.2:\\\\n - myCommitMessage (#myPullRequestNumber)\\",\\"head\\":\\"sqren:backport/6.2/pr-myPullRequestNumber\\",\\"base\\":\\"6.2\\"}",
"data": "{\\"title\\":\\"[6.2] myCommitMessage\\",\\"body\\":\\"Backports the following commits to 6.2:\\\\n - myCommitMessage (#myPullRequestNumber)\\\\n\\\\nmyPrDescription\\",\\"head\\":\\"sqren:backport/6.2/pr-myPullRequestNumber\\",\\"base\\":\\"6.2\\"}",
"headers": Object {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
Expand Down
20 changes: 14 additions & 6 deletions test/steps/doBackportVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ describe('doBackportVersion', () => {
commits,
'6.x',
'sqren',
['backport']
['backport'],
'myPrSuffix'
);
});

Expand All @@ -76,7 +77,7 @@ describe('doBackportVersion', () => {
{
title: '[6.x] myCommitMessage | myOtherCommitMessage',
body:
'Backports the following commits to 6.x:\n - myCommitMessage (#1000)\n - myOtherCommitMessage (#2000)',
'Backports the following commits to 6.x:\n - myCommitMessage (#1000)\n - myOtherCommitMessage (#2000)\n\nmyPrSuffix',
head: 'sqren:backport/6.x/pr-1000_pr-2000',
base: '6.x'
}
Expand All @@ -99,9 +100,15 @@ describe('doBackportVersion', () => {
}
];

await doBackportVersion('elastic', 'kibana', commits, '6.x', 'sqren', [
'backport'
]);
await doBackportVersion(
'elastic',
'kibana',
commits,
'6.x',
'sqren',
['backport'],
undefined
);
});

it('should create pull request and add labels', () => {
Expand Down Expand Up @@ -160,7 +167,8 @@ describe('doBackportVersion', () => {
commits,
'6.x',
'sqren',
['backport']
['backport'],
undefined
);

return { logSpy, execSpy, promise };
Expand Down
21 changes: 11 additions & 10 deletions test/steps/steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,24 @@ describe('run through steps', () => {
});

await initSteps({
username: 'sqren',
branches: [],
sha: undefined,
accessToken: 'myAccessToken',
multiple: false,
multipleBranches: false,
multipleCommits: false,
upstream,
all: false,
branches: [],
branchChoices: [
{ name: '6.x' },
{ name: '6.0' },
{ name: '5.6' },
{ name: '5.5' },
{ name: '5.4' }
],
all: false,
labels: []
labels: [],
multiple: false,
multipleBranches: false,
multipleCommits: false,
prDescription: 'myPrDescription',
sha: undefined,
upstream,
username: 'sqren'
});
});

Expand All @@ -183,7 +184,7 @@ describe('run through steps', () => {
it('createPullRequest should be called with correct args', () => {
expect(github.createPullRequest).toHaveBeenCalledWith('elastic', 'kibana', {
base: '6.2',
body: `Backports the following commits to 6.2:\n - myCommitMessage (#myPullRequestNumber)`,
body: `Backports the following commits to 6.2:\n - myCommitMessage (#myPullRequestNumber)\n\nmyPrDescription`,
head: 'sqren:backport/6.2/pr-myPullRequestNumber',
title: '[6.2] myCommitMessage'
});
Expand Down

0 comments on commit 536bdad

Please sign in to comment.