Skip to content

Commit

Permalink
Pre-select branches based on PR labels (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Apr 15, 2020
1 parent 24be079 commit 908c669
Show file tree
Hide file tree
Showing 44 changed files with 621 additions and 222 deletions.
34 changes: 26 additions & 8 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,27 @@ Config:

#### `prTitle`

Pull request title pattern. You can access the base branch (`{baseBranch}`) and commit message (`{commitMessages}`) via the special accessors in quotes.
Multiple commits will be concatenated and separated by pipes.
Pull request title pattern.
Template values:

Default: `"[{baseBranch}] {commitMessages}"`
- `{targetBranch}`: Branch the backport PR will be targeting
- `{commitMessages}`: Multiple commits will be concatenated and separated by pipes (`|`).

CLI: `--pr-title "{commitMessages} backport for {baseBranch}"`
Default: `"[{targetBranch}] {commitMessages}"`

CLI: `--pr-title "{commitMessages} backport for {targetBranch}"`

Config:

```json
{
"prTitle": "{commitMessages} backport for {targetBranch}"
}
```

#### `prDescription`

Pull request description.
Will be added to the end of the pull request description.
Text that will be appended to the pull request description.

For people who often need to add the same description to PRs they can create a bash alias:

Expand All @@ -178,6 +188,14 @@ alias backport-skip-ci='backport --prDescription "[skip-ci]"'

CLI: `--pr-description "skip-ci"`

Config:

```json
{
"prDescription": "skip-ci"
}
```

#### `sourceBranch`

By default the list of commits will be sourced from the repository's default branch (mostly "master"). Use `sourceBranch` to list and backport commits from other branches than the default.
Expand All @@ -204,15 +222,15 @@ CLI: `--git-hostname "github.my-private-company.com"`

#### `githubApiBaseUrlV3`

Base url for Github's Rest (v3) API
Base url for Github's REST (v3) API

Default: `https://api.github.com`

CLI: `--github-api-base-url-v3 "https://api.github.my-private-company.com"`

#### `githubApiBaseUrlV4`

Base url for Github's Rest (v3) API
Base url for Github's GraphQL (v4) API

Default: `https://api.github.com/graphql`

Expand Down
5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module.exports = {
snapshotSerializers: ['jest-snapshot-serializer-ansi'],
setupFiles: ['./src/test/setupFiles/automatic-mocks.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
preset: 'ts-jest',
testRegex: '(test|src)/.*test.ts$',
moduleFileExtensions: ['ts', 'js', 'json'],
globals: {
'ts-jest': {
diagnostics: false,
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"del": "^5.1.0",
"find-up": "^4.1.0",
"inquirer": "^7.1.0",
"lodash.flatmap": "^4.5.0",
"lodash.isempty": "^4.4.0",
"lodash.isstring": "^4.0.1",
"lodash.uniq": "^4.5.0",
Expand All @@ -83,20 +84,21 @@
"@types/inquirer": "^6.5.0",
"@types/jest": "^25.2.1",
"@types/lodash": "^4.14.144",
"@types/lodash.flatmap": "^4.5.6",
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.isstring": "^4.0.6",
"@types/lodash.uniq": "^4.5.6",
"@types/node": "^12.12.31",
"@types/safe-json-stringify": "^1.1.0",
"@types/yargs": "^15.0.4",
"@types/yargs-parser": "^15.0.0",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.8.2",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.3",
"jest": "^25.3.0",
"jest-snapshot-serializer-ansi": "^1.0.0",
Expand Down
26 changes: 20 additions & 6 deletions src/__snapshots__/runWithOptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,30 @@ Array [
"name": "1. Add 👻 (2e63475c) ",
"short": "Add 👻 (2e63475c)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add 👻 (2e63475c)",
"pullNumber": undefined,
"sha": "2e63475c483f7844b0f2833bc57fdee32095bacb",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "2. Add witch (#85) ",
"short": "Add witch (#85)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add witch (#85)",
"pullNumber": 85,
"sha": "f3b618b9421fdecdb36862f907afbdd6344b361d",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "3. Add SF mention (#80) 6.3",
"short": "Add SF mention (#80)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [
Object {
"branch": "6.3",
Expand All @@ -120,34 +121,42 @@ Array [
"formattedMessage": "Add SF mention (#80)",
"pullNumber": 80,
"sha": "79cf18453ec32a4677009dcbab1c9c8c73fc14fe",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "4. Add backport config (3827bbba) ",
"short": "Add backport config (3827bbba)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Add backport config (3827bbba)",
"pullNumber": undefined,
"sha": "3827bbbaf39914eda4f02f6940189844375fd097",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
"name": "5. Initial commit (5ea0da55) ",
"short": "Initial commit (5ea0da55)",
"value": Object {
"branch": "mySourceBranch",
"existingBackports": Array [],
"formattedMessage": "Initial commit (5ea0da55)",
"pullNumber": undefined,
"sha": "5ea0da550ac191029459289d67f99ad7d310812b",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Separator {
"line": "──────────────",
"type": "separator",
},
],
"message": "Select commit to backport",
"name": "promptResult",
"pageSize": 5,
"pageSize": 15,
"type": "list",
},
],
Expand All @@ -171,9 +180,14 @@ Array [
Object {
"name": "5.4",
},
Separator {
"line": "──────────────",
"type": "separator",
},
],
"message": "Select branch to backport to",
"name": "promptResult",
"pageSize": 15,
"type": "list",
},
],
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

import { runWithArgs } from './runWithArgs';
const args = process.argv.slice(2);

Expand Down
22 changes: 11 additions & 11 deletions src/options/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ It must contain a valid \\"username\\" and \\"accessToken\\".
Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#global-config-backportconfigjson"
`;

exports[`validateRequiredOptions should throw when both branches and branchChoices are missing 1`] = `
"Invalid option \\"branches\\"
exports[`validateRequiredOptions should throw when both branches and targetBranchChoices are missing 1`] = `
"You must specify a target branch
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--branches 6.1\\""
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--branch 6.1\\""
`;

exports[`validateRequiredOptions should throw when upstream is empty 1`] = `
"Invalid option \\"upstream\\"
"You must specify a valid Github repository
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--upstream elastic/kibana\\""
`;

exports[`validateRequiredOptions should throw when upstream is missing 1`] = `
"Invalid option \\"upstream\\"
"You must specify a valid Github repository
You can add it with either:
- Config file: \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
You can specify it via either:
- Config file (recommended): \\".backportrc.json\\". Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f561e6bb2769/docs/configuration.md#project-config-backportrcjson
- CLI: \\"--upstream elastic/kibana\\""
`;

Expand Down
6 changes: 3 additions & 3 deletions src/options/cliArgs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('getOptionsFromCliArgs', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branchChoices: [],
targetBranchChoices: [],
fork: true,
gitHostname: 'github.com',
labels: [],
Expand Down Expand Up @@ -41,8 +41,8 @@ describe('getOptionsFromCliArgs', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branches: ['6.0', '6.1'],
branchChoices: [],
targetBranches: ['6.0', '6.1'],
targetBranchChoices: [],
fork: true,
gitHostname: 'github.com',
labels: [],
Expand Down
6 changes: 4 additions & 2 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getOptionsFromCliArgs(
description: 'Pull request labels for the original PR',
type: 'array',
})
.option('branches', {
.option('targetBranches', {
default: [] as string[],
description: 'Branch(es) to backport to',
type: 'array',
Expand All @@ -43,6 +43,7 @@ export function getOptionsFromCliArgs(
.option('commitsCount', {
default: configOptions.commitsCount,
description: 'Number of commits to choose from',
alias: 'count',
type: 'number',
})
.option('editor', {
Expand Down Expand Up @@ -170,7 +171,8 @@ export function getOptionsFromCliArgs(
return {
...rest,
accessToken: cliArgs.accessToken || configOptions.accessToken,
branchChoices: configOptions.branchChoices,
targetBranchChoices: configOptions.targetBranchChoices, // not available as cli argument
branchLabelMapping: configOptions.branchLabelMapping, // not available as cli argument
multipleBranches: cliArgs.multipleBranches || cliArgs.multiple,
multipleCommits: cliArgs.multipleCommits || cliArgs.multiple,
};
Expand Down
4 changes: 2 additions & 2 deletions src/options/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('getOptionsFromConfigFiles', () => {
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
backportCreatedLabels: [],
branchChoices: [
targetBranchChoices: [
{ checked: false, name: '6.0' },
{ checked: false, name: '5.9' },
],
Expand All @@ -25,7 +25,7 @@ describe('getOptionsFromConfigFiles', () => {
multiple: false,
multipleBranches: true,
multipleCommits: false,
prTitle: '[{baseBranch}] {commitMessages}',
prTitle: '[{targetBranch}] {commitMessages}',
upstream: 'elastic/backport-demo',
username: 'sqren',
});
Expand Down
8 changes: 4 additions & 4 deletions src/options/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export async function getOptionsFromConfigFiles() {
multipleBranches: true,
all: false,
labels: [] as string[],
prTitle: '[{baseBranch}] {commitMessages}',
prTitle: '[{targetBranch}] {commitMessages}',
gitHostname: 'github.com',
githubApiBaseUrlV3: 'https://api.github.com',
githubApiBaseUrlV4: 'https://api.github.com/graphql',
branchChoices: getBranchesAsObjects(branches),
targetBranchChoices: getTargetBranchChoices(branches),
...combinedConfig,
};
}

// in the config `branches` can either a string or an object.
// in the config `branches` can either be a string or an object.
// We need to transform it so that it is always treated as an object troughout the application
function getBranchesAsObjects(branches?: Config['branches']) {
function getTargetBranchChoices(branches?: Config['branches']) {
if (!branches) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/options/config/projectConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('getProjectConfig', () => {
it('should return empty config', async () => {
jest.spyOn(fs, 'readFile').mockResolvedValueOnce('{}');
const projectConfig = await getProjectConfig();
expect(projectConfig).toEqual({ branchChoices: undefined });
expect(projectConfig).toEqual({ targetBranchChoices: undefined });
});
});

Expand Down
Loading

0 comments on commit 908c669

Please sign in to comment.