backport 6.0.0 → 6.1.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 (98) hide show
  1. package/README.md +158 -42
  2. package/dist/entrypoint.cli.js +2 -0
  3. package/dist/entrypoint.module.d.ts +16 -4
  4. package/dist/entrypoint.module.js +41 -13
  5. package/dist/entrypoint.module.private.test.d.ts +1 -0
  6. package/dist/entrypoint.module.private.test.js +33 -0
  7. package/dist/main.js +1 -2
  8. package/dist/options/ConfigOptions.d.ts +15 -6
  9. package/dist/options/cliArgs.d.ts +33 -34
  10. package/dist/options/cliArgs.js +19 -19
  11. package/dist/options/cliArgs.test.js +8 -5
  12. package/dist/options/config/config.d.ts +27 -52
  13. package/dist/options/config/config.js +2 -36
  14. package/dist/options/config/config.test.js +10 -27
  15. package/dist/options/config/getOptionsFromGit.d.ts +3 -2
  16. package/dist/options/config/getOptionsFromGit.js +1 -3
  17. package/dist/options/config/globalConfig.js +1 -4
  18. package/dist/options/config/globalConfig.test.js +0 -2
  19. package/dist/options/config/projectConfig.test.js +51 -14
  20. package/dist/options/config/readConfigFile.d.ts +51 -0
  21. package/dist/options/config/readConfigFile.js +28 -2
  22. package/dist/options/options.d.ts +52 -33
  23. package/dist/options/options.js +98 -7
  24. package/dist/options/options.test.js +180 -73
  25. package/dist/options/parseRequiredOptions.d.ts +3 -3
  26. package/dist/options/parseRequiredOptions.js +2 -4
  27. package/dist/options/parseRequiredOptions.test.js +6 -11
  28. package/dist/runSequentially.test.js +35 -78
  29. package/dist/services/git.d.ts +5 -2
  30. package/dist/services/git.js +11 -10
  31. package/dist/services/git.test.js +12 -9
  32. package/dist/services/github/v3/addAssigneesToPullRequest.d.ts +1 -1
  33. package/dist/services/github/v3/addAssigneesToPullRequest.js +2 -3
  34. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +4 -5
  35. package/dist/services/github/v3/createPullRequest.js +10 -13
  36. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  37. package/dist/services/github/v3/createPullRequest.test.js +78 -0
  38. package/dist/services/github/v4/apiRequestV4.d.ts +1 -1
  39. package/dist/services/github/v4/apiRequestV4.js +1 -1
  40. package/dist/services/github/v4/enablePullRequestAutoMerge.js +1 -1
  41. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +0 -1
  42. package/dist/services/github/v4/fetchAuthorId.d.ts +5 -2
  43. package/dist/services/github/v4/fetchAuthorId.js +5 -6
  44. package/dist/services/github/v4/fetchAuthorId.private.test.js +3 -6
  45. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.d.ts +8 -1
  46. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.js +1 -1
  47. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +6 -3
  48. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.d.ts +8 -1
  49. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.js +1 -1
  50. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +3 -3
  51. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.d.ts +12 -1
  52. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.js +6 -8
  53. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +10 -12
  54. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +5 -7
  55. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.d.ts +3 -0
  56. package/dist/services/github/v4/fetchCommits/fetchProjectConfig.js +55 -0
  57. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.js +5 -5
  58. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +2 -6
  59. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +0 -2
  60. package/dist/services/github/v4/fetchProjectConfig.d.ts +8 -0
  61. package/dist/services/github/v4/fetchProjectConfig.js +56 -0
  62. package/dist/services/github/v4/fetchProjectConfig.private.test.d.ts +1 -0
  63. package/dist/services/github/v4/fetchProjectConfig.private.test.js +52 -0
  64. package/dist/services/github/v4/fetchProjectConfig.test.d.ts +1 -0
  65. package/dist/services/github/v4/fetchProjectConfig.test.js +37 -0
  66. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +66 -0
  67. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +130 -0
  68. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  69. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +130 -0
  70. package/dist/services/github/v4/getOptionsFromGithub/index.d.ts +3 -1
  71. package/dist/services/github/v4/getOptionsFromGithub/index.private.test.js +22 -1
  72. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +4 -1
  73. package/dist/services/github/v4/getOptionsFromGithub/query.js +3 -0
  74. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +15 -15
  75. package/dist/services/sourceCommit/parseSourceCommit.d.ts +5 -1
  76. package/dist/services/sourceCommit/parseSourceCommit.js +2 -2
  77. package/dist/services/sourceCommit/parseSourceCommit.test.js +120 -5
  78. package/dist/test/inquirer.private.test.js +42 -34
  79. package/dist/test/integration/integration.private.test.js +104 -95
  80. package/dist/test/mockConfigFiles.d.ts +5 -0
  81. package/dist/test/mockConfigFiles.js +37 -0
  82. package/dist/test/nockHelpers.d.ts +1 -1
  83. package/dist/test/nockHelpers.js +4 -4
  84. package/dist/test/private/getDevAccessToken.js +1 -1
  85. package/dist/test/setupFiles/automatic-mocks.js +1 -29
  86. package/dist/ui/cherrypickAndCreatePullRequest.test.js +67 -28
  87. package/dist/ui/cherrypickAndCreateTargetPullRequest.js +6 -3
  88. package/dist/ui/getCommits.d.ts +1 -1
  89. package/dist/ui/getTargetBranches.d.ts +2 -1
  90. package/dist/ui/getTargetBranches.js +17 -1
  91. package/dist/ui/maybeSetupRepo.js +4 -3
  92. package/dist/ui/maybeSetupRepo.test.js +49 -3
  93. package/dist/utils/excludeUndefined.d.ts +6 -0
  94. package/dist/utils/excludeUndefined.js +7 -0
  95. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  96. package/dist/utils/excludeUndefined.test.js +24 -0
  97. package/package.json +9 -8
  98. package/yarn.lock +98 -77
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.pushBackportBranch = exports.getRepoForkOwner = exports.deleteBackportBranch = exports.createBackportBranch = exports.setCommitAuthor = exports.getUnstagedFiles = exports.getConflictingFiles = exports.commitChanges = exports.cherrypick = exports.fetchBranch = exports.addRemote = exports.deleteRemote = exports.getIsCommitInBranch = exports.getUpstreamFromGitRemote = exports.isLocalConfigFileModified = exports.isLocalConfigFileUntracked = exports.getLocalConfigFileCommitDate = exports.cloneRepo = exports.getRemoteUrl = exports.deleteRepo = exports.repoExists = void 0;
6
+ exports.pushBackportBranch = exports.getRepoForkOwner = exports.deleteBackportBranch = exports.createBackportBranch = exports.setCommitAuthor = exports.getUnstagedFiles = exports.getConflictingFiles = exports.commitChanges = exports.cherrypick = exports.fetchBranch = exports.addRemote = exports.deleteRemote = exports.getIsCommitInBranch = exports.getRepoOwnerAndNameFromGitRemote = exports.isLocalConfigFileModified = exports.isLocalConfigFileUntracked = exports.getLocalConfigFileCommitDate = exports.cloneRepo = exports.getRemoteUrl = exports.deleteRepo = exports.repoExists = void 0;
7
7
  const path_1 = require("path");
8
8
  const del_1 = __importDefault(require("del"));
9
9
  const lodash_1 = require("lodash");
@@ -35,7 +35,7 @@ function deleteRepo(options) {
35
35
  return (0, del_1.default)(repoPath);
36
36
  }
37
37
  exports.deleteRepo = deleteRepo;
38
- function getRemoteUrl({ repoName, accessToken, gitHostname }, repoOwner) {
38
+ function getRemoteUrl({ repoName, accessToken, gitHostname = 'github.com' }, repoOwner) {
39
39
  return `https://x-access-token:${accessToken}@${gitHostname}/${repoOwner}/${repoName}.git`;
40
40
  }
41
41
  exports.getRemoteUrl = getRemoteUrl;
@@ -97,19 +97,20 @@ async function isLocalConfigFileModified() {
97
97
  }
98
98
  }
99
99
  exports.isLocalConfigFileModified = isLocalConfigFileModified;
100
- async function getUpstreamFromGitRemote() {
100
+ async function getRepoOwnerAndNameFromGitRemote() {
101
101
  try {
102
102
  const { stdout } = await (0, child_process_promisified_1.exec)('git remote --verbose', {});
103
103
  const matches = stdout.match(/github.com[:/]{1}(.*).git/);
104
104
  if (matches !== null) {
105
- return matches[1];
105
+ const [repoOwner, repoName] = matches[1].split('/');
106
+ return { repoOwner, repoName };
106
107
  }
107
108
  }
108
109
  catch (e) {
109
110
  return;
110
111
  }
111
112
  }
112
- exports.getUpstreamFromGitRemote = getUpstreamFromGitRemote;
113
+ exports.getRepoOwnerAndNameFromGitRemote = getRepoOwnerAndNameFromGitRemote;
113
114
  async function getIsCommitInBranch(options, commitSha) {
114
115
  try {
115
116
  const cwd = (0, env_1.getRepoPath)(options);
@@ -159,7 +160,7 @@ async function fetchBranch(options, branch) {
159
160
  exports.fetchBranch = fetchBranch;
160
161
  async function cherrypick(options, sha) {
161
162
  const mainlinArg = options.mainline != undefined ? ` --mainline ${options.mainline}` : '';
162
- const cherrypickRefArg = options.cherrypickRef ? ' -x' : '';
163
+ const cherrypickRefArg = options.cherrypickRef === false ? '' : ' -x';
163
164
  const cmd = `git cherry-pick${cherrypickRefArg}${mainlinArg} ${sha}`;
164
165
  try {
165
166
  await (0, child_process_promisified_1.exec)(cmd, { cwd: (0, env_1.getRepoPath)(options) });
@@ -264,10 +265,10 @@ async function getUnstagedFiles(options) {
264
265
  return (0, lodash_1.uniq)(files);
265
266
  }
266
267
  exports.getUnstagedFiles = getUnstagedFiles;
267
- async function setCommitAuthor(options, username) {
268
- const spinner = (0, ora_1.default)(`Changing author to "${username}"`).start();
268
+ async function setCommitAuthor(options, author) {
269
+ const spinner = (0, ora_1.default)(`Changing author to "${author}"`).start();
269
270
  try {
270
- const res = await (0, child_process_promisified_1.exec)(`git commit --amend --no-edit --author "${username} <${username}@users.noreply.github.com>"`, { cwd: (0, env_1.getRepoPath)(options) });
271
+ const res = await (0, child_process_promisified_1.exec)(`git commit --amend --no-edit --author "${author} <${author}@users.noreply.github.com>"`, { cwd: (0, env_1.getRepoPath)(options) });
271
272
  spinner.succeed();
272
273
  return res;
273
274
  }
@@ -308,7 +309,7 @@ exports.deleteBackportBranch = deleteBackportBranch;
308
309
  * Returns the repo owner of the forked repo or the source repo
309
310
  */
310
311
  function getRepoForkOwner(options) {
311
- return options.fork ? options.username : options.repoOwner;
312
+ return options.fork ? options.authenticatedUsername : options.repoOwner;
312
313
  }
313
314
  exports.getRepoForkOwner = getRepoForkOwner;
314
315
  async function pushBackportBranch({ options, backportBranch, }) {
@@ -139,19 +139,22 @@ describe('isLocalConfigFileModified', () => {
139
139
  expect(await (0, git_1.isLocalConfigFileModified)()).toEqual(undefined);
140
140
  });
141
141
  });
142
- describe('getUpstreamFromGitRemote', () => {
143
- it('returns first upstream matching pattern', async () => {
142
+ describe('getRepoOwnerAndNameFromGitRemote', () => {
143
+ it('returns first matching pattern', async () => {
144
144
  const res = {
145
145
  stdout: 'origin\tgit@github.com:elastic/kibana.git (fetch)\norigin\tgit@github.com:elastic/kibana.git (push)\nsqren\tgit@github.com:sqren/kibana.git (fetch)\nsqren\tgit@github.com:sqren/kibana.git (push)\n',
146
146
  stderr: '',
147
147
  };
148
148
  jest.spyOn(childProcess, 'exec').mockResolvedValue(res);
149
- expect(await (0, git_1.getUpstreamFromGitRemote)()).toEqual('elastic/kibana');
149
+ expect(await (0, git_1.getRepoOwnerAndNameFromGitRemote)()).toEqual({
150
+ repoOwner: 'elastic',
151
+ repoName: 'kibana',
152
+ });
150
153
  });
151
154
  it('returns undefined when no remotes exist', async () => {
152
155
  const res = { stdout: '', stderr: '' };
153
156
  jest.spyOn(childProcess, 'exec').mockResolvedValue(res);
154
- expect(await (0, git_1.getUpstreamFromGitRemote)()).toEqual(undefined);
157
+ expect(await (0, git_1.getRepoOwnerAndNameFromGitRemote)()).toEqual(undefined);
155
158
  });
156
159
  it('handles errors', async () => {
157
160
  const err = {
@@ -163,7 +166,7 @@ describe('getUpstreamFromGitRemote', () => {
163
166
  stderr: 'fatal: not a git repository (or any of the parent directories): .git\n',
164
167
  };
165
168
  jest.spyOn(childProcess, 'exec').mockRejectedValueOnce(err);
166
- expect(await (0, git_1.getUpstreamFromGitRemote)()).toEqual(undefined);
169
+ expect(await (0, git_1.getRepoOwnerAndNameFromGitRemote)()).toEqual(undefined);
167
170
  });
168
171
  });
169
172
  describe('getConflictingFiles', () => {
@@ -277,7 +280,7 @@ describe('cherrypick', () => {
277
280
  // mock cherry pick command
278
281
  .mockResolvedValueOnce({ stderr: '', stdout: '' });
279
282
  await (0, git_1.cherrypick)({ ...options, mainline: 1 }, 'abcd');
280
- expect(execSpy.mock.calls[0][0]).toBe('git cherry-pick --mainline 1 abcd');
283
+ expect(execSpy.mock.calls[0][0]).toBe('git cherry-pick -x --mainline 1 abcd');
281
284
  });
282
285
  it('should return `needsResolving: true` upon cherrypick error', async () => {
283
286
  jest
@@ -286,7 +289,7 @@ describe('cherrypick', () => {
286
289
  .mockRejectedValueOnce(new ExecError_1.ExecError({
287
290
  killed: false,
288
291
  code: 128,
289
- cmd: 'git cherry-pick abcd',
292
+ cmd: 'git cherry-pick -x abcd',
290
293
  stdout: '',
291
294
  stderr: '',
292
295
  }))
@@ -480,8 +483,8 @@ describe('addRemote', () => {
480
483
  });
481
484
  describe('pushBackportBranch', () => {
482
485
  const options = {
486
+ authenticatedUsername: 'sqren_authenticated',
483
487
  fork: true,
484
- username: 'sqren',
485
488
  repoOwner: 'elastic',
486
489
  repoName: 'kibana',
487
490
  };
@@ -495,6 +498,6 @@ describe('pushBackportBranch', () => {
495
498
  stdout: '',
496
499
  stderr: "remote: Repository not found.\nfatal: repository 'https://github.com/sqren/kibana.git/' not found\n",
497
500
  });
498
- await expect((0, git_1.pushBackportBranch)({ options, backportBranch })).rejects.toThrowErrorMatchingInlineSnapshot(`"Error pushing to https://github.com/sqren/kibana. Repository does not exist. Either fork the source repository (https://github.com/elastic/kibana) or disable fork mode \\"--no-fork\\". Read more about \\"fork mode\\" in the docs: https://github.com/sqren/backport/blob/3a182b17e0e7237c12915895aea9d71f49eb2886/docs/configuration.md#fork"`);
501
+ await expect((0, git_1.pushBackportBranch)({ options, backportBranch })).rejects.toThrowErrorMatchingInlineSnapshot(`"Error pushing to https://github.com/sqren_authenticated/kibana. Repository does not exist. Either fork the source repository (https://github.com/elastic/kibana) or disable fork mode \\"--no-fork\\". Read more about \\"fork mode\\" in the docs: https://github.com/sqren/backport/blob/3a182b17e0e7237c12915895aea9d71f49eb2886/docs/configuration.md#fork"`);
499
502
  });
500
503
  });
@@ -1,2 +1,2 @@
1
1
  import { ValidConfigOptions } from '../../../options/options';
2
- export declare function addAssigneesToPullRequest({ githubApiBaseUrlV3, repoName, repoOwner, accessToken, username, }: ValidConfigOptions, pullNumber: number, assignees: string[]): Promise<void>;
2
+ export declare function addAssigneesToPullRequest({ githubApiBaseUrlV3, repoName, repoOwner, accessToken, autoAssign, }: ValidConfigOptions, pullNumber: number, assignees: string[]): Promise<void>;
@@ -7,9 +7,8 @@ exports.addAssigneesToPullRequest = void 0;
7
7
  const rest_1 = require("@octokit/rest");
8
8
  const ora_1 = __importDefault(require("ora"));
9
9
  const logger_1 = require("../../logger");
10
- async function addAssigneesToPullRequest({ githubApiBaseUrlV3, repoName, repoOwner, accessToken, username, }, pullNumber, assignees) {
11
- const isSelfAssigning = assignees.length === 1 && assignees[0] === username;
12
- const text = isSelfAssigning
10
+ async function addAssigneesToPullRequest({ githubApiBaseUrlV3, repoName, repoOwner, accessToken, autoAssign, }, pullNumber, assignees) {
11
+ const text = autoAssign
13
12
  ? `Self-assigning to #${pullNumber}`
14
13
  : `Adding assignees to #${pullNumber}: ${assignees.join(', ')}`;
15
14
  logger_1.logger.info(text);
@@ -10,16 +10,15 @@ describe('addAssigneesToPullRequest', () => {
10
10
  const pullNumber = 216;
11
11
  const assignees = ['sqren'];
12
12
  const scope = (0, nock_1.default)('https://api.github.com')
13
- .post('/repos/backport-org/backport-demo/issues/216/assignees', {
13
+ .post('/repos/elastic/kibana/issues/216/assignees', {
14
14
  assignees: ['sqren'],
15
15
  })
16
16
  .reply(200, 'some response');
17
17
  const res = await (0, addAssigneesToPullRequest_1.addAssigneesToPullRequest)({
18
- githubApiBaseUrlV3: 'https://api.github.com',
19
- repoName: 'backport-demo',
20
- repoOwner: 'backport-org',
18
+ author: 'sqren',
19
+ repoName: 'kibana',
20
+ repoOwner: 'elastic',
21
21
  accessToken: 'my-token',
22
- username: 'sqren',
23
22
  }, pullNumber, assignees);
24
23
  expect(res).toBe(undefined);
25
24
  scope.done();
@@ -55,17 +55,7 @@ async function createPullRequest({ options, prPayload, }) {
55
55
  }
56
56
  exports.createPullRequest = createPullRequest;
57
57
  function getPullRequestBody({ options, commits, targetBranch, }) {
58
- const bodySuffix = options.prDescription
59
- ? `\n\n${options.prDescription}`
60
- : '';
61
- const backportDocsNote = `\n\nPlease refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information`;
62
- if (commits.length === 1) {
63
- const c = commits[0];
64
- const ref = c.pullNumber
65
- ? `pull request #${c.pullNumber}`
66
- : `commit ${c.sha}`;
67
- return `This is an automatic backport of ${ref} to ${targetBranch}.${backportDocsNote}${bodySuffix}`;
68
- }
58
+ const bodySuffix = options.prDescription ? `\n${options.prDescription}` : '';
69
59
  const commitMessages = commits
70
60
  .map((c) => {
71
61
  const message = c.pullNumber
@@ -74,14 +64,21 @@ function getPullRequestBody({ options, commits, targetBranch, }) {
74
64
  return ` - ${message}`;
75
65
  })
76
66
  .join('\n');
77
- return `This is an automatic backport of the following commits to ${targetBranch}:\n${commitMessages}${backportDocsNote}${bodySuffix}`;
67
+ return `# Backport
68
+
69
+ This is an automatic backport to ${targetBranch} of:
70
+ ${commitMessages}
71
+
72
+ ### Questions ?
73
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
74
+ ${bodySuffix}`;
78
75
  }
79
76
  exports.getPullRequestBody = getPullRequestBody;
80
77
  function getTitle({ options, commits, targetBranch, }) {
81
78
  const commitMessages = commits
82
79
  .map((commit) => (0, commitFormatters_1.getFirstLine)(commit.originalMessage))
83
80
  .join(' | ');
84
- return options.prTitle
81
+ return (options.prTitle ?? '[{targetBranch}] {commitMessages}')
85
82
  .replace('{targetBranch}', targetBranch)
86
83
  .replace('{commitMessages}', commitMessages)
87
84
  .slice(0, 240);
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const createPullRequest_1 = require("./createPullRequest");
4
+ describe('getPullRequestBody', () => {
5
+ it('when single pull request is backported', () => {
6
+ expect((0, createPullRequest_1.getPullRequestBody)({
7
+ options: {},
8
+ commits: [
9
+ {
10
+ pullNumber: 55,
11
+ sha: 'abcdefghi',
12
+ originalMessage: 'My commit message',
13
+ },
14
+ ],
15
+ targetBranch: '7.x',
16
+ })).toMatchInlineSnapshot(`
17
+ "This is an automatic backport of pull request #55 to 7.x.
18
+
19
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information"
20
+ `);
21
+ });
22
+ it('when a single commit (non pull request) is backported', () => {
23
+ expect((0, createPullRequest_1.getPullRequestBody)({
24
+ options: {},
25
+ commits: [
26
+ {
27
+ sha: 'abcdefghijklmw',
28
+ originalMessage: 'My commit message',
29
+ },
30
+ ],
31
+ targetBranch: '7.x',
32
+ })).toMatchInlineSnapshot(`
33
+ "This is an automatic backport of commit abcdefghijklmw to 7.x.
34
+
35
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information"
36
+ `);
37
+ });
38
+ it('when multiple commits are backported', () => {
39
+ expect((0, createPullRequest_1.getPullRequestBody)({
40
+ options: {},
41
+ commits: [
42
+ {
43
+ pullNumber: 55,
44
+ sha: 'abcdefghijklm',
45
+ originalMessage: 'My commit message',
46
+ },
47
+ {
48
+ sha: 'qwertyuiop',
49
+ originalMessage: 'Another commit message',
50
+ },
51
+ ],
52
+ targetBranch: '7.x',
53
+ })).toMatchInlineSnapshot(`
54
+ "This is an automatic backport of the following commits to 7.x:
55
+ - #55
56
+ - Another commit message (qwertyui)
57
+
58
+ Please refer to the [Backport tool documentation](https://github.com/sqren/backport) for additional information"
59
+ `);
60
+ });
61
+ });
62
+ describe('getTitle', () => {
63
+ it('when a custom PR title is used', () => {
64
+ expect((0, createPullRequest_1.getTitle)({
65
+ options: {
66
+ prTitle: 'Branch: "{targetBranch}". Messages: {commitMessages}',
67
+ },
68
+ commits: [
69
+ {
70
+ pullNumber: 55,
71
+ sha: 'abcdefghi',
72
+ originalMessage: 'My commit message',
73
+ },
74
+ ],
75
+ targetBranch: '7.x',
76
+ })).toEqual('Branch: "7.x". Messages: My commit message');
77
+ });
78
+ });
@@ -13,7 +13,7 @@ export interface GithubV4Response<DataResponse> {
13
13
  }[];
14
14
  }
15
15
  export declare function apiRequestV4<DataResponse>({ githubApiBaseUrlV4, accessToken, query, variables, handleError, }: {
16
- githubApiBaseUrlV4: string;
16
+ githubApiBaseUrlV4?: string;
17
17
  accessToken: string;
18
18
  query: string;
19
19
  variables?: {
@@ -7,7 +7,7 @@ exports.handleGithubV4Error = exports.apiRequestV4 = void 0;
7
7
  const axios_1 = __importDefault(require("axios"));
8
8
  const HandledError_1 = require("../../HandledError");
9
9
  const logger_1 = require("../../logger");
10
- async function apiRequestV4({ githubApiBaseUrlV4, accessToken, query, variables, handleError = true, }) {
10
+ async function apiRequestV4({ githubApiBaseUrlV4 = 'https://api.github.com/graphql', accessToken, query, variables, handleError = true, }) {
11
11
  try {
12
12
  const response = await axios_1.default.post(githubApiBaseUrlV4, { query, variables }, {
13
13
  headers: {
@@ -9,7 +9,7 @@ const logger_1 = require("../../logger");
9
9
  const FetchPullRequestId_1 = require("./FetchPullRequestId");
10
10
  const apiRequestV4_1 = require("./apiRequestV4");
11
11
  async function enablePullRequestAutoMerge(options, targetPullRequestNumber) {
12
- const { accessToken, githubApiBaseUrlV4, autoMergeMethod } = options;
12
+ const { accessToken, githubApiBaseUrlV4, autoMergeMethod = 'merge', } = options;
13
13
  const text = `Enabling auto merging via ${options.autoMergeMethod}`;
14
14
  logger_1.logger.info(text);
15
15
  const spinner = (0, ora_1.default)(text).start();
@@ -68,7 +68,6 @@ describe.skip('enablePullRequestAutoMerge', () => {
68
68
  branchName = `test-${randomString}`;
69
69
  options = {
70
70
  accessToken,
71
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
72
71
  repoOwner: TEST_REPO_OWNER,
73
72
  repoName: TEST_REPO_NAME,
74
73
  };
@@ -1,7 +1,10 @@
1
- import { ValidConfigOptions } from '../../../options/options';
2
1
  export interface AuthorIdResponse {
3
2
  user: {
4
3
  id: string;
5
4
  };
6
5
  }
7
- export declare function fetchAuthorId(options: ValidConfigOptions): Promise<string | null>;
6
+ export declare function fetchAuthorId({ accessToken, author, githubApiBaseUrlV4, }: {
7
+ accessToken: string;
8
+ author: string | null;
9
+ githubApiBaseUrlV4?: string;
10
+ }): Promise<string | null>;
@@ -2,14 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.fetchAuthorId = void 0;
4
4
  const apiRequestV4_1 = require("./apiRequestV4");
5
- async function fetchAuthorId(options) {
6
- const { all, author, accessToken, githubApiBaseUrlV4 } = options;
7
- if (all) {
5
+ async function fetchAuthorId({ accessToken, author, githubApiBaseUrlV4 = 'https://api.github.com/graphql', }) {
6
+ if (author === null) {
8
7
  return null;
9
8
  }
10
9
  const query = /* GraphQL */ `
11
- query AuthorId($login: String!) {
12
- user(login: $login) {
10
+ query AuthorId($author: String!) {
11
+ user(login: $author) {
13
12
  id
14
13
  }
15
14
  }
@@ -18,7 +17,7 @@ async function fetchAuthorId(options) {
18
17
  githubApiBaseUrlV4,
19
18
  accessToken,
20
19
  query,
21
- variables: { login: author },
20
+ variables: { author },
22
21
  });
23
22
  return res.user.id;
24
23
  }
@@ -7,22 +7,19 @@ describe('fetchAuthorId', () => {
7
7
  beforeAll(async () => {
8
8
  devAccessToken = await (0, getDevAccessToken_1.getDevAccessToken)();
9
9
  });
10
- describe('all = true', () => {
10
+ describe('author = null', () => {
11
11
  it('returns null', async () => {
12
12
  const options = {
13
13
  accessToken: devAccessToken,
14
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
15
- all: true,
14
+ author: null,
16
15
  };
17
16
  expect(await (0, fetchAuthorId_1.fetchAuthorId)(options)).toEqual(null);
18
17
  });
19
18
  });
20
- describe('all = false', () => {
19
+ describe('author is "sqren"', () => {
21
20
  it('returns author id', async () => {
22
21
  const options = {
23
22
  accessToken: devAccessToken,
24
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
25
- all: false,
26
23
  author: 'sqren',
27
24
  };
28
25
  expect(await (0, fetchAuthorId_1.fetchAuthorId)(options)).toEqual('MDQ6VXNlcjIwOTk2Ng==');
@@ -1,5 +1,12 @@
1
1
  import { ValidConfigOptions } from '../../../../options/options';
2
2
  import { Commit } from '../../../sourceCommit/parseSourceCommit';
3
- export declare function fetchCommitByPullNumber(options: ValidConfigOptions & {
3
+ export declare function fetchCommitByPullNumber(options: {
4
+ accessToken: string;
5
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
6
+ githubApiBaseUrlV4?: string;
7
+ historicalBranchLabelMappings: ValidConfigOptions['historicalBranchLabelMappings'];
4
8
  pullNumber: number;
9
+ repoName: string;
10
+ repoOwner: string;
11
+ sourceBranch: string;
5
12
  }): Promise<Commit>;
@@ -5,7 +5,7 @@ const HandledError_1 = require("../../../HandledError");
5
5
  const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
6
6
  const apiRequestV4_1 = require("../apiRequestV4");
7
7
  async function fetchCommitByPullNumber(options) {
8
- const { accessToken, githubApiBaseUrlV4, pullNumber, repoName, repoOwner } = options;
8
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', pullNumber, repoName, repoOwner, } = options;
9
9
  const query = /* GraphQL */ `
10
10
  query CommitByPullNumber(
11
11
  $repoOwner: String!
@@ -11,10 +11,11 @@ describe('fetchCommitByPullNumber', () => {
11
11
  it('is returned', async () => {
12
12
  const options = {
13
13
  accessToken: devAccessToken,
14
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
15
14
  pullNumber: 5,
16
15
  repoName: 'backport-e2e',
17
16
  repoOwner: 'backport-org',
17
+ sourceBranch: 'main',
18
+ historicalBranchLabelMappings: [],
18
19
  };
19
20
  expect(await (0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).toEqual({
20
21
  committedDate: '2020-08-15T12:40:19Z',
@@ -44,10 +45,11 @@ describe('fetchCommitByPullNumber', () => {
44
45
  it('throws an error', async () => {
45
46
  const options = {
46
47
  accessToken: devAccessToken,
47
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
48
48
  pullNumber: 11,
49
49
  repoName: 'backport-e2e',
50
50
  repoOwner: 'backport-org',
51
+ sourceBranch: 'main',
52
+ historicalBranchLabelMappings: [],
51
53
  };
52
54
  await expect((0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).rejects.toThrowError(`The PR #11 is not merged`);
53
55
  });
@@ -56,10 +58,11 @@ describe('fetchCommitByPullNumber', () => {
56
58
  it('throws an error', async () => {
57
59
  const options = {
58
60
  accessToken: devAccessToken,
59
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
60
61
  pullNumber: 9999999999999,
61
62
  repoName: 'backport-e2e',
62
63
  repoOwner: 'backport-org',
64
+ sourceBranch: 'main',
65
+ historicalBranchLabelMappings: [],
63
66
  };
64
67
  await expect((0, fetchCommitByPullNumber_1.fetchCommitByPullNumber)(options)).rejects.toThrowError(`Could not resolve to a PullRequest with the number of 9999999999999. (Unhandled Github v4 error)`);
65
68
  });
@@ -1,5 +1,12 @@
1
1
  import { ValidConfigOptions } from '../../../../options/options';
2
2
  import { Commit } from '../../../sourceCommit/parseSourceCommit';
3
- export declare function fetchCommitBySha(options: ValidConfigOptions & {
3
+ export declare function fetchCommitBySha(options: {
4
+ accessToken: string;
5
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
6
+ githubApiBaseUrlV4?: string;
7
+ historicalBranchLabelMappings: ValidConfigOptions['historicalBranchLabelMappings'];
8
+ repoName: string;
9
+ repoOwner: string;
4
10
  sha: string;
11
+ sourceBranch: string;
5
12
  }): Promise<Commit>;
@@ -5,7 +5,7 @@ const HandledError_1 = require("../../../HandledError");
5
5
  const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
6
6
  const apiRequestV4_1 = require("../apiRequestV4");
7
7
  async function fetchCommitBySha(options) {
8
- const { accessToken, githubApiBaseUrlV4, repoName, repoOwner, sha, sourceBranch, } = options;
8
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', repoName, repoOwner, sha, sourceBranch, } = options;
9
9
  const query = /* GraphQL */ `
10
10
  query CommitsBySha($repoOwner: String!, $repoName: String!, $oid: String!) {
11
11
  repository(owner: $repoOwner, name: $repoName) {
@@ -13,8 +13,8 @@ describe('fetchCommitBySha', () => {
13
13
  repoName: 'kibana',
14
14
  accessToken: devAccessToken,
15
15
  sha: 'cb6fbc0',
16
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
17
16
  sourceBranch: 'master',
17
+ historicalBranchLabelMappings: [],
18
18
  })).toEqual({
19
19
  committedDate: '2020-07-07T20:40:28Z',
20
20
  originalMessage: '[APM] Add API tests (#70740)',
@@ -38,8 +38,8 @@ describe('fetchCommitBySha', () => {
38
38
  repoName: 'kibana',
39
39
  accessToken: devAccessToken,
40
40
  sha: 'fc22f59',
41
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
42
41
  sourceBranch: 'main',
42
+ historicalBranchLabelMappings: [],
43
43
  })).rejects.toThrowError('No commit found on branch "main" with sha "fc22f59"');
44
44
  });
45
45
  it('throws if sha is invalid', async () => {
@@ -48,8 +48,8 @@ describe('fetchCommitBySha', () => {
48
48
  repoName: 'kibana',
49
49
  accessToken: devAccessToken,
50
50
  sha: 'myCommitSha',
51
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
52
51
  sourceBranch: 'main',
52
+ historicalBranchLabelMappings: [],
53
53
  })).rejects.toThrowError('No commit found on branch "main" with sha "myCommitSha"');
54
54
  });
55
55
  });
@@ -1,6 +1,17 @@
1
1
  import { ValidConfigOptions } from '../../../../options/options';
2
2
  import { Commit, SourceCommitWithTargetPullRequest } from '../../../sourceCommit/parseSourceCommit';
3
- export declare function fetchCommitsByAuthor(options: ValidConfigOptions): Promise<Commit[]>;
3
+ export declare function fetchCommitsByAuthor(options: {
4
+ accessToken: string;
5
+ author: string | null;
6
+ branchLabelMapping?: ValidConfigOptions['branchLabelMapping'];
7
+ commitPaths?: string[];
8
+ githubApiBaseUrlV4?: string;
9
+ historicalBranchLabelMappings: ValidConfigOptions['historicalBranchLabelMappings'];
10
+ maxNumber?: number;
11
+ repoName: string;
12
+ repoOwner: string;
13
+ sourceBranch: string;
14
+ }): Promise<Commit[]>;
4
15
  export interface CommitByAuthorResponse {
5
16
  repository: {
6
17
  ref: {
@@ -8,7 +8,7 @@ const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit");
8
8
  const apiRequestV4_1 = require("../apiRequestV4");
9
9
  const fetchAuthorId_1 = require("../fetchAuthorId");
10
10
  function fetchByCommitPath({ options, authorId, commitPath, }) {
11
- const { accessToken, githubApiBaseUrlV4, maxNumber, repoName, repoOwner, sourceBranch, } = options;
11
+ const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', maxNumber = 10, repoName, repoOwner, sourceBranch, } = options;
12
12
  const query = /* GraphQL */ `
13
13
  query CommitsByAuthor(
14
14
  $repoOwner: String!
@@ -56,7 +56,7 @@ function fetchByCommitPath({ options, authorId, commitPath, }) {
56
56
  });
57
57
  }
58
58
  async function fetchCommitsByAuthor(options) {
59
- const { sourceBranch, commitPaths } = options;
59
+ const { sourceBranch, commitPaths = [] } = options;
60
60
  const authorId = await (0, fetchAuthorId_1.fetchAuthorId)(options);
61
61
  const responses = await Promise.all((0, lodash_1.isEmpty)(commitPaths)
62
62
  ? [fetchByCommitPath({ options, authorId, commitPath: null })]
@@ -75,12 +75,10 @@ async function fetchCommitsByAuthor(options) {
75
75
  .filter(filterEmpty_1.filterNil);
76
76
  // terminate if not commits were found
77
77
  if ((0, lodash_1.isEmpty)(commits)) {
78
- const pathText = options.commitPaths.length > 0
79
- ? ` touching files in path: "${options.commitPaths}"`
80
- : '';
81
- const errorText = options.all
82
- ? `There are no commits in this repository${pathText}`
83
- : `There are no commits by "${options.author}" in this repository${pathText}. Try with \`--all\` for commits by all users or \`--author=<username>\` for commits from a specific user`;
78
+ const pathText = commitPaths.length > 0 ? ` touching files in path: "${commitPaths}"` : '';
79
+ const errorText = options.author
80
+ ? `There are no commits by "${options.author}" in this repository${pathText}. Try with \`--all\` for commits by all users or \`--author=<username>\` for commits from a specific user`
81
+ : `There are no commits in this repository${pathText}`;
84
82
  throw new HandledError_1.HandledError(errorText);
85
83
  }
86
84
  const commitsUnique = (0, lodash_1.uniqBy)(commits, 'sha');
@@ -9,14 +9,13 @@ describe('fetchCommitsByAuthor', () => {
9
9
  });
10
10
  describe('commitPaths', () => {
11
11
  const getOptions = () => ({
12
- repoOwner: 'backport-org',
13
- repoName: 'repo-with-different-commit-paths',
14
- sourceBranch: 'main',
15
12
  accessToken: devAccessToken,
16
- username: 'sqren',
17
13
  author: 'sqren',
14
+ historicalBranchLabelMappings: [],
18
15
  maxNumber: 10,
19
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
16
+ repoName: 'repo-with-different-commit-paths',
17
+ repoOwner: 'backport-org',
18
+ sourceBranch: 'main',
20
19
  });
21
20
  const getCommitMessages = (commits) => {
22
21
  return commits.map((c) => c.originalMessage.replace(/(\r\n|\n|\r)/gm, ''));
@@ -83,15 +82,14 @@ describe('fetchCommitsByAuthor', () => {
83
82
  let res;
84
83
  beforeEach(async () => {
85
84
  res = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
86
- repoOwner: 'backport-org',
87
- repoName: 'backport-e2e',
88
- sourceBranch: 'master',
89
85
  accessToken: devAccessToken,
90
- username: 'sqren',
91
- author: 'sqren',
92
- maxNumber: 10,
93
- githubApiBaseUrlV4: 'https://api.github.com/graphql',
94
86
  commitPaths: [],
87
+ historicalBranchLabelMappings: [],
88
+ maxNumber: 10,
89
+ repoName: 'backport-e2e',
90
+ repoOwner: 'backport-org',
91
+ sourceBranch: 'master',
92
+ author: null,
95
93
  });
96
94
  });
97
95
  it('returns related OPEN PRs', async () => {