UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

262 lines (251 loc) • 12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const promises_1 = __importDefault(require("fs/promises")); const childProcessHelper_1 = require("../../childProcessHelper"); const getDevAccessToken_1 = require("../../private/getDevAccessToken"); const replaceStringAndLinebreaks_1 = require("../../replaceStringAndLinebreaks"); const sandbox_1 = require("../../sandbox"); const runBackportViaCli_1 = require("./runBackportViaCli"); const accessToken = (0, getDevAccessToken_1.getDevAccessToken)(); jest.setTimeout(40000); describe('different-merge-strategies', () => { it('list all commits regardless how they were merged', async () => { const { output } = await (0, runBackportViaCli_1.runBackportViaCli)([ '--branch=7.x', '--repo=backport-org/different-merge-strategies', `--accessToken=${accessToken}`, '-n=20', ], { waitForString: 'Select commit', timeoutSeconds: 4 }); expect(output).toMatchInlineSnapshot(` "repo: backport-org/different-merge-strategies šŸ”¹ sourceBranch: main šŸ”¹ author: sorenlouv šŸ”¹ maxNumber: 20 ? Select commit (Use arrow keys) āÆ 1. Downsides with "Rebase and merge" 2. Add description for "Rebase and merge" 3. Add "Rebase and merge" header 4. Create rebase-and-merge.txt 5. Merge pull request #9 from backport-org/many-merge-commits 7.x 6. Merge strategy: Eighth of many merges 7.x 7. Merge strategy: Seventh of many merges 7.x 8. Merge strategy: Sixth of many merges 7.x 9. Merge strategy: Fifth of many merges 7.x 10.Merge strategy: Fourth of many merges 7.x 11.Merge strategy: Third of many merges 7.x 12.Merge strategy: Second of many merges 7.x 13.Merge strategy: First of many merges 7.x 14.Using squash to merge commits (#3) 15.Rebase strategy: Second commit 16.Rebase strategy: First commit 17.Merge pull request #1 from backport-org/merge-strategy 18.Merge strategy: Second commit 19.Merge strategy: First commit 20.Initial commit" `); }); describe('when selecting a merge commit with eight commits', () => { let output; let sandboxPath; beforeAll(async () => { sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename }); await (0, sandbox_1.resetSandbox)(sandboxPath); const res = await (0, runBackportViaCli_1.runBackportViaCli)([ '--branch=7.x', '--repo=backport-org/different-merge-strategies', `--accessToken=${accessToken}`, `--dir=${sandboxPath}`, '--pr=9', '--dry-run', ], { showOra: true, timeoutSeconds: 5 }); output = res.output; }); it('runs to completion without errors', () => { expect(output).toMatchInlineSnapshot(` "- Initializing... repo: backport-org/different-merge-strategies šŸ”¹ sourceBranch: main šŸ”¹ pullNumber: 9 šŸ”¹ author: sorenlouv ? Select pull request Merge pull request #9 from backport-org/many-merge-commits āœ” 100% Cloning repository from github.com (one-time operation) Backporting to 7.x: - Pulling latest changes āœ” Pulling latest changes - Cherry-picking: Merge strategy: First of many merges āœ” Cherry-picking: Merge strategy: First of many merges - Cherry-picking: Merge strategy: Second of many merges āœ” Cherry-picking: Merge strategy: Second of many merges - Cherry-picking: Merge strategy: Third of many merges āœ” Cherry-picking: Merge strategy: Third of many merges - Cherry-picking: Merge strategy: Fourth of many merges āœ” Cherry-picking: Merge strategy: Fourth of many merges - Cherry-picking: Merge strategy: Fifth of many merges āœ” Cherry-picking: Merge strategy: Fifth of many merges - Cherry-picking: Merge strategy: Sixth of many merges āœ” Cherry-picking: Merge strategy: Sixth of many merges - Cherry-picking: Merge strategy: Seventh of many merges āœ” Cherry-picking: Merge strategy: Seventh of many merges - Cherry-picking: Merge strategy: Eighth of many merges āœ” Cherry-picking: Merge strategy: Eighth of many merges - Creating pull request āœ” Creating pull request View pull request: this-is-a-dry-run" `); }); it('backports all immediate children of the merge commit', async () => { const commits = await listCommits(sandboxPath); expect(commits.slice(0, 8)).toEqual([ 'Merge strategy: Eighth of many merges', 'Merge strategy: Seventh of many merges', 'Merge strategy: Sixth of many merges', 'Merge strategy: Fifth of many merges', 'Merge strategy: Fourth of many merges', 'Merge strategy: Third of many merges', 'Merge strategy: Second of many merges', 'Merge strategy: First of many merges', ]); }); }); describe('when selecting a merge commit with two commits', () => { let sandboxPath; beforeAll(async () => { sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename }); await (0, sandbox_1.resetSandbox)(sandboxPath); await (0, runBackportViaCli_1.runBackportViaCli)([ '--branch=7.x', '--repo=backport-org/different-merge-strategies', `--accessToken=${accessToken}`, `--dir=${sandboxPath}`, '--pr=1', '--dry-run', ], { showOra: true }); }); it('backports all immediate children of the merge commit', async () => { const commits = await listCommits(sandboxPath); expect(commits.slice(0, 2)).toEqual([ 'Merge strategy: Second commit', 'Merge strategy: First commit', ]); }); }); describe('when selecting a merge commit with eight commits and a conflict occurs', () => { let sandboxPath; let output; beforeAll(async () => { sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename }); await (0, sandbox_1.resetSandbox)(sandboxPath); const proc = await (0, runBackportViaCli_1.runBackportViaCli)([ '--branch=7.1', '--repo=backport-org/different-merge-strategies', `--accessToken=${accessToken}`, `--dir=${sandboxPath}`, '--pr=9', '--dry-run', '--editor=false', ], { keepAlive: true, timeoutSeconds: 5, showOra: true, waitForString: 'Press ENTER when the conflicts are resolved and files are staged', }); await (0, childProcessHelper_1.exec)(`git status`, { cwd: sandboxPath }); await promises_1.default.writeFile(`${sandboxPath}/new-file-added-with-many-merge-commits.txt`, `File added directly to 7.1 to cause merge conflict\nPrevious merge commit didn't have enough commits\n`); await (0, childProcessHelper_1.exec)(`git add -A`, { cwd: sandboxPath }); const res = await proc.keypress('enter', { showOra: true, timeoutSeconds: 5, }); output = (0, replaceStringAndLinebreaks_1.removeLinesBreaksInConflictingFiles)(res.output).replaceAll(sandboxPath, '<SANDBOX_PATH>'); }); it('has the right output', async () => { expect(output).toMatchInlineSnapshot(` "- Initializing... repo: backport-org/different-merge-strategies šŸ”¹ sourceBranch: main šŸ”¹ pullNumber: 9 šŸ”¹ author: sorenlouv ? Select pull request Merge pull request #9 from backport-org/many-merge-commits āœ” 100% Cloning repository from github.com (one-time operation) Backporting to 7.1: - Pulling latest changes āœ” Pulling latest changes - Cherry-picking: Merge strategy: First of many merges āœ– Cherry-picking: Merge strategy: First of many merges The commit could not be backported due to conflicts Please fix the conflicts in <SANDBOX_PATH> ? Fix the following conflicts manually: Conflicting files: - <SANDBOX_PATH>/new-file-added-with-many-merge-commits.txt Press ENTER when the conflicts are resolved and files are staged (Y/n) ? Fix the following conflicts manually: Conflicting files: - <SANDBOX_PATH>/new-file-added-with-many-merge-commits.txt Press ENTER when the conflicts are resolved and files are staged Yes āœ” Cherry-picking: Merge strategy: First of many merges - Cherry-picking: Merge strategy: Second of many merges āœ” Cherry-picking: Merge strategy: Second of many merges - Cherry-picking: Merge strategy: Third of many merges āœ” Cherry-picking: Merge strategy: Third of many merges - Cherry-picking: Merge strategy: Fourth of many merges āœ” Cherry-picking: Merge strategy: Fourth of many merges - Cherry-picking: Merge strategy: Fifth of many merges āœ” Cherry-picking: Merge strategy: Fifth of many merges - Cherry-picking: Merge strategy: Sixth of many merges āœ” Cherry-picking: Merge strategy: Sixth of many merges - Cherry-picking: Merge strategy: Seventh of many merges āœ” Cherry-picking: Merge strategy: Seventh of many merges - Cherry-picking: Merge strategy: Eighth of many merges āœ” Cherry-picking: Merge strategy: Eighth of many merges - Creating pull request āœ” Creating pull request View pull request: this-is-a-dry-run" `); }); it('backports all immediate children of the merge commit', async () => { const commits = await listCommits(sandboxPath); expect(commits.slice(0, 8)).toEqual([ 'Merge strategy: Eighth of many merges', 'Merge strategy: Seventh of many merges', 'Merge strategy: Sixth of many merges', 'Merge strategy: Fifth of many merges', 'Merge strategy: Fourth of many merges', 'Merge strategy: Third of many merges', 'Merge strategy: Second of many merges', 'Merge strategy: First of many merges', ]); }); }); describe('when using "Rebase and Merge" strategy', () => { let sandboxPath; let output; beforeAll(async () => { sandboxPath = (0, sandbox_1.getSandboxPath)({ filename: __filename }); await (0, sandbox_1.resetSandbox)(sandboxPath); const res = await (0, runBackportViaCli_1.runBackportViaCli)([ '--branch=7.x', '--repo=backport-org/different-merge-strategies', `--accessToken=${accessToken}`, `--dir=${sandboxPath}`, '--pr=21', '--dry-run', ], { showOra: true }); output = res.output; }); it('backports all commits', async () => { const commits = await (await listCommits(sandboxPath)).slice(0, 3); expect(commits).toEqual([ 'Downsides with "Rebase and merge"', 'Add description for "Rebase and merge"', 'Add "Rebase and merge" header', ]); }); it('shows every commit in output', async () => { expect(output .split('\n') .filter((line) => line.startsWith('āœ” Cherry-picking:'))).toEqual([ 'āœ” Cherry-picking: Add "Rebase and merge" header', 'āœ” Cherry-picking: Add description for "Rebase and merge"', 'āœ” Cherry-picking: Downsides with "Rebase and merge"', ]); }); }); }); async function listCommits(sandboxPath) { const { stdout } = await (0, childProcessHelper_1.exec)('git --no-pager log --pretty=format:%s', { cwd: sandboxPath, }); return stdout.split('\n'); } //# sourceMappingURL=different-merge-strategies.private.test.js.map