Skip to content

Commit

Permalink
Merge pull request #47 from zeebe-io/comment-unsupported-merge-strate…
Browse files Browse the repository at this point in the history
…gies

Comment about unsupported merge strategies
  • Loading branch information
korthout authored May 28, 2021
2 parents fffca39 + 8228051 commit fe30eec
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 31 deletions.
7 changes: 6 additions & 1 deletion backport.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ then
1: incorrect usage / this message
2: unable to access worktree directory
3: unable to create new branch
4: unable to cherry-pick commit"
4: unable to cherry-pick commit
5: headref not found
6: baseref not found"
exit 1
fi

Expand All @@ -46,6 +48,9 @@ export GIT_COMMITTER_NAME="$user_name"
user_email="github-actions[bot]@users.noreply.github.com"
export GIT_COMMITTER_EMAIL="$user_email"

git cat-file -t "$headref" || exit 5
git cat-file -t "$baseref" || exit 6

echo "Find common ancestor between $baseref and $headref"
ancref=$(git merge-base "$baseref" "$headref")

Expand Down
24 changes: 15 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

27 changes: 18 additions & 9 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,28 @@ export class Backport {
2: "because it was unable to create/access the git worktree directory",
3: "because it was unable to create a new branch",
4: "because it was unable to cherry-pick the commit(s)",
5: "because 1 or more of the commits are not available",
6: "because 1 or more of the commits are not available",
};
const reason = reasons[exitcode] ?? "due to an unknown script error";

const suggestion =
exitcode <= 4
? dedent`\`\`\`bash
git fetch origin ${target}
git worktree add -d .worktree/${branchname} origin/${target}
cd .worktree/${branchname}
git checkout -b ${branchname}
ancref=$(git merge-base ${baseref} ${headref})
git cherry-pick -x $ancref..${headref}
\`\`\``
: dedent`Note that rebase and squash merges are not supported at this time.
For more information see https://github.com/zeebe-io/backport-action/issues/46.`;

return dedent`Backport failed for \`${target}\`, ${reason}.
Please cherry-pick the changes locally:
\`\`\`bash
git fetch origin ${target}
git worktree add -d .worktree/${branchname} origin/${target}
cd .worktree/${branchname}
git checkout -b ${branchname}
ancref=$(git merge-base ${baseref} ${headref})
git cherry-pick -x $ancref..${headref}
\`\`\``;
Please cherry-pick the changes locally.
${suggestion}`;
}

private composeMessageForGitPushFailure(
Expand Down
23 changes: 22 additions & 1 deletion src/test/backport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("the backport action", () => {
issue_number: 1347,
body: dedent`Backport failed for \`stable/0.25\`, due to an unknown script error.
Please cherry-pick the changes locally:
Please cherry-pick the changes locally.
\`\`\`bash
git fetch origin stable/0.25
git worktree add -d .worktree/backport-1347-to-stable/0.25 origin/stable/0.25
Expand All @@ -65,6 +65,27 @@ describe("the backport action", () => {
});
});

describe("and backport.sh returns exit code 5", () => {
beforeEach(() => {
mockedExec.callBackportScript.mockResolvedValue(5);
});
it("comments on failure", async () => {
await backport.run();
expect(
mockedDefaultGithubWithBackportLabel.createComment
).toHaveBeenCalledWith({
owner: "octocat",
repo: "Hello-World",
issue_number: 1347,
body: dedent`Backport failed for \`stable/0.25\`, because 1 or more of the commits are not available.
Please cherry-pick the changes locally.
Note that rebase and squash merges are not supported at this time.
For more information see https://github.com/zeebe-io/backport-action/issues/46.`,
});
});
});

describe("and backport.sh returns exit code 0", () => {
beforeEach(() => {
mockedExec.callBackportScript.mockResolvedValue(0);
Expand Down
42 changes: 32 additions & 10 deletions test/acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ async function exec({
command,
args = [],
options = { cwd: "test" },
quiet = true,
verbose = false,
}: Command): Promise<Output> {
const fullCommand = `${command} ${args?.join(" ")}`;
const execution = execPromised(fullCommand, options);
execution.then((ps) => {
console.log(fullCommand);
if (ps.stdout) console.log(ps.stdout);
if (ps.stderr) console.log(ps.stderr);
});
execution.catch(console.error);
if (!quiet) {
execution.then((ps) => {
console.log(fullCommand);
if (verbose) {
if (ps.stdout) console.log(ps.stdout);
if (ps.stderr) console.log(ps.stderr);
}
});
if (verbose) {
execution.catch(console.error);
}
}
return execution;
}

describe("given a git repository with a merged pr", () => {
beforeEach(async () => {
beforeAll(async () => {
await exec({ command: "./setup.sh" });

// print and check the history graph
// check the history graph
const { stdout } = await exec({
command: "git log --graph --oneline --decorate",
options: {
Expand All @@ -34,8 +42,19 @@ describe("given a git repository with a merged pr", () => {
);
});

describe("when backport.sh script is executed with unavailable headref", () => {
test("then it returns exit code 5", () => {
// promisedExec's error is unhandled when exit code is non-zero, use child.exec instead
child
.exec(
"./backport.sh test/repo abcdef123456 master^ release-1 backport-b-to-1"
)
.on("exit", (code) => expect(code).toBe(5));
});
});

describe("when backport.sh script is executed", () => {
beforeEach(async () => {
beforeAll(async () => {
await exec({
command: "./backport.sh",
args: [
Expand All @@ -46,6 +65,7 @@ describe("given a git repository with a merged pr", () => {
"backport-b-to-1", // branchname (name of new backport branch)
],
options: { cwd: "./" },
quiet: false,
});
});

Expand Down Expand Up @@ -82,7 +102,7 @@ describe("given a git repository with a merged pr", () => {
});
});

afterEach(async () => {
afterAll(async () => {
await exec({ command: "./cleanup.sh" });
});
});
Expand All @@ -91,5 +111,7 @@ type Command = {
command: string;
args?: string[];
options?: child.ExecOptions;
quiet?: boolean;
verbose?: boolean;
};
type Output = { stdout: string; stderr: string };
3 changes: 3 additions & 0 deletions test/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ setup () {

git init

echo "[commit]" >> .git/config
echo " gpgsign = false" >> .git/config

name="test-setup[bot]"
export GIT_AUTHOR_NAME="$name"
export GIT_COMMITTER_NAME="$name"
Expand Down

0 comments on commit fe30eec

Please sign in to comment.