Skip to content

Commit eb734e1

Browse files
authored
Respect config file source branch setting and display in UI (#518)
1 parent 6e1d364 commit eb734e1

16 files changed

+82
-61
lines changed

src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const TEST_REPO_NAME = 'repo-with-auto-merge-enabled';
1919
const INITIAL_SHA = '70aa879411e95b6662f8ddcb80a944fc4444579f';
2020
const accessToken = getDevAccessToken();
2121

22-
jest.setTimeout(10_000);
22+
jest.setTimeout(20_000);
2323

2424
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
2525

@@ -327,7 +327,8 @@ describe('enablePullRequestAutoMerge', () => {
327327
await resetReference(octokit);
328328
});
329329

330-
it('should not be possible to enable auto-merge', async () => {
330+
// eslint-disable-next-line jest/no-disabled-tests
331+
it.skip('should not be possible to enable auto-merge', async () => {
331332
let isMissingStatusChecks;
332333
let errorMessage;
333334

@@ -343,6 +344,13 @@ describe('enablePullRequestAutoMerge', () => {
343344
isMissingStatusChecks = res.isMissingStatusChecks;
344345
}
345346

347+
const autoMergeMethod = await fetchPullRequestAutoMergeMethod(
348+
options,
349+
pullNumber,
350+
);
351+
352+
expect(autoMergeMethod).toBe(undefined);
353+
346354
expect(errorMessage).toMatchInlineSnapshot(
347355
`"Pull request Pull request is in clean status (Github API v4)"`,
348356
);

src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ describe('fetchCommitBySha', () => {
8585
],
8686
};
8787

88-
await expect(
89-
await fetchCommitBySha({
90-
repoOwner: 'elastic',
91-
repoName: 'kibana',
92-
accessToken,
93-
sha: 'cb6fbc0e',
94-
sourceBranch: 'master',
95-
}),
96-
).toEqual(expectedCommit);
88+
const commit = await fetchCommitBySha({
89+
repoOwner: 'elastic',
90+
repoName: 'kibana',
91+
accessToken,
92+
sha: 'cb6fbc0e',
93+
sourceBranch: 'master',
94+
});
95+
96+
expect(commit).toEqual(expectedCommit);
9797
});
9898

9999
it('throws if sha does not exist', async () => {

src/lib/github/v4/fetchCommits/fetchCommitBySha.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export async function fetchCommitBySha(options: {
5858
}
5959

6060
const sourceCommit = data.repository.object;
61+
6162
if (!sourceCommit) {
6263
throw new BackportError(
6364
`No commit found on branch "${sourceBranch}" with sha "${sha}"`,

src/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export async function getOptionsFromGithub(options: {
3535
repoOwner: string;
3636
skipRemoteConfig?: boolean;
3737
globalConfigFile?: string;
38+
sourceBranch?: string;
3839
}) {
3940
const {
4041
accessToken,
@@ -86,7 +87,7 @@ export async function getOptionsFromGithub(options: {
8687

8788
return {
8889
authenticatedUsername: data.viewer.login,
89-
sourceBranch: data.repository.defaultBranchRef.name,
90+
sourceBranch: options.sourceBranch ?? data.repository.defaultBranchRef.name,
9091
isRepoPrivate: data.repository.isPrivate,
9192
...remoteConfig,
9293
};

src/options/config/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export async function getOptionsFromConfigFiles({
1818

1919
const globalConfigFile = optionsFromCliArgs.globalConfigFile;
2020

21+
const cwd = optionsFromCliArgs.cwd ?? process.cwd();
2122
const [projectConfig, globalConfig] = await Promise.all([
22-
getProjectConfig(projectConfigFile),
23+
getProjectConfig(projectConfigFile, cwd),
2324
getGlobalConfig(globalConfigFile),
2425
]);
2526

src/options/config/projectConfig.test.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('getProjectConfig', () => {
1717
}),
1818
);
1919

20-
const projectConfig = await getProjectConfig();
20+
const projectConfig = await getProjectConfig(undefined, undefined);
2121
expect(projectConfig?.targetBranchChoices).toEqual(['6.x']);
2222
});
2323
});
@@ -30,7 +30,7 @@ describe('getProjectConfig', () => {
3030
}),
3131
);
3232

33-
const projectConfig = await getProjectConfig();
33+
const projectConfig = await getProjectConfig(undefined, undefined);
3434
expect(projectConfig?.targetPRLabels).toEqual(['backport']);
3535
});
3636
});
@@ -43,7 +43,7 @@ describe('getProjectConfig', () => {
4343
}),
4444
);
4545

46-
const projectConfig = await getProjectConfig();
46+
const projectConfig = await getProjectConfig(undefined, undefined);
4747
expect(projectConfig?.repoOwner).toEqual('elastic');
4848
expect(projectConfig?.repoName).toEqual('kibana');
4949
});
@@ -61,11 +61,13 @@ describe('getProjectConfig', () => {
6161
}),
6262
);
6363

64-
projectConfig = await getProjectConfig();
64+
projectConfig = await getProjectConfig(undefined, '/my/cwd');
6565
});
6666

6767
it('should call findUp', () => {
68-
expect(findUp).toHaveBeenCalledWith('.backportrc.json');
68+
expect(findUp).toHaveBeenCalledWith('.backportrc.json', {
69+
cwd: '/my/cwd',
70+
});
6971
});
7072

7173
it('should return config', () => {
@@ -94,6 +96,7 @@ describe('getProjectConfig', () => {
9496

9597
projectConfig = await getProjectConfig(
9698
'/custom/path/to/project/.backportrc.json',
99+
undefined,
97100
);
98101
});
99102

@@ -122,15 +125,15 @@ describe('getProjectConfig', () => {
122125
describe('when projectConfig is empty', () => {
123126
it('should return empty config', async () => {
124127
jest.spyOn(fs, 'readFile').mockResolvedValueOnce('{}');
125-
const projectConfig = await getProjectConfig();
128+
const projectConfig = await getProjectConfig(undefined, undefined);
126129
expect(projectConfig).toEqual({});
127130
});
128131
});
129132

130133
describe('when projectConfig is missing', () => {
131134
it('should return empty config', async () => {
132135
(findUp as any as jest.SpyInstance).mockReturnValueOnce(undefined);
133-
const projectConfig = await getProjectConfig();
136+
const projectConfig = await getProjectConfig(undefined, undefined);
134137
expect(projectConfig).toEqual(undefined);
135138
});
136139
});

src/options/config/projectConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { ConfigFileOptions } from '../ConfigOptions';
44
import { readConfigFile } from '../config/readConfigFile';
55

66
export async function getProjectConfig(
7-
projectConfigFile?: string,
7+
projectConfigFile: string | undefined,
8+
cwd: string | undefined,
89
): Promise<ConfigFileOptions | undefined> {
910
const filepath = projectConfigFile
1011
? path.resolve(projectConfigFile)
11-
: await findUp('.backportrc.json');
12+
: await findUp('.backportrc.json', { cwd });
1213

1314
if (!filepath) {
1415
return;

src/options/options.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ function getMergedOptionsFromConfigAndCli({
228228
}
229229

230230
export function getActiveOptionsFormatted(options: ValidConfigOptions) {
231-
const customOptions = [['repo', `${options.repoOwner}/${options.repoName}`]];
231+
const customOptions = [
232+
['repo', `${options.repoOwner}/${options.repoName}`],
233+
['sourceBranch', `${options.sourceBranch}`],
234+
];
232235

233236
if (options.pullNumber) {
234237
customOptions.push(['pullNumber', `${options.pullNumber}`]);

src/test/e2e/cli/date-filters.private.test.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('date filters (dateSince, dateUntil)', () => {
1818
);
1919

2020
expect(output).toMatchInlineSnapshot(`
21-
"repo: backport-org/backport-e2e • since: 2020-08-15T10:00:00.000Z • until: 2020-08-15T10:30:00.000Z
21+
"repo: backport-org/backport-e2e • sourceBranch: master • since: 2020-08-15T10:00:00.000Z • until: 2020-08-15T10:30:00.000Z
2222
2323
? Select commit (Use arrow keys)
2424
❯ 1. Bump to 8.0.0
@@ -29,36 +29,39 @@ describe('date filters (dateSince, dateUntil)', () => {
2929
});
3030

3131
it('combined with --pr-filter', async () => {
32-
const { output } = await runBackportViaCli(
33-
[
34-
'--branch=7.x',
35-
'--repo=elastic/kibana',
36-
`--accessToken=${accessToken}`,
37-
`--author=sorenlouv`,
38-
'--since=2021-09-20',
39-
'--until=2021-10-01',
40-
],
41-
{ waitForString: 'Select commit' },
42-
);
32+
const options = [
33+
'--branch=7.x',
34+
'--repo=elastic/kibana',
35+
`--accessToken=${accessToken}`,
36+
'--since=2023-09-01',
37+
'--until=2023-10-01',
38+
];
4339

44-
const { output: outputFromPrFilter } = await runBackportViaCli(
45-
[
46-
'--branch=7.x',
47-
'--repo=elastic/kibana',
48-
`--accessToken=${accessToken}`,
49-
`--pr-filter="author:sorenlouv"`,
50-
'--since=2021-09-20',
51-
'--until=2021-10-01',
52-
'--source-branch=master',
53-
],
40+
const { output: outputWithoutPrFilter } = await runBackportViaCli(options, {
41+
waitForString: 'Select commit',
42+
});
43+
44+
expect(outputWithoutPrFilter).toMatchInlineSnapshot(`
45+
"repo: elastic/kibana • sourceBranch: main • autoMerge: true • since: 2023-09-01T00:00:00.000Z • until: 2023-10-01T00:00:00.000Z
46+
47+
? Select commit (Use arrow keys)
48+
❯ 1. [APM] Add support for versioned APIs in diagnostics tool (#167050)
49+
2. [APM] Add permissions for "input-only" package (#166234)
50+
3. [APM] Add docs for Serverless API tests (#166147)
51+
4. [APM] Paginate big traces (#165584) 8.10
52+
5. [APM] Move index settings persistence to data access plugn (#165560)"
53+
`);
54+
55+
const { output: outputWithPrFilter } = await runBackportViaCli(
56+
[...options, `--pr-filter="label:release_note:fix"`],
5457
{ waitForString: 'Select commit' },
5558
);
56-
expect(output).toMatchInlineSnapshot(`
57-
"repo: elastic/kibana • autoMerge: true • since: 2021-09-20T00:00:00.000Z • until: 2021-10-01T00:00:00.000Z
59+
60+
expect(outputWithPrFilter).toMatchInlineSnapshot(`
61+
"repo: elastic/kibana • sourceBranch: main • autoMerge: true • since: 2023-09-01T00:00:00.000Z • until: 2023-10-01T00:00:00.000Z
5862
5963
? Select commit (Use arrow keys)
60-
❯ 1. [APM] Add link to officials docs for APM UI settings (#113396) 7.x"
64+
❯ 1. [APM] Paginate big traces (#165584) 8.10"
6165
`);
62-
expect(output).toEqual(outputFromPrFilter);
6366
});
6467
});

src/test/e2e/cli/different-merge-strategies.private.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('different-merge-strategies', () => {
2121
);
2222

2323
expect(output).toMatchInlineSnapshot(`
24-
"repo: backport-org/different-merge-strategies • maxNumber: 20
24+
"repo: backport-org/different-merge-strategies • sourceBranch: main • maxNumber: 20
2525
2626
? Select commit (Use arrow keys)
2727
❯ 1. Downsides with "Rebase and merge"
@@ -71,7 +71,7 @@ describe('different-merge-strategies', () => {
7171
it('runs to completion without errors', () => {
7272
expect(output).toMatchInlineSnapshot(`
7373
"- Initializing...
74-
repo: backport-org/different-merge-strategies • pullNumber: 9
74+
repo: backport-org/different-merge-strategies • sourceBranch: main • pullNumber: 9
7575
7676
? Select pull request Merge pull request #9 from backport-org/many-merge-commits
7777
✔ 100% Cloning repository from github.com (one-time operation)
@@ -191,7 +191,7 @@ View pull request: this-is-a-dry-run"
191191
it('has the right output', async () => {
192192
expect(output).toMatchInlineSnapshot(`
193193
"- Initializing...
194-
repo: backport-org/different-merge-strategies • pullNumber: 9
194+
repo: backport-org/different-merge-strategies • sourceBranch: main • pullNumber: 9
195195
196196
? Select pull request Merge pull request #9 from backport-org/many-merge-commits
197197
✔ 100% Cloning repository from github.com (one-time operation)

0 commit comments

Comments
 (0)