Compare commits

...

7 Commits

Author SHA1 Message Date
Alexander Cranga
71fb903058
Merge 6b8be4cb30 into 11bd71901b 2024-10-23 17:45:24 +03:00
John Wesley Walker III
11bd71901b
Prepare 4.2.2 Release (#1953)
* Prepare 4.2.2 Release

---------

Co-authored-by: Josh Gross <joshmgross@github.com>
2024-10-23 16:24:28 +02:00
John Wesley Walker III
e3d2460bbb
Expand unit test coverage (#1946) 2024-10-23 15:59:08 +02:00
alexanderkranga
6b8be4cb30 Add a test 2024-02-19 19:28:53 +02:00
alexanderkranga
47b9382799 update action.yml and readme 2024-02-19 18:24:39 +02:00
alexanderkranga
5bbdf118df Default branch checkout option 2024-02-19 18:20:02 +02:00
alexanderkranga
e72243fb91 add our feature 2024-02-06 15:30:36 +02:00
12 changed files with 116 additions and 11 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## v4.2.2
* `url-helper.ts` now leverages well-known environment variables by @jww3 in https://github.com/actions/checkout/pull/1941
* Expand unit test coverage for `isGhes` by @jww3 in https://github.com/actions/checkout/pull/1946
## v4.2.1 ## v4.2.1
* Check out other refs/* by commit if provided, fall back to ref by @orhantoy in https://github.com/actions/checkout/pull/1924 * Check out other refs/* by commit if provided, fall back to ref by @orhantoy in https://github.com/actions/checkout/pull/1924

View File

@ -29,6 +29,11 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# Otherwise, uses the default branch. # Otherwise, uses the default branch.
ref: '' ref: ''
# 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 # 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 # with the local git config, which enables your scripts to run authenticated git
# commands. The post-job step removes the PAT. # commands. The post-job step removes the PAT.

View File

@ -815,6 +815,7 @@ async function setup(testName: string): Promise<void> {
nestedSubmodules: false, nestedSubmodules: false,
persistCredentials: true, persistCredentials: true,
ref: 'refs/heads/main', ref: 'refs/heads/main',
defaultBranchCheckout: false,
repositoryName: 'my-repo', repositoryName: 'my-repo',
repositoryOwner: 'my-org', repositoryOwner: 'my-org',
repositoryPath: '', repositoryPath: '',

View File

@ -80,6 +80,7 @@ describe('input-helper tests', () => {
expect(settings.commit).toBeTruthy() expect(settings.commit).toBeTruthy()
expect(settings.commit).toBe('1234567890123456789012345678901234567890') expect(settings.commit).toBe('1234567890123456789012345678901234567890')
expect(settings.filter).toBe(undefined) expect(settings.filter).toBe(undefined)
expect(settings.defaultBranchCheckout).toBe(false)
expect(settings.sparseCheckout).toBe(undefined) expect(settings.sparseCheckout).toBe(undefined)
expect(settings.sparseCheckoutConeMode).toBe(true) expect(settings.sparseCheckoutConeMode).toBe(true)
expect(settings.fetchDepth).toBe(1) expect(settings.fetchDepth).toBe(1)

View File

@ -24,13 +24,50 @@ describe('getServerUrl tests', () => {
}) })
describe('isGhes tests', () => { describe('isGhes tests', () => {
const pristineEnv = process.env
beforeEach(() => {
jest.resetModules()
process.env = {...pristineEnv}
})
afterAll(() => {
process.env = pristineEnv
})
it('basics', async () => { it('basics', async () => {
delete process.env['GITHUB_SERVER_URL']
expect(urlHelper.isGhes()).toBeFalsy() expect(urlHelper.isGhes()).toBeFalsy()
expect(urlHelper.isGhes('https://github.com')).toBeFalsy() expect(urlHelper.isGhes('https://github.com')).toBeFalsy()
expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy() expect(urlHelper.isGhes('https://contoso.ghe.com')).toBeFalsy()
expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy() expect(urlHelper.isGhes('https://test.github.localhost')).toBeFalsy()
expect(urlHelper.isGhes('https://src.onpremise.fabrikam.com')).toBeTruthy() expect(urlHelper.isGhes('https://src.onpremise.fabrikam.com')).toBeTruthy()
}) })
it('returns false when the GITHUB_SERVER_URL environment variable is not defined', async () => {
delete process.env['GITHUB_SERVER_URL']
expect(urlHelper.isGhes()).toBeFalsy()
})
it('returns false when the GITHUB_SERVER_URL environment variable is set to github.com', async () => {
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
expect(urlHelper.isGhes()).toBeFalsy()
})
it('returns false when the GITHUB_SERVER_URL environment variable is set to a GitHub Enterprise Cloud-style URL', async () => {
process.env['GITHUB_SERVER_URL'] = 'https://contoso.ghe.com'
expect(urlHelper.isGhes()).toBeFalsy()
})
it('returns false when the GITHUB_SERVER_URL environment variable has a .localhost suffix', async () => {
process.env['GITHUB_SERVER_URL'] = 'https://mock-github.localhost'
expect(urlHelper.isGhes()).toBeFalsy()
})
it('returns true when the GITHUB_SERVER_URL environment variable is set to some other URL', async () => {
process.env['GITHUB_SERVER_URL'] = 'https://src.onpremise.fabrikam.com'
expect(urlHelper.isGhes()).toBeTruthy()
})
}) })
describe('getServerApiUrl tests', () => { describe('getServerApiUrl tests', () => {

View File

@ -9,6 +9,9 @@ inputs:
The branch, tag or SHA to checkout. When checking out the repository that 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 triggered a workflow, this defaults to the reference or SHA for that
event. Otherwise, uses the default branch. event. Otherwise, uses the default branch.
default-branch-checkout:
description: 'Indicates whether to checkout the default repository branch if the requested ref does not exist'
default: false
token: token:
description: > description: >
Personal access token (PAT) used to fetch the repository. The PAT is configured Personal access token (PAT) used to fetch the repository. The PAT is configured

24
dist/index.js vendored
View File

@ -1278,7 +1278,7 @@ function getSource(settings) {
else if (settings.sparseCheckout) { else if (settings.sparseCheckout) {
fetchOptions.filter = 'blob:none'; fetchOptions.filter = 'blob:none';
} }
if (settings.fetchDepth <= 0) { if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags // Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit); let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit);
yield git.fetch(refSpec, fetchOptions); yield git.fetch(refSpec, fetchOptions);
@ -1298,7 +1298,22 @@ function getSource(settings) {
core.endGroup(); core.endGroup();
// Checkout info // Checkout info
core.startGroup('Determining the checkout info'); core.startGroup('Determining the checkout info');
const checkoutInfo = yield refHelper.getCheckoutInfo(git, settings.ref, settings.commit); let checkoutInfo;
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(); core.endGroup();
// LFS fetch // LFS fetch
// Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time). // Explicit lfs-fetch to avoid slow checkout (fetches one lfs object at a time).
@ -1763,6 +1778,11 @@ function getInputs() {
} }
core.debug(`ref = '${result.ref}'`); core.debug(`ref = '${result.ref}'`);
core.debug(`commit = '${result.commit}'`); core.debug(`commit = '${result.commit}'`);
// Default branch checkout
result.defaultBranchCheckout =
(core.getInput('default-branch-checkout') || 'false').toUpperCase() ===
'TRUE';
core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`);
// Clean // Clean
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'; result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE';
core.debug(`clean = ${result.clean}`); core.debug(`clean = ${result.clean}`);

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "checkout", "name": "checkout",
"version": "4.2.1", "version": "4.2.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "checkout", "name": "checkout",
"version": "4.2.1", "version": "4.2.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "checkout", "name": "checkout",
"version": "4.2.1", "version": "4.2.2",
"description": "checkout action", "description": "checkout action",
"main": "lib/main.js", "main": "lib/main.js",
"scripts": { "scripts": {

View File

@ -169,7 +169,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
fetchOptions.filter = 'blob:none' fetchOptions.filter = 'blob:none'
} }
if (settings.fetchDepth <= 0) { if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags // Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory( let refSpec = refHelper.getRefSpecForAllHistory(
settings.ref, settings.ref,
@ -193,11 +193,34 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
// Checkout info // Checkout info
core.startGroup('Determining the checkout info') core.startGroup('Determining the checkout info')
const checkoutInfo = await refHelper.getCheckoutInfo( let checkoutInfo: refHelper.ICheckoutInfo
try {
checkoutInfo = await refHelper.getCheckoutInfo(
git, git,
settings.ref, settings.ref,
settings.commit 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() core.endGroup()
// LFS fetch // LFS fetch

View File

@ -19,6 +19,11 @@ export interface IGitSourceSettings {
*/ */
ref: string ref: string
/**
* Indicates whether to checkout the default repository branch if the requested ref does not exist
*/
defaultBranchCheckout: boolean
/** /**
* The commit to checkout * The commit to checkout
*/ */

View File

@ -78,6 +78,12 @@ export async function getInputs(): Promise<IGitSourceSettings> {
core.debug(`ref = '${result.ref}'`) core.debug(`ref = '${result.ref}'`)
core.debug(`commit = '${result.commit}'`) core.debug(`commit = '${result.commit}'`)
// Default branch checkout
result.defaultBranchCheckout =
(core.getInput('default-branch-checkout') || 'false').toUpperCase() ===
'TRUE'
core.debug(`default-branch-checkout = '${result.defaultBranchCheckout}'`)
// Clean // Clean
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE' result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
core.debug(`clean = ${result.clean}`) core.debug(`clean = ${result.clean}`)