Skip to content

Change branch name from 'main' to 'master' #2

Change branch name from 'main' to 'master'

Change branch name from 'main' to 'master' #2

name: Discord Notifications
on:
push:
branches: [master]
pull_request:
branches: [master]
types: [opened, reopened, synchronize, closed, ready_for_review, converted_to_draft, edited, labeled, unlabeled, assigned, unassigned, review_requested, review_request_removed]
pull_request_review:
types: [submitted, edited, dismissed]
pull_request_review_comment:
types: [created, edited, deleted]
issues:
types: [opened, reopened, closed, edited, labeled, unlabeled, assigned, unassigned, milestoned, demilestoned, transferred, pinned, unpinned, locked, unlocked, deleted]
issue_comment:
types: [created, edited, deleted]
release:
types: [published, unpublished, created, edited, deleted, prereleased, released]
create:
delete:
fork:
watch:
types: [started]
discussion:
types: [created, edited, deleted, pinned, unpinned, locked, unlocked, transferred, category_changed, answered, unanswered]
discussion_comment:
types: [created, edited, deleted]
workflow_dispatch:
repository_dispatch:
public:
member:
types: [added, edited, removed]
milestone:
types: [created, closed, opened, edited, deleted]
label:
types: [created, edited, deleted]
project:
types: [created, closed, reopened, edited, deleted]
project_card:
types: [created, moved, converted, edited, deleted]
project_column:
types: [created, updated, moved, deleted]
status:
deployment:
deployment_status:
page_build:
gollum:
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Send Discord embed
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
EVENT_NAME: ${{ github.event_name }}
ACTION: ${{ github.event.action }}
SHA: ${{ github.sha }}
run: |
EVENT_JSON="$GITHUB_EVENT_PATH"
REPO_URL="$(jq -r '.repository.html_url // ""' "$EVENT_JSON")"
REPO_FULL="$(jq -r '.repository.full_name // ""' "$EVENT_JSON")"
ACTOR_NAME="$(jq -r '.sender.login // ""' "$EVENT_JSON")"
ACTOR_AVATAR="$(jq -r '.sender.avatar_url // ""' "$EVENT_JSON")"
ACTOR_URL="$(jq -r '.sender.html_url // ""' "$EVENT_JSON")"
TS="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
ACTION_PART=""
if [ -n "${ACTION}" ] && [ "${ACTION}" != "null" ]; then
ACTION_PART=" (${ACTION})"
fi
BASE_LINE="A new **${EVENT_NAME}${ACTION_PART}** was performed on **${REPO_FULL}** by **${ACTOR_NAME}**."
MESSAGE=""
EXTRA_NAME=""
EXTRA_VALUE=""
COLOR=3092790
if [ "$EVENT_NAME" = "push" ]; then
MESSAGE="$(jq -r '.head_commit.message // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 600)"
elif [ "$EVENT_NAME" = "pull_request" ]; then
PR_TITLE="$(jq -r '.pull_request.title // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 400)"
PR_NUM="$(jq -r '.pull_request.number // ""' "$EVENT_JSON")"
PR_STATE="$(jq -r '.pull_request.state // ""' "$EVENT_JSON")"
MESSAGE="PR #${PR_NUM}: ${PR_TITLE}"
if [ -n "$PR_STATE" ]; then
EXTRA_NAME="State"
EXTRA_VALUE="$PR_STATE"
fi
elif [ "$EVENT_NAME" = "pull_request_review" ]; then
PR_NUM="$(jq -r '.pull_request.number // ""' "$EVENT_JSON")"
STATE="$(jq -r '.review.state // ""' "$EVENT_JSON")"
MESSAGE="Review on PR #${PR_NUM}: ${STATE}"
elif [ "$EVENT_NAME" = "pull_request_review_comment" ]; then
PR_NUM="$(jq -r '.pull_request.number // ""' "$EVENT_JSON")"
MESSAGE="Review comment on PR #${PR_NUM}"
PTH="$(jq -r '.comment.path // ""' "$EVENT_JSON")"
if [ -n "$PTH" ]; then
EXTRA_NAME="File"
EXTRA_VALUE="$PTH"
fi
elif [ "$EVENT_NAME" = "issues" ]; then
I_TITLE="$(jq -r '.issue.title // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 400)"
I_NUM="$(jq -r '.issue.number // ""' "$EVENT_JSON")"
I_STATE="$(jq -r '.issue.state // ""' "$EVENT_JSON")"
MESSAGE="Issue #${I_NUM}: ${I_TITLE}"
if [ -n "$I_STATE" ]; then
EXTRA_NAME="State"
EXTRA_VALUE="$I_STATE"
fi
elif [ "$EVENT_NAME" = "issue_comment" ]; then
I_NUM="$(jq -r '.issue.number // ""' "$EVENT_JSON")"
BODY="$(jq -r '.comment.body // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 250)"
MESSAGE="$(printf "Comment on Issue #%s\n\n%s" "$I_NUM" "$BODY")"
elif [ "$EVENT_NAME" = "release" ]; then
R_NAME="$(jq -r '.release.name // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 400)"
R_TAG="$(jq -r '.release.tag_name // ""' "$EVENT_JSON")"
MESSAGE="Release: ${R_NAME}"
if [ -n "$R_TAG" ]; then
EXTRA_NAME="Tag"
EXTRA_VALUE="$R_TAG"
fi
elif [ "$EVENT_NAME" = "discussion" ]; then
D_TITLE="$(jq -r '.discussion.title // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 400)"
MESSAGE="Discussion: ${D_TITLE}"
CAT="$(jq -r '.discussion.category.name // ""' "$EVENT_JSON")"
if [ -n "$CAT" ]; then
EXTRA_NAME="Category"
EXTRA_VALUE="$CAT"
fi
elif [ "$EVENT_NAME" = "discussion_comment" ]; then
BODY="$(jq -r '.comment.body // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 250)"
MESSAGE="$(printf "Discussion comment\n\n%s" "$BODY")"
elif [ "$EVENT_NAME" = "create" ]; then
RT="$(jq -r '.ref_type // ""' "$EVENT_JSON")"
R="$(jq -r '.ref // ""' "$EVENT_JSON")"
MESSAGE="Created ${RT}: ${R}"
elif [ "$EVENT_NAME" = "delete" ]; then
RT="$(jq -r '.ref_type // ""' "$EVENT_JSON")"
R="$(jq -r '.ref // ""' "$EVENT_JSON")"
MESSAGE="Deleted ${RT}: ${R}"
elif [ "$EVENT_NAME" = "fork" ]; then
F_FULL="$(jq -r '.forkee.full_name // ""' "$EVENT_JSON")"
MESSAGE="Repository was forked."
if [ -n "$F_FULL" ]; then
EXTRA_NAME="Fork"
EXTRA_VALUE="$F_FULL"
fi
elif [ "$EVENT_NAME" = "watch" ]; then
MESSAGE="Repository was starred."
elif [ "$EVENT_NAME" = "public" ]; then
MESSAGE="Repository is now public."
elif [ "$EVENT_NAME" = "member" ]; then
M="$(jq -r '.member.login // ""' "$EVENT_JSON")"
MESSAGE="A collaborator change occurred."
if [ -n "$M" ]; then
EXTRA_NAME="Member"
EXTRA_VALUE="$M"
fi
elif [ "$EVENT_NAME" = "milestone" ]; then
MT="$(jq -r '.milestone.title // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 200)"
MS="$(jq -r '.milestone.state // ""' "$EVENT_JSON")"
MESSAGE="Milestone: ${MT}"
if [ -n "$MS" ]; then
EXTRA_NAME="State"
EXTRA_VALUE="$MS"
fi
elif [ "$EVENT_NAME" = "label" ]; then
LN="$(jq -r '.label.name // ""' "$EVENT_JSON")"
MESSAGE="Label updated: ${LN}"
elif [ "$EVENT_NAME" = "project" ]; then
PN="$(jq -r '.project.name // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 200)"
MESSAGE="Project: ${PN}"
elif [ "$EVENT_NAME" = "project_column" ]; then
CN="$(jq -r '.project_column.name // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 200)"
MESSAGE="Project column updated: ${CN}"
elif [ "$EVENT_NAME" = "project_card" ]; then
MESSAGE="Project card updated."
elif [ "$EVENT_NAME" = "status" ]; then
STATE="$(jq -r '.state // ""' "$EVENT_JSON")"
DESC="$(jq -r '.description // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 200)"
MESSAGE="Commit status: ${STATE}"
if [ -n "$DESC" ]; then
MESSAGE="$(printf "%s\n\n%s" "$MESSAGE" "$DESC")"
fi
elif [ "$EVENT_NAME" = "deployment" ]; then
ENVN="$(jq -r '.deployment.environment // ""' "$EVENT_JSON")"
MESSAGE="Deployment created."
if [ -n "$ENVN" ]; then
EXTRA_NAME="Env"
EXTRA_VALUE="$ENVN"
fi
elif [ "$EVENT_NAME" = "deployment_status" ]; then
ENVN="$(jq -r '.deployment.environment // ""' "$EVENT_JSON")"
STATE="$(jq -r '.deployment_status.state // ""' "$EVENT_JSON")"
MESSAGE="Deployment status: ${STATE}"
if [ -n "$ENVN" ] || [ -n "$STATE" ]; then
EXTRA_NAME="Env"
EXTRA_VALUE="${ENVN}${ENVN:+ · }${STATE}"
fi
elif [ "$EVENT_NAME" = "page_build" ]; then
MESSAGE="GitHub Pages build completed."
elif [ "$EVENT_NAME" = "gollum" ]; then
P_TITLE="$(jq -r '.pages[0].title // "" | gsub("\\\\n"; "\n")' "$EVENT_JSON" | head -c 200)"
P_ACTION="$(jq -r '.pages[0].action // ""' "$EVENT_JSON")"
MESSAGE="Wiki update: ${P_TITLE}"
if [ -n "$P_ACTION" ]; then
EXTRA_NAME="Action"
EXTRA_VALUE="$P_ACTION"
fi
elif [ "$EVENT_NAME" = "repository_dispatch" ]; then
ET="$(jq -r '.action // .client_payload.event_type // ""' "$EVENT_JSON")"
MESSAGE="Repository dispatch received."
if [ -n "$ET" ]; then
EXTRA_NAME="Type"
EXTRA_VALUE="$ET"
fi
elif [ "$EVENT_NAME" = "workflow_dispatch" ]; then
MESSAGE="Workflow was manually triggered."
fi
BASE_LINE="$(printf '%s' "$BASE_LINE" | tr -d '\r')"
MESSAGE="$(printf '%s' "$MESSAGE" | tr -d '\r')"
if [ -n "$MESSAGE" ]; then
DESCRIPTION="$(printf "%s\n\n%s" "$BASE_LINE" "$MESSAGE")"
else
DESCRIPTION="$BASE_LINE"
fi
DESCRIPTION="$(printf '%s' "$DESCRIPTION" | sed -E ':a;N;$!ba;s/\n{3,}/\n\n/g')"
payload="$(jq -n \
--arg title "$REPO_FULL" \
--arg url "$REPO_URL" \
--arg desc "$DESCRIPTION" \
--arg author_name "$ACTOR_NAME" \
--arg author_icon "$ACTOR_AVATAR" \
--arg author_url "$ACTOR_URL" \
--arg extra_name "$EXTRA_NAME" \
--arg extra_value "$EXTRA_VALUE" \
--arg ts "$TS" \
--arg footer "View-Marketplace Systems" \
--argjson color "$COLOR" \
'{
embeds: [
{
title: $title,
url: $url,
description: $desc,
color: $color,
author: {
name: $author_name,
url: (if ($author_url|length)>0 then $author_url else null end),
icon_url: (if ($author_icon|length)>0 then $author_icon else null end)
},
fields: (
(if ($extra_name|length)>0 and ($extra_value|length)>0 then [{name:$extra_name, value:$extra_value, inline:true}] else [] end)
),
timestamp: $ts,
footer: {text: $footer}
}
]
}')"
curl -H "Content-Type: application/json" -d "$payload" "$DISCORD_WEBHOOK_URL"