Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip merge commits in suggestion #397

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading