Skip to content

Commit d365b21

Browse files
authored
Merge branch 'main' into fix/jest-config-esm-imports
2 parents c589f90 + 9332811 commit d365b21

12 files changed

Lines changed: 241 additions & 154 deletions

docs/cli-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The CLI options will override the [config-file-options.md](/docs/config-file-opt
3838
| --reviewer | | Add reviewer to the target PR | |
3939
| --sha | | Sha of commit to backport | |
4040
| --signoff | -s | Pass the --signoff option to the cherry-pick command | false |
41-
| --copySourcePRLabels | | Copy labels from source PR to the target PR | false |
41+
| --copySourcePRLabels | | Copy labels from source PR to the target PR (regex support via config) | false |
4242
| --copySourcePRReviewers | | Copy reviewers from source PR to the target PR | false |
4343
| --backportBranchName | | Name template to use for the branch name of the backport | |
4444
| --source-branch | | Specify a non-default branch to backport from | |

docs/config-file-options.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,17 @@ Assign the same reviewers to the target pull request that were assigned to the o
490490

491491
#### `copySourcePRLabels`
492492

493-
Copies all labels from the original (source) pull request to the backport (target) pull request.
493+
Copy labels from the original (source) pull request to the backport (target) pull request. When set to `true`, every non-backport label is copied. To copy only specific labels, provide one or more regular expressions; labels that match at least one pattern will be copied. (default: `false`)
494494

495495
```json
496496
{
497-
"copySourcePRLabels": false
497+
"copySourcePRLabels": true
498+
}
499+
```
500+
501+
```json
502+
{
503+
"copySourcePRLabels": ["^version-\\d+$", "^release_note:\\w+$"]
498504
}
499505
```
500506

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"branches",
1515
"branching"
1616
],
17-
"version": "10.0.3",
17+
"version": "10.2.0",
1818
"main": "./dist/src/entrypoint.api.js",
1919
"types": "dist/src/entrypoint.api.d.ts",
2020
"bin": {
@@ -29,8 +29,8 @@
2929
"lint": "tsc --noEmit --incremental && eslint './**/*.{ts,js}' --fix --cache",
3030
"start": "NODE_ENV=development ts-node --transpile-only ./src/entrypoint.cli.ts",
3131
"test-all": "jest --config ./jest.config.all.ts",
32-
"test-mutation": "jest --config ./jest.config.mutation.ts",
33-
"test-private": "jest --config ./jest.config.private.ts",
32+
"test-mutation": "jest --config ./jest.config.mutation.ts --runInBand",
33+
"test-private": "jest --config ./jest.config.private.ts --runInBand",
3434
"test": "jest",
3535
"codegen": "graphql-codegen --config codegen.ts",
3636
"build": "yarn codegen && tsc --incremental"

src/lib/cherrypickAndCreateTargetPullRequest/cherrypick-and-create-target-pull-request.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import { consoleLog } from '../logger';
2020
import { sequentially } from '../sequentially';
2121
import type { Commit } from '../sourceCommit/parse-source-commit';
2222
import { autoMergeNowOrLater } from './auto-merge-now-or-later';
23-
import { copySourcePullRequestLabelsToTargetPullRequest } from './copy-source-pull-request-labels';
2423
import { copySourcePullRequestReviewersToTargetPullRequest } from './copy-source-pull-request-reviewers-to-target-pull-request';
2524
import { getBackportBranchName } from './get-backport-branch-name';
2625
import { getMergeCommits } from './get-merge-commit';
27-
import { getTargetPRLabels } from './get-target-prlabels';
26+
import { getTargetPRLabels } from './getTargetPRLabels/get-target-prlabels';
2827
import { waitForCherrypick } from './wait-for-cherrypick';
2928

3029
export async function cherrypickAndCreateTargetPullRequest({
@@ -111,30 +110,23 @@ export async function cherrypickAndCreateTargetPullRequest({
111110
);
112111
}
113112

114-
// add labels to target pull request
115-
if (options.targetPRLabels.length > 0) {
116-
const labels = getTargetPRLabels({
117-
interactive: options.interactive,
118-
targetPRLabels: options.targetPRLabels,
119-
commits,
120-
targetBranch,
121-
});
113+
const targetPRLabels = getTargetPRLabels({
114+
interactive: options.interactive,
115+
targetPRLabels: options.targetPRLabels,
116+
copySourcePRLabels: options.copySourcePRLabels,
117+
commits,
118+
targetBranch,
119+
});
122120

121+
// add labels to target pull request
122+
if (targetPRLabels.length > 0) {
123123
await addLabelsToPullRequest({
124124
...options,
125125
pullNumber: targetPullRequest.number,
126-
labels,
126+
labels: targetPRLabels,
127127
});
128128
}
129129

130-
if (options.copySourcePRLabels) {
131-
await copySourcePullRequestLabelsToTargetPullRequest(
132-
options,
133-
commits,
134-
targetPullRequest.number,
135-
);
136-
}
137-
138130
// make PR auto mergable
139131
if (options.autoMerge && !hasAnyCommitWithConflicts) {
140132
await autoMergeNowOrLater(options, targetPullRequest.number);

src/lib/cherrypickAndCreateTargetPullRequest/copy-source-pull-request-labels.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/lib/cherrypickAndCreateTargetPullRequest/get-target-prlabels.test.ts renamed to src/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels/get-configured-target-pr-labels.test.ts

Lines changed: 37 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Commit } from '../../entrypoint.api';
2-
import { getTargetPRLabels } from './get-target-prlabels';
1+
import type { Commit } from '../../../entrypoint.api';
2+
import { getConfiguredTargetPRLabels } from './get-configured-target-pr-labels';
33

44
const commits: Commit[] = [
55
{
@@ -25,7 +25,6 @@ const commits: Commit[] = [
2525
},
2626
sourceBranch: 'master',
2727
targetPullRequestStates: [
28-
// PR to 7.11 branch was created automatically using the label `backport-to-7.11`
2928
{
3029
url: 'https://github.com/elastic/kibana/pull/88289',
3130
number: 88289,
@@ -39,8 +38,6 @@ const commits: Commit[] = [
3938
message: 'Fix major bug (#88188) (#88289)',
4039
},
4140
},
42-
43-
// PR to 7.x branch was created manually (not via labels) and does not contain branchLabelMappingKey
4441
{
4542
url: 'https://github.com/elastic/kibana/pull/88288',
4643
number: 88288,
@@ -55,10 +52,10 @@ const commits: Commit[] = [
5552
},
5653
];
5754

58-
describe('getTargetPRLabels', () => {
55+
describe('getConfiguredTargetPRLabels', () => {
5956
describe('replaces template values', () => {
6057
it('replaces {{targetBranch}}', () => {
61-
const labels = getTargetPRLabels({
58+
const labels = getConfiguredTargetPRLabels({
6259
interactive: false,
6360
commits,
6461
targetPRLabels: ['backported-to-{{targetBranch}}'],
@@ -68,7 +65,7 @@ describe('getTargetPRLabels', () => {
6865
});
6966

7067
it('replaces {{sourceBranch}}', () => {
71-
const labels = getTargetPRLabels({
68+
const labels = getConfiguredTargetPRLabels({
7269
interactive: false,
7370
commits,
7471
targetPRLabels: ['backported-from-{{sourceBranch}}'],
@@ -78,101 +75,71 @@ describe('getTargetPRLabels', () => {
7875
});
7976
});
8077

81-
describe('when label is static', () => {
82-
describe('and when interactive=false', () => {
83-
it('adds static label for 7.11', () => {
84-
const labels = getTargetPRLabels({
78+
describe('static labels', () => {
79+
it('keeps static labels regardless of interactivity', () => {
80+
expect(
81+
getConfiguredTargetPRLabels({
8582
interactive: false,
8683
commits,
8784
targetPRLabels: ['some-static-label'],
8885
targetBranch: '7.11',
89-
});
90-
expect(labels).toEqual(['some-static-label']);
91-
});
92-
93-
it('adds static label for 7.x', () => {
94-
const labels = getTargetPRLabels({
95-
interactive: false,
96-
commits,
97-
targetPRLabels: ['some-static-label'],
98-
targetBranch: '7.x',
99-
});
100-
expect(labels).toEqual(['some-static-label']);
101-
});
102-
});
86+
}),
87+
).toEqual(['some-static-label']);
10388

104-
describe('and when interactive=true', () => {
105-
it('adds static label for 7.x', () => {
106-
const labels = getTargetPRLabels({
89+
expect(
90+
getConfiguredTargetPRLabels({
10791
interactive: true,
10892
commits,
109-
targetPRLabels: ['backport'],
110-
targetBranch: '7.x',
111-
});
112-
expect(labels).toEqual(['backport']);
113-
});
93+
targetPRLabels: ['some-static-label'],
94+
targetBranch: '7.11',
95+
}),
96+
).toEqual(['some-static-label']);
11497
});
11598
});
11699

117-
describe('when label is dynamic', () => {
118-
describe('and when interactive=false', () => {
119-
it('adds dynamic label for 7.11', () => {
120-
const labels = getTargetPRLabels({
100+
describe('dynamic labels', () => {
101+
it('applies dynamic label when branch mapping is available', () => {
102+
expect(
103+
getConfiguredTargetPRLabels({
121104
interactive: false,
122105
commits,
123106
targetPRLabels: ['backport-$1'],
124107
targetBranch: '7.11',
125-
});
126-
expect(labels).toEqual(['backport-7.11']);
127-
});
108+
}),
109+
).toEqual(['backport-7.11']);
128110
});
129111

130-
describe('when interactive=true', () => {
131-
it('adds dynamic label for 7.11', () => {
132-
const labels = getTargetPRLabels({
133-
interactive: false,
134-
commits,
135-
targetPRLabels: ['backport-$1'],
136-
targetBranch: '7.11',
137-
});
138-
expect(labels).toEqual(['backport-7.11']);
139-
});
140-
141-
it('does not add dynamic label for 7.x', () => {
142-
const labels = getTargetPRLabels({
112+
it('drops dynamic label when interactive and mapping is missing', () => {
113+
expect(
114+
getConfiguredTargetPRLabels({
143115
interactive: true,
144116
commits,
145117
targetPRLabels: ['backport-$1'],
146118
targetBranch: '7.x',
147-
});
148-
expect(labels).toEqual([]);
149-
});
119+
}),
120+
).toEqual([]);
150121
});
151122
});
152123

153124
describe('multiple dynamic labels', () => {
154-
describe('interactive=false', () => {
155-
it('adds dynamic and static labels for 7.11', () => {
156-
const labels = getTargetPRLabels({
125+
it('keeps resolved labels and drops unresolved ones based on interactivity', () => {
126+
expect(
127+
getConfiguredTargetPRLabels({
157128
interactive: false,
158129
commits,
159130
targetPRLabels: ['backport-$1', '$1', 'my-static-label'],
160131
targetBranch: '7.11',
161-
});
162-
expect(labels).toEqual(['backport-7.11', '7.11', 'my-static-label']);
163-
});
164-
});
132+
}),
133+
).toEqual(['backport-7.11', '7.11', 'my-static-label']);
165134

166-
describe('interactive=true', () => {
167-
it('only add the static labels for 7.x', () => {
168-
const labels = getTargetPRLabels({
135+
expect(
136+
getConfiguredTargetPRLabels({
169137
interactive: true,
170138
commits,
171139
targetPRLabels: ['backport-$1', '$1', 'my-static-label'],
172140
targetBranch: '7.x',
173-
});
174-
expect(labels).toEqual(['my-static-label']);
175-
});
141+
}),
142+
).toEqual(['my-static-label']);
176143
});
177144
});
178145
});

src/lib/cherrypickAndCreateTargetPullRequest/get-target-prlabels.ts renamed to src/lib/cherrypickAndCreateTargetPullRequest/getTargetPRLabels/get-configured-target-pr-labels.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,13 @@
1-
import { uniq } from 'lodash';
2-
import type { Commit } from '../../entrypoint.api';
3-
import { filterNil } from '../../utils/filter-empty';
4-
import { getSourceBranchFromCommits } from '../get-source-branch-from-commits';
5-
import { logger } from '../logger';
6-
7-
export function getTargetPRLabels({
8-
interactive,
9-
targetPRLabels,
10-
commits,
11-
targetBranch,
12-
}: {
13-
interactive: boolean;
14-
targetPRLabels: string[];
15-
commits: Commit[];
16-
targetBranch: string;
17-
}) {
18-
const labels = getLabels({
19-
commits,
20-
targetBranch,
21-
targetPRLabels,
22-
interactive,
23-
});
24-
25-
return uniq(labels);
26-
}
27-
28-
function getLabels({
1+
import type { Commit } from '../../../entrypoint.api';
2+
import { filterNil } from '../../../utils/filter-empty';
3+
import { getSourceBranchFromCommits } from '../../get-source-branch-from-commits';
4+
import { logger } from '../../logger';
5+
6+
// Resolve labels defined in configuration (`targetPRLabels`) into their concrete
7+
// values for the current target branch. This includes expanding regex captures,
8+
// replacing template placeholders and skipping dynamic labels when we lack
9+
// branch mapping context in interactive mode.
10+
export function getConfiguredTargetPRLabels({
2911
commits,
3012
targetBranch,
3113
targetPRLabels,

0 commit comments

Comments
 (0)