From 5bbdf118df41e4c3bea3374ab810ad850bc23704 Mon Sep 17 00:00:00 2001 From: alexanderkranga Date: Mon, 19 Feb 2024 18:20:02 +0200 Subject: [PATCH] Default branch checkout option --- __test__/git-auth-helper.test.ts | 3 +- dist/index.js | 45 ++++++++++-------------- src/git-source-provider.ts | 59 ++++++++++++-------------------- src/git-source-settings.ts | 9 ++--- src/input-helper.ts | 9 ++--- 5 files changed, 48 insertions(+), 77 deletions(-) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index e8ea886..8fe7b31 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -813,8 +813,7 @@ async function setup(testName: string): Promise { nestedSubmodules: false, persistCredentials: true, ref: 'refs/heads/main', - defaultRefOnError: true, - defaultBranch: 'main', + defaultBranchCheckout: false, repositoryName: 'my-repo', repositoryOwner: 'my-org', repositoryPath: '', diff --git a/dist/index.js b/dist/index.js index 5b474f5..774dc6f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1226,17 +1226,6 @@ function getSource(settings) { core.startGroup('Setting up auth'); yield authHelper.configureAuth(); core.endGroup(); - if (settings.defaultRefOnError && settings.defaultRefOnError === true) { - // Configure default branch - core.startGroup('Setting up default branch'); - if (settings.sshKey) { - settings.defaultBranch = yield git.getDefaultBranch(repositoryUrl); - } - else { - settings.defaultBranch = yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName); - } - core.endGroup(); - } // Determine the default branch if (!settings.ref && !settings.commit) { core.startGroup('Determining the default branch'); @@ -1261,8 +1250,7 @@ function getSource(settings) { else if (settings.sparseCheckout) { fetchOptions.filter = 'blob:none'; } - if (settings.fetchDepth <= 0 || - (settings.defaultRefOnError && settings.defaultRefOnError === true)) { + if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit); yield git.fetch(refSpec, fetchOptions); @@ -1283,18 +1271,21 @@ function getSource(settings) { // Checkout info core.startGroup('Determining the checkout info'); let checkoutInfo; - if (settings.defaultRefOnError && settings.defaultRefOnError === true) { - try { - checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); - } - catch (error) { - core.info('Could not determine the checkout info. Trying the default repo branch'); - checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.defaultBranch, settings.commit); - } - } - else { + try { checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); } + catch (error) { + if (settings.defaultBranchCheckout) { + core.info('Could not determine the checkout info. Trying the default repository branch'); + const repositoryDefaultBranch = settings.sshKey + ? yield git.getDefaultBranch(repositoryUrl) + : yield githubApiHelper.getDefaultBranch(settings.authToken, settings.repositoryOwner, settings.repositoryName); + checkoutInfo = yield refHelper.getCheckoutInfo(git, repositoryDefaultBranch, settings.commit); + } + else { + throw error; + } + } core.endGroup(); // LFS fetch // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time). @@ -1748,10 +1739,10 @@ function getInputs() { } core.debug(`ref = '${result.ref}'`); core.debug(`commit = '${result.commit}'`); - // Default ref on error - result.defaultRefOnError = - (core.getInput('default-ref-on-error') || 'true').toUpperCase() === 'TRUE'; - core.debug(`default-ref-on-error = '${result.defaultRefOnError}'`); + // Default branch checkout + result.defaultBranchCheckout = + (core.getInput('default-branch-checkout') || 'true').toUpperCase() === 'TRUE'; + core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`); // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; core.debug(`clean = ${result.clean}`); diff --git a/src/git-source-provider.ts b/src/git-source-provider.ts index a174802..b239750 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -130,21 +130,6 @@ export async function getSource(settings: IGitSourceSettings): Promise { await authHelper.configureAuth() core.endGroup() - if (settings.defaultRefOnError && settings.defaultRefOnError === true) { - // Configure default branch - core.startGroup('Setting up default branch') - if (settings.sshKey) { - settings.defaultBranch = await git.getDefaultBranch(repositoryUrl) - } else { - settings.defaultBranch = await githubApiHelper.getDefaultBranch( - settings.authToken, - settings.repositoryOwner, - settings.repositoryName - ) - } - core.endGroup() - } - // Determine the default branch if (!settings.ref && !settings.commit) { core.startGroup('Determining the default branch') @@ -181,10 +166,7 @@ export async function getSource(settings: IGitSourceSettings): Promise { fetchOptions.filter = 'blob:none' } - if ( - settings.fetchDepth <= 0 || - (settings.defaultRefOnError && settings.defaultRefOnError === true) - ) { + if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory( settings.ref, @@ -209,29 +191,32 @@ export async function getSource(settings: IGitSourceSettings): Promise { // Checkout info core.startGroup('Determining the checkout info') let checkoutInfo: refHelper.ICheckoutInfo - if (settings.defaultRefOnError && settings.defaultRefOnError === true) { - try { - checkoutInfo = await refHelper.getCheckoutInfo( - git, - settings.ref, - settings.commit - ) - } catch (error) { - core.info( - 'Could not determine the checkout info. Trying the default repo branch' - ) - checkoutInfo = await refHelper.getCheckoutInfo( - git, - settings.defaultBranch, - settings.commit - ) - } - } else { + try { checkoutInfo = await refHelper.getCheckoutInfo( git, settings.ref, settings.commit ) + } catch (error) { + if (settings.defaultBranchCheckout) { + core.info( + 'Could not determine the checkout info. Trying the default repository branch' + ) + const repositoryDefaultBranch = settings.sshKey + ? await git.getDefaultBranch(repositoryUrl) + : await githubApiHelper.getDefaultBranch( + settings.authToken, + settings.repositoryOwner, + settings.repositoryName + ) + checkoutInfo = await refHelper.getCheckoutInfo( + git, + repositoryDefaultBranch, + settings.commit + ) + } else { + throw error + } } core.endGroup() diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index f94209b..413e772 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -20,14 +20,9 @@ export interface IGitSourceSettings { ref: string /** - * Whether to checkout the default repository branch if specified ref does not exist. + * Indicates whether to checkout the default repository branch if the requested ref does not exist */ - defaultRefOnError: boolean - - /** - * The target ref to fetch if it exists - */ - defaultBranch: string + defaultBranchCheckout: boolean /** * The commit to checkout diff --git a/src/input-helper.ts b/src/input-helper.ts index cc854b0..b89a523 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -78,10 +78,11 @@ export async function getInputs(): Promise { core.debug(`ref = '${result.ref}'`) core.debug(`commit = '${result.commit}'`) - // Default ref on error - result.defaultRefOnError = - (core.getInput('default-ref-on-error') || 'true').toUpperCase() === 'TRUE' - core.debug(`default-ref-on-error = '${result.defaultRefOnError}'`) + // Default branch checkout + result.defaultBranchCheckout = + (core.getInput('default-branch-checkout') || 'true').toUpperCase() === + 'TRUE' + core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`) // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'