From e72243fb91f9fec2a16dd4e0b4175dc1e2337c6f Mon Sep 17 00:00:00 2001 From: alexanderkranga Date: Tue, 6 Feb 2024 15:30:36 +0200 Subject: [PATCH 1/4] add our feature --- README.md | 5 ++++ __test__/git-auth-helper.test.ts | 2 ++ action.yml | 5 ++++ dist/index.js | 32 ++++++++++++++++++-- src/git-source-provider.ts | 50 ++++++++++++++++++++++++++++---- src/git-source-settings.ts | 10 +++++++ src/input-helper.ts | 5 ++++ 7 files changed, 101 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bfecf464d..a47dab9f8 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # Otherwise, uses the default branch. ref: '' + # Whether to checkout the default repository branch if specified ref does not + # exist. If this is set to true, then fetch-depth should be 0 + # Default: true + default-ref-on-error: '' + # Personal access token (PAT) used to fetch the repository. The PAT is configured # with the local git config, which enables your scripts to run authenticated git # commands. The post-job step removes the PAT. diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index 411faed05..e8ea8865c 100644 --- a/__test__/git-auth-helper.test.ts +++ b/__test__/git-auth-helper.test.ts @@ -813,6 +813,8 @@ async function setup(testName: string): Promise { nestedSubmodules: false, persistCredentials: true, ref: 'refs/heads/main', + defaultRefOnError: true, + defaultBranch: 'main', repositoryName: 'my-repo', repositoryOwner: 'my-org', repositoryPath: '', diff --git a/action.yml b/action.yml index 5aa90a738..ad48198f2 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,11 @@ inputs: The branch, tag or SHA to checkout. When checking out the repository that triggered a workflow, this defaults to the reference or SHA for that event. Otherwise, uses the default branch. + default-ref-on-error: + description: > + Whether to checkout the default repository branch if specified ref does not exist. + If this is set to true, then fetch-depth should be 0 + default: true token: description: > Personal access token (PAT) used to fetch the repository. The PAT is configured diff --git a/dist/index.js b/dist/index.js index ddf2b3d89..5b474f562 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1226,6 +1226,17 @@ 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'); @@ -1250,7 +1261,8 @@ function getSource(settings) { else if (settings.sparseCheckout) { fetchOptions.filter = 'blob:none'; } - if (settings.fetchDepth <= 0) { + if (settings.fetchDepth <= 0 || + (settings.defaultRefOnError && settings.defaultRefOnError === true)) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit); yield git.fetch(refSpec, fetchOptions); @@ -1270,7 +1282,19 @@ function getSource(settings) { core.endGroup(); // Checkout info core.startGroup('Determining the checkout info'); - const checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); + 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 { + checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); + } core.endGroup(); // LFS fetch // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time). @@ -1724,6 +1748,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}'`); // 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 5c98e9f73..a17480245 100644 --- a/src/git-source-provider.ts +++ b/src/git-source-provider.ts @@ -130,6 +130,21 @@ 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') @@ -166,7 +181,10 @@ export async function getSource(settings: IGitSourceSettings): Promise { fetchOptions.filter = 'blob:none' } - if (settings.fetchDepth <= 0) { + if ( + settings.fetchDepth <= 0 || + (settings.defaultRefOnError && settings.defaultRefOnError === true) + ) { // Fetch all branches and tags let refSpec = refHelper.getRefSpecForAllHistory( settings.ref, @@ -190,11 +208,31 @@ export async function getSource(settings: IGitSourceSettings): Promise { // Checkout info core.startGroup('Determining the checkout info') - const checkoutInfo = await refHelper.getCheckoutInfo( - git, - settings.ref, - settings.commit - ) + 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 { + checkoutInfo = await refHelper.getCheckoutInfo( + git, + settings.ref, + settings.commit + ) + } core.endGroup() // LFS fetch diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index 629350b2c..f94209b78 100644 --- a/src/git-source-settings.ts +++ b/src/git-source-settings.ts @@ -19,6 +19,16 @@ export interface IGitSourceSettings { */ ref: string + /** + * Whether to checkout the default repository branch if specified ref does not exist. + */ + defaultRefOnError: boolean + + /** + * The target ref to fetch if it exists + */ + defaultBranch: string + /** * The commit to checkout */ diff --git a/src/input-helper.ts b/src/input-helper.ts index e546c196d..cc854b077 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -78,6 +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}'`) + // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE' core.debug(`clean = ${result.clean}`) From 5bbdf118df41e4c3bea3374ab810ad850bc23704 Mon Sep 17 00:00:00 2001 From: alexanderkranga Date: Mon, 19 Feb 2024 18:20:02 +0200 Subject: [PATCH 2/4] Default branch checkout option --- __test__/git-auth-helper.test.ts | 3 +- dist/index.js | 43 ++++++++++--------------- src/git-source-provider.ts | 55 ++++++++++++-------------------- src/git-source-settings.ts | 9 ++---- src/input-helper.ts | 9 +++--- 5 files changed, 45 insertions(+), 74 deletions(-) diff --git a/__test__/git-auth-helper.test.ts b/__test__/git-auth-helper.test.ts index e8ea8865c..8fe7b312c 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 5b474f562..774dc6ff4 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); + 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); } - 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 { + throw error; } } - else { - checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); - } 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 a17480245..b23975097 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) { + 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 repo branch' + '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, - settings.defaultBranch, + repositoryDefaultBranch, settings.commit ) + } else { + throw error } - } else { - checkoutInfo = await refHelper.getCheckoutInfo( - git, - settings.ref, - settings.commit - ) } core.endGroup() diff --git a/src/git-source-settings.ts b/src/git-source-settings.ts index f94209b78..413e772f9 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 cc854b077..b89a52329 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' From 47b938279919d0395ab4a2e3cb825383104b5471 Mon Sep 17 00:00:00 2001 From: alexanderkranga Date: Mon, 19 Feb 2024 18:24:39 +0200 Subject: [PATCH 3/4] update action.yml and readme --- README.md | 8 ++++---- action.yml | 8 +++----- dist/index.js | 3 ++- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a47dab9f8..8beacf51d 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,10 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/ # Otherwise, uses the default branch. ref: '' - # Whether to checkout the default repository branch if specified ref does not - # exist. If this is set to true, then fetch-depth should be 0 - # Default: true - default-ref-on-error: '' + # Indicates whether to checkout the default repository branch if the requested ref + # does not exist + # Default: false + default-branch-checkout: '' # Personal access token (PAT) used to fetch the repository. The PAT is configured # with the local git config, which enables your scripts to run authenticated git diff --git a/action.yml b/action.yml index ad48198f2..93f2bc62a 100644 --- a/action.yml +++ b/action.yml @@ -9,11 +9,9 @@ inputs: The branch, tag or SHA to checkout. When checking out the repository that triggered a workflow, this defaults to the reference or SHA for that event. Otherwise, uses the default branch. - default-ref-on-error: - description: > - Whether to checkout the default repository branch if specified ref does not exist. - If this is set to true, then fetch-depth should be 0 - default: true + default-branch-checkout: + description: 'Indicates whether to checkout the default repository branch if the requested ref does not exist' + default: false token: description: > Personal access token (PAT) used to fetch the repository. The PAT is configured diff --git a/dist/index.js b/dist/index.js index 774dc6ff4..77146ceb8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1741,7 +1741,8 @@ function getInputs() { core.debug(`commit = '${result.commit}'`); // Default branch checkout result.defaultBranchCheckout = - (core.getInput('default-branch-checkout') || 'true').toUpperCase() === 'TRUE'; + (core.getInput('default-branch-checkout') || 'true').toUpperCase() === + 'TRUE'; core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`); // Clean result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; From 6b8be4cb30971d494e7d481a90a251d3b74355d7 Mon Sep 17 00:00:00 2001 From: alexanderkranga Date: Mon, 19 Feb 2024 19:28:53 +0200 Subject: [PATCH 4/4] Add a test --- __test__/input-helper.test.ts | 1 + dist/index.js | 2 +- src/input-helper.ts | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/__test__/input-helper.test.ts b/__test__/input-helper.test.ts index 9514cb42d..fcb242a31 100644 --- a/__test__/input-helper.test.ts +++ b/__test__/input-helper.test.ts @@ -80,6 +80,7 @@ describe('input-helper tests', () => { expect(settings.commit).toBeTruthy() expect(settings.commit).toBe('1234567890123456789012345678901234567890') expect(settings.filter).toBe(undefined) + expect(settings.defaultBranchCheckout).toBe(false) expect(settings.sparseCheckout).toBe(undefined) expect(settings.sparseCheckoutConeMode).toBe(true) expect(settings.fetchDepth).toBe(1) diff --git a/dist/index.js b/dist/index.js index 77146ceb8..6712e8f78 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1741,7 +1741,7 @@ function getInputs() { core.debug(`commit = '${result.commit}'`); // Default branch checkout result.defaultBranchCheckout = - (core.getInput('default-branch-checkout') || 'true').toUpperCase() === + (core.getInput('default-branch-checkout') || 'false').toUpperCase() === 'TRUE'; core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`); // Clean diff --git a/src/input-helper.ts b/src/input-helper.ts index b89a52329..11ea29420 100644 --- a/src/input-helper.ts +++ b/src/input-helper.ts @@ -80,7 +80,7 @@ export async function getInputs(): Promise { // Default branch checkout result.defaultBranchCheckout = - (core.getInput('default-branch-checkout') || 'true').toUpperCase() === + (core.getInput('default-branch-checkout') || 'false').toUpperCase() === 'TRUE' core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`)