Last active
March 24, 2025 19:38
-
-
Save zkamvar/b9c3bcf909c817bd4648feabf444389e to your computer and use it in GitHub Desktop.
Replace tagged action with a commit hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if ! type gh > /dev/null; then | |
echo "github CLI (gh) must be installed" | |
exit 1 | |
fi | |
action=${1} | |
if [ -z "$action" ]; then | |
echo "Replace a tagged action with a commit hash" | |
echo | |
echo "USAGE:" | |
echo " get-tag-commit.sh OWNER/REPO@TAG" | |
echo | |
echo "EXAMPLE:" | |
echo " get-tag-commit.sh r-lib/actions/check-r-package@v2" | |
echo " # r-lib/actions/check-r-package@14a7e741c1cb130261263aa1593718ba42cf443b #v2.11.2" | |
echo | |
exit 0 | |
fi | |
FULL=${action/@*/} | |
# extract just the repo portion (in case of several actions in a single repo) | |
REPO=${action%*/*} | |
TAG=${action/*@/} | |
# First, we grab the SHA from the git ref tags. | |
SHA=$(gh api "repos/${REPO}/git/refs/tags/${TAG}" --jq '.object.sha') | |
if [ -z "$SHA" ]; then | |
# This shouldn't happen, but if the first turns up empty, we can scan the tags | |
SHA=$(gh api "repos/${REPO}/tags" --jq ".[] | select((.name==\"$TAG\")).commit.sha") | |
fi | |
# Get the real tag (not just floating tag) by searching through the tags for the SHA | |
VTAG=$(gh api "repos/${REPO}/tags" --jq "[.[] | select((.commit.sha==\"$SHA\")).name][0]") | |
echo "${FULL}@${SHA} #${VTAG/*tree\/}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have ripgrep installed, you can use it like this from the top of your repository (I'm on a Mac, so this might work different on Linux):