Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow attaching commit status to specific commit. #14

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 2 additions & 22 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,17 @@ jobs:

shellcheck:
# Use newer os for more up-to-date shellcheck
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run shellcheck on .sh and .bash files files
run: |
find . -name '*.sh' -exec shellcheck -s bash '{}' \+
find . -name '*.bash' -exec shellcheck -s bash '{}' \+

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: "2.7"
- name: Install kojo
run: gem install kojo
- run: make
- name: Check for changes
run: |
git status
git_changes="$(git status --porcelain || true)"
# If the output is not empty, there are changes; fail the action
if [ -n "$git_changes" ]; then
echo "Changes found; build the action with 'make' and commit the changes"
exit 1
fi

finalize_status:
runs-on: ubuntu-latest
needs: [shellcheck, build, create_status]
needs: [shellcheck, create_status]
if: ${{ always() }}
steps:
- uses: actions/checkout@v4
Expand Down
32 changes: 10 additions & 22 deletions create/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ inputs:
target-url:
description: "URL to include in the status"
default: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
sha:
description: "Commit sha to attach the status to if it's different from the workflow run commit"
required: false
default: ""

outputs:
commit_status_sha:
Expand All @@ -23,16 +27,12 @@ runs:
- name: Determine commit to put the build status on
id: commit_status_sha
shell: bash
env:
COMMIT_STATUS_SHA: ${{ inputs.sha }}
PULL_REQUEST_SHA: ${{ github.event.pull_request.head.sha }}
GITHUB_SHA: ${{ github.sha }}
run: |
set -eu -o pipefail

if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
SHA="${{ github.event.pull_request.head.sha }}"
else
SHA="${{ github.sha }}"
fi
echo sha="$SHA" >> "$GITHUB_OUTPUT"

"${GITHUB_ACTION_PATH}/get-commit-sha.bash"
- name: Create commit status
shell: bash
env:
Expand All @@ -42,16 +42,4 @@ runs:
DESCRIPTION: "Build status is pending"
CONTEXT: ${{ inputs.context }}
run: |
set -eu -o pipefail

declare -a ARGS
if [[ -n "$TARGET_URL" ]]; then
ARGS=("-f" "target_url=${TARGET_URL}")
fi

gh api "/repos/${GITHUB_REPOSITORY}/statuses/${COMMIT_STATUS_SHA}" \
-X POST \
-f state=pending \
-f description="$DESCRIPTION" \
-f context="$CONTEXT" \
"${ARGS[@]}"
"${GITHUB_ACTION_PATH}/create-commit-status.bash"
37 changes: 0 additions & 37 deletions create/action.yml.erb

This file was deleted.

2 changes: 2 additions & 0 deletions create/create-commit-status.bash
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

set -eu -o pipefail

declare -a ARGS
Expand Down
10 changes: 7 additions & 3 deletions create/get-commit-sha.bash
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env bash

set -eu -o pipefail

if [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
SHA="${{ github.event.pull_request.head.sha }}"
if [ -n "$COMMIT_STATUS_SHA" ]; then
SHA="$COMMIT_STATUS_SHA"
elif [[ "$GITHUB_EVENT_NAME" == "pull_request" ]]; then
SHA="$PULL_REQUEST_SHA"
else
SHA="${{ github.sha }}"
SHA="$GITHUB_SHA"
fi
echo sha="$SHA" >> "$GITHUB_OUTPUT"
61 changes: 2 additions & 59 deletions update/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,7 @@ runs:
env:
GITHUB_NEEDS: ${{ inputs.github-needs }}
run: |
set -eu -o pipefail

cat > update_commit_status-needs.json <<EOFNEEDS
${GITHUB_NEEDS}
EOFNEEDS

ERROR=error
CANCELLED=cancelled
FAILURE=failure
SUCCESS=success

function countbyStatus() {
local status="$1"
local with_status=
local count=0
with_status="$(jq '. | with_entries(select(.value.result == "'"$status"'"))' <update_commit_status-needs.json)"
echo "$with_status" 1>&2
count="$(echo "$with_status" | jq '. | length')"
echo "$count"
}
function jobsWithStatus() {
local status="$1"
jq -r '. | to_entries | .[] | select(.value.result == "'"$status"'") | .key' <update_commit_status-needs.json | tr $'\n' ' '
}
function jobsStatuses() {
jq -r '. | to_entries | .[] | .key + ": " .value.result' <update_commit_status-needs.json | tr $'\n' '; ' | sed 's/; $//'
}
successes="$(countbyStatus "$SUCCESS")"
failures="$(countbyStatus "$FAILURE")"
cancelled="$(countbyStatus "$CANCELLED")"
if [[ "$cancelled" -gt 0 ]]; then
final_result="$ERROR"
description="Build ended as 'error' with one or more cancelled stages: $(jobsWithStatus "$CANCELLED")"
elif [[ "$failures" -gt 0 ]]; then
final_result="$FAILURE"
description="Build ended as 'failed' with one or more failed stages: $(jobsWithStatus "$FAILURE")"
elif [[ "$successes" -eq 0 ]]; then
final_result="$ERROR"
description="Build encountered unknown error with no successful stages reported. Job statuses are $(jobsStatuses)"
else
final_result="$SUCCESS"
description="Build succeeded"
fi
echo status="$final_result" >> "$GITHUB_OUTPUT"
echo description="$description" >> "$GITHUB_OUTPUT"

"${GITHUB_ACTION_PATH}/get-commit-status.bash"
- name: Update commit status
shell: bash
env:
Expand All @@ -89,16 +44,4 @@ runs:
CONTEXT: ${{ inputs.context }}
STATUS: ${{ steps.commit_status.outputs.status }}
run: |
set -eu -o pipefail

declare -a ARGS
if [[ -n "$TARGET_URL" ]]; then
ARGS=("-f" "target_url=${TARGET_URL}")
fi

gh api "/repos/${GITHUB_REPOSITORY}/statuses/${COMMIT_STATUS_SHA}" \
-XPOST \
-f state="$STATUS" \
-f description="$DESCRIPTION" \
-f context="$CONTEXT" \
"${ARGS[@]}"
"${GITHUB_ACTION_PATH}/update-commit-status.bash"
47 changes: 0 additions & 47 deletions update/action.yml.erb

This file was deleted.

2 changes: 2 additions & 0 deletions update/get-commit-status.bash
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

set -eu -o pipefail

cat > update_commit_status-needs.json <<EOFNEEDS
Expand Down
2 changes: 2 additions & 0 deletions update/update-commit-status.bash
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

set -eu -o pipefail

declare -a ARGS
Expand Down