backport 6.1.0 → 6.1.4

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 (63) hide show
  1. package/README.md +45 -34
  2. package/dist/entrypoint.module.d.ts +2 -0
  3. package/dist/entrypoint.module.js +5 -1
  4. package/dist/entrypoint.module.private.test.js +16 -12
  5. package/dist/main.js +1 -1
  6. package/dist/options/ConfigOptions.d.ts +1 -0
  7. package/dist/options/cliArgs.d.ts +5 -3
  8. package/dist/options/cliArgs.js +20 -6
  9. package/dist/options/cliArgs.test.js +0 -7
  10. package/dist/options/config/config.d.ts +1 -0
  11. package/dist/options/config/globalConfig.js +1 -1
  12. package/dist/options/config/readConfigFile.d.ts +1 -0
  13. package/dist/options/options.d.ts +8 -0
  14. package/dist/options/options.js +9 -4
  15. package/dist/options/options.test.js +150 -104
  16. package/dist/runSequentially.test.js +13 -6
  17. package/dist/services/HandledError.d.ts +1 -1
  18. package/dist/services/git.d.ts +2 -1
  19. package/dist/services/git.js +4 -2
  20. package/dist/services/github/v3/createPullRequest.js +1 -1
  21. package/dist/services/github/v3/createPullRequest.test.js +38 -6
  22. package/dist/services/github/v3/createStatusComment.d.ts +1 -1
  23. package/dist/services/github/v3/createStatusComment.js +43 -25
  24. package/dist/services/github/v3/createStatusComment.test.js +155 -73
  25. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  26. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +78 -0
  27. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  28. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +64 -0
  29. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +5 -5
  30. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +60 -0
  31. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +2 -0
  32. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +17 -10
  33. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +72 -0
  34. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +6 -0
  35. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +8 -7
  36. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +8 -0
  37. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +1 -0
  38. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +16 -5
  39. package/dist/services/sourceCommit/getExpectedTargetPullRequests.d.ts +4 -0
  40. package/dist/services/sourceCommit/getExpectedTargetPullRequests.js +8 -2
  41. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +37 -0
  42. package/dist/services/sourceCommit/getMockSourceCommit.d.ts +1 -0
  43. package/dist/services/sourceCommit/getMockSourceCommit.js +72 -46
  44. package/dist/services/sourceCommit/parseSourceCommit.d.ts +22 -8
  45. package/dist/services/sourceCommit/parseSourceCommit.js +29 -10
  46. package/dist/services/sourceCommit/parseSourceCommit.test.js +10 -2
  47. package/dist/test/inquirer.private.test.js +4 -15
  48. package/dist/test/integration/integration.private.test.js +14 -4
  49. package/dist/test/nockHelpers.js +1 -3
  50. package/dist/test/setupFiles/automatic-mocks.js +1 -0
  51. package/dist/ui/cherrypickAndCreatePullRequest.test.js +19 -6
  52. package/dist/ui/cherrypickAndCreateTargetPullRequest.d.ts +0 -9
  53. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +11 -48
  54. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +224 -260
  55. package/dist/ui/getCommits.js +1 -1
  56. package/dist/ui/getCommitsWithoutBackports.d.ts +11 -0
  57. package/dist/ui/getCommitsWithoutBackports.js +68 -0
  58. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  59. package/dist/ui/getCommitsWithoutBackports.test.js +259 -0
  60. package/dist/utils/excludeUndefined.js +1 -1
  61. package/dist/utils/excludeUndefined.test.js +3 -3
  62. package/package.json +3 -3
  63. package/yarn.lock +462 -284
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getCommitsWithoutBackports = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const git_1 = require("../services/git");
9
+ const commitFormatters_1 = require("../services/github/commitFormatters");
10
+ const fetchCommitsByAuthor_1 = require("../services/github/v4/fetchCommits/fetchCommitsByAuthor");
11
+ // when the user is facing a git conflict we should help them understand
12
+ // why the conflict occurs. In many cases it's because one or more commits haven't been backported yet
13
+ async function getCommitsWithoutBackports({ options,
14
+ // commit that is being backported
15
+ commit,
16
+ // branch the commit is being backported to
17
+ targetBranch,
18
+ // the files with conflicts on the target branch
19
+ conflictingFiles, }) {
20
+ // commits on the source branch that also touched the conflicting files
21
+ const commitsInConflictingPaths = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
22
+ ...options,
23
+ author: null,
24
+ dateSince: null,
25
+ dateUntil: commit.committedDate,
26
+ commitPaths: conflictingFiles,
27
+ });
28
+ const promises = await Promise.all(commitsInConflictingPaths
29
+ .filter((c) => {
30
+ // exclude the commit we are currently trying to backport
31
+ if (c.sha === commit.sha) {
32
+ return false;
33
+ }
34
+ // exclude commits that are newer than the commit we are trying to backport
35
+ if (c.committedDate > commit.committedDate) {
36
+ return false;
37
+ }
38
+ // only consider commits that have an associated pull request
39
+ if (!c.pullUrl) {
40
+ return false;
41
+ }
42
+ // only include commit if it has an unmerged PR for the given target branch
43
+ const hasUnmergedPr = c.expectedTargetPullRequests.some((pr) => pr.branch === targetBranch && pr.state !== 'MERGED');
44
+ return hasUnmergedPr;
45
+ })
46
+ .slice(0, 10) // limit to max 10 commits
47
+ .map(async (c) => {
48
+ const results = await Promise.all(c.expectedTargetPullRequests.map(async (targetPr) => {
49
+ if (!targetPr.mergeCommit) {
50
+ return false;
51
+ }
52
+ return (0, git_1.getIsCommitInBranch)(options, targetPr.mergeCommit.sha);
53
+ }));
54
+ const isCommitInBranch = results.some((inBranch) => inBranch === true);
55
+ return { c, isCommitInBranch };
56
+ }));
57
+ return promises
58
+ .filter(({ isCommitInBranch }) => !isCommitInBranch)
59
+ .map(({ c }) => {
60
+ // get pull request for the target branch (if it exists)
61
+ const pendingBackportPr = c.expectedTargetPullRequests.find((pr) => pr.branch === targetBranch && pr.state === 'OPEN');
62
+ const formatted = pendingBackportPr
63
+ ? ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.gray('(backport pending)')}\n ${pendingBackportPr.url}`
64
+ : ` - ${(0, commitFormatters_1.getFirstLine)(c.originalMessage)} ${chalk_1.default.red('(backport missing)')}\n ${c.pullUrl}`;
65
+ return { formatted, commit: c };
66
+ });
67
+ }
68
+ exports.getCommitsWithoutBackports = getCommitsWithoutBackports;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ var __importDefault = (this && this.__importDefault) || function (mod) {
22
+ return (mod && mod.__esModule) ? mod : { "default": mod };
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const strip_ansi_1 = __importDefault(require("strip-ansi"));
26
+ const git = __importStar(require("../services/git"));
27
+ const fetchCommitsByAuthorModule = __importStar(require("../services/github/v4/fetchCommits/fetchCommitsByAuthor"));
28
+ const getCommitsWithoutBackports_1 = require("./getCommitsWithoutBackports");
29
+ describe('getCommitsWithoutBackports', () => {
30
+ afterEach(() => {
31
+ jest.restoreAllMocks();
32
+ });
33
+ describe('expectedTargetPullRequests', () => {
34
+ function setupExpectedPullRequests({ expectedTargetPullRequests, }) {
35
+ // simulate 1 unbackported commit
36
+ jest
37
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
38
+ .mockResolvedValueOnce([
39
+ {
40
+ committedDate: '10',
41
+ expectedTargetPullRequests,
42
+ originalMessage: 'First commit (#1)',
43
+ sha: 'xyz',
44
+ sourceBranch: 'main',
45
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
46
+ },
47
+ ]);
48
+ // simulate commit is definitely not backported
49
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
50
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
51
+ options: {},
52
+ commit: {
53
+ committedDate: '100',
54
+ expectedTargetPullRequests: [],
55
+ originalMessage: 'Second commit (#2)',
56
+ sha: 'abc',
57
+ sourceBranch: 'main',
58
+ },
59
+ targetBranch: '7.x',
60
+ conflictingFiles: ['/foo/bar/baz.ts'],
61
+ });
62
+ }
63
+ it('should not unbackported commits if no backports were expected', async () => {
64
+ const commitsWithoutBackports = await setupExpectedPullRequests({
65
+ expectedTargetPullRequests: [],
66
+ });
67
+ expect(commitsWithoutBackports.length).toEqual(0);
68
+ });
69
+ it('should display as missing if a backport is CLOSED', async () => {
70
+ const commitsWithoutBackports = await setupExpectedPullRequests({
71
+ expectedTargetPullRequests: [
72
+ {
73
+ state: 'CLOSED',
74
+ branch: '7.x',
75
+ number: 456,
76
+ url: 'https://www.github.com/foo/bar/pull/456',
77
+ },
78
+ ],
79
+ });
80
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport missing)\n https://www.github.com/foo/bar/pull/123');
81
+ });
82
+ it('should display as missing if a backport is MISSING', async () => {
83
+ const commitsWithoutBackports = await setupExpectedPullRequests({
84
+ expectedTargetPullRequests: [{ state: 'MISSING', branch: '7.x' }],
85
+ });
86
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport missing)\n https://www.github.com/foo/bar/pull/123');
87
+ });
88
+ it('should display as pending if a backport is OPEN', async () => {
89
+ const commitsWithoutBackports = await setupExpectedPullRequests({
90
+ expectedTargetPullRequests: [
91
+ {
92
+ state: 'OPEN',
93
+ branch: '7.x',
94
+ url: 'https://www.github.com/foo/bar/pull/456',
95
+ number: 456,
96
+ },
97
+ ],
98
+ });
99
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
100
+ });
101
+ it('should not display commit as unbackported if a backport was MERGED', async () => {
102
+ const commitsWithoutBackports = await setupExpectedPullRequests({
103
+ expectedTargetPullRequests: [
104
+ {
105
+ state: 'MERGED',
106
+ branch: '7.x',
107
+ number: 456,
108
+ url: 'https://www.github.com/foo/bar/pull/456',
109
+ },
110
+ ],
111
+ });
112
+ expect(commitsWithoutBackports).toEqual([]);
113
+ });
114
+ });
115
+ describe('commitDate', () => {
116
+ function setupCommitDateDiff({ offendingCommitDate, currentCommitDate, }) {
117
+ // simulate 1 commit with a pending backport
118
+ jest
119
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
120
+ .mockResolvedValueOnce([
121
+ {
122
+ committedDate: offendingCommitDate,
123
+ expectedTargetPullRequests: [
124
+ {
125
+ state: 'OPEN',
126
+ branch: '7.x',
127
+ number: 456,
128
+ url: 'https://www.github.com/foo/bar/pull/456',
129
+ },
130
+ ],
131
+ originalMessage: 'First commit (#1)',
132
+ sha: 'xyz',
133
+ sourceBranch: 'main',
134
+ pullNumber: 123,
135
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
136
+ },
137
+ ]);
138
+ // simulate commit is definitely not backported
139
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
140
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
141
+ options: {},
142
+ commit: {
143
+ committedDate: currentCommitDate,
144
+ expectedTargetPullRequests: [],
145
+ originalMessage: 'Second commit (#2)',
146
+ sha: 'abc',
147
+ sourceBranch: 'main',
148
+ },
149
+ targetBranch: '7.x',
150
+ conflictingFiles: ['/foo/bar/baz.ts'],
151
+ });
152
+ }
153
+ it('should not display commit as unbackport if is newer than the commit currently being backported', async () => {
154
+ const commitsWithoutBackports = await setupCommitDateDiff({
155
+ offendingCommitDate: '100',
156
+ currentCommitDate: '10',
157
+ });
158
+ expect(commitsWithoutBackports).toEqual([]);
159
+ });
160
+ it('should display commit as unbackport if is older than the commit currently being backported', async () => {
161
+ const commitsWithoutBackports = await setupCommitDateDiff({
162
+ offendingCommitDate: '10',
163
+ currentCommitDate: '100',
164
+ });
165
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
166
+ });
167
+ });
168
+ describe('targetBranch', () => {
169
+ function setupTargetBranchDiff({ offendingCommitTargetBranch, currentCommitTargetBranch, }) {
170
+ // return mock commits that also touched the conflicting files
171
+ jest
172
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
173
+ .mockResolvedValueOnce([
174
+ {
175
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
176
+ pullNumber: 123,
177
+ committedDate: '10',
178
+ expectedTargetPullRequests: [
179
+ {
180
+ branch: offendingCommitTargetBranch,
181
+ state: 'OPEN',
182
+ url: 'https://www.github.com/foo/bar/pull/456',
183
+ number: 456,
184
+ },
185
+ ],
186
+ originalMessage: 'First commit (#1)',
187
+ sha: 'xyz',
188
+ sourceBranch: 'main',
189
+ },
190
+ ]);
191
+ // simulate commit is definitely not backported
192
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
193
+ return (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
194
+ options: {},
195
+ // commit that is being backported
196
+ commit: {
197
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
198
+ pullNumber: 123,
199
+ committedDate: '100',
200
+ expectedTargetPullRequests: [],
201
+ originalMessage: 'Second commit (#2)',
202
+ sha: 'abc',
203
+ sourceBranch: 'main',
204
+ },
205
+ targetBranch: currentCommitTargetBranch,
206
+ conflictingFiles: ['/foo/bar/baz.ts'],
207
+ });
208
+ }
209
+ it('should display backport as pending if target branches match', async () => {
210
+ const commitsWithoutBackports = await setupTargetBranchDiff({
211
+ offendingCommitTargetBranch: '7.x',
212
+ currentCommitTargetBranch: '7.x',
213
+ });
214
+ expect((0, strip_ansi_1.default)(commitsWithoutBackports[0].formatted)).toEqual(' - First commit (#1) (backport pending)\n https://www.github.com/foo/bar/pull/456');
215
+ });
216
+ it("should not display commit if target branches don't match", async () => {
217
+ const commitsWithoutBackports = await setupTargetBranchDiff({
218
+ offendingCommitTargetBranch: '7.x',
219
+ currentCommitTargetBranch: '8.x',
220
+ });
221
+ expect(commitsWithoutBackports.length).toBe(0);
222
+ });
223
+ });
224
+ describe("when conflicting commit doesn't have an associated pull request", () => {
225
+ it('should be ignored', async () => {
226
+ jest
227
+ .spyOn(fetchCommitsByAuthorModule, 'fetchCommitsByAuthor')
228
+ .mockResolvedValueOnce([
229
+ {
230
+ // intentionally left out:
231
+ // pullNumber: 33,
232
+ // pullUrl: 'https://www.github.com/foo/bar/pull/123',
233
+ committedDate: '10',
234
+ expectedTargetPullRequests: [],
235
+ originalMessage: 'First commit (#1)',
236
+ sha: 'xyz',
237
+ sourceBranch: 'main',
238
+ },
239
+ ]);
240
+ // simulate commit is definitely not backported
241
+ jest.spyOn(git, 'getIsCommitInBranch').mockResolvedValueOnce(false);
242
+ const commitsWithoutBackports = await (0, getCommitsWithoutBackports_1.getCommitsWithoutBackports)({
243
+ options: {},
244
+ commit: {
245
+ pullUrl: 'https://www.github.com/foo/bar/pull/123',
246
+ pullNumber: 123,
247
+ committedDate: '100',
248
+ expectedTargetPullRequests: [],
249
+ originalMessage: 'Second commit (#2)',
250
+ sha: 'abc',
251
+ sourceBranch: 'main',
252
+ },
253
+ targetBranch: '7.x',
254
+ conflictingFiles: ['/foo/bar/baz.ts'],
255
+ });
256
+ expect(commitsWithoutBackports.length).toEqual(0);
257
+ });
258
+ });
259
+ });
@@ -2,6 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.excludeUndefined = void 0;
4
4
  function excludeUndefined(obj) {
5
- return Object.fromEntries(Object.entries(obj).filter(([, v]) => v != null));
5
+ return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== undefined));
6
6
  }
7
7
  exports.excludeUndefined = excludeUndefined;
@@ -7,9 +7,9 @@ describe('excludeUndefined', () => {
7
7
  foo: 'foo',
8
8
  bar: undefined,
9
9
  baz: null,
10
- })).toEqual({ foo: 'foo' });
10
+ })).toEqual({ foo: 'foo', baz: null });
11
11
  });
12
- it('has the correct types', () => {
12
+ it('has the correct types after merging', () => {
13
13
  const obj = (0, excludeUndefined_1.excludeUndefined)({
14
14
  foo: 'foo',
15
15
  bar: undefined,
@@ -19,6 +19,6 @@ describe('excludeUndefined', () => {
19
19
  bar: 'bar',
20
20
  ...obj,
21
21
  };
22
- expect(res).toEqual({ bar: 'bar', foo: 'foo' });
22
+ expect(res).toEqual({ bar: 'bar', foo: 'foo', baz: null });
23
23
  });
24
24
  });
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "branches",
15
15
  "branching"
16
16
  ],
17
- "version": "6.1.0",
17
+ "version": "6.1.4",
18
18
  "main": "./dist/entrypoint.module.js",
19
19
  "types": "dist/entrypoint.module.d.ts",
20
20
  "bin": {
@@ -95,9 +95,9 @@
95
95
  "graphql-config": "^4.1.0",
96
96
  "graphql-tag": "^2.12.6",
97
97
  "husky": "^7.0.4",
98
- "jest": "^27.4.5",
98
+ "jest": "^27.4.7",
99
99
  "jest-snapshot-serializer-ansi": "^1.0.0",
100
- "lint-staged": "^12.1.5",
100
+ "lint-staged": "^12.1.7",
101
101
  "nock": "^13.2.1",
102
102
  "prettier": "^2.5.1",
103
103
  "strip-ansi": "^6.0.1",