From 91d6337ca8b0e7411ad0949c11949cd41dd37fa1 Mon Sep 17 00:00:00 2001 From: Nico Korthout Date: Mon, 26 Dec 2022 12:25:20 +0100 Subject: [PATCH] fix(backport): comment error when target not found 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 f16f7049afce60848b3d6ed2eba8f53a163cc310) --- src/backport.ts | 23 ++++++++++++++++++++++- src/git.ts | 17 ++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/backport.ts b/src/backport.ts index 977a552..ffbfe8d 100644 --- a/src/backport.ts +++ b/src/backport.ts @@ -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}`; @@ -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, diff --git a/src/git.ts b/src/git.ts index 326cfc2..4f15ee0 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,11 +1,21 @@ 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( @@ -13,7 +23,12 @@ export async function fetch(ref: string, pwd: string, depth: number) { [`--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}` );