Skip to content

Commit

Permalink
refactor: remove unused error reasons
Browse files Browse the repository at this point in the history
This compose for backport script failure method was a remnant from when
this action used a shell script to perform the git commands. Most
reasons were dead code.
  • Loading branch information
korthout committed Nov 7, 2023
1 parent e45fafa commit bf66681
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,8 @@ export class Backport {
this.config.pwd,
);
} catch (error) {
const message = this.composeMessageForBackportScriptFailure(
const message = this.composeMessageForCheckoutFailure(
target,
3,
baseref,
headref,
branchname,
Expand All @@ -197,9 +196,8 @@ 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,
Expand Down Expand Up @@ -380,42 +378,60 @@ 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,
): 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,
baseref,
headref,
);
return dedent`Backport failed for \`${target}\`, ${reason}.
Please cherry-pick the changes locally.
${suggestion}`;
}

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

private composeSuggestion(
target: string,
branchname: string,
baseref: string,
headref: 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}
\`\`\``;
}

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

0 comments on commit bf66681

Please sign in to comment.