Skip to content

Commit

Permalink
add our feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkranga committed Feb 6, 2024
1 parent b4ffde6 commit e72243f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions __test__/git-auth-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,8 @@ async function setup(testName: string): Promise<void> {
nestedSubmodules: false,
persistCredentials: true,
ref: 'refs/heads/main',
defaultRefOnError: true,
defaultBranch: 'main',
repositoryName: 'my-repo',
repositoryOwner: 'my-org',
repositoryPath: '',
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 30 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -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).
Expand Down Expand Up @@ -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}`);
Expand Down
50 changes: 44 additions & 6 deletions src/git-source-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
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')
Expand Down Expand Up @@ -166,7 +181,10 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
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,
Expand All @@ -190,11 +208,31 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {

// 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
Expand Down
10 changes: 10 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
5 changes: 5 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export async function getInputs(): Promise<IGitSourceSettings> {
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}`)
Expand Down

0 comments on commit e72243f

Please sign in to comment.