Skip to content

Commit

Permalink
Merge of #16
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 20, 2024
2 parents 2f1a92b + 577e54d commit dd422a4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 113 deletions.
117 changes: 4 additions & 113 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,133 +67,24 @@ runs:
PROJECT: ${{ inputs.project }}
REPOSITORY: ${{ github.repository }}
JOB_ID: ${{ steps.fetch-check-run.outputs.job_id }}
JOB_URL: ${{ steps.fetch-check-run.outputs.job_url }}
JOB_STATUS: ${{ job.status }}
REF_NAME: ${{ github.ref_name }}
OVERRIDE_JOB_STATUS: ${{ inputs.OVERRIDE_JOB_STATUS }}
EXTRA_MESSAGE: ${{ inputs.extra_message }}
run: |
if [ "${OVERRIDE_JOB_STATUS}" ]; then
JOB_STATUS="${OVERRIDE_JOB_STATUS}"
fi
if [ "${JOB_STATUS}" == "success" ] ; then
emoji="🦾"
elif [ "${JOB_STATUS}" == "cancelled" ] ; then
emoji="✋"
elif [ "${JOB_STATUS}" == "failure" ] ; then
emoji="💥"
else
emoji="❔"
fi
if [ -z "$PROJECT" ]; then
PROJECT=${REPOSITORY##*\/}
fi
if [ "${NOTIF_TYPE}" == "message" ]; then
SLACK_TEXT_MESSAGE=$(cat <<EOF
*$SLACK_TEXT_MESSAGE :${emoji}*
Conclusion: ${JOB_STATUS}
EOF)
elif [ "${NOTIF_TYPE}" == "release-start" ]; then
SLACK_TEXT_MESSAGE="*Starting release (${JOB_ID}) of $PROJECT ${REF_NAME}*"
elif [ "${NOTIF_TYPE}" == "release-finish" ]; then
SLACK_TEXT_MESSAGE=$(cat <<EOF
*Release (${JOB_ID}) of $PROJECT ${REF_NAME} finished ${emoji}*
Conclusion: ${JOB_STATUS}
${EXTRA_MESSAGE}
EOF
)
elif [ "${NOTIF_TYPE}" == "deploy-start" ]; then
SLACK_TEXT_MESSAGE="*Starting deployment (${JOB_ID}) of $PROJECT*"
elif [ "${NOTIF_TYPE}" == "deploy-finish" ]; then
if [ -d .git ]; then
SLACK_TEXT_MESSAGE=$(cat <<EOF
*Deployment ($JOB_ID) of $PROJECT finished ${emoji}*
Conclusion: ${JOB_STATUS}
\`\`\`
$(git log -1 | sed -e 's/```/~~~/g')
\`\`\`
${EXTRA_MESSAGE}
EOF
)
else
SLACK_TEXT_MESSAGE=$(cat <<EOF
*Deployment ($JOB_ID) of $PROJECT finished ${emoji}*
Conclusion: ${JOB_STATUS}
${EXTRA_MESSAGE}
EOF
)
fi
else
echo "Unsupported notification type: ${NOTIF_TYPE}"
exit 1
fi
# Escape newline/quote/... with jq
SLACK_TEXT_MESSAGE=$(echo "$SLACK_TEXT_MESSAGE" | jq -Rsa .)
echo "SLACK_TEXT_MESSAGE=${SLACK_TEXT_MESSAGE}" >> "$GITHUB_OUTPUT"
run: python generate_slack_payload.py

- uses: slackapi/[email protected]
with:
# yamllint disable rule:line-length
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ${{ steps.report-status.outputs.SLACK_TEXT_MESSAGE }}
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Job log",
"emoji": true
},
"url": "${{ steps.fetch-check-run.outputs.job_url }}"
}
}
]
}
# yamllint enable rule:line-length
payload: ${{ steps.report-status.outputs.SLACK_PAYLOAD }}
env:
SLACK_WEBHOOK_URL: ${{ inputs.SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK


- uses: slackapi/[email protected]
if: ${{ job.status == 'failure' && inputs.FAILURE_ONLY_SLACK_WEBHOOK_URL != '' }}
with:
# yamllint disable rule:line-length
payload: |
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ${{ steps.report-status.outputs.SLACK_TEXT_MESSAGE }}
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Job log",
"emoji": true
},
"url": "${{ steps.fetch-check-run.outputs.job_url }}"
}
}
]
}
# yamllint enable rule:line-length
payload: ${{ steps.report-status.outputs.SLACK_PAYLOAD }}
env:
SLACK_WEBHOOK_URL: ${{ inputs.FAILURE_ONLY_SLACK_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
76 changes: 76 additions & 0 deletions generate_slack_payload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import os
import subprocess
import json
import sys

EMOJIS = {
"success": "🦾",
"cancelled": "✋",
"failure": "💥",
}

GITHUB_OUTPUT = os.environ["GITHUB_OUTPUT"]

repository = os.environ["REPOSITORY"]

project = os.environ["PROJECT"] or repository.split("/")[-1]

job_id = os.environ["JOB_ID"]
job_url = os.environ["JOB_URL"]
ref_name = os.environ["REF_NAME"]

notif_type = os.environ["NOTIF_TYPE"]

extra_message = os.environ["EXTRA_MESSAGE"]

job_status = os.environ["OVERRIDE_JOB_STATUS"] or os.environ["JOB_STATUS"]

emoji = EMOJIS.get(job_status, "❔")

if notif_type == "message":
slack_text_message = (
f"*{os.environ['SLACK_TEXT_MESSAGE']} :{emoji}*\nConclusion: {job_status}"
)

elif notif_type == "release-start":
slack_text_message = f"*Starting release ({job_id}) of {project} {ref_name}*"

elif notif_type == "release-finish":
slack_text_message = f"*Release ({job_id}) of {project} {ref_name} finished {emoji}*\n Conclusion: {job_status}\n {extra_message}"

elif notif_type == "deploy-start":
slack_text_message = f"*Starting deployment ({job_id}) of {project}*"

elif notif_type == "deploy-finish":
if os.path.isdir(".git"):
p = subprocess.run(
["git", "log", "-1", "--pretty=format:%s"], capture_output=True, check=True
)
commit = p.stdout.decode()
escaped_commit = f"```\n{commit.replace('```', '~~~')}```"
extra_message = f"{escaped_commit}\n{extra_message}"

slack_text_message = f"*Deployment ({job_id}) of {project} finished {emoji}*\nConclusion: {job_status}\n{extra_message}"
else:
print(f"Unsupported notification type: {notif_type}")
sys.exit(1)


slack_payload = {
"blocks": [
{
"type": "section",
"text": {"type": "mrkdwn", "text": slack_text_message},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": "Job log", "emoji": True},
"url": job_url,
},
}
]
}

compacted_slack_payload = json.dumps(slack_payload, separators=(",", ":"))

with open(GITHUB_OUTPUT, "w") as f:
f.write(f"SLACK_PAYLOAD={compacted_slack_payload}\n")

0 comments on commit dd422a4

Please sign in to comment.