Skip to content

Commit

Permalink
Break script back up, test via Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Apr 22, 2024
1 parent 0cbbe3a commit 92035f3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 244 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: bats-core/[email protected]
with:
support-path: /usr/lib/bats/bats-support
assert-path: /usr/lib/bats/bats-assert
- run: bats --recursive tests
env:
GH_TOKEN: ${{ github.token }}

- uses: ./
- run: |
env # TODO
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Minimal inputs action to notify Slack of Job status
| `slack-users` | <p>A JSON object (as a string in the Yaml) mapping GitHub usernames to Slack User Ids (e.g. UXXXXXX). If present, the commit author is looked up in the map and the Slack user, if found, is at-mentioned in the notification details. If a Slack user is not found, an error is generated as a build annotation.</p> | `false` | `""` |
| `slack-users-file` | <p>Relative path within the repository to read the slack-users JSON from a file. The file is read from the default branch via the API.</p> | `false` | `""` |
| `github-token` | | `false` | `${{ github.token }}` |
| `dry-run` | <p>Don't actually notify (useful for testing)</p> | `false` | `false` |

<!-- action-docs-inputs source="action.yml" -->

Expand Down Expand Up @@ -113,6 +114,12 @@ This action is a `composite` action.
#
# Required: false
# Default: ${{ github.token }}

dry-run:
# Don't actually notify (useful for testing)
#
# Required: false
# Default: false
```

<!-- action-docs-usage action="action.yml" project="freckle/slack-notify-action" version="v1" -->
Expand Down
119 changes: 103 additions & 16 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,117 @@ inputs:
required: false
default: '${{ github.token }}'

dry-run:
description: "Don't actually notify (useful for testing)"
required: false
default: false

runs:
using: composite
env:
COMMIT_JSON: /tmp/commit.json
SLACK_USERS_JSON: /tmp/slack-users.json

steps:
- shell: bash
name: Fetch commit.json
run: gh api "/repos/${{ github.repository }}/commits/$COMMIT_SHA" | tee "$COMMIT_JSON"
env:
GH_TOKEN: ${{ inputs.github-token }}
COMMIT_SHA: ${{ inputs.commit-sha }}

- if: ${{ inputs.slack-users }}
shell: bash
name: Write slack-users.json from input
run: echo "$USERS" >"$SLACK_USERS_JSON"
env:
USERS: ${{ inputs.slack-users }}

- if: ${{ inputs.slack-users-file }}
shell: bash
name: Write slack-users.json from repository file
run: |
echo "$GITHUB_ACTION_PATH/bin" >>"$GITHUB_PATH"
mkslackenv >>"$GITHUB_ENV" <<'EOM'
${{ inputs.message }}
EOM
gh api "/repos/${{ github.repository }}/contents/$USERS_FILE" --jq '.content' |
base64 -d >"$SLACK_USERS_JSON"
env:
GH_TOKEN: ${{ inputs.github-token }}
USERS_FILE: ${{ inputs.slack-users-file }}

- shell: bash
name: Set SLACK_TITLE
run: |
event_name=$EVENT_NAME
event_name=${event_name:-${{ github.workflow }} ${{ github.job }}}
case "${{ job.status }}" in
success)
echo "SLACK_TITLE=$event_name succeeded"
echo "SLACK_COLOR=success"
;;
failure)
echo "SLACK_TITLE=$event_name failed"
echo "SLACK_COLOR=danger"
;;
cancelled)
echo "SLACK_TITLE=$event_name was cancelled"
echo "SLACK_COLOR=grey"
;;
esac >> "$GITHUB_ENV"
env:
EVENT_NAME: ${{ inputs.event-name }}

- shell: bash
name: Set SLACK_MESSAGE
run: |
# https://unix.stackexchange.com/a/451250
relative_date() {
awk -v date="$(date +%s -d "$1")" -v now="$(date +%s)" '
BEGIN { diff = now - date;
if (diff > (24*60*60)) printf "%.0f days ago", diff/(24*60*60);
else if (diff > (60*60)) printf "%.0f hours ago", diff/(60*60);
else if (diff > 60) printf "%.0f minutes ago", diff/60;
else printf "%s seconds ago", diff;
}'
}
get_slack_author() {
if [[ -f "$SLACK_USERS_JSON" ]]; then
author=$(jq -r ".\"$commit_author\" // \"unknown\" | \"<@\" + . + \">\"" "$SLACK_USERS_JSON")
else
author="$commit_author"
fi
}
commit_author=$(jq -r '.author.login' "$COMMIT_JSON")
commit_timestamp=$(jq -r '.commit.committer.date' "$COMMIT_JSON")
commit_timestamp_r=$(relative_date "$commit_timestamp")
commit_sha_short=$(jq -r '.sha' "$COMMIT_JSON" | head -c 7)
commit_url=$(jq -r '.url' "$COMMIT_JSON")
echo "SLACK_MESSAGE<<EOM"
printf '%s committed <%s|%s> %s (%s)\n' \
"$author" \
"$commit_url" \
"$commit_sha_short" \
"$commit_timestamp_r" \
"$commit_timestamp"
cat # extra message on stdin
echo "EOM"
if [[ "$author" == '<@unknown>' ]]; then
cat "$GITHUB_ACTION_PATH/unknown.txt" >&2
fi
- shell: bash
name: Set SLACK_FOOTER
run: |
gh --repo '${{ github.repository }}' run view '${{ github.run_id }}' \
--json jobs \
--jq '.jobs[] | select(.name == "${{ github.job }}") | "SLACK_FOOTER=" + .url' >>"$GITHUB_ENV"
# Some of these may already be set, but we'll be explicit
GITHUB_JOB: ${{ github.job }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_RUN_ID: ${{ github.run_id }}
GITHUB_WORKFLOW: ${{ github.workflow }}
INPUTS_COMMIT_SHA: ${{ inputs.commit-sha }}
INPUTS_EVENT_NAME: ${{ inputs.event_name }}
INPUTS_SLACK_USERS: ${{ inputs.slack-users }}
INPUTS_SLACK_USERS_FILE: ${{ inputs.slack-users-file }}
JOB_STATUS: ${{ job.status }}

- uses: rtCamp/action-slack-notify@v2
- if: ${{ inputs.dry-run != 'true' }}
name: Notify
uses: rtCamp/action-slack-notify@v2
env:
MSG_MINIMAL: "true" # SLACK_MESSAGE will include all details
SLACK_ICON: https://github.com/freckle-automation.png?size=48
Expand Down
81 changes: 0 additions & 81 deletions bin/mkslackenv

This file was deleted.

139 changes: 0 additions & 139 deletions tests/mkslackenv.bats

This file was deleted.

0 comments on commit 92035f3

Please sign in to comment.