-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
79 lines (75 loc) · 2.59 KB
/
main.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
77
78
79
#!/usr/local/bin/python3
import os
import json
import urllib3
from datetime import datetime
slack_webhook_url = os.environ.get("SLACK_WEBHOOK_URL")
service_name = os.getenv("SERVICE_NAME")
branch = os.getenv("GITHUB_REF").rsplit("/", 1)[1]
workflow = os.getenv("GITHUB_WORKFLOW")
repository = os.getenv("GITHUB_REPOSITORY")
repository_owner = os.getenv("GITHUB_REPOSITORY_OWNER")
server_url = os.getenv("GITHUB_SERVER_URL")
event_type = os.getenv("GITHUB_EVENT_NAME")
event_path = os.getenv("GITHUB_EVENT_PATH")
sha = os.getenv("GITHUB_SHA")
actor = os.getenv("GITHUB_ACTOR")
event_payload = json.load(open(event_path))
message = {
"text": f":rocket: Running *{workflow}* on *{repository}/{branch}* :rocket:",
"attachments": [
{
"color": "warning",
"fields": [
{
"title": "Service Name",
"value": service_name,
"short": True
},
{
"title": "Repository/Branch",
"value": f"<{server_url}/{repository}/tree/{branch}|{repository}/{branch}>",
"short": True
},
{
"title": "Author",
"value": f"<{server_url}/{actor}|{actor}>",
"short": True
},
{
"title": "Event Type",
"value": event_type,
"short": True
},
{
"title": "Action URL",
"value": f"<{server_url}/{repository}/commit/{sha}/checks|{workflow}>",
"short": True
},
{
"title": "Commit",
"value": f"<{server_url}/{repository}/commit/{sha}|{sha[0:6]}>",
"short": True
},
{
"title": "Commit Message",
"value": '```' + event_payload["commits"][-1]["message"] + '```' if event_payload.get("commits") else '',
"short": False
}
],
"footer": server_url.replace("https://", "") + "/" + repository_owner,
"footer_icon": server_url + "/" + repository_owner + ".png?size=32",
"ts": datetime.now().timestamp()
}
]
}
pool = urllib3.PoolManager()
try:
pool.request(
"POST",
slack_webhook_url,
body=json.dumps(message).encode("utf-8"),
headers={"Content-Type": "application/json"}
)
except urllib3.exceptions.HTTPError:
pass