diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 339e12a..e991d27 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -10,11 +10,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- - uses: bats-core/bats-action@2.0.0
- 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
diff --git a/README.md b/README.md
index 4adc172..858bed2 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ Minimal inputs action to notify Slack of Job status
| `slack-users` |
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.
| `false` | `""` |
| `slack-users-file` | 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.
| `false` | `""` |
| `github-token` | | `false` | `${{ github.token }}` |
+| `dry-run` | Don't actually notify (useful for testing)
| `false` | `false` |
@@ -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
```
diff --git a/action.yml b/action.yml
index 86e06d2..8cd6a9f 100644
--- a/action.yml
+++ b/action.yml
@@ -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< %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
diff --git a/bin/mkslackenv b/bin/mkslackenv
deleted file mode 100755
index f8c10b8..0000000
--- a/bin/mkslackenv
+++ /dev/null
@@ -1,81 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-# 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;
- }'
-}
-
-# If slack-users is set, print it. If slack-users-file is set, read it.
-# Otherwise returns 1 to signify slack-users were not configured.
-get_slack_json() {
- if [[ -n "$INPUTS_SLACK_USERS" ]]; then
- echo "$INPUTS_SLACK_USERS"
- elif [[ -n "$INPUTS_SLACK_USERS_FILE" ]]; then
- gh api "/repos/$GITHUB_REPOSITORY/contents/$INPUTS_SLACK_USERS_FILE" --jq '.content' | base64 -d
- else
- return 1
- fi
-}
-
-# Find and format the slack user for the given GH username, or return the
-# username as-is if slack-users was not configured.
-get_slack_author() {
- if ! get_slack_json | jq -r ".\"$1\" // \"unknown\" | \"<@\" + . + \">\""; then
- echo "$1"
- fi
-}
-
-print_job_url() {
- gh --repo "$GITHUB_REPOSITORY" run view "$GITHUB_RUN_ID" \
- --json jobs \
- --jq ".jobs[] | select(.name == \"$GITHUB_JOB\") | .url"
-}
-
-event_name=$INPUTS_EVENT_NAME
-event_name=${event_name:-$GITHUB_WORKFLOW $GITHUB_JOB}
-
-commit_sha_short=$(head -c 7 <<<"$INPUTS_COMMIT_SHA")
-commit_url=https://github.com/$GITHUB_REPOSITORY/commit/$INPUTS_COMMIT_SHA
-
-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
-
-commit_json=$(gh api "/repos/$GITHUB_REPOSITORY/commits/$INPUTS_COMMIT_SHA")
-commit_timestamp=$(jq -r '.commit.committer.date' <<<"$commit_json")
-commit_author=$(jq -r '.author.login' <<<"$commit_json")
-author="$(get_slack_author "$commit_author")"
-
-echo "SLACK_MESSAGE< %s (%s)\n' \
- "$author" \
- "$commit_url" \
- "$commit_sha_short" \
- "$(relative_date "$commit_timestamp")" \
- "$commit_timestamp"
-cat # extra message on stdin
-echo "EOM"
-
-printf "SLACK_FOOTER=%s\n" "$(print_job_url)"
-
-if [[ "$author" == '<@unknown>' ]]; then
- cat "$GITHUB_ACTION_PATH/unknown.txt" >&2
-fi
diff --git a/tests/mkslackenv.bats b/tests/mkslackenv.bats
deleted file mode 100644
index 236c2ce..0000000
--- a/tests/mkslackenv.bats
+++ /dev/null
@@ -1,139 +0,0 @@
-# shellcheck disable=SC2030,SC2031
-bats_load_library bats-support
-bats_load_library bats-assert
-
-src=$(cd "$BATS_TEST_DIRNAME/.." && pwd)
-export GITHUB_ACTION_PATH=$src
-export "PATH=$GITHUB_ACTION_PATH/bin:$PATH"
-
-# Example commit and Job Run from megarepo
-export GITHUB_JOB=test
-export GITHUB_REPOSITORY=freckle/slack-notify-action
-export GITHUB_RUN_ID=8786664573
-export GITHUB_WORKFLOW=CI
-export INPUTS_COMMIT_SHA=9ffeae33947f9abb59fac9aaf1eab7261395b449
-export INPUTS_EVENT_NAME=
-export INPUTS_SLACK_USERS=
-export INPUTS_SLACK_USERS_FILE=
-
-@test "failure with all optional inputs omitted" {
- export JOB_STATUS=failure
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test failed
-SLACK_COLOR=danger
-SLACK_MESSAGE< 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "with extra message" {
- export JOB_STATUS=failure
-
- run bash -c "echo hi there | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test failed
-SLACK_COLOR=danger
-SLACK_MESSAGE< 3 days ago (2024-04-19T17:45:25Z)
-hi there
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "success" {
- export JOB_STATUS=success
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test succeeded
-SLACK_COLOR=success
-SLACK_MESSAGE< 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "custom event name" {
- export INPUTS_EVENT_NAME="My awesome job"
- export JOB_STATUS=success
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=My awesome job succeeded
-SLACK_COLOR=success
-SLACK_MESSAGE< 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "reading slack users" {
- export INPUTS_SLACK_USERS='{"pbrisbin":"U6PM52FPY"}'
- export JOB_STATUS=success
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test succeeded
-SLACK_COLOR=success
-SLACK_MESSAGE< committed 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "reading slack users via contents" {
- export INPUTS_SLACK_USERS_FILE=.github/slack.json
- export JOB_STATUS=success
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test succeeded
-SLACK_COLOR=success
-SLACK_MESSAGE< committed 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-EOASSERT
-}
-
-@test "unknown slack users" {
- export INPUTS_SLACK_USERS='{}'
- export JOB_STATUS=success
-
- run bash -c "echo | mkslackenv"
- assert_output - <<'EOASSERT'
-SLACK_TITLE=CI test succeeded
-SLACK_COLOR=success
-SLACK_MESSAGE< committed 3 days ago (2024-04-19T17:45:25Z)
-
-EOM
-SLACK_FOOTER=https://github.com/freckle/megarepo/actions/runs/8757571976/job/24036522054
-Your username is missing from slack-notify users mapping.
-
-Without your username present in this file, Slack notifications may not
-highlight you correctly.
-
-::group::Expand for instructions to find your Slack User Id
-
-1. View your profile
-2. Click the three-dots in the upper section
-3. Select "Copy member ID"
-
-::endgroup::
-::error title=Your username is missing from slack-notify users::Please add your GitHub username as a key, with Slack User ID as value
-EOASSERT
-}