Skip to content

Commit 25c9c4a

Browse files
authored
Improve robustness of GDIT Action (#56)
* Add test workflow for invoking GDIT Action * Modularize the GDIT Action * Remove extra `if` from Action step * Expand displayed arrays using `[@]` * Add missing `{}` to displayed arrays * Replace arrays with strings * Fix names of step outputs * Mock Story tags output * Remove `,` prefix from Story tags * Use GitHub API to determine PR number * Add `GITHUB_TOKEN` input variable * Try uncommenting API call to Shortcut * Try using `github.token` in action * Add debug information * Derive better SHA * Fix indentation of SHA step * Add names to steps * Remove mock Shortcut API token * Add PR number requirement to Shortcut Stories step * Temporarily add back mock Shortcut API token to test error * Remove mock Shortcut API token * Remove test workflow
1 parent 1df1729 commit 25c9c4a

File tree

1 file changed

+87
-44
lines changed

1 file changed

+87
-44
lines changed

generate-docker-image-tags/action.yml

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@ description: Generate a list of Docker image tags for the current workflow.
1515

1616
inputs:
1717
shortcut-api-token:
18-
description: Shortcut API token for fetching Story IDs related to a PR.
18+
description: >
19+
Shortcut API token for fetching Story IDs related to a PR.
1920
required: false
2021
prefix:
21-
description: |
22-
The prefix for tagging commits that are not yet merged to main/master.
23-
For example: a PR from some-branch would receive the tags <prefix>.some-branch, <prefix>.<COMMIT_SHA>.
22+
description: >
23+
Prefix for tagging commits that are not yet merged to main/master.
24+
For example, a PR from some-branch would receive tags "<prefix>.some-branch"
25+
and "<prefix>.<COMMIT_SHA>".
2426
required: false
2527
default: temp
2628

2729
outputs:
2830
tags:
2931
description: Comma-separated list of tags.
30-
value: ${{ steps.get-tags.outputs.tags }}
32+
value: ${{ steps.get-all-tags.outputs.tags }}
3133

3234
runs:
3335
using: composite
3436
steps:
35-
- id: get-branch
37+
- name: Get Branch
38+
id: get-branch
3639
shell: bash
3740
run: |
3841
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
@@ -45,55 +48,95 @@ runs:
4548
printf 'Cannot determine tags: Workflow must be triggered by "push", "pull_request", or "release" event.' && exit 1
4649
fi
4750
51+
echo "Branch is '$BRANCH'."
4852
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
4953
50-
- id: get-tags
54+
- name: Get Commit SHA
55+
id: get-sha
5156
shell: bash
5257
run: |
53-
BRANCH=${{ steps.get-branch.outputs.branch }}
54-
COMMIT_SHA=${GITHUB_SHA:0:7}
58+
SHA="${{ github.event.pull_request.head.sha || github.sha }}"
59+
SHA=${SHA:0:7}
60+
echo "SHA is '$SHA'."
61+
echo "sha=$SHA" >> $GITHUB_OUTPUT
62+
63+
- name: Get PR Number
64+
id: get-pr-number
65+
shell: bash
66+
env:
67+
GITHUB_TOKEN: ${{ github.token }}
68+
run: |
69+
SHA="${{ steps.get-sha.outputs.sha }}"
70+
71+
PR_NUMBER=$(
72+
gh api \
73+
-H "Accept: application/vnd.github+json" \
74+
-H "X-GitHub-Api-Version: 2022-11-28" \
75+
/repos/$GITHUB_REPOSITORY/commits/$SHA/pulls \
76+
| jq -r ".[].number"
77+
)
78+
79+
echo "PR number is '$PR_NUMBER'."
80+
echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT
81+
82+
- name: Get Tags for Shortcut Stories
83+
id: get-story-tags
84+
shell: bash
85+
if: ${{ inputs.shortcut-api-token && steps.get-pr-number.outputs.pr-number }}
86+
run: |
87+
PR_NUMBER="${{ steps.get-pr-number.outputs.pr-number }}"
88+
url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
89+
90+
ids=$(
91+
curl --silent -X GET \
92+
-H "Content-Type: application/json" \
93+
-H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
94+
-d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
95+
-L "https://api.app.shortcut.com/api/v3/search" \
96+
| jq ".stories.data[].id"
97+
)
98+
99+
TAGS=""
100+
while read -r id; do
101+
if [ ! -z "$id" ]; then
102+
TAGS+="sc-$id,"
103+
fi
104+
done <<< "$ids"
105+
106+
# Remove the trailing comma.
107+
TAGS="${TAGS::-1}"
108+
109+
echo "Story tags are '$TAGS'."
110+
echo "tags=$TAGS" >> $GITHUB_OUTPUT
111+
112+
- name: Get Tags for Branch
113+
id: get-branch-tags
114+
shell: bash
115+
run: |
116+
BRANCH="${{ steps.get-branch.outputs.branch }}"
117+
SHA="${{ steps.get-sha.outputs.sha }}"
55118
56119
if [ "$BRANCH" == "main" ] || [ "$BRANCH" == "master" ]
57120
then
58-
TAGS="latest,$BRANCH,$COMMIT_SHA"
121+
TAGS="latest,$BRANCH,$SHA"
59122
else
60123
SLASHLESS_BRANCH=`echo $BRANCH | tr / -`
61-
TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$COMMIT_SHA"
124+
TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$SHA"
62125
fi
63126
64-
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
65-
then
66-
PR_NUMBER="${{ github.event.number }}"
67-
elif [ "$GITHUB_EVENT_NAME" == "push" ]
68-
then
69-
# Workflow was triggered by a "push" event.
70-
PR_NUMBER=$( \
71-
(echo '${{ github.event.head_commit.message }}' \
72-
| grep -o "\(#[0-9]\+\)" || true) \
73-
| sed "s/[#()]//g" \
74-
)
75-
else
76-
PR_NUMBER=""
77-
fi
127+
echo "Branch tags are '$TAGS'."
128+
echo "tags=$TAGS" >> $GITHUB_OUTPUT
78129
79-
# Add additional tags for each Story ID associated with this PR.
80-
if [ ! -z "$PR_NUMBER" ] && [ ! -z "${{ inputs.shortcut-api-token }}" ]; then
81-
url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
82-
83-
ids=$(
84-
curl --silent -X GET \
85-
-H "Content-Type: application/json" \
86-
-H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
87-
-d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
88-
-L "https://api.app.shortcut.com/api/v3/search" \
89-
| jq ".stories.data[].id"
90-
)
91-
92-
while read -r id; do
93-
if [ ! -z "$id" ]; then
94-
TAGS="$TAGS,sc-$id"
95-
fi
96-
done <<< "$ids"
97-
fi
130+
- name: Get All Tags
131+
id: get-all-tags
132+
shell: bash
133+
run: |
134+
BRANCH_TAGS="${{ steps.get-branch-tags.outputs.tags }}"
135+
STORY_TAGS="${{ steps.get-story-tags.outputs.tags }}"
136+
137+
TAGS="$BRANCH_TAGS,$STORY_TAGS"
138+
TAGS="${TAGS/%,/}"
139+
TAGS="${TAGS/#,/}"
98140
141+
echo "Tags are '$TAGS'."
99142
echo "tags=$TAGS" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)