Skip to content

Commit 87dc9f7

Browse files
committed
fix: enhance tag resolution to handle both lightweight and annotated tags
1 parent 3016e44 commit 87dc9f7

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

gh-cli/get-actions-usage-in-organization.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,16 @@ resolve_sha_to_tag() {
9292

9393
# Check if we have tags cached for this action
9494
if [ ! -f "$action_cache_file" ]; then
95-
# Fetch and cache all tags for this action
96-
gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | \
97-
jq -r '.[] | "\(.object.sha)|\(.ref | sub("refs/tags/"; ""))"' 2>/dev/null > "$action_cache_file" || \
98-
touch "$action_cache_file"
95+
# Fetch and cache all tags for this action (handles both lightweight and annotated tags)
96+
{
97+
# Get lightweight tags
98+
gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | \
99+
jq -r '.[] | "\(.object.sha)|\(.ref | sub("refs/tags/"; ""))"' 2>/dev/null
100+
101+
# Get annotated tags (dereference to commit SHA)
102+
gh api repos/"$action_name"/tags --paginate 2>/dev/null | \
103+
jq -r '.[] | "\(.commit.sha)|\(.name)"' 2>/dev/null
104+
} | sort -u > "$action_cache_file" || touch "$action_cache_file"
99105
fi
100106

101107
# Look up the SHA in the cached tags

gh-cli/get-actions-usage-in-repository.sh

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,34 @@ resolve_sha_to_tag() {
3636

3737
# Only process if it looks like a SHA (40 character hex string)
3838
if [[ ${#sha} -eq 40 && "$sha" =~ ^[a-f0-9]+$ ]]; then
39-
# Try to find a tag that points to this commit SHA
39+
# Try to find a tag that points to this commit SHA (handles both lightweight and annotated tags)
4040
local tag_name
4141
# First try to find a semantic version tag (prefer v1.2.3 over v1)
42-
tag_name=$(gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | jq -r --arg sha "$sha" '.[] | select(.object.sha == $sha) | .ref | sub("refs/tags/"; "")' 2>/dev/null | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | head -1)
42+
tag_name=$(
43+
{
44+
# Get lightweight tags
45+
gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | \
46+
jq -r --arg sha "$sha" '.[] | select(.object.sha == $sha) | .ref | sub("refs/tags/"; "")' 2>/dev/null
47+
48+
# Get annotated tags (dereference to commit SHA)
49+
gh api repos/"$action_name"/tags --paginate 2>/dev/null | \
50+
jq -r --arg sha "$sha" '.[] | select(.commit.sha == $sha) | .name' 2>/dev/null
51+
} | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | head -1
52+
)
4353

4454
# If no semantic version found, fall back to any tag
4555
if [ -z "$tag_name" ]; then
46-
tag_name=$(gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | jq -r --arg sha "$sha" '.[] | select(.object.sha == $sha) | .ref | sub("refs/tags/"; "")' 2>/dev/null | head -1)
56+
tag_name=$(
57+
{
58+
# Get lightweight tags
59+
gh api repos/"$action_name"/git/refs/tags --paginate 2>/dev/null | \
60+
jq -r --arg sha "$sha" '.[] | select(.object.sha == $sha) | .ref | sub("refs/tags/"; "")' 2>/dev/null
61+
62+
# Get annotated tags (dereference to commit SHA)
63+
gh api repos/"$action_name"/tags --paginate 2>/dev/null | \
64+
jq -r --arg sha "$sha" '.[] | select(.commit.sha == $sha) | .name' 2>/dev/null
65+
} | head -1
66+
)
4767
fi
4868

4969
if [ -n "$tag_name" ] && [ "$tag_name" != "null" ]; then
@@ -93,4 +113,7 @@ if [ "$resolve_shas" == "true" ]; then
93113
results=$(echo -e "$temp_results" | sed '/^$/d')
94114
fi
95115

116+
# Sort results alphabetically
117+
results=$(echo -e "$results" | sort)
118+
96119
echo -e "$results"

0 commit comments

Comments
 (0)