backport 6.3.0-beta → 6.3.0-beta-5

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 (118) hide show
  1. package/dist/backportRun.js +3 -2
  2. package/dist/entrypoint.cli.e2e.private.test.d.ts +1 -0
  3. package/dist/entrypoint.cli.e2e.private.test.js +278 -0
  4. package/dist/entrypoint.cli.js +0 -0
  5. package/dist/entrypoint.cli.private.test.d.ts +1 -0
  6. package/dist/entrypoint.cli.private.test.js +46 -0
  7. package/dist/options/ConfigOptions.d.ts +3 -2
  8. package/dist/options/cliArgs.d.ts +1 -0
  9. package/dist/options/cliArgs.js +24 -4
  10. package/dist/options/cliArgs.test.d.ts +1 -0
  11. package/dist/options/cliArgs.test.js +170 -0
  12. package/dist/options/config/config.d.ts +2 -7
  13. package/dist/options/config/config.test.d.ts +1 -0
  14. package/dist/options/config/config.test.js +32 -0
  15. package/dist/options/config/globalConfig.test.d.ts +1 -0
  16. package/dist/options/config/globalConfig.test.js +61 -0
  17. package/dist/options/config/projectConfig.test.d.ts +1 -0
  18. package/dist/options/config/projectConfig.test.js +109 -0
  19. package/dist/options/config/readConfigFile.d.ts +2 -7
  20. package/dist/options/options.d.ts +2 -7
  21. package/dist/options/options.test.d.ts +1 -0
  22. package/dist/options/options.test.js +369 -0
  23. package/dist/runSequentially.js +1 -0
  24. package/dist/runSequentially.test.d.ts +1 -0
  25. package/dist/runSequentially.test.js +218 -0
  26. package/dist/scripts/postinstall.test.d.ts +1 -0
  27. package/dist/scripts/postinstall.test.js +53 -0
  28. package/dist/services/env.test.d.ts +1 -0
  29. package/dist/services/env.test.js +21 -0
  30. package/dist/services/git.integration.test.d.ts +1 -0
  31. package/dist/services/git.integration.test.js +267 -0
  32. package/dist/services/git.test.d.ts +1 -0
  33. package/dist/services/git.test.js +518 -0
  34. package/dist/services/github/commitFormatters.test.d.ts +1 -0
  35. package/dist/services/github/commitFormatters.test.js +11 -0
  36. package/dist/services/github/v3/addAssigneesToPullRequest.test.d.ts +1 -0
  37. package/dist/services/github/v3/addAssigneesToPullRequest.test.js +27 -0
  38. package/dist/services/github/v3/createPullRequest.test.d.ts +1 -0
  39. package/dist/services/github/v3/createPullRequest.test.js +325 -0
  40. package/dist/services/github/v3/createStatusComment.test.d.ts +1 -0
  41. package/dist/services/github/v3/createStatusComment.test.js +386 -0
  42. package/dist/services/github/v3/index.d.ts +1 -0
  43. package/dist/services/github/v3/index.js +61 -0
  44. package/dist/services/github/v4/apiRequestV4.js +10 -2
  45. package/dist/services/github/v4/apiRequestV4.test.d.ts +1 -0
  46. package/dist/services/github/v4/apiRequestV4.test.js +82 -0
  47. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.d.ts +1 -0
  48. package/dist/services/github/v4/enablePullRequestAutoMerge.private.test.js +112 -0
  49. package/dist/services/github/v4/fetchAuthorId.private.test.d.ts +1 -0
  50. package/dist/services/github/v4/fetchAuthorId.private.test.js +28 -0
  51. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.d.ts +1 -0
  52. package/dist/services/github/v4/fetchCommits/allFetchers.private.test.js +96 -0
  53. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.d.ts +1 -0
  54. package/dist/services/github/v4/fetchCommits/fetchCommitByPullNumber.private.test.js +143 -0
  55. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.d.ts +1 -0
  56. package/dist/services/github/v4/fetchCommits/fetchCommitBySha.private.test.js +124 -0
  57. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.d.ts +1 -0
  58. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.private.test.js +200 -0
  59. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.d.ts +1 -0
  60. package/dist/services/github/v4/fetchCommits/fetchCommitsByAuthor.test.js +153 -0
  61. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.d.ts +1 -0
  62. package/dist/services/github/v4/fetchCommits/fetchPullRequestBySearchQuery.private.test.js +102 -0
  63. package/dist/services/github/v4/fetchExistingPullRequest.private.test.d.ts +1 -0
  64. package/dist/services/github/v4/fetchExistingPullRequest.private.test.js +47 -0
  65. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.d.ts +1 -0
  66. package/dist/services/github/v4/fetchRemoteProjectConfig.private.test.js +52 -0
  67. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.d.ts +3 -7
  68. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.js +12 -10
  69. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.d.ts +1 -0
  70. package/dist/services/github/v4/getOptionsFromGithub/getOptionsFromGithub.private.test.js +136 -0
  71. package/dist/services/github/v4/getOptionsFromGithub/query.d.ts +15 -24
  72. package/dist/services/github/v4/getOptionsFromGithub/query.js +17 -25
  73. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.d.ts +1 -0
  74. package/dist/services/github/v4/getRepoOwnerAndNameFromGitRemotes.private.test.js +71 -0
  75. package/dist/services/github/v4/mocks/commitsByAuthorMock.d.ts +2 -0
  76. package/dist/services/github/v4/mocks/commitsByAuthorMock.js +154 -0
  77. package/dist/services/github/v4/throwOnInvalidAccessToken.test.d.ts +1 -0
  78. package/dist/services/github/v4/throwOnInvalidAccessToken.test.js +87 -0
  79. package/dist/services/logger.d.ts +3 -15
  80. package/dist/services/logger.js +10 -50
  81. package/dist/services/prompt.test.d.ts +1 -0
  82. package/dist/services/prompt.test.js +134 -0
  83. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.d.ts +1 -0
  84. package/dist/services/sourceCommit/getExpectedTargetPullRequests.test.js +433 -0
  85. package/dist/services/sourceCommit/parseSourceCommit.test.d.ts +1 -0
  86. package/dist/services/sourceCommit/parseSourceCommit.test.js +235 -0
  87. package/dist/test/ExecError.d.ts +16 -0
  88. package/dist/test/ExecError.js +15 -0
  89. package/dist/test/backport-e2e.mutation.test.d.ts +1 -0
  90. package/dist/test/backport-e2e.mutation.test.js +347 -0
  91. package/dist/test/handleUnbackportedPullRequests.private.test.d.ts +1 -0
  92. package/dist/test/handleUnbackportedPullRequests.private.test.js +57 -0
  93. package/dist/test/mockConfigFiles.d.ts +5 -0
  94. package/dist/test/mockConfigFiles.js +21 -0
  95. package/dist/test/mocks.d.ts +10 -0
  96. package/dist/test/mocks.js +23 -0
  97. package/dist/test/nockHelpers.d.ts +16 -0
  98. package/dist/test/nockHelpers.js +32 -0
  99. package/dist/test/private/getDevAccessToken.d.ts +2 -0
  100. package/dist/test/private/getDevAccessToken.js +12 -0
  101. package/dist/test/sandbox.d.ts +5 -0
  102. package/dist/test/sandbox.js +43 -0
  103. package/dist/test/setupFiles/automatic-mocks.d.ts +1 -0
  104. package/dist/test/setupFiles/automatic-mocks.js +59 -0
  105. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.d.ts +1 -0
  106. package/dist/ui/cherrypickAndCreateTargetPullRequest.test.js +455 -0
  107. package/dist/ui/getCommitsWithoutBackports.test.d.ts +1 -0
  108. package/dist/ui/getCommitsWithoutBackports.test.js +311 -0
  109. package/dist/ui/getTargetBranches.test.d.ts +1 -0
  110. package/dist/ui/getTargetBranches.test.js +198 -0
  111. package/dist/ui/maybeSetupRepo.test.d.ts +1 -0
  112. package/dist/ui/maybeSetupRepo.test.js +204 -0
  113. package/dist/utils/excludeUndefined.test.d.ts +1 -0
  114. package/dist/utils/excludeUndefined.test.js +24 -0
  115. package/dist/utils/packageVersion.d.ts +1 -1
  116. package/dist/utils/packageVersion.js +1 -1
  117. package/package.json +5 -3
  118. package/yarn.lock +5 -0
@@ -0,0 +1,153 @@
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
+ const nock_1 = __importDefault(require("nock"));
7
+ const nockHelpers_1 = require("../../../../test/nockHelpers");
8
+ const commitsByAuthorMock_1 = require("../mocks/commitsByAuthorMock");
9
+ const fetchCommitsByAuthor_1 = require("./fetchCommitsByAuthor");
10
+ const defaultOptions = {
11
+ accessToken: 'myAccessToken',
12
+ author: 'sqren',
13
+ githubApiBaseUrlV4: 'http://localhost/graphql',
14
+ historicalBranchLabelMappings: [],
15
+ maxNumber: 10,
16
+ repoName: 'kibana',
17
+ repoOwner: 'elastic',
18
+ sourceBranch: 'source-branch-from-options',
19
+ dateSince: null,
20
+ dateUntil: null,
21
+ };
22
+ const authorIdMockData = { user: { id: 'myUserId' } };
23
+ describe('fetchCommitsByAuthor', () => {
24
+ beforeEach(() => {
25
+ jest.clearAllMocks();
26
+ });
27
+ afterEach(() => {
28
+ nock_1.default.cleanAll();
29
+ });
30
+ describe('when commit has an associated pull request', () => {
31
+ let res;
32
+ let authorIdCalls;
33
+ let commitsByAuthorCalls;
34
+ beforeEach(async () => {
35
+ authorIdCalls = (0, nockHelpers_1.mockGqlRequest)({
36
+ name: 'AuthorId',
37
+ statusCode: 200,
38
+ body: { data: authorIdMockData },
39
+ });
40
+ commitsByAuthorCalls = (0, nockHelpers_1.mockGqlRequest)({
41
+ name: 'CommitsByAuthor',
42
+ statusCode: 200,
43
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
44
+ });
45
+ res = await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)(defaultOptions);
46
+ });
47
+ it('should return a list of commits with pullNumber and existing backports', () => {
48
+ const expectedCommits = [
49
+ {
50
+ sourceCommit: {
51
+ committedDate: '2021-12-24T00:00:00Z',
52
+ sha: '2e63475c483f7844b0f2833bc57fdee32095bacb',
53
+ message: 'Add 👻',
54
+ },
55
+ expectedTargetPullRequests: [],
56
+ sourceBranch: 'source-branch-from-options',
57
+ },
58
+ {
59
+ sourceCommit: {
60
+ committedDate: '2021-12-23T00:00:00Z',
61
+ sha: 'f3b618b9421fdecdb36862f907afbdd6344b361d',
62
+ message: 'Add witch (#85)',
63
+ },
64
+ sourcePullRequest: {
65
+ number: 85,
66
+ url: 'https://github.com/elastic/kibana/pull/85',
67
+ mergeCommit: {
68
+ sha: 'f3b618b9421fdecdb36862f907afbdd6344b361d',
69
+ message: 'Add witch (#85)',
70
+ },
71
+ },
72
+ expectedTargetPullRequests: [],
73
+ sourceBranch: 'master',
74
+ },
75
+ {
76
+ sourceCommit: {
77
+ committedDate: '2021-12-22T00:00:00Z',
78
+ sha: '79cf18453ec32a4677009dcbab1c9c8c73fc14fe',
79
+ message: 'Add SF mention (#80)\n\n* Add SF mention\r\n\r\n* Add several emojis!',
80
+ },
81
+ sourcePullRequest: {
82
+ number: 80,
83
+ url: 'https://github.com/elastic/kibana/pull/80',
84
+ mergeCommit: {
85
+ sha: '79cf18453ec32a4677009dcbab1c9c8c73fc14fe',
86
+ message: 'Add SF mention (#80)\n\n* Add SF mention\r\n\r\n* Add several emojis!',
87
+ },
88
+ },
89
+ expectedTargetPullRequests: [
90
+ {
91
+ branch: '6.3',
92
+ state: 'MERGED',
93
+ number: 99,
94
+ url: 'https://github.com/elastic/kibana/pull/99',
95
+ mergeCommit: {
96
+ message: 'Add SF mention (#80)\n\n* Add SF mention\r\n\r\n* Add several emojis!',
97
+ sha: 'target-merge-commit-sha',
98
+ },
99
+ },
100
+ ],
101
+ sourceBranch: 'master',
102
+ },
103
+ {
104
+ sourceCommit: {
105
+ committedDate: '2021-12-21T00:00:00Z',
106
+ sha: '3827bbbaf39914eda4f02f6940189844375fd097',
107
+ message: 'Add backport config',
108
+ },
109
+ expectedTargetPullRequests: [],
110
+ sourceBranch: 'source-branch-from-options',
111
+ },
112
+ {
113
+ sourceCommit: {
114
+ committedDate: '2021-12-20T00:00:00Z',
115
+ sha: '5ea0da550ac191029459289d67f99ad7d310812b',
116
+ message: 'Initial commit',
117
+ },
118
+ expectedTargetPullRequests: [],
119
+ sourceBranch: 'source-branch-from-options',
120
+ },
121
+ ];
122
+ expect(res).toEqual(expectedCommits);
123
+ });
124
+ it('should call with correct args to fetch author id', () => {
125
+ expect(authorIdCalls).toMatchSnapshot();
126
+ });
127
+ it('should call with correct args to fetch commits', () => {
128
+ expect(commitsByAuthorCalls).toMatchSnapshot();
129
+ });
130
+ });
131
+ describe('when a custom github api hostname is supplied', () => {
132
+ it('should be used in gql requests', async () => {
133
+ const authorIdCalls = (0, nockHelpers_1.mockGqlRequest)({
134
+ name: 'AuthorId',
135
+ statusCode: 200,
136
+ body: { data: authorIdMockData },
137
+ apiBaseUrl: 'http://localhost/my-custom-api',
138
+ });
139
+ const commitsByAuthorCalls = (0, nockHelpers_1.mockGqlRequest)({
140
+ name: 'CommitsByAuthor',
141
+ statusCode: 200,
142
+ body: { data: commitsByAuthorMock_1.commitsByAuthorMock },
143
+ apiBaseUrl: 'http://localhost/my-custom-api',
144
+ });
145
+ await (0, fetchCommitsByAuthor_1.fetchCommitsByAuthor)({
146
+ ...defaultOptions,
147
+ githubApiBaseUrlV4: 'http://localhost/my-custom-api',
148
+ });
149
+ expect(authorIdCalls.length).toBe(1);
150
+ expect(commitsByAuthorCalls.length).toBe(1);
151
+ });
152
+ });
153
+ });
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
4
+ const fetchPullRequestBySearchQuery_1 = require("./fetchPullRequestBySearchQuery");
5
+ describe('fetchPullRequestBySearchQuery', () => {
6
+ let devAccessToken;
7
+ beforeAll(() => {
8
+ devAccessToken = (0, getDevAccessToken_1.getDevAccessToken)();
9
+ });
10
+ describe('when filter does not match any PRs', () => {
11
+ it('throws an error', async () => {
12
+ const options = {
13
+ accessToken: devAccessToken,
14
+ maxNumber: 10,
15
+ prFilter: 'label:non-existing',
16
+ repoName: 'backport-e2e',
17
+ repoOwner: 'backport-org',
18
+ sourceBranch: 'master',
19
+ author: 'sqren',
20
+ };
21
+ await expect((0, fetchPullRequestBySearchQuery_1.fetchPullRequestBySearchQuery)(options)).rejects.toThrowError('There are no commits by "sqren" matching the filter "label:non-existing". Try with `--all` for commits by all users or `--author=<username>` for commits from a specific user');
22
+ });
23
+ });
24
+ describe('when filter matches PRs', () => {
25
+ it('returns the merge commits for those PRs', async () => {
26
+ const options = {
27
+ accessToken: devAccessToken,
28
+ maxNumber: 10,
29
+ prFilter: 'label:v7.8.0',
30
+ repoName: 'backport-e2e',
31
+ repoOwner: 'backport-org',
32
+ sourceBranch: 'master',
33
+ author: 'sqren',
34
+ };
35
+ const expectedCommits = [
36
+ {
37
+ sourceCommit: {
38
+ committedDate: '2020-08-16T21:44:28Z',
39
+ message: 'Add sheep emoji (#9)',
40
+ sha: 'eebf165c82a4b718d95c11b3877e365b1949ff28',
41
+ },
42
+ sourcePullRequest: {
43
+ number: 9,
44
+ url: 'https://github.com/backport-org/backport-e2e/pull/9',
45
+ mergeCommit: {
46
+ message: 'Add sheep emoji (#9)',
47
+ sha: 'eebf165c82a4b718d95c11b3877e365b1949ff28',
48
+ },
49
+ },
50
+ sourceBranch: 'master',
51
+ expectedTargetPullRequests: [
52
+ {
53
+ branch: '7.8',
54
+ state: 'OPEN',
55
+ number: 10,
56
+ url: 'https://github.com/backport-org/backport-e2e/pull/10',
57
+ },
58
+ ],
59
+ },
60
+ {
61
+ sourceCommit: {
62
+ committedDate: '2020-08-15T12:40:19Z',
63
+ message: 'Add 🍏 emoji (#5)',
64
+ sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
65
+ },
66
+ sourcePullRequest: {
67
+ number: 5,
68
+ url: 'https://github.com/backport-org/backport-e2e/pull/5',
69
+ mergeCommit: {
70
+ message: 'Add 🍏 emoji (#5)',
71
+ sha: 'ee8c492334cef1ca077a56addb79a26f79821d2f',
72
+ },
73
+ },
74
+ sourceBranch: 'master',
75
+ expectedTargetPullRequests: [
76
+ {
77
+ branch: '7.x',
78
+ state: 'MERGED',
79
+ number: 6,
80
+ url: 'https://github.com/backport-org/backport-e2e/pull/6',
81
+ mergeCommit: {
82
+ message: 'Add 🍏 emoji (#5) (#6)',
83
+ sha: '4bcd876d4ceaa73cf437bfc89b74d1a4e704c0a6',
84
+ },
85
+ },
86
+ {
87
+ branch: '7.8',
88
+ state: 'MERGED',
89
+ number: 7,
90
+ url: 'https://github.com/backport-org/backport-e2e/pull/7',
91
+ mergeCommit: {
92
+ message: 'Add 🍏 emoji (#5) (#7)',
93
+ sha: '46cd6f9999effdf894a36dbc7db90e890f4be840',
94
+ },
95
+ },
96
+ ],
97
+ },
98
+ ];
99
+ expect(await (0, fetchPullRequestBySearchQuery_1.fetchPullRequestBySearchQuery)(options)).toEqual(expectedCommits);
100
+ });
101
+ });
102
+ });
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../test/private/getDevAccessToken");
4
+ const fetchExistingPullRequest_1 = require("./fetchExistingPullRequest");
5
+ describe('fetchExistingPullRequest', () => {
6
+ let devAccessToken;
7
+ beforeAll(() => {
8
+ devAccessToken = (0, getDevAccessToken_1.getDevAccessToken)();
9
+ });
10
+ describe('when PR does not exist', () => {
11
+ it('returns undefined', async () => {
12
+ const options = {
13
+ accessToken: devAccessToken,
14
+ };
15
+ const prPayload = {
16
+ owner: 'backport-org',
17
+ repo: 'backport-e2e',
18
+ title: 'My PR title',
19
+ body: 'My PR body',
20
+ head: 'sqren:backport/7.8/pr-foo',
21
+ base: '7.8',
22
+ };
23
+ const res = await (0, fetchExistingPullRequest_1.fetchExistingPullRequest)({ options, prPayload });
24
+ expect(res).toBe(undefined);
25
+ });
26
+ });
27
+ describe('when PR exists', () => {
28
+ it('returns the PR number and url', async () => {
29
+ const options = {
30
+ accessToken: devAccessToken,
31
+ };
32
+ const prPayload = {
33
+ owner: 'backport-org',
34
+ repo: 'backport-e2e',
35
+ title: 'My PR title',
36
+ body: 'My PR body',
37
+ head: 'sqren:backport/7.8/pr-9',
38
+ base: '7.8',
39
+ };
40
+ const res = await (0, fetchExistingPullRequest_1.fetchExistingPullRequest)({ options, prPayload });
41
+ expect(res).toEqual({
42
+ url: 'https://github.com/backport-org/backport-e2e/pull/10',
43
+ number: 10,
44
+ });
45
+ });
46
+ });
47
+ });
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const getDevAccessToken_1 = require("../../../test/private/getDevAccessToken");
4
+ const fetchRemoteProjectConfig_1 = require("./fetchRemoteProjectConfig");
5
+ describe('fetchRemoteProjectConfig', () => {
6
+ let devAccessToken;
7
+ beforeEach(() => {
8
+ devAccessToken = (0, getDevAccessToken_1.getDevAccessToken)();
9
+ });
10
+ it('returns the backport config from "main" branch', async () => {
11
+ const projectConfig = await (0, fetchRemoteProjectConfig_1.fetchRemoteProjectConfig)({
12
+ repoOwner: 'backport-org',
13
+ repoName: 'repo-with-project-config',
14
+ accessToken: devAccessToken,
15
+ sourceBranch: 'main',
16
+ });
17
+ expect(projectConfig).toEqual({
18
+ autoMerge: true,
19
+ autoMergeMethod: 'squash',
20
+ branchLabelMapping: {
21
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
22
+ '^v8.1.0$': 'main',
23
+ },
24
+ targetBranchChoices: ['main', '8.0', '7.17', '7.16'],
25
+ targetPRLabels: ['backport'],
26
+ repoOwner: 'backport-org',
27
+ repoName: 'repo-with-project-config',
28
+ });
29
+ });
30
+ it('returns the backport config from "branch-with-legacy-config" branch', async () => {
31
+ const projectConfig = await (0, fetchRemoteProjectConfig_1.fetchRemoteProjectConfig)({
32
+ repoOwner: 'backport-org',
33
+ repoName: 'repo-with-project-config',
34
+ accessToken: devAccessToken,
35
+ sourceBranch: 'branch-with-legacy-config',
36
+ });
37
+ expect(projectConfig).toEqual({
38
+ targetBranchChoices: ['main', '8.0', '7.17', '7.16'],
39
+ repoOwner: 'backport-org',
40
+ repoName: 'repo-with-project-config',
41
+ });
42
+ });
43
+ it('throws an error if config does not exist', async () => {
44
+ const promise = (0, fetchRemoteProjectConfig_1.fetchRemoteProjectConfig)({
45
+ repoOwner: 'backport-org',
46
+ repoName: 'repo-with-project-config',
47
+ accessToken: devAccessToken,
48
+ sourceBranch: 'branch-with-no-config',
49
+ });
50
+ await expect(promise).rejects.toThrowError('Project config does not exist');
51
+ });
52
+ });
@@ -3,6 +3,7 @@ export declare function getOptionsFromGithub(options: {
3
3
  githubApiBaseUrlV4?: string;
4
4
  repoName: string;
5
5
  repoOwner: string;
6
+ username?: string;
6
7
  skipRemoteConfig?: boolean;
7
8
  cwd?: string;
8
9
  }): Promise<{
@@ -17,13 +18,7 @@ export declare function getOptionsFromGithub(options: {
17
18
  autoFixConflicts?: (({ files, directory, logger, targetBranch, }: {
18
19
  files: string[];
19
20
  directory: string;
20
- logger: {
21
- error: (message: string, meta?: unknown) => void;
22
- warn: (message: string, meta?: unknown) => void;
23
- info: (message: string, meta?: unknown) => void;
24
- verbose: (message: string, meta?: unknown) => void;
25
- debug: (message: string, meta?: unknown) => void;
26
- };
21
+ logger: import("winston").Logger;
27
22
  targetBranch: string;
28
23
  }) => boolean | Promise<boolean>) | undefined;
29
24
  autoMerge?: boolean | undefined;
@@ -62,6 +57,7 @@ export declare function getOptionsFromGithub(options: {
62
57
  targetBranchChoices?: import("../../../../options/ConfigOptions").TargetBranchChoiceOrString[] | undefined;
63
58
  targetBranches?: string[] | undefined;
64
59
  targetPRLabels?: string[] | undefined;
60
+ username?: string | undefined;
65
61
  verbose?: boolean | undefined;
66
62
  help?: boolean | undefined;
67
63
  version?: boolean | undefined;
@@ -32,19 +32,20 @@ async function getOptionsFromGithub(options) {
32
32
  (0, throwOnInvalidAccessToken_1.throwOnInvalidAccessToken)({ error, repoName, repoOwner });
33
33
  res = swallowErrorIfConfigFileIsMissing(error);
34
34
  }
35
- // get the original repo (not the fork)
36
- const repo = res.repository.isFork ? res.repository.parent : res.repository;
37
35
  // it is not possible to have a branch named "backport"
38
- if (repo.ref?.name === 'backport') {
36
+ if (res.repository.illegalBackportBranch) {
39
37
  throw new HandledError_1.HandledError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
40
38
  }
41
- const historicalRemoteConfigs = repo.defaultBranchRef.target.history.edges;
39
+ const historicalRemoteConfigs = res.repository.defaultBranchRef.target.history.edges;
42
40
  const latestRemoteConfig = historicalRemoteConfigs[0]?.remoteConfig;
43
41
  const skipRemoteConfig = await getSkipRemoteConfigFile(options.cwd, options.skipRemoteConfig, latestRemoteConfig);
42
+ const remoteConfig = skipRemoteConfig
43
+ ? {}
44
+ : parseRemoteConfig(latestRemoteConfig);
44
45
  return {
45
- authenticatedUsername: res.viewer.login,
46
- sourceBranch: repo.defaultBranchRef.name,
47
- ...(skipRemoteConfig ? {} : parseRemoteConfig(latestRemoteConfig)),
46
+ authenticatedUsername: options.username ?? res.viewer.login,
47
+ sourceBranch: res.repository.defaultBranchRef.name,
48
+ ...remoteConfig,
48
49
  historicalBranchLabelMappings: skipRemoteConfig
49
50
  ? []
50
51
  : getHistoricalBranchLabelMappings(historicalRemoteConfigs),
@@ -117,9 +118,10 @@ function getHistoricalBranchLabelMappings(historicalRemoteConfigs) {
117
118
  }
118
119
  function swallowErrorIfConfigFileIsMissing(error) {
119
120
  const { data, errors } = error.axiosResponse.data;
120
- const wasMissingConfigError = errors?.some((error) => error.type === 'NOT_FOUND' &&
121
- error.path.join('.') ===
122
- 'repository.defaultBranchRef.target.history.edges.0.remoteConfig.file');
121
+ const wasMissingConfigError = errors?.some((error) => {
122
+ const isMatch = /^repository.defaultBranchRef.target.history.edges.\d+.remoteConfig.file$/.test(error.path.join('.'));
123
+ return isMatch && error.type === 'NOT_FOUND';
124
+ });
123
125
  // swallow error if it's just the config file that's missing
124
126
  if (wasMissingConfigError && data != null) {
125
127
  return data;
@@ -0,0 +1,136 @@
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
+ const os_1 = __importDefault(require("os"));
7
+ const getDevAccessToken_1 = require("../../../../test/private/getDevAccessToken");
8
+ const getOptionsFromGithub_1 = require("./getOptionsFromGithub");
9
+ describe('getOptionsFromGithub', () => {
10
+ let devAccessToken;
11
+ beforeAll(async () => {
12
+ devAccessToken = (0, getDevAccessToken_1.getDevAccessToken)();
13
+ jest.spyOn(os_1.default, 'homedir').mockReturnValue('/myHomeDir');
14
+ });
15
+ describe('access token', () => {
16
+ describe('is invalid', () => {
17
+ it('throws an error', async () => {
18
+ const combinedOptions = {
19
+ author: 'sqren',
20
+ accessToken: 'foo',
21
+ repoOwner: 'backport-org',
22
+ repoName: 'backport-e2e',
23
+ cwd: process.cwd(),
24
+ };
25
+ await expect((0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions)).rejects.toThrowError('Please check your access token and make sure it is valid.\nConfig: /myHomeDir/.backport/config.json');
26
+ });
27
+ });
28
+ describe('is valid', () => {
29
+ it('returns the options', async () => {
30
+ const combinedOptions = {
31
+ author: 'sqren',
32
+ accessToken: devAccessToken,
33
+ repoOwner: 'backport-org',
34
+ repoName: 'backport-e2e',
35
+ cwd: process.cwd(),
36
+ };
37
+ expect(await (0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions)).toEqual({
38
+ authenticatedUsername: 'sqren',
39
+ branchLabelMapping: {
40
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
41
+ '^v7.9.0$': '7.x',
42
+ '^v8.0.0$': 'master',
43
+ },
44
+ historicalBranchLabelMappings: [
45
+ {
46
+ branchLabelMapping: {
47
+ '^v(\\d+).(\\d+).\\d+$': '$1.$2',
48
+ '^v7.9.0$': '7.x',
49
+ '^v8.0.0$': 'master',
50
+ },
51
+ committedDate: '2020-08-15T10:33:06Z',
52
+ },
53
+ {
54
+ branchLabelMapping: { '6.1': '6.1', '6.3': '6.3' },
55
+ committedDate: '2020-08-15T10:20:23Z',
56
+ },
57
+ {
58
+ branchLabelMapping: { '6.1': '6.1', '6.3': '6.3' },
59
+ committedDate: '2020-08-15T10:17:57Z',
60
+ },
61
+ ],
62
+ sourceBranch: 'master',
63
+ targetBranchChoices: [
64
+ { checked: true, name: 'master' },
65
+ { checked: true, name: '7.x' },
66
+ '7.8',
67
+ ],
68
+ targetPRLabels: ['backport'],
69
+ repoOwner: 'backport-org',
70
+ repoName: 'backport-e2e',
71
+ });
72
+ });
73
+ });
74
+ });
75
+ describe('when `repoOwner` is fork', () => {
76
+ it('returns original repoOwner', async () => {
77
+ const combinedOptions = {
78
+ author: 'sqren',
79
+ accessToken: devAccessToken,
80
+ repoOwner: 'sqren',
81
+ repoName: 'backport-e2e',
82
+ cwd: process.cwd(),
83
+ };
84
+ const options = await (0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions);
85
+ await expect(options.repoOwner).toEqual('backport-org');
86
+ });
87
+ });
88
+ describe('sourceBranch', () => {
89
+ describe('when no sourceBranch is specified', () => {
90
+ it('uses the default branch of the repo', async () => {
91
+ const combinedOptions = {
92
+ author: 'sqren',
93
+ accessToken: devAccessToken,
94
+ repoOwner: 'backport-org',
95
+ repoName: 'repo-with-non-standard-main-branch',
96
+ cwd: process.cwd(),
97
+ };
98
+ const options = await (0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions);
99
+ await expect(options.sourceBranch).toEqual('my-custom-default-branch');
100
+ });
101
+ });
102
+ });
103
+ describe('when a branch named "backport"', () => {
104
+ describe('exists', () => {
105
+ it('shows a warning', async () => {
106
+ const combinedOptions = {
107
+ author: 'sqren',
108
+ accessToken: devAccessToken,
109
+ repoOwner: 'backport-org',
110
+ repoName: 'repo-with-branch-named-backport',
111
+ cwd: process.cwd(),
112
+ };
113
+ await expect(async () => {
114
+ await (0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions);
115
+ }).rejects.toThrowError('You must delete the branch "backport" to continue. See https://github.com/sqren/backport/issues/155 for details');
116
+ });
117
+ });
118
+ });
119
+ describe('when a backportrc.json config was deleted from repo', () => {
120
+ it('does not throw', async () => {
121
+ const combinedOptions = {
122
+ author: 'sqren',
123
+ accessToken: devAccessToken,
124
+ repoOwner: 'backport-org',
125
+ repoName: 'repo-with-backportrc-removed',
126
+ cwd: process.cwd(),
127
+ };
128
+ const options = await (0, getOptionsFromGithub_1.getOptionsFromGithub)(combinedOptions);
129
+ expect(options).toEqual({
130
+ authenticatedUsername: 'sqren',
131
+ sourceBranch: 'main',
132
+ historicalBranchLabelMappings: [],
133
+ });
134
+ });
135
+ });
136
+ });
@@ -6,33 +6,24 @@ export declare type RemoteConfig = {
6
6
  };
7
7
  };
8
8
  };
9
- declare type Repository = {
10
- ref: {
11
- name: string;
12
- } | null;
13
- defaultBranchRef: {
14
- name: string;
15
- target: {
16
- history: {
17
- edges: Array<{
18
- remoteConfig: RemoteConfig;
19
- }>;
20
- };
21
- };
22
- };
23
- };
24
9
  export interface GithubConfigOptionsResponse {
25
10
  viewer: {
26
11
  login: string;
27
12
  };
28
13
  repository: {
29
- isFork: true;
30
- defaultBranchRef: null;
31
- parent: Repository;
32
- } | ({
33
- isFork: false;
34
- parent: null;
35
- } & Repository);
14
+ illegalBackportBranch: {
15
+ id: string;
16
+ } | null;
17
+ defaultBranchRef: {
18
+ name: string;
19
+ target: {
20
+ history: {
21
+ edges: Array<{
22
+ remoteConfig: RemoteConfig;
23
+ }>;
24
+ };
25
+ };
26
+ };
27
+ };
36
28
  }
37
- export declare const query = "\n query GithubConfigOptions($repoOwner: String!, $repoName: String!) {\n viewer {\n login\n }\n repository(owner: $repoOwner, name: $repoName) {\n isFork\n ...Repo\n parent {\n ...Repo\n }\n }\n }\n\n fragment Repo on Repository {\n # check to see if a branch named \"backport\" exists\n ref(qualifiedName: \"refs/heads/backport\") {\n name\n }\n defaultBranchRef {\n name\n target {\n ... on Commit {\n history(first: 20, path: \".backportrc.json\") {\n edges {\n remoteConfig: node {\n committedDate\n file(path: \".backportrc.json\") {\n ... on TreeEntry {\n object {\n ... on Blob {\n text\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n";
38
- export {};
29
+ export declare const query = "\n query GithubConfigOptions($repoOwner: String!, $repoName: String!) {\n viewer {\n login\n }\n repository(owner: $repoOwner, name: $repoName) {\n # check to see if a branch named \"backport\" exists\n illegalBackportBranch: ref(qualifiedName: \"refs/heads/backport\") {\n id\n }\n defaultBranchRef {\n name\n target {\n ... on Commit {\n history(first: 20, path: \".backportrc.json\") {\n edges {\n remoteConfig: node {\n committedDate\n file(path: \".backportrc.json\") {\n ... on TreeEntry {\n object {\n ... on Blob {\n text\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n }\n";