Skip to content

Commit

Permalink
Merge pull request #397 from korthout/395-skip-merge-commits-in-sugge…
Browse files Browse the repository at this point in the history
…stion

Skip merge commits in suggestion
  • Loading branch information
korthout authored Nov 7, 2023
2 parents e45fafa + 19a4042 commit 487faf2
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export class Backport {
const repo = payload.repository?.name ?? this.github.getRepo().repo;
const pull_number = this.github.getPullNumber();
const mainpr = await this.github.getPullRequest(pull_number);
const headref = mainpr.head.sha;
const baseref = mainpr.base.sha;

if (!(await this.github.isMerged(mainpr))) {
const message = "Only merged pull requests can be backported.";
Expand Down Expand Up @@ -176,12 +174,10 @@ export class Backport {
this.config.pwd,
);
} catch (error) {
const message = this.composeMessageForBackportScriptFailure(
const message = this.composeMessageForCheckoutFailure(
target,
3,
baseref,
headref,
branchname,
commitShasToCherryPick,
);
console.error(message);
successByTarget.set(target, false);
Expand All @@ -197,12 +193,10 @@ export class Backport {
try {
await this.git.cherryPick(commitShasToCherryPick, this.config.pwd);
} catch (error) {
const message = this.composeMessageForBackportScriptFailure(
const message = this.composeMessageForCherryPickFailure(
target,
4,
baseref,
headref,
branchname,
commitShasToCherryPick,
);
console.error(message);
successByTarget.set(target, false);
Expand Down Expand Up @@ -380,42 +374,54 @@ export class Backport {
Please ensure that this Github repo has a branch named \`${target}\`.`;
}

private composeMessageForBackportScriptFailure(
private composeMessageForCheckoutFailure(
target: string,
exitcode: number,
baseref: string,
headref: string,
branchname: string,
commitShasToCherryPick: string[],
): string {
const reasons: { [key: number]: string } = {
1: "due to an unknown script error",
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/korthout/backport-action/issues/46.`;

const reason = "because it was unable to create a new branch";
const suggestion = this.composeSuggestion(
target,
branchname,
commitShasToCherryPick,
);
return dedent`Backport failed for \`${target}\`, ${reason}.
Please cherry-pick the changes locally.
${suggestion}`;
}

private composeMessageForCherryPickFailure(
target: string,
branchname: string,
commitShasToCherryPick: string[],
): string {
const reason = "because it was unable to cherry-pick the commit(s)";
const suggestion = this.composeSuggestion(
target,
branchname,
commitShasToCherryPick,
);
return dedent`Backport failed for \`${target}\`, ${reason}.
Please cherry-pick the changes locally and resolve any conflicts.
${suggestion}`;
}

private composeSuggestion(
target: string,
branchname: string,
commitShasToCherryPick: string[],
) {
return dedent`\`\`\`bash
git fetch origin ${target}
git worktree add -d .worktree/${branchname} origin/${target}
cd .worktree/${branchname}
git switch --create ${branchname}
git cherry-pick -x ${commitShasToCherryPick.join(" ")}
\`\`\``;
}

private composeMessageForGitPushFailure(
target: string,
exitcode: number,
Expand Down

0 comments on commit 487faf2

Please sign in to comment.