Skip to content

Commit

Permalink
Respect config file source branch setting and display in UI (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Nov 18, 2024
1 parent 6e1d364 commit eb734e1
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 61 deletions.
12 changes: 10 additions & 2 deletions src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const TEST_REPO_NAME = 'repo-with-auto-merge-enabled';
const INITIAL_SHA = '70aa879411e95b6662f8ddcb80a944fc4444579f';
const accessToken = getDevAccessToken();

jest.setTimeout(10_000);
jest.setTimeout(20_000);

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

Expand Down Expand Up @@ -327,7 +327,8 @@ describe('enablePullRequestAutoMerge', () => {
await resetReference(octokit);
});

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

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

const autoMergeMethod = await fetchPullRequestAutoMergeMethod(
options,
pullNumber,
);

expect(autoMergeMethod).toBe(undefined);

expect(errorMessage).toMatchInlineSnapshot(
`"Pull request Pull request is in clean status (Github API v4)"`,
);
Expand Down
18 changes: 9 additions & 9 deletions src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ describe('fetchCommitBySha', () => {
],
};

await expect(
await fetchCommitBySha({
repoOwner: 'elastic',
repoName: 'kibana',
accessToken,
sha: 'cb6fbc0e',
sourceBranch: 'master',
}),
).toEqual(expectedCommit);
const commit = await fetchCommitBySha({
repoOwner: 'elastic',
repoName: 'kibana',
accessToken,
sha: 'cb6fbc0e',
sourceBranch: 'master',
});

expect(commit).toEqual(expectedCommit);
});

it('throws if sha does not exist', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/github/v4/fetchCommits/fetchCommitBySha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function fetchCommitBySha(options: {
}

const sourceCommit = data.repository.object;

if (!sourceCommit) {
throw new BackportError(
`No commit found on branch "${sourceBranch}" with sha "${sha}"`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export async function getOptionsFromGithub(options: {
repoOwner: string;
skipRemoteConfig?: boolean;
globalConfigFile?: string;
sourceBranch?: string;
}) {
const {
accessToken,
Expand Down Expand Up @@ -86,7 +87,7 @@ export async function getOptionsFromGithub(options: {

return {
authenticatedUsername: data.viewer.login,
sourceBranch: data.repository.defaultBranchRef.name,
sourceBranch: options.sourceBranch ?? data.repository.defaultBranchRef.name,
isRepoPrivate: data.repository.isPrivate,
...remoteConfig,
};
Expand Down
3 changes: 2 additions & 1 deletion src/options/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export async function getOptionsFromConfigFiles({

const globalConfigFile = optionsFromCliArgs.globalConfigFile;

const cwd = optionsFromCliArgs.cwd ?? process.cwd();
const [projectConfig, globalConfig] = await Promise.all([
getProjectConfig(projectConfigFile),
getProjectConfig(projectConfigFile, cwd),
getGlobalConfig(globalConfigFile),
]);

Expand Down
17 changes: 10 additions & 7 deletions src/options/config/projectConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('getProjectConfig', () => {
}),
);

const projectConfig = await getProjectConfig();
const projectConfig = await getProjectConfig(undefined, undefined);
expect(projectConfig?.targetBranchChoices).toEqual(['6.x']);
});
});
Expand All @@ -30,7 +30,7 @@ describe('getProjectConfig', () => {
}),
);

const projectConfig = await getProjectConfig();
const projectConfig = await getProjectConfig(undefined, undefined);
expect(projectConfig?.targetPRLabels).toEqual(['backport']);
});
});
Expand All @@ -43,7 +43,7 @@ describe('getProjectConfig', () => {
}),
);

const projectConfig = await getProjectConfig();
const projectConfig = await getProjectConfig(undefined, undefined);
expect(projectConfig?.repoOwner).toEqual('elastic');
expect(projectConfig?.repoName).toEqual('kibana');
});
Expand All @@ -61,11 +61,13 @@ describe('getProjectConfig', () => {
}),
);

projectConfig = await getProjectConfig();
projectConfig = await getProjectConfig(undefined, '/my/cwd');
});

it('should call findUp', () => {
expect(findUp).toHaveBeenCalledWith('.backportrc.json');
expect(findUp).toHaveBeenCalledWith('.backportrc.json', {
cwd: '/my/cwd',
});
});

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

projectConfig = await getProjectConfig(
'/custom/path/to/project/.backportrc.json',
undefined,
);
});

Expand Down Expand Up @@ -122,15 +125,15 @@ describe('getProjectConfig', () => {
describe('when projectConfig is empty', () => {
it('should return empty config', async () => {
jest.spyOn(fs, 'readFile').mockResolvedValueOnce('{}');
const projectConfig = await getProjectConfig();
const projectConfig = await getProjectConfig(undefined, undefined);
expect(projectConfig).toEqual({});
});
});

describe('when projectConfig is missing', () => {
it('should return empty config', async () => {
(findUp as any as jest.SpyInstance).mockReturnValueOnce(undefined);
const projectConfig = await getProjectConfig();
const projectConfig = await getProjectConfig(undefined, undefined);
expect(projectConfig).toEqual(undefined);
});
});
Expand Down
5 changes: 3 additions & 2 deletions src/options/config/projectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { ConfigFileOptions } from '../ConfigOptions';
import { readConfigFile } from '../config/readConfigFile';

export async function getProjectConfig(
projectConfigFile?: string,
projectConfigFile: string | undefined,
cwd: string | undefined,
): Promise<ConfigFileOptions | undefined> {
const filepath = projectConfigFile
? path.resolve(projectConfigFile)
: await findUp('.backportrc.json');
: await findUp('.backportrc.json', { cwd });

if (!filepath) {
return;
Expand Down
5 changes: 4 additions & 1 deletion src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ function getMergedOptionsFromConfigAndCli({
}

export function getActiveOptionsFormatted(options: ValidConfigOptions) {
const customOptions = [['repo', `${options.repoOwner}/${options.repoName}`]];
const customOptions = [
['repo', `${options.repoOwner}/${options.repoName}`],
['sourceBranch', `${options.sourceBranch}`],
];

if (options.pullNumber) {
customOptions.push(['pullNumber', `${options.pullNumber}`]);
Expand Down
55 changes: 29 additions & 26 deletions src/test/e2e/cli/date-filters.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('date filters (dateSince, dateUntil)', () => {
);

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

it('combined with --pr-filter', async () => {
const { output } = await runBackportViaCli(
[
'--branch=7.x',
'--repo=elastic/kibana',
`--accessToken=${accessToken}`,
`--author=sorenlouv`,
'--since=2021-09-20',
'--until=2021-10-01',
],
{ waitForString: 'Select commit' },
);
const options = [
'--branch=7.x',
'--repo=elastic/kibana',
`--accessToken=${accessToken}`,
'--since=2023-09-01',
'--until=2023-10-01',
];

const { output: outputFromPrFilter } = await runBackportViaCli(
[
'--branch=7.x',
'--repo=elastic/kibana',
`--accessToken=${accessToken}`,
`--pr-filter="author:sorenlouv"`,
'--since=2021-09-20',
'--until=2021-10-01',
'--source-branch=master',
],
const { output: outputWithoutPrFilter } = await runBackportViaCli(options, {
waitForString: 'Select commit',
});

expect(outputWithoutPrFilter).toMatchInlineSnapshot(`
"repo: elastic/kibana • sourceBranch: main • autoMerge: true • since: 2023-09-01T00:00:00.000Z • until: 2023-10-01T00:00:00.000Z
? Select commit (Use arrow keys)
❯ 1. [APM] Add support for versioned APIs in diagnostics tool (#167050)
2. [APM] Add permissions for "input-only" package (#166234)
3. [APM] Add docs for Serverless API tests (#166147)
4. [APM] Paginate big traces (#165584) 8.10
5. [APM] Move index settings persistence to data access plugn (#165560)"
`);

const { output: outputWithPrFilter } = await runBackportViaCli(
[...options, `--pr-filter="label:release_note:fix"`],
{ waitForString: 'Select commit' },
);
expect(output).toMatchInlineSnapshot(`
"repo: elastic/kibana • autoMerge: true • since: 2021-09-20T00:00:00.000Z • until: 2021-10-01T00:00:00.000Z

expect(outputWithPrFilter).toMatchInlineSnapshot(`
"repo: elastic/kibana • sourceBranch: main • autoMerge: true • since: 2023-09-01T00:00:00.000Z • until: 2023-10-01T00:00:00.000Z
? Select commit (Use arrow keys)
❯ 1. [APM] Add link to officials docs for APM UI settings (#113396) 7.x"
❯ 1. [APM] Paginate big traces (#165584) 8.10"
`);
expect(output).toEqual(outputFromPrFilter);
});
});
6 changes: 3 additions & 3 deletions src/test/e2e/cli/different-merge-strategies.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('different-merge-strategies', () => {
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/different-merge-strategies • maxNumber: 20
"repo: backport-org/different-merge-strategies • sourceBranch: main • maxNumber: 20
? Select commit (Use arrow keys)
❯ 1. Downsides with "Rebase and merge"
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('different-merge-strategies', () => {
it('runs to completion without errors', () => {
expect(output).toMatchInlineSnapshot(`
"- Initializing...
repo: backport-org/different-merge-strategies • pullNumber: 9
repo: backport-org/different-merge-strategies • sourceBranch: main • pullNumber: 9
? Select pull request Merge pull request #9 from backport-org/many-merge-commits
✔ 100% Cloning repository from github.com (one-time operation)
Expand Down Expand Up @@ -191,7 +191,7 @@ View pull request: this-is-a-dry-run"
it('has the right output', async () => {
expect(output).toMatchInlineSnapshot(`
"- Initializing...
repo: backport-org/different-merge-strategies • pullNumber: 9
repo: backport-org/different-merge-strategies • sourceBranch: main • pullNumber: 9
? Select pull request Merge pull request #9 from backport-org/many-merge-commits
✔ 100% Cloning repository from github.com (one-time operation)
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/cli/entrypoint.cli.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Or contact me directly: "
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/backport-e2e
"repo: backport-org/backport-e2e • sourceBranch: master
? Select commit (Use arrow keys)
❯ 1. Add sheep emoji (#9) 7.8
Expand Down Expand Up @@ -179,7 +179,7 @@ Or contact me directly: "
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/backport-e2e • maxNumber: 6
"repo: backport-org/backport-e2e • sourceBranch: master • maxNumber: 6
? Select commit (Use arrow keys)
❯ 1. Add sheep emoji (#9) 7.8
Expand All @@ -205,7 +205,7 @@ Or contact me directly: "
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/backport-e2e • maxNumber: 6
"repo: backport-org/backport-e2e • sourceBranch: 7.x • maxNumber: 6
? Select commit (Use arrow keys)
❯ 1. Add 🍏 emoji (#5) (#6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('interactive error handling', () => {
stringAfter: '<BACKPORT_DIR>',
}),
).toMatchInlineSnapshot(`
"repo: backport-org/repo-with-conflicts • pullNumber: 12
"repo: backport-org/repo-with-conflicts • sourceBranch: main • pullNumber: 12
Backporting to 7.x:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('gracefully handle corrupted repo', () => {
// second run: backport should re-create remotes and branches correctly
expect(output).toMatchInlineSnapshot(`
"- Initializing...
repo: backport-org/integration-test
repo: backport-org/integration-test • sourceBranch: master
? Select commit Bump to 8.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('repo-with-backportrc-removed (missing .backportrc.json config file)',
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/repo-with-backportrc-removed
"repo: backport-org/repo-with-backportrc-removed • sourceBranch: main
? Select commit (Use arrow keys)
❯ 1. Rename README.me to README.md
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('backport-org/repo-with-changing-branchLabelMapping', () => {
);

expect(output).toMatchInlineSnapshot(`
"repo: backport-org/repo-with-changing-branchLabelMapping • pullNumber: 6
"repo: backport-org/repo-with-changing-branchLabelMapping • sourceBranch: main • pullNumber: 6
? Select branch (Press <space> to select, <a> to toggle all, <i> to invert
selection, and <enter> to proceed)
Expand Down
4 changes: 2 additions & 2 deletions src/test/e2e/cli/test-that-repo-can-be-cloned.private.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('test-that-repo-can-be-cloned', () => {
expect(output).toContain('Cloning repository from github.com');
expect(output).toMatchInlineSnapshot(`
"- Initializing...
repo: backport-org/test-that-repo-can-be-cloned • pullNumber: 1
repo: backport-org/test-that-repo-can-be-cloned • sourceBranch: main • pullNumber: 1
? Select pull request Beginning of a beautiful repo (#1)
✔ 100% Cloning repository from github.com (one-time operation)
Expand All @@ -56,7 +56,7 @@ View pull request: this-is-a-dry-run"
expect(output).not.toContain('Cloning repository from github.com');
expect(output).toMatchInlineSnapshot(`
"- Initializing...
repo: backport-org/test-that-repo-can-be-cloned • pullNumber: 1
repo: backport-org/test-that-repo-can-be-cloned • sourceBranch: main • pullNumber: 1
? Select pull request Beginning of a beautiful repo (#1)
Expand Down

0 comments on commit eb734e1

Please sign in to comment.