-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_slack_payload.py
76 lines (57 loc) · 2.16 KB
/
generate_slack_payload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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")