Skip to content

Commit

Permalink
fix: suggest to cherry-pick specific commits
Browse files Browse the repository at this point in the history
The manual instructions still suggested to determine the commits to
cherry-pick by determining the common ancestor (merge-base) of the head
and base refs, first. However, this way has been abandoned for quite
some time now. This especially became a problem now that merge commits
can be skipped, meaning that the suggestion no longer produced a
reliable result.

Instead, we can simply suggest the same as the action does. It knows
exactly which commits to cherry-pick.
  • Loading branch information
korthout committed Nov 7, 2023
1 parent bf66681 commit 3e20570
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 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 @@ -178,9 +176,8 @@ export class Backport {
} catch (error) {
const message = this.composeMessageForCheckoutFailure(
target,
baseref,
headref,
branchname,
commitShasToCherryPick,
);
console.error(message);
successByTarget.set(target, false);
Expand All @@ -198,9 +195,8 @@ export class Backport {
} catch (error) {
const message = this.composeMessageForCherryPickFailure(
target,
baseref,
headref,
branchname,
commitShasToCherryPick,
);
console.error(message);
successByTarget.set(target, false);
Expand Down Expand Up @@ -380,16 +376,14 @@ export class Backport {

private composeMessageForCheckoutFailure(
target: string,
baseref: string,
headref: string,
branchname: string,
commitShasToCherryPick: string[],
): string {
const reason = "because it was unable to create a new branch";
const suggestion = this.composeSuggestion(
target,
branchname,
baseref,
headref,
commitShasToCherryPick,
);
return dedent`Backport failed for \`${target}\`, ${reason}.
Expand All @@ -399,16 +393,14 @@ export class Backport {

private composeMessageForCherryPickFailure(
target: string,
baseref: string,
headref: string,
branchname: string,
commitShasToCherryPick: string[],
): string {
const reason = "because it was unable to cherry-pick the commit(s)";
const suggestion = this.composeSuggestion(
target,
branchname,
baseref,
headref,
commitShasToCherryPick,
);
return dedent`Backport failed for \`${target}\`, ${reason}.
Expand All @@ -419,16 +411,14 @@ export class Backport {
private composeSuggestion(
target: string,
branchname: string,
baseref: string,
headref: string,
commitShasToCherryPick: string[],
) {
return 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}
git cherry-pick -x ${commitShasToCherryPick.join(" ")}
\`\`\``;
}

Expand Down

0 comments on commit 3e20570

Please sign in to comment.