-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmain.py
188 lines (152 loc) · 5.04 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
A Github Action to send Github Actions workflow status notifications to Slack.
Main module for the app.
"""
import json
import os
import requests
from dotenv import load_dotenv
def action_color(status):
"""
Get a action color based on the workflow status.
"""
if status == 'success':
return 'good'
elif status == 'failure':
return 'danger'
else:
return 'warning'
def action_status(status):
"""
Get a transformed status based on the workflow status.
"""
if status == 'success':
return 'passed'
elif status == 'failure':
return 'failed'
else:
return 'passed with warnings'
def action_emoji(status):
"""
Get an emoji based on the workflow status.
"""
if status == 'success':
return ':heavy_check_mark:'
elif status == 'failure':
return ':x:'
else:
return ':large_orange_diamond:'
def get_workflow_url(inputs):
"""
Get Workflow URL responsible for the Action run.
"""
repo = os.getenv('GITHUB_REPOSITORY')
token = inputs['token']
url = f'https://api.github.com/repos/{repo}/actions/workflows'
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': f'token {token}',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
workflows = response.json()['workflows']
for workflow in workflows:
if workflow['name'] == os.getenv('GITHUB_WORKFLOW'):
return workflow['html_url']
return ''
def construct_payload(inputs):
"""
Creates a message payload which can be sent to Slack.
"""
# derived from workflow environment
workflow = os.getenv('GITHUB_WORKFLOW')
repo = os.getenv('GITHUB_REPOSITORY')
branch = os.getenv('GITHUB_REF')
commit_sha = os.getenv('GITHUB_SHA')[:7]
run_id = os.getenv('GITHUB_RUN_ID')
job_id = os.getenv('GITHUB_JOB')
# derived from action inputs
job_status = inputs['job_status']
message = inputs['message_format']
title = inputs['notification_title']
footer = inputs['footer']
mention_users = inputs['mention_users']
mention_users_when = inputs['mention_users_when']
mention_groups = inputs['mention_groups']
mention_groups_when = inputs['mention_groups_when']
# self constructed
patterns = dict(
repo=repo,
branch=branch,
commit_sha=commit_sha,
commit_url=f'https://github.com/{repo}/commit/{commit_sha}',
repo_url=f'https://github.com/{repo}',
run_url=f'https://github.com/{repo}/actions/runs/{run_id}',
workflow=workflow,
workflow_url=get_workflow_url(inputs),
color=action_color(job_status),
status_message=action_status(job_status),
emoji=action_emoji(job_status),
)
# construct notification title
for k, v in patterns.items():
title = title.replace('{%s}' % k, v)
# construct the message
for k, v in patterns.items():
message = message.replace('{%s}' % k, v)
# add user mentions to the message
if job_status in mention_users_when and mention_users.strip() != '':
message += '\n'
for user in mention_users.split(','):
message = message + f'<@{user}> '
# add group mentions to the message
if job_status in mention_groups_when and mention_groups.strip() != '':
message += '\n'
for group in mention_groups.split(','):
message = message + f'<!subteam^{group}> '
# construct the footer
for k, v in patterns.items():
footer = footer.replace('{%s}' % k, v)
payload = {
'attachments': [
{
'text': message,
'fallback': title,
'pretext': title,
'color': patterns['color'],
'mrkdwn_in': ['text'],
'footer': footer,
}
]
}
return json.dumps(payload)
def notify_slack(payload, testing=False):
"""
Send a Slack notification.
"""
if not testing:
headers = {'Content-Type': 'application/json'}
url = os.getenv('SLACK_WEBHOOK_URL')
requests.post(url, data=payload, headers=headers)
def main(testing=False):
"""
Main function for the app.
"""
inputs = {
'job_status': os.getenv('INPUT_STATUS'),
'token': os.getenv('INPUT_TOKEN'),
'notification_title': os.getenv('INPUT_NOTIFICATION_TITLE'),
'message_format': os.getenv('INPUT_MESSAGE_FORMAT'),
'footer': os.getenv('INPUT_FOOTER'),
'notify_when': os.getenv('INPUT_NOTIFY_WHEN'),
'mention_users': os.getenv('INPUT_MENTION_USERS'),
'mention_users_when': os.getenv('INPUT_MENTION_USERS_WHEN'),
'mention_groups': os.getenv('INPUT_MENTION_GROUPS'),
'mention_groups_when': os.getenv('INPUT_MENTION_GROUPS_WHEN'),
}
payload = construct_payload(inputs)
if inputs['job_status'] in inputs['notify_when'] and not testing:
notify_slack(payload)
if __name__ == '__main__':
load_dotenv()
main()