diff --git a/src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts b/src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts index e1d6c251..c729c5ff 100644 --- a/src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts +++ b/src/lib/github/v4/enablePullRequestAutoMerge.mutation.test.ts @@ -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)); @@ -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; @@ -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)"`, ); diff --git a/src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts b/src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts index 5cc6740b..e6a6fa4c 100644 --- a/src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts +++ b/src/lib/github/v4/fetchCommits/fetchCommitBySha.private.test.ts @@ -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 () => { diff --git a/src/lib/github/v4/fetchCommits/fetchCommitBySha.ts b/src/lib/github/v4/fetchCommits/fetchCommitBySha.ts index b17cbad5..81e8bf71 100644 --- a/src/lib/github/v4/fetchCommits/fetchCommitBySha.ts +++ b/src/lib/github/v4/fetchCommits/fetchCommitBySha.ts @@ -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}"`, diff --git a/src/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.ts b/src/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.ts index 11823027..2a5ba03c 100644 --- a/src/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.ts +++ b/src/lib/github/v4/getOptionsFromGithub/getOptionsFromGithub.ts @@ -35,6 +35,7 @@ export async function getOptionsFromGithub(options: { repoOwner: string; skipRemoteConfig?: boolean; globalConfigFile?: string; + sourceBranch?: string; }) { const { accessToken, @@ -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, }; diff --git a/src/options/config/config.ts b/src/options/config/config.ts index f8b9a731..26a6ed52 100644 --- a/src/options/config/config.ts +++ b/src/options/config/config.ts @@ -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), ]); diff --git a/src/options/config/projectConfig.test.ts b/src/options/config/projectConfig.test.ts index 1abac7af..2940dd37 100644 --- a/src/options/config/projectConfig.test.ts +++ b/src/options/config/projectConfig.test.ts @@ -17,7 +17,7 @@ describe('getProjectConfig', () => { }), ); - const projectConfig = await getProjectConfig(); + const projectConfig = await getProjectConfig(undefined, undefined); expect(projectConfig?.targetBranchChoices).toEqual(['6.x']); }); }); @@ -30,7 +30,7 @@ describe('getProjectConfig', () => { }), ); - const projectConfig = await getProjectConfig(); + const projectConfig = await getProjectConfig(undefined, undefined); expect(projectConfig?.targetPRLabels).toEqual(['backport']); }); }); @@ -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'); }); @@ -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', () => { @@ -94,6 +96,7 @@ describe('getProjectConfig', () => { projectConfig = await getProjectConfig( '/custom/path/to/project/.backportrc.json', + undefined, ); }); @@ -122,7 +125,7 @@ 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({}); }); }); @@ -130,7 +133,7 @@ describe('getProjectConfig', () => { 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); }); }); diff --git a/src/options/config/projectConfig.ts b/src/options/config/projectConfig.ts index 71bef041..b0721105 100644 --- a/src/options/config/projectConfig.ts +++ b/src/options/config/projectConfig.ts @@ -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 { const filepath = projectConfigFile ? path.resolve(projectConfigFile) - : await findUp('.backportrc.json'); + : await findUp('.backportrc.json', { cwd }); if (!filepath) { return; diff --git a/src/options/options.ts b/src/options/options.ts index 6da9ca0b..443334c4 100644 --- a/src/options/options.ts +++ b/src/options/options.ts @@ -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}`]); diff --git a/src/test/e2e/cli/date-filters.private.test.ts b/src/test/e2e/cli/date-filters.private.test.ts index bd2716bc..536802c8 100644 --- a/src/test/e2e/cli/date-filters.private.test.ts +++ b/src/test/e2e/cli/date-filters.private.test.ts @@ -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 @@ -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); }); }); diff --git a/src/test/e2e/cli/different-merge-strategies.private.test.ts b/src/test/e2e/cli/different-merge-strategies.private.test.ts index ce1527ae..0b0d7202 100644 --- a/src/test/e2e/cli/different-merge-strategies.private.test.ts +++ b/src/test/e2e/cli/different-merge-strategies.private.test.ts @@ -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" @@ -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) @@ -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) diff --git a/src/test/e2e/cli/entrypoint.cli.private.test.ts b/src/test/e2e/cli/entrypoint.cli.private.test.ts index 9b1a1921..3a5febe1 100644 --- a/src/test/e2e/cli/entrypoint.cli.private.test.ts +++ b/src/test/e2e/cli/entrypoint.cli.private.test.ts @@ -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 @@ -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 @@ -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) diff --git a/src/test/e2e/cli/error-handling-interactive-mode.private.test.ts b/src/test/e2e/cli/error-handling-interactive-mode.private.test.ts index 4b2d39f6..0cb631aa 100644 --- a/src/test/e2e/cli/error-handling-interactive-mode.private.test.ts +++ b/src/test/e2e/cli/error-handling-interactive-mode.private.test.ts @@ -90,7 +90,7 @@ describe('interactive error handling', () => { stringAfter: '', }), ).toMatchInlineSnapshot(` -"repo: backport-org/repo-with-conflicts • pullNumber: 12 +"repo: backport-org/repo-with-conflicts • sourceBranch: main • pullNumber: 12 Backporting to 7.x: diff --git a/src/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.ts b/src/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.ts index db34d929..94c867c8 100644 --- a/src/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.ts +++ b/src/test/e2e/cli/gracefully-handles-corrupt-repo.private.test.ts @@ -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 diff --git a/src/test/e2e/cli/repo-with-backportrc-removed.private.test.ts b/src/test/e2e/cli/repo-with-backportrc-removed.private.test.ts index bf713efc..24cd240f 100644 --- a/src/test/e2e/cli/repo-with-backportrc-removed.private.test.ts +++ b/src/test/e2e/cli/repo-with-backportrc-removed.private.test.ts @@ -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 diff --git a/src/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.ts b/src/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.ts index 898bffe1..038d7f07 100644 --- a/src/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.ts +++ b/src/test/e2e/cli/repo-with-changing-branchLabelMapping.private.test.ts @@ -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 to select, to toggle all, to invert selection, and to proceed) diff --git a/src/test/e2e/cli/test-that-repo-can-be-cloned.private.test.ts b/src/test/e2e/cli/test-that-repo-can-be-cloned.private.test.ts index 425855ca..9e6607a0 100644 --- a/src/test/e2e/cli/test-that-repo-can-be-cloned.private.test.ts +++ b/src/test/e2e/cli/test-that-repo-can-be-cloned.private.test.ts @@ -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) @@ -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)