backport 8.4.2 → 8.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/entrypoint.module.d.ts +1 -1
  2. package/dist/entrypoint.module.js +8 -7
  3. package/dist/entrypoint.module.private.test.js +79 -29
  4. package/dist/lib/author.d.ts +13 -0
  5. package/dist/lib/author.js +28 -0
  6. package/dist/lib/cherrypickAndCreateTargetPullRequest.js +14 -20
  7. package/dist/lib/cherrypickAndCreateTargetPullRequest.test.js +14 -4
  8. package/dist/lib/child-process-promisified.d.ts +1 -1
  9. package/dist/lib/child-process-promisified.js +14 -3
  10. package/dist/lib/getCommits.js +14 -9
  11. package/dist/lib/getCommitsWithoutBackports.js +3 -3
  12. package/dist/lib/getCommitsWithoutBackports.test.js +38 -15
  13. package/dist/lib/getTargetBranches.d.ts +1 -1
  14. package/dist/lib/getTargetBranches.js +6 -10
  15. package/dist/lib/getTargetBranches.test.js +9 -5
  16. package/dist/lib/git.d.ts +9 -7
  17. package/dist/lib/git.js +12 -4
  18. package/dist/lib/git.private.test.js +21 -4
  19. package/dist/lib/git.test.js +31 -7
  20. package/dist/lib/github/v3/createPullRequest.test.js +39 -13
  21. package/dist/lib/github/v3/createStatusComment.js +5 -2
  22. package/dist/lib/github/v3/createStatusComment.test.js +7 -3
  23. package/dist/lib/github/v4/fetchCommits/allFetchers.private.test.js +20 -1
  24. package/dist/lib/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +32 -10
  25. package/dist/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.js +9 -1
  26. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +49 -14
  27. package/dist/lib/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +15 -5
  28. package/dist/lib/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +41 -11
  29. package/dist/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +1 -1
  30. package/dist/lib/prompt.test.js +37 -9
  31. package/dist/lib/prompts.js +5 -2
  32. package/dist/lib/setupRepo.js +3 -3
  33. package/dist/lib/setupRepo.test.js +5 -5
  34. package/dist/lib/sourceCommit/getPullRequestStates.d.ts +27 -0
  35. package/dist/lib/sourceCommit/{getExpectedTargetPullRequests.js → getPullRequestStates.js} +62 -27
  36. package/dist/lib/sourceCommit/{getExpectedTargetPullRequests.test.d.ts → getPullRequestStates.test.d.ts} +0 -0
  37. package/dist/lib/sourceCommit/{getExpectedTargetPullRequests.test.js → getPullRequestStates.test.js} +283 -87
  38. package/dist/lib/sourceCommit/parseSourceCommit.d.ts +4 -2
  39. package/dist/lib/sourceCommit/parseSourceCommit.js +20 -5
  40. package/dist/lib/sourceCommit/parseSourceCommit.test.js +78 -25
  41. package/dist/options/ConfigOptions.d.ts +1 -1
  42. package/dist/options/config/config.d.ts +1 -1
  43. package/dist/options/config/readConfigFile.d.ts +1 -1
  44. package/dist/options/options.d.ts +1 -1
  45. package/dist/test/e2e/cli/entrypoint.cli.private.test.js +2 -2
  46. package/dist/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.d.ts +1 -0
  47. package/dist/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.js +95 -0
  48. package/dist/test/e2e/cli/runBackportViaCli.js +1 -1
  49. package/dist/test/e2e/module/entrypoint.module.mutation.test.js +11 -4
  50. package/dist/test/handleUnbackportedPullRequests.private.test.js +10 -1
  51. package/dist/test/sandbox.d.ts +1 -0
  52. package/dist/test/sandbox.js +3 -2
  53. package/dist/utils/filterUnmergedCommits.js +1 -1
  54. package/dist/utils/packageVersion.d.ts +1 -1
  55. package/dist/utils/packageVersion.js +1 -1
  56. package/package.json +15 -15
  57. package/yarn.lock +561 -471
  58. package/dist/lib/getGitConfigAuthor.d.ts +0 -6
  59. package/dist/lib/getGitConfigAuthor.js +0 -15
  60. package/dist/lib/sourceCommit/getExpectedTargetPullRequests.d.ts +0 -16
@@ -42,7 +42,13 @@ describe('fetchPullRequestsBySearchQuery', () => {
42
42
  const expectedCommits = [
43
43
  {
44
44
  author: { email: 'sorenlouv@gmail.com', name: 'Søren Louv-Jansen' },
45
+ suggestedTargetBranches: [],
45
46
  sourceCommit: {
47
+ branchLabelMapping: {
48
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
49
+ '^v7.9.0$': '7.x',
50
+ '^v8.0.0$': 'master',
51
+ },
46
52
  committedDate: '2020-08-16T21:44:28Z',
47
53
  message: 'Add sheep emoji (#9)',
48
54
  sha: 'eebf165c82a4b718d95c11b3877e365b1949ff28',
@@ -56,9 +62,11 @@ describe('fetchPullRequestsBySearchQuery', () => {
56
62
  },
57
63
  },
58
64
  sourceBranch: 'master',
59
- expectedTargetPullRequests: [
65
+ pullRequestStates: [
60
66
  {
61
67
  branch: '7.8',
68
+ label: 'v7.8.0',
69
+ isSourceBranch: false,
62
70
  state: 'OPEN',
63
71
  number: 10,
64
72
  url: 'https://github.com/backport-org/backport-e2e/pull/10',
@@ -67,7 +75,13 @@ describe('fetchPullRequestsBySearchQuery', () => {
67
75
  },
68
76
  {
69
77
  author: { email: 'sorenlouv@gmail.com', name: 'Søren Louv-Jansen' },
78
+ suggestedTargetBranches: [],
70
79
  sourceCommit: {
80
+ branchLabelMapping: {
81
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
82
+ '^v7.9.0$': '7.x',
83
+ '^v8.0.0$': 'master',
84
+ },
71
85
  committedDate: '2020-08-15T12:40:19Z',
72
86
  message: 'Add 🍏 emoji (#5)',
73
87
  sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
@@ -81,26 +95,42 @@ describe('fetchPullRequestsBySearchQuery', () => {
81
95
  },
82
96
  },
83
97
  sourceBranch: 'master',
84
- expectedTargetPullRequests: [
98
+ pullRequestStates: [
85
99
  {
86
- branch: '7.x',
100
+ branch: '7.8',
101
+ isSourceBranch: false,
102
+ label: 'v7.8.0',
103
+ mergeCommit: {
104
+ message: 'Add 🍏 emoji (#5) (#7)',
105
+ sha: '46cd6f9999effdf894a36dbc7db90e890f4be840',
106
+ },
107
+ number: 7,
87
108
  state: 'MERGED',
88
- number: 6,
89
- url: 'https://github.com/backport-org/backport-e2e/pull/6',
109
+ url: 'https://github.com/backport-org/backport-e2e/pull/7',
110
+ },
111
+ {
112
+ branch: '7.x',
113
+ isSourceBranch: false,
114
+ label: 'v7.9.0',
90
115
  mergeCommit: {
91
116
  message: 'Add 🍏 emoji (#5) (#6)',
92
117
  sha: '4bcd876d4ceaa73cf437bfc89b74d1a4e704c0a6',
93
118
  },
119
+ number: 6,
120
+ state: 'MERGED',
121
+ url: 'https://github.com/backport-org/backport-e2e/pull/6',
94
122
  },
95
123
  {
96
- branch: '7.8',
97
- state: 'MERGED',
98
- number: 7,
99
- url: 'https://github.com/backport-org/backport-e2e/pull/7',
124
+ branch: 'master',
125
+ isSourceBranch: true,
126
+ label: 'v8.0.0',
100
127
  mergeCommit: {
101
- message: 'Add 🍏 emoji (#5) (#7)',
102
- sha: '46cd6f9999effdf894a36dbc7db90e890f4be840',
128
+ message: 'Add 🍏 emoji (#5)',
129
+ sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
103
130
  },
131
+ number: 5,
132
+ state: 'MERGED',
133
+ url: 'https://github.com/backport-org/backport-e2e/pull/5',
104
134
  },
105
135
  ],
106
136
  },
@@ -48,7 +48,7 @@ export declare function getOptionsFromGithub(options: {
48
48
  publishStatusCommentOnAbort?: boolean | undefined;
49
49
  publishStatusCommentOnFailure?: boolean | undefined;
50
50
  publishStatusCommentOnSuccess?: boolean | undefined;
51
- pullNumber?: number | undefined;
51
+ pullNumber?: number | number[] | undefined;
52
52
  repoForkOwner?: string | undefined;
53
53
  repoName?: string | undefined;
54
54
  repoOwner?: string | undefined;
@@ -7,12 +7,14 @@ const strip_ansi_1 = __importDefault(require("strip-ansi"));
7
7
  const prompts_1 = require("./prompts");
8
8
  describe('prompt', () => {
9
9
  describe('getChoicesForCommitPrompt', () => {
10
- it('should display status badges via `expectedTargetPullRequests`', () => {
10
+ it('should display status badges via `pullRequestStates`', () => {
11
11
  const commits = [
12
12
  {
13
13
  author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
14
14
  sourceBranch: 'master',
15
+ suggestedTargetBranches: [],
15
16
  sourceCommit: {
17
+ branchLabelMapping: {},
16
18
  committedDate: '1',
17
19
  sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
18
20
  message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
@@ -25,10 +27,28 @@ describe('prompt', () => {
25
27
  message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
26
28
  },
27
29
  },
28
- expectedTargetPullRequests: [
29
- { number: 120178, branch: '8.0', state: 'MERGED' },
30
- { number: 120179, branch: '7.16', state: 'MERGED' },
31
- { number: 120179, branch: '7.15', state: 'NOT_CREATED' },
30
+ pullRequestStates: [
31
+ {
32
+ number: 120178,
33
+ branch: '8.0',
34
+ label: '',
35
+ isSourceBranch: false,
36
+ state: 'MERGED',
37
+ },
38
+ {
39
+ number: 120179,
40
+ branch: '7.16',
41
+ state: 'MERGED',
42
+ label: '',
43
+ isSourceBranch: false,
44
+ },
45
+ {
46
+ number: 120179,
47
+ branch: '7.15',
48
+ state: 'NOT_CREATED',
49
+ label: '',
50
+ isSourceBranch: false,
51
+ },
32
52
  ],
33
53
  },
34
54
  ];
@@ -45,7 +65,9 @@ describe('prompt', () => {
45
65
  {
46
66
  author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
47
67
  sourceBranch: 'master',
68
+ suggestedTargetBranches: [],
48
69
  sourceCommit: {
70
+ branchLabelMapping: {},
49
71
  committedDate: '',
50
72
  sha: 'b1b491959dab47aeb83c88ee2accb2db46d23793',
51
73
  message: '[APM] Prefer service.name for logs correlation (#120694)',
@@ -58,12 +80,14 @@ describe('prompt', () => {
58
80
  message: '[APM] Prefer service.name for logs correlation (#120694)',
59
81
  },
60
82
  },
61
- expectedTargetPullRequests: [],
83
+ pullRequestStates: [],
62
84
  },
63
85
  {
64
86
  author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
65
87
  sourceBranch: 'master',
88
+ suggestedTargetBranches: [],
66
89
  sourceCommit: {
90
+ branchLabelMapping: {},
67
91
  committedDate: '',
68
92
  sha: 'b1bb4a93959f19a653b9cfb207a5c6acb6559482',
69
93
  message: '[APM] Disable telemetry in agent config endpoint (#120106)',
@@ -76,12 +100,14 @@ describe('prompt', () => {
76
100
  message: '[APM] Disable telemetry in agent config endpoint (#120106)',
77
101
  },
78
102
  },
79
- expectedTargetPullRequests: [],
103
+ pullRequestStates: [],
80
104
  },
81
105
  {
82
106
  author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
83
107
  sourceBranch: 'master',
108
+ suggestedTargetBranches: [],
84
109
  sourceCommit: {
110
+ branchLabelMapping: {},
85
111
  committedDate: '',
86
112
  sha: '434f6e6a88faf24dc1ea41f9f726db78e46355a7',
87
113
  message: '[APM] Remove index_pattern.json and add custom field formatters (#119915)',
@@ -94,12 +120,14 @@ describe('prompt', () => {
94
120
  message: '[APM] Remove index_pattern.json and add custom field formatters (#119915)',
95
121
  },
96
122
  },
97
- expectedTargetPullRequests: [],
123
+ pullRequestStates: [],
98
124
  },
99
125
  {
100
126
  author: { email: 'soren.louv@elastic.co', name: 'Søren Louv-Jansen' },
101
127
  sourceBranch: 'master',
128
+ suggestedTargetBranches: [],
102
129
  sourceCommit: {
130
+ branchLabelMapping: {},
103
131
  committedDate: '',
104
132
  sha: 'ae9d51b7fe3ee6f30d0d196c782e0dcabb7ac5ff',
105
133
  message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
@@ -112,7 +140,7 @@ describe('prompt', () => {
112
140
  message: '[APM] Remove log-log descriptions from correlation charts (#119700)',
113
141
  },
114
142
  },
115
- expectedTargetPullRequests: [],
143
+ pullRequestStates: [],
116
144
  },
117
145
  ];
118
146
  const choices = (0, prompts_1.getChoicesForCommitPrompt)(commits, false).map(({ name, short }) => ({ name: (0, strip_ansi_1.default)(name), short }));
@@ -44,7 +44,9 @@ function getPrLink(number, url) {
44
44
  return url ? `(${(0, terminal_link_1.default)(`#${number}`, url)})` : '';
45
45
  }
46
46
  function getDetailedPullStatus(c) {
47
- const items = c.expectedTargetPullRequests.map((pr) => {
47
+ const items = c.pullRequestStates
48
+ .filter(({ isSourceBranch }) => !isSourceBranch)
49
+ .map((pr) => {
48
50
  const prLink = getPrLink(pr.number, pr.url);
49
51
  return ` └ ${getPrStateIcon(pr.state)} ${pr.branch} ${prLink} ${getPrStateText(pr.state)}`;
50
52
  });
@@ -54,7 +56,8 @@ function getDetailedPullStatus(c) {
54
56
  return `${list}\n`;
55
57
  }
56
58
  function getSimplePullStatus(c) {
57
- return c.expectedTargetPullRequests
59
+ return c.pullRequestStates
60
+ .filter(({ isSourceBranch }) => !isSourceBranch)
58
61
  .map(({ state, branch }) => {
59
62
  if (state === 'MERGED') {
60
63
  return chalk_1.default.green(branch);
@@ -9,11 +9,11 @@ const ora_1 = require("./ora");
9
9
  async function setupRepo(options) {
10
10
  const repoPath = (0, env_1.getRepoPath)(options);
11
11
  const isAlreadyCloned = await getIsRepoCloned(options);
12
- if (options.cwd.includes(repoPath)) {
13
- throw new BackportError_1.BackportError(`Refusing to clone repo into "${repoPath}" when current working directory is "${options.cwd}". Please change backport directory via \`--dir\` option or run backport from another location`);
14
- }
15
12
  // clone repo if folder does not already exists
16
13
  if (!isAlreadyCloned) {
14
+ if (options.cwd.includes(repoPath)) {
15
+ throw new BackportError_1.BackportError(`Refusing to clone repo into "${repoPath}" when current working directory is "${options.cwd}". Please change backport directory via \`--dir\` option or run backport from another location`);
16
+ }
17
17
  const spinner = (0, ora_1.ora)(options.interactive).start();
18
18
  try {
19
19
  const localRepoPath = await (0, git_1.getLocalSourceRepoPath)(options);
@@ -47,7 +47,7 @@ describe('setupRepo', () => {
47
47
  it('should delete repo', async () => {
48
48
  expect.assertions(2);
49
49
  jest
50
- .spyOn(childProcess, 'spawnOriginal')
50
+ .spyOn(childProcess, 'spawnStream')
51
51
  .mockImplementation((cmd, cmdArgs) => {
52
52
  if (cmdArgs.includes('clone')) {
53
53
  throw new Error('Simulated git clone failure');
@@ -74,7 +74,7 @@ describe('setupRepo', () => {
74
74
  .spyOn(gitModule, 'getLocalSourceRepoPath')
75
75
  .mockResolvedValue(undefined);
76
76
  jest
77
- .spyOn(childProcess, 'spawnOriginal')
77
+ .spyOn(childProcess, 'spawnStream')
78
78
  //@ts-expect-error
79
79
  .mockImplementation(() => {
80
80
  return {
@@ -194,7 +194,7 @@ describe('setupRepo', () => {
194
194
  });
195
195
  function mockGitClone() {
196
196
  jest
197
- .spyOn(childProcess, 'spawnOriginal')
197
+ .spyOn(childProcess, 'spawnStream')
198
198
  //@ts-expect-error
199
199
  .mockImplementation((cmd, cmdArgs) => {
200
200
  if (cmdArgs.includes('clone')) {
@@ -227,7 +227,7 @@ describe('setupRepo', () => {
227
227
  });
228
228
  it('should clone it from github.com', async () => {
229
229
  expect(spinnerSuccessSpy).toHaveBeenCalledWith('100% Cloning repository from github.com (one-time operation)');
230
- expect(childProcess.spawnOriginal).toHaveBeenCalledWith('git', [
230
+ expect(childProcess.spawnStream).toHaveBeenCalledWith('git', [
231
231
  'clone',
232
232
  'https://x-access-token:myAccessToken@github.com/elastic/kibana.git',
233
233
  '/myHomeDir/.backport/repositories/elastic/kibana',
@@ -256,7 +256,7 @@ describe('setupRepo', () => {
256
256
  });
257
257
  it('should clone it from local folder', async () => {
258
258
  expect(spinnerSuccessSpy).toHaveBeenCalledWith('100% Cloning repository from /path/to/source/repo (one-time operation)');
259
- expect(childProcess.spawnOriginal).toHaveBeenCalledWith('git', [
259
+ expect(childProcess.spawnStream).toHaveBeenCalledWith('git', [
260
260
  'clone',
261
261
  '/path/to/source/repo',
262
262
  '/myHomeDir/.backport/repositories/elastic/kibana',
@@ -0,0 +1,27 @@
1
+ import { ValidConfigOptions } from '../../options/options';
2
+ import { SourceCommitWithTargetPullRequest } from './parseSourceCommit';
3
+ export declare type PullRequestState = 'OPEN' | 'CLOSED' | 'MERGED' | 'NOT_CREATED';
4
+ declare type CreatedPullRequest = {
5
+ url: string;
6
+ number: number;
7
+ branch: string;
8
+ state: PullRequestState;
9
+ mergeCommit?: {
10
+ sha: string;
11
+ message: string;
12
+ };
13
+ };
14
+ declare type TargetBranchWithLabel = {
15
+ branch: string;
16
+ label: string;
17
+ isSourceBranch: boolean;
18
+ };
19
+ export declare type TargetPullRequest = (CreatedPullRequest & Partial<TargetBranchWithLabel>) | ((TargetBranchWithLabel & Partial<CreatedPullRequest>) & {
20
+ state: PullRequestState;
21
+ });
22
+ export declare function getSourceCommitBranchLabelMapping(sourceCommit: SourceCommitWithTargetPullRequest): ValidConfigOptions['branchLabelMapping'];
23
+ export declare function getPullRequestStates({ sourceCommit, branchLabelMapping, }: {
24
+ sourceCommit: SourceCommitWithTargetPullRequest;
25
+ branchLabelMapping: ValidConfigOptions['branchLabelMapping'];
26
+ }): TargetPullRequest[];
27
+ export {};
@@ -1,30 +1,66 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getExpectedTargetPullRequests = void 0;
3
+ exports.getPullRequestStates = exports.getSourceCommitBranchLabelMapping = void 0;
4
4
  const lodash_1 = require("lodash");
5
5
  const filterEmpty_1 = require("../../utils/filterEmpty");
6
6
  const commitFormatters_1 = require("../github/commitFormatters");
7
7
  const remoteConfig_1 = require("../remoteConfig");
8
- function getExpectedTargetPullRequests({ sourceCommit, latestBranchLabelMapping, }) {
9
- const sourcePullRequest = sourceCommit.associatedPullRequests.edges?.[0]?.node;
8
+ function getSourcePullRequest(sourceCommit) {
9
+ return sourceCommit.associatedPullRequests.edges?.[0]?.node;
10
+ }
11
+ function getSourceCommitBranchLabelMapping(sourceCommit) {
12
+ const sourcePullRequest = getSourcePullRequest(sourceCommit);
10
13
  const remoteConfig = sourcePullRequest?.mergeCommit.remoteConfigHistory.edges?.[0]?.remoteConfig;
11
- const branchLabelMapping = (remoteConfig && (0, remoteConfig_1.parseRemoteConfig)(remoteConfig)?.branchLabelMapping) ??
12
- latestBranchLabelMapping;
14
+ if (remoteConfig) {
15
+ return (0, remoteConfig_1.parseRemoteConfig)(remoteConfig)?.branchLabelMapping;
16
+ }
17
+ }
18
+ exports.getSourceCommitBranchLabelMapping = getSourceCommitBranchLabelMapping;
19
+ function mergeByKey(obj1, obj2, key) {
20
+ const merged = (0, lodash_1.merge)((0, lodash_1.keyBy)(obj1, key), (0, lodash_1.keyBy)(obj2, key));
21
+ const a = (0, lodash_1.values)(merged);
22
+ return a;
23
+ }
24
+ function getPullRequestStates({ sourceCommit, branchLabelMapping, }) {
25
+ const sourcePullRequest = getSourcePullRequest(sourceCommit);
13
26
  // if there is no source pull request the commit was pushed directly to the source branch
14
27
  // in that case there will be no labels, and thus not possible to deduce the expected target branches
15
28
  if (!sourcePullRequest) {
16
29
  return [];
17
30
  }
18
- const existingTargetPullRequests = getExistingTargetPullRequests(sourceCommit, sourcePullRequest);
19
- // if there's no `branchLabelMapping`, it's not possible to deduce the missing target branches
31
+ const createdTargetPullRequests = getCreatedTargetPullRequests(sourceCommit);
32
+ // if there's no `branchLabelMapping`, it's not possible to determine the missing target branches
20
33
  if (!branchLabelMapping) {
21
- return existingTargetPullRequests;
34
+ return createdTargetPullRequests;
22
35
  }
23
- const missingTargetPullRequests = getMissingTargetPullRequests(sourcePullRequest, existingTargetPullRequests, branchLabelMapping);
24
- return [...existingTargetPullRequests, ...missingTargetPullRequests];
36
+ const targetBranchesFromLabels = getTargetBranchesFromLabels(sourcePullRequest, branchLabelMapping);
37
+ return mergeByKey(targetBranchesFromLabels, createdTargetPullRequests, 'branch').map((res) => {
38
+ if (res.state) {
39
+ return { ...res, state: res.state };
40
+ }
41
+ // MERGED (source branch)
42
+ if (res.isSourceBranch) {
43
+ return {
44
+ ...res,
45
+ state: 'MERGED',
46
+ url: sourcePullRequest.url,
47
+ number: sourcePullRequest.number,
48
+ mergeCommit: {
49
+ message: sourcePullRequest.mergeCommit.message,
50
+ sha: sourcePullRequest.mergeCommit.sha,
51
+ },
52
+ };
53
+ }
54
+ // NOT_CREATED
55
+ return { ...res, state: 'NOT_CREATED' };
56
+ });
25
57
  }
26
- exports.getExpectedTargetPullRequests = getExpectedTargetPullRequests;
27
- function getExistingTargetPullRequests(sourceCommit, sourcePullRequest) {
58
+ exports.getPullRequestStates = getPullRequestStates;
59
+ function getCreatedTargetPullRequests(sourceCommit) {
60
+ const sourcePullRequest = getSourcePullRequest(sourceCommit);
61
+ if (!sourcePullRequest) {
62
+ return [];
63
+ }
28
64
  const sourceCommitMessage = (0, commitFormatters_1.getFirstLine)(sourceCommit.message);
29
65
  return sourcePullRequest.timelineItems.edges
30
66
  .filter(filterEmpty_1.filterNil)
@@ -65,26 +101,25 @@ function getExistingTargetPullRequests(sourceCommit, sourcePullRequest) {
65
101
  };
66
102
  });
67
103
  }
68
- function getMissingTargetPullRequests(sourcePullRequest, existingTargetPullRequests, branchLabelMapping) {
69
- const labels = sourcePullRequest.labels.nodes.map((label) => label.name);
70
- const targetBranchesFromLabels = labels
71
- .map((label) => getTargetBranchForLabel({ branchLabelMapping, label }))
72
- .filter(filterEmpty_1.filterNil)
73
- .filter((targetBranch) => targetBranch !== sourcePullRequest.baseRefName);
74
- const expectedTargetBranches = existingTargetPullRequests.map((pr) => pr.branch);
75
- const expected = (0, lodash_1.uniq)(targetBranchesFromLabels);
76
- return expected
77
- .filter((targetBranch) => !expectedTargetBranches.includes(targetBranch))
78
- .map((branch) => {
79
- return { branch, state: 'NOT_CREATED' };
80
- });
81
- }
82
104
  // narrow TimelineEdge to TimelinePullRequestEdge
83
105
  function filterPullRequests(item) {
84
106
  const { targetPullRequest } = item.node;
85
107
  return targetPullRequest.__typename === 'PullRequest';
86
108
  }
87
- function getTargetBranchForLabel({ branchLabelMapping, label, }) {
109
+ function getTargetBranchesFromLabels(sourcePullRequest, branchLabelMapping) {
110
+ const targetBranchesFromLabels = sourcePullRequest.labels.nodes
111
+ .map((label) => label.name)
112
+ .map((label) => {
113
+ const branch = getTargetBranchFromLabel({ branchLabelMapping, label });
114
+ if (branch) {
115
+ const isSourceBranch = branch === sourcePullRequest.baseRefName;
116
+ return { branch, label, isSourceBranch };
117
+ }
118
+ })
119
+ .filter(filterEmpty_1.filterNil);
120
+ return (0, lodash_1.uniqBy)(targetBranchesFromLabels, ({ branch }) => branch);
121
+ }
122
+ function getTargetBranchFromLabel({ branchLabelMapping, label, }) {
88
123
  // only get first match
89
124
  const result = Object.entries(branchLabelMapping).find(([labelPattern]) => {
90
125
  const regex = new RegExp(labelPattern);