Compare commits

...

6 Commits

Author SHA1 Message Date
Alexander Cranga
41c08184ec
Merge 6b8be4cb30 into b684943689 2024-09-05 20:16:49 +03:00
Luca Comellini
b684943689
Add Ref and Commit outputs (#1180)
Signed-off-by: Luca Comellini <luca.com@gmail.com>
2024-09-05 11:57:13 -04: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
10 changed files with 117 additions and 10 deletions

View File

@ -295,3 +295,37 @@ jobs:
uses: actions/checkout@v4.1.6
with:
path: localClone
test-output:
runs-on: ubuntu-latest
steps:
# Clone this repo
- name: Checkout
uses: actions/checkout@v4.1.6
# Basic checkout using git
- name: Checkout basic
id: checkout
uses: ./
with:
ref: test-data/v2/basic
# Verify output
- name: Verify output
run: |
echo "Commit: ${{ steps.checkout.outputs.commit }}"
echo "Ref: ${{ steps.checkout.outputs.ref }}"
if [ "${{ steps.checkout.outputs.ref }}" != "test-data/v2/basic" ]; then
echo "Expected ref to be test-data/v2/basic"
exit 1
fi
if [ "${{ steps.checkout.outputs.commit }}" != "82f71901cf8c021332310dcc8cdba84c4193ff5d" ]; then
echo "Expected commit to be 82f71901cf8c021332310dcc8cdba84c4193ff5d"
exit 1
fi
# needed to make checkout post cleanup succeed
- name: Fix Checkout
uses: actions/checkout@v4.1.6

View File

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

View File

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

View File

@ -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)

View File

@ -9,6 +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-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
@ -98,6 +101,11 @@ inputs:
github-server-url:
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
required: false
outputs:
ref:
description: 'The branch, tag or SHA that was checked out'
commit:
description: 'The commit SHA that was checked out'
runs:
using: node20
main: dist/index.js

28
dist/index.js vendored
View File

@ -1280,7 +1280,7 @@ function getSource(settings) {
else if (settings.sparseCheckout) {
fetchOptions.filter = 'blob:none';
}
if (settings.fetchDepth <= 0) {
if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory(settings.ref, settings.commit);
yield git.fetch(refSpec, fetchOptions);
@ -1300,7 +1300,22 @@ 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;
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).
@ -1355,7 +1370,8 @@ function getSource(settings) {
// Get commit information
const commitInfo = yield git.log1();
// Log commit sha
yield git.log1("--format='%H'");
const commitSHA = yield git.log1('--format=%H');
core.setOutput('commit', commitSHA.trim());
// Check for incorrect pull request merge commit
yield refHelper.checkCommitInfo(settings.authToken, commitInfo, settings.repositoryOwner, settings.repositoryName, settings.ref, settings.commit, settings.githubServerUrl);
}
@ -1767,6 +1783,11 @@ function getInputs() {
}
core.debug(`ref = '${result.ref}'`);
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
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE';
core.debug(`clean = ${result.clean}`);
@ -1897,6 +1918,7 @@ function run() {
coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json'));
// Get sources
yield gitSourceProvider.getSource(sourceSettings);
core.setOutput('ref', sourceSettings.ref);
}
finally {
// Unregister problem matcher

View File

@ -169,7 +169,7 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
fetchOptions.filter = 'blob:none'
}
if (settings.fetchDepth <= 0) {
if (settings.fetchDepth <= 0 || settings.defaultBranchCheckout) {
// Fetch all branches and tags
let refSpec = refHelper.getRefSpecForAllHistory(
settings.ref,
@ -193,11 +193,34 @@ 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
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()
// LFS fetch
@ -261,7 +284,8 @@ export async function getSource(settings: IGitSourceSettings): Promise<void> {
const commitInfo = await git.log1()
// Log commit sha
await git.log1("--format='%H'")
const commitSHA = await git.log1('--format=%H')
core.setOutput('commit', commitSHA.trim())
// Check for incorrect pull request merge commit
await refHelper.checkCommitInfo(

View File

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

View File

@ -78,6 +78,12 @@ export async function getInputs(): Promise<IGitSourceSettings> {
core.debug(`ref = '${result.ref}'`)
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
result.clean = (core.getInput('clean') || 'true').toUpperCase() === 'TRUE'
core.debug(`clean = ${result.clean}`)

View File

@ -19,6 +19,7 @@ async function run(): Promise<void> {
// Get sources
await gitSourceProvider.getSource(sourceSettings)
core.setOutput('ref', sourceSettings.ref)
} finally {
// Unregister problem matcher
coreCommand.issueCommand('remove-matcher', {owner: 'checkout-git'}, '')