Skip to content

Commit

Permalink
fix(backport): comment error when target not found
Browse files Browse the repository at this point in the history
When the target cannot be found on remote, then the git fetch fails
with: (example for `maintenance/8.1`)
```
fatal: couldn't find remote ref maintenance/8.1
'git fetch origin maintenance/8.1' failed with exit code 128
```

We can improve this by throwing a special error and handling it
explicitly by commenting on the original pull request about this
situation. We can also help the user figure out how to resolve the
problem by making it clear that a branch with that name should exist.

(cherry picked from commit f16f704)
  • Loading branch information
korthout committed Dec 26, 2022
1 parent b42dc40 commit 91d6337
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,23 @@ export class Backport {
const target = match[1];
console.log(`Found target in label: ${target}`);

await git.fetch(target, this.config.pwd, 1);
try {
await git.fetch(target, this.config.pwd, 1);
} catch (error) {
if (error instanceof git.GitRefNotFoundError) {
const message = this.composeMessageForFetchTargetFailure(error.ref);
console.error(message);
await this.github.createComment({
owner,
repo,
issue_number: pull_number,
body: message,
});
continue;
} else {
throw error;
}
}

try {
const branchname = `backport-${pull_number}-to-${target}`;
Expand Down Expand Up @@ -243,6 +259,11 @@ export class Backport {
return { title, body };
}

private composeMessageForFetchTargetFailure(target: string) {
return dedent`Backport failed for \`${target}\`: couldn't find remote ref \`${target}\`.
Please ensure that this Github repo has a branch named \`${target}\`.`;
}

private composeMessageForBackportScriptFailure(
target: string,
exitcode: number,
Expand Down
17 changes: 16 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import execa from "execa";

export class GitRefNotFoundError extends Error {
ref: string;
constructor(message: string, ref: string) {
super(message);
this.ref = ref;
}
}

/**
* Fetches a ref from origin
*
* @param ref the sha, branchname, etc to fetch
* @param pwd the root of the git repository
* @param depth the number of commits to fetch
* @throws GitRefNotFoundError when ref not found
* @throws Error for any other non-zero exit code
*/
export async function fetch(ref: string, pwd: string, depth: number) {
const { exitCode } = await git(
"fetch",
[`--depth=${depth}`, "origin", ref],
pwd
);
if (exitCode !== 0) {
if (exitCode === 128) {
throw new GitRefNotFoundError(
`Expected to fetch '${ref}', but couldn't find it`,
ref
);
} else if (exitCode !== 0) {
throw new Error(
`'git fetch origin ${ref}' failed with exit code ${exitCode}`
);
Expand Down

0 comments on commit 91d6337

Please sign in to comment.