Skip to content

Commit

Permalink
Improve robustness of GDIT Action (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Mandrenkov authored Oct 30, 2023
1 parent 1df1729 commit 25c9c4a
Showing 1 changed file with 87 additions and 44 deletions.
131 changes: 87 additions & 44 deletions generate-docker-image-tags/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@ description: Generate a list of Docker image tags for the current workflow.

inputs:
shortcut-api-token:
description: Shortcut API token for fetching Story IDs related to a PR.
description: >
Shortcut API token for fetching Story IDs related to a PR.
required: false
prefix:
description: |
The prefix for tagging commits that are not yet merged to main/master.
For example: a PR from some-branch would receive the tags <prefix>.some-branch, <prefix>.<COMMIT_SHA>.
description: >
Prefix for tagging commits that are not yet merged to main/master.
For example, a PR from some-branch would receive tags "<prefix>.some-branch"
and "<prefix>.<COMMIT_SHA>".
required: false
default: temp

outputs:
tags:
description: Comma-separated list of tags.
value: ${{ steps.get-tags.outputs.tags }}
value: ${{ steps.get-all-tags.outputs.tags }}

runs:
using: composite
steps:
- id: get-branch
- name: Get Branch
id: get-branch
shell: bash
run: |
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
Expand All @@ -45,55 +48,95 @@ runs:
printf 'Cannot determine tags: Workflow must be triggered by "push", "pull_request", or "release" event.' && exit 1
fi
echo "Branch is '$BRANCH'."
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
- id: get-tags
- name: Get Commit SHA
id: get-sha
shell: bash
run: |
BRANCH=${{ steps.get-branch.outputs.branch }}
COMMIT_SHA=${GITHUB_SHA:0:7}
SHA="${{ github.event.pull_request.head.sha || github.sha }}"
SHA=${SHA:0:7}
echo "SHA is '$SHA'."
echo "sha=$SHA" >> $GITHUB_OUTPUT
- name: Get PR Number
id: get-pr-number
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
SHA="${{ steps.get-sha.outputs.sha }}"
PR_NUMBER=$(
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/$GITHUB_REPOSITORY/commits/$SHA/pulls \
| jq -r ".[].number"
)
echo "PR number is '$PR_NUMBER'."
echo "pr-number=$PR_NUMBER" >> $GITHUB_OUTPUT
- name: Get Tags for Shortcut Stories
id: get-story-tags
shell: bash
if: ${{ inputs.shortcut-api-token && steps.get-pr-number.outputs.pr-number }}
run: |
PR_NUMBER="${{ steps.get-pr-number.outputs.pr-number }}"
url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
ids=$(
curl --silent -X GET \
-H "Content-Type: application/json" \
-H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
-d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
-L "https://api.app.shortcut.com/api/v3/search" \
| jq ".stories.data[].id"
)
TAGS=""
while read -r id; do
if [ ! -z "$id" ]; then
TAGS+="sc-$id,"
fi
done <<< "$ids"
# Remove the trailing comma.
TAGS="${TAGS::-1}"
echo "Story tags are '$TAGS'."
echo "tags=$TAGS" >> $GITHUB_OUTPUT
- name: Get Tags for Branch
id: get-branch-tags
shell: bash
run: |
BRANCH="${{ steps.get-branch.outputs.branch }}"
SHA="${{ steps.get-sha.outputs.sha }}"
if [ "$BRANCH" == "main" ] || [ "$BRANCH" == "master" ]
then
TAGS="latest,$BRANCH,$COMMIT_SHA"
TAGS="latest,$BRANCH,$SHA"
else
SLASHLESS_BRANCH=`echo $BRANCH | tr / -`
TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$COMMIT_SHA"
TAGS="${{ inputs.prefix }}.$SLASHLESS_BRANCH,${{ inputs.prefix }}.$SHA"
fi
if [ "$GITHUB_EVENT_NAME" == "pull_request" ]
then
PR_NUMBER="${{ github.event.number }}"
elif [ "$GITHUB_EVENT_NAME" == "push" ]
then
# Workflow was triggered by a "push" event.
PR_NUMBER=$( \
(echo '${{ github.event.head_commit.message }}' \
| grep -o "\(#[0-9]\+\)" || true) \
| sed "s/[#()]//g" \
)
else
PR_NUMBER=""
fi
echo "Branch tags are '$TAGS'."
echo "tags=$TAGS" >> $GITHUB_OUTPUT
# Add additional tags for each Story ID associated with this PR.
if [ ! -z "$PR_NUMBER" ] && [ ! -z "${{ inputs.shortcut-api-token }}" ]; then
url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/pull/$PR_NUMBER"
ids=$(
curl --silent -X GET \
-H "Content-Type: application/json" \
-H "Shortcut-Token: ${{ inputs.shortcut-api-token }}" \
-d "{ \"page_size\": 20, \"query\": \"is:story and pr:$url\" }" \
-L "https://api.app.shortcut.com/api/v3/search" \
| jq ".stories.data[].id"
)
while read -r id; do
if [ ! -z "$id" ]; then
TAGS="$TAGS,sc-$id"
fi
done <<< "$ids"
fi
- name: Get All Tags
id: get-all-tags
shell: bash
run: |
BRANCH_TAGS="${{ steps.get-branch-tags.outputs.tags }}"
STORY_TAGS="${{ steps.get-story-tags.outputs.tags }}"
TAGS="$BRANCH_TAGS,$STORY_TAGS"
TAGS="${TAGS/%,/}"
TAGS="${TAGS/#,/}"
echo "Tags are '$TAGS'."
echo "tags=$TAGS" >> $GITHUB_OUTPUT

0 comments on commit 25c9c4a

Please sign in to comment.