Skip to content

Commit

Permalink
Automatically check brances when selecting multiple commits
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Apr 15, 2020
1 parent d005939 commit 206f086
Show file tree
Hide file tree
Showing 25 changed files with 246 additions and 114 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
globals: {
'ts-jest': {
diagnostics: false,
tsConfig: './src/test/tsconfig.json',
},
},
};
16 changes: 15 additions & 1 deletion src/__snapshots__/runWithOptions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Array [
"pullNumber": undefined,
"sha": "2e63475c483f7844b0f2833bc57fdee32095bacb",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -104,6 +105,7 @@ Array [
"pullNumber": 85,
"sha": "f3b618b9421fdecdb36862f907afbdd6344b361d",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -120,6 +122,7 @@ Array [
"pullNumber": 80,
"sha": "79cf18453ec32a4677009dcbab1c9c8c73fc14fe",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -131,6 +134,7 @@ Array [
"pullNumber": undefined,
"sha": "3827bbbaf39914eda4f02f6940189844375fd097",
"sourceBranch": "mySourceBranch",
"targetBranches": Array [],
},
},
Object {
Expand All @@ -142,12 +146,17 @@ Array [
"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
2 changes: 1 addition & 1 deletion src/options/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Read more: https://github.com/sqren/backport/blob/434a28b431bb58c9a014d4489a95f5
`;

exports[`validateRequiredOptions should throw when both branches and targetBranchChoices are missing 1`] = `
"You must specify one or more target branches
"You must specify a target branch
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
Expand Down
1 change: 1 addition & 0 deletions src/options/cliArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function validateRequiredOptions({

if (isEmpty(options.targetBranches) && isEmpty(options.targetBranchChoices)) {
throw new HandledError(
`You must specify one or more target branches\n\nYou can specify it via either:\n - Config file (recommended): ".backportrc.json". Read more: ${PROJECT_CONFIG_DOCS_LINK}\n - CLI: "--branch 6.1"`
`You must specify a target branch\n\nYou can specify it via either:\n - Config file (recommended): ".backportrc.json". Read more: ${PROJECT_CONFIG_DOCS_LINK}\n - CLI: "--branch 6.1"`
);
}

Expand Down
1 change: 1 addition & 0 deletions src/services/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ describe('cherrypick', () => {
sourceBranch: '7.x',
formattedMessage: '',
sha: 'abcd',
targetBranches: [],
};

it('should swallow cherrypick error', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/services/github/v3/fetchCommitBySha.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('fetchCommitBySha', () => {
formattedMessage: 'myMessage (sha12345)',
pullNumber: undefined,
sha: 'sha123456789',
targetBranches: [],
});

expect(axiosSpy.mock.calls).toMatchSnapshot();
Expand Down
1 change: 1 addition & 0 deletions src/services/github/v3/fetchCommitBySha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function fetchCommitBySha(

return {
sourceBranch: 'master',
targetBranches: [],
formattedMessage,
sha: fullSha,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Array [
mergeCommit {
oid
}
labels(first: 50) {
nodes {
name
}
}
timelineItems(
last: 20
itemTypes: CROSS_REFERENCED_EVENT
Expand Down Expand Up @@ -109,7 +114,7 @@ Array [
"historyPath": null,
"repoName": "kibana",
"repoOwner": "elastic",
"sourceBranch": undefined,
"sourceBranch": "master",
},
},
Object {
Expand Down
32 changes: 16 additions & 16 deletions src/services/github/v4/fetchCommitByPullNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTargetBranchesFromLabels } from './fetchCommitByPullNumber';
import { getTargetBranchesFromLabels } from './getTargetBranchesFromLabels';

describe('getTargetBranchesFromLabels', () => {
it(`should support Kibana's label format`, () => {
Expand Down Expand Up @@ -32,10 +32,10 @@ describe('getTargetBranchesFromLabels', () => {
'v7.8.0', // 7.x
'v8.0.0', // master
];
const targetBranches = getTargetBranchesFromLabels(
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping
);
branchLabelMapping,
});
expect(targetBranches).toEqual([
'5.4',
'5.5',
Expand Down Expand Up @@ -67,10 +67,10 @@ describe('getTargetBranchesFromLabels', () => {
'label-(\\d+)': 'branch-$1',
};
const labels = ['label-2'];
const targetBranches = getTargetBranchesFromLabels(
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping
);
branchLabelMapping,
});
expect(targetBranches).toEqual(['branch-b']);
});

Expand All @@ -79,10 +79,10 @@ describe('getTargetBranchesFromLabels', () => {
'label-(\\d+)': 'branch-$1',
};
const labels = ['label-1', 'label-2', 'label-2'];
const targetBranches = getTargetBranchesFromLabels(
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping
);
branchLabelMapping,
});
expect(targetBranches).toEqual(['branch-1', 'branch-2']);
});

Expand All @@ -91,10 +91,10 @@ describe('getTargetBranchesFromLabels', () => {
'label-(\\d+)': 'branch-$1',
};
const labels = ['label-1', 'label-2', 'foo', 'bar'];
const targetBranches = getTargetBranchesFromLabels(
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping
);
branchLabelMapping,
});
expect(targetBranches).toEqual(['branch-1', 'branch-2']);
});

Expand All @@ -104,10 +104,10 @@ describe('getTargetBranchesFromLabels', () => {
'label-(\\d+)': 'branch-$1',
};
const labels = ['label-1', 'label-2'];
const targetBranches = getTargetBranchesFromLabels(
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping
);
branchLabelMapping,
});
expect(targetBranches).toEqual(['branch-1']);
});
});
45 changes: 7 additions & 38 deletions src/services/github/v4/fetchCommitByPullNumber.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import uniq from 'lodash.uniq';
import { BackportOptions } from '../../../options/options';
import { CommitSelected } from '../../../types/Commit';
import { filterEmpty } from '../../../utils/filterEmpty';
import { HandledError } from '../../HandledError';
import { getFormattedCommitMessage } from '../commitFormatters';
import { apiRequestV4 } from './apiRequestV4';
import { getTargetBranchesFromLabels } from './getTargetBranchesFromLabels';

export async function fetchCommitByPullNumber(
options: BackportOptions & { pullNumber: number }
Expand Down Expand Up @@ -57,9 +56,6 @@ export async function fetchCommitByPullNumber(
throw new HandledError(`The PR #${pullNumber} is not merged`);
}

const labels = res.repository.pullRequest.labels.nodes.map(
(label) => label.name
);
const sourceBranch = res.repository.pullRequest.baseRef.name;
const sha = res.repository.pullRequest.mergeCommit.oid;
const formattedMessage = getFormattedCommitMessage({
Expand All @@ -68,10 +64,13 @@ export async function fetchCommitByPullNumber(
pullNumber,
});

const targetBranches = getTargetBranchesFromLabels(
labels,
branchLabelMapping
const labels = res.repository.pullRequest.labels.nodes.map(
(label) => label.name
);
const targetBranches = getTargetBranchesFromLabels({
labels,
branchLabelMapping,
});

return {
sourceBranch,
Expand Down Expand Up @@ -100,33 +99,3 @@ interface DataResponse {
};
};
}

export function getTargetBranchesFromLabels(
labels: string[],
branchLabelMapping?: Record<string, string>
) {
if (!branchLabelMapping) {
return [];
}
const targetBranches = labels
.flatMap((label) => {
// only get first match
const result = Object.entries(branchLabelMapping).find(
([labelPattern]) => {
const regex = new RegExp(labelPattern);
const isMatch = label.match(regex) !== null;
return isMatch;
}
);

if (result) {
const [labelPattern, targetBranch] = result;
const regex = new RegExp(labelPattern);
return label.replace(regex, targetBranch);
}
})
.filter((targetBranch) => targetBranch !== '')
.filter(filterEmpty);

return uniq(targetBranches);
}
Loading

0 comments on commit 206f086

Please sign in to comment.