Discord Notifications #1819
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Discord Notifications | |
| on: | |
| watch: | |
| types: [started] | |
| issues: | |
| types: [opened] | |
| pull_request: | |
| types: [opened, ready_for_review] | |
| release: | |
| types: [published] | |
| workflow_run: | |
| workflows: ["CI", "Security Scan"] | |
| types: [completed] | |
| jobs: | |
| notify: | |
| runs-on: ubuntu-latest | |
| env: | |
| DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | |
| ACTOR: ${{ github.actor }} | |
| EVENT_NAME: ${{ github.event_name }} | |
| IGNORED_PR_ACTOR: ccarvalho-eng | |
| REPO: ${{ github.repository }} | |
| steps: | |
| - name: Notify Discord | |
| if: env.DISCORD_WEBHOOK != '' | |
| run: | | |
| set -euo pipefail | |
| case "$EVENT_NAME" in | |
| watch) | |
| content="$(printf '⭐ **%s** starred **%s**\nhttps://github.com/%s\nhttps://github.com/%s' "$ACTOR" "$REPO" "$REPO" "$ACTOR")" | |
| ;; | |
| issues) | |
| title="$(jq -r '.issue.title' "$GITHUB_EVENT_PATH")" | |
| url="$(jq -r '.issue.html_url' "$GITHUB_EVENT_PATH")" | |
| content="$(printf '📝 New issue in **%s** by **%s**\n%s\n%s' "$REPO" "$ACTOR" "$title" "$url")" | |
| ;; | |
| pull_request) | |
| action="$(jq -r '.action' "$GITHUB_EVENT_PATH")" | |
| pr_author="$(jq -r '.pull_request.user.login' "$GITHUB_EVENT_PATH")" | |
| sender="$(jq -r '.sender.login' "$GITHUB_EVENT_PATH")" | |
| if [ "$pr_author" = "$IGNORED_PR_ACTOR" ] || [ "$sender" = "$IGNORED_PR_ACTOR" ]; then | |
| exit 0 | |
| fi | |
| title="$(jq -r '.pull_request.title' "$GITHUB_EVENT_PATH")" | |
| url="$(jq -r '.pull_request.html_url' "$GITHUB_EVENT_PATH")" | |
| label="New PR" | |
| if [ "$action" = "ready_for_review" ]; then | |
| label="PR ready for review" | |
| fi | |
| content="$(printf '🔀 %s in **%s** by **%s**\n%s\n%s' "$label" "$REPO" "$ACTOR" "$title" "$url")" | |
| ;; | |
| release) | |
| tag="$(jq -r '.release.tag_name' "$GITHUB_EVENT_PATH")" | |
| url="$(jq -r '.release.html_url' "$GITHUB_EVENT_PATH")" | |
| content="$(printf '🚀 **%s** released **%s**\n%s' "$REPO" "$tag" "$url")" | |
| ;; | |
| workflow_run) | |
| conclusion="$(jq -r '.workflow_run.conclusion' "$GITHUB_EVENT_PATH")" | |
| default_branch="$(jq -r '.repository.default_branch' "$GITHUB_EVENT_PATH")" | |
| workflow="$(jq -r '.workflow_run.name' "$GITHUB_EVENT_PATH")" | |
| branch="$(jq -r '.workflow_run.head_branch' "$GITHUB_EVENT_PATH")" | |
| url="$(jq -r '.workflow_run.html_url' "$GITHUB_EVENT_PATH")" | |
| if [ "$conclusion" != "failure" ] || [ "$branch" != "$default_branch" ]; then | |
| exit 0 | |
| fi | |
| content="$(printf '❌ **%s** failed on **%s** for **%s**\n%s' "$workflow" "$branch" "$REPO" "$url")" | |
| ;; | |
| esac | |
| payload="$(jq -n --arg content "$content" '{content: $content}')" | |
| curl -H "Content-Type: application/json" \ | |
| -X POST \ | |
| -d "$payload" \ | |
| "$DISCORD_WEBHOOK" |