Skip to content

Commit

Permalink
Refactor and add CI
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Apr 22, 2024
1 parent 9ffeae3 commit eeca4b1
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 238 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "daily"
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI

on:
push:
branches: main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./
with:
slack-webhook-url: unused
dry-run: true
- run: |
[[ "${{ env.SLACK_COLOR }}" == 'success' ]]
[[ "${{ env.SLACK_TITLE }}" == 'CI test succeeded' ]]
message_re='^.* committed <.*\|.*> .* (.*)$'
[[ "${{ env.SLACK_MESSAGE }}" == $message_re ]]
job_url_re='^https://github\.com/freckle/slack-notify-action/actions/runs/.*/job/.*$'
[[ "${{ env.SLACK_FOOTER }}" =~ $footer_re ]]
18 changes: 18 additions & 0 deletions .github/workflows/mergeabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Mergeabot

on:
pull_request:
schedule:
- cron: "0 0 * * *"

permissions:
contents: write
pull-requests: write

jobs:
mergeabot:
runs-on: ubuntu-latest
steps:
- uses: freckle/mergeabot-action@v1
with:
quarantine-days: -1
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ Minimal inputs action to notify Slack of Job status

| name | description | required | default |
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --------------------------------------- | --- | -------------- |
| `slack-webhook-url` | | `true` | `""` |
| `slack-webhook-url` | <p>Slack webhook url, typically a repository secret.</p> | `true` | `""` |
| `slack-channel` | <p>Explicit channel for this notification. If omitted (the default), the channel configured in the webhook is used.</p> | `false` | `""` |
| `event-name` | <p>A name for the event being notified about. If not given, the workflow and job id is used.</p> | `false` | `""` |
| `message` | <p>Additional content to add to the message. Details about the commit that triggered the Job in which this step is run are always included.</p> | `false` | `""` |
| `commit-sha` | <p>Commit SHA to fetch details for. Default is the PR head sha or github.sha if not in the context of a PR.</p> | `false` | `${{ github.event.pull_request.head.sha | | github.sha }}` |
| `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 All @@ -59,6 +60,7 @@ This action is a `composite` action.
- uses: freckle/slack-notify-action@v1
with:
slack-webhook-url:
# Slack webhook url, typically a repository secret.
#
# Required: true
# Default: ""
Expand Down Expand Up @@ -112,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 All @@ -120,7 +128,7 @@ This action is a `composite` action.

```yaml
- uses: freckle/slack-notify-action@v1
with
with:
slack-webhook: ${{ secrets.SLACK_WEBHOOK }}
slack-users: |
{
Expand Down
128 changes: 112 additions & 16 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ description: |
inputs:
slack-webhook-url:
description: ""
description: |
Slack webhook url, typically a repository secret.
required: true

slack-channel:
Expand Down Expand Up @@ -58,30 +59,125 @@ inputs:
required: false
default: '${{ github.token }}'

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

runs:
using: composite
steps:
- shell: bash
name: Set global ENV
run: |
echo "$GITHUB_ACTION_PATH/bin" >>"$GITHUB_PATH"
mkslackenv >>"$GITHUB_ENV" <<'EOM'
${{ inputs.message }}
cat >>"$GITHUB_ENV" <<'EOM'
COMMIT_JSON=/tmp/commit.json
SLACK_USERS_JSON=/tmp/slack-users.json
EOM
- shell: bash
name: Fetch commit.json
run: gh api "/repos/${{ github.repository }}/commits/$COMMIT_SHA" >"$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: |
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;
}'
}
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")
if [[ -f "$SLACK_USERS_JSON" ]]; then
author=$(jq -r ".\"$commit_author\" // \"unknown\" | \"<@\" + . + \">\"" "$SLACK_USERS_JSON")
else
author="$commit_author"
fi
{
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"
} >>"$GITHUB_ENV"
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"
env:
GH_TOKEN: ${{ inputs.github-token }}
# 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.

Loading

0 comments on commit eeca4b1

Please sign in to comment.