Skip to content

Commit c7c49a1

Browse files
committed
Add get-git-ref-info action, update READMEs
1 parent 03d17ee commit c7c49a1

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PR Comment Action
2+
3+
This action is a shorthand way to determine whether a given git ref is a branch, tag or commit.
4+
5+
If the specified ref doesn't exist, this action will exit.
6+
7+
## Inputs
8+
9+
| Name | Description | Required | Default | Example |
10+
| ---- | ----------- | -------- | ------- | ------- |
11+
| `repository` | The repository to get the git ref info from, in the format `owner/repo` | `true` | N/A | `"ACCESS-NRI/spack-packages"` |
12+
| `ref` | The simple git reference (branch, tag, or commit SHA) to retrieve information about | `true` | N/A | `some-feature-branch` (branch), `v2.1` (tag), `8d29u23` (short SHA), `03d17eebfda5b873c576bad1dd77acdb8` (full SHA) |
13+
| `token` | A GitHub Token/PAT that allows cloning of the repository | `false` | `github.token` | `gha_pat_abcds...` |
14+
15+
## Outputs
16+
17+
| Name | Description | Example |
18+
| ---- | ----------- | ------- |
19+
| `ref-type` | Type of the given ref, if it's valid | One of `branch`, `tag`, `commit` |
20+
| `sha` | Full SHA of the given ref, if it's valid | `03d17eebfda5b873c576bad1dd77acdb8` |
21+
22+
## Examples
23+
24+
```yaml
25+
# ...
26+
jobs:
27+
get-ref:
28+
runs-on: ubuntu-latest
29+
env:
30+
ref: main
31+
steps:
32+
- id: resolve-ref
33+
uses: access-nri/actions/.github/actions/pr-comment@main
34+
with:
35+
repository: ACCESS-NRI/actions@main
36+
ref: ${{ env.ref }}
37+
38+
- run: echo "Ref ${{ env.ref }} is a ${{ steps.resolve-ref.outputs.ref-type }} with SHA ${{ steps.resolve-ref.outputs.sha }}"
39+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Get git ref info
2+
description: Action that retrieves information about a git reference
3+
inputs:
4+
repository:
5+
required: true
6+
description: The repository to get the git ref info from, in the format 'owner/repo'.
7+
ref:
8+
required: true
9+
description: The git reference (branch, tag, or commit SHA) to retrieve information about
10+
token:
11+
required: false
12+
description: |
13+
The GitHub token to use for authentication when accessing the repository.
14+
Defaults to github.token.
15+
outputs:
16+
ref-type:
17+
description: The type of the git reference (branch, tag, or commit)
18+
value: ${{ steps.ref-info.outputs.ref-type }}
19+
sha:
20+
description: The commit SHA that the git reference points to
21+
value: ${{ steps.ref-info.outputs.sha }}
22+
runs:
23+
using: composite
24+
steps:
25+
- name: Checkout ${{ inputs.repository }}
26+
uses: actions/checkout@v5
27+
with:
28+
repository: ${{ inputs.repository }}
29+
fetch-depth: 0
30+
fetch_tags: true
31+
token: ${{ inputs.token || github.token }}
32+
33+
- name: Get Git Ref Info
34+
id: ref-info
35+
shell: bash
36+
# Determine if the ref is a branch, tag, or commit SHA, and get the SHA for the given ref
37+
run: |
38+
if git show-ref --verify --quiet "refs/heads/${{ inputs.ref }}"; then
39+
echo "ref-type=branch" >> $GITHUB_OUTPUT
40+
echo "sha=$(git rev-parse "refs/heads/${{ inputs.ref }}")" >> $GITHUB_OUTPUT
41+
42+
elif git show-ref --verify --quiet "refs/tags/${{ inputs.ref }}"; then
43+
echo "ref-type=tag" >> $GITHUB_OUTPUT
44+
echo "sha=$(git rev-parse "refs/tags/${{ inputs.ref }}")" >> $GITHUB_OUTPUT
45+
46+
elif git cat-file -e "${{ inputs.ref }}^{commit}"; then
47+
echo "ref-type=commit" >> $GITHUB_OUTPUT
48+
echo "sha=$(git rev-parse "${{ inputs.ref }}")" >> $GITHUB_OUTPUT
49+
50+
else
51+
echo "::error::The specified ref '${{ inputs.ref }}' does not exist in the repository '${{ inputs.repository }}'."
52+
exit 1
53+
fi

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Custom GitHub Actions and Workflows for use across the ACCESS-NRI.
1212
* [commenter-permissions README.md](.github/actions/commenter-permission-check/README.md): Determine whether a commenter on a PR has write permissions on the repository.
1313
* [comment](.github/actions/comment/README.md): Add, modify or delete a comment on a given issue or PR.
1414
* [cache-svn-auth](.github/actions/cache-svn-auth/README.md): Cache Subversion (SVN)'s authentication password.
15+
* [get-git-ref-info](.github/actions/get-git-ref-info/README.md): Determine if a given git ref is a branch, tag or SHA, if it exists.
1516

1617
## Workflows
1718

0 commit comments

Comments
 (0)