forked from ravsamhq/notify-slack-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (71 loc) · 2.26 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
import requests
import os
import json
import sys
def actionColor(status):
"""
Get a action color based on the workflow status.
"""
if status == 'success':
return 'good'
elif status == 'failure':
return 'danger'
return 'warning'
def actionStatus(status):
"""
Get a transformed status based on the workflow status.
"""
if status == 'success':
return 'passed'
elif status == 'failure':
return 'failed'
return 'passed with warnings'
def actionEmoji(status):
"""
Get an emoji based on the workflow status.
"""
if status == 'success':
return ':sunglasses:'
elif status == 'failure':
return ':worried:'
return ':zipper_mouth_face:'
def notify_slack(job_status, notify_when):
url = os.getenv('SLACK_WEBHOOK_URL')
workflow = os.getenv('GITHUB_WORKFLOW')
repo = os.getenv('GITHUB_REPOSITORY')
branch = os.getenv('GITHUB_REF')
commit = os.getenv('GITHUB_SHA')
commit_url = f'https://github.com/{repo}/commit/{commit}'
repo_url = f'https://github.com/{repo}/tree/{branch}'
color = actionColor(job_status)
status_message = actionStatus(job_status)
emoji = actionEmoji(job_status)
message = f'{emoji} {workflow} {status_message} in <{repo_url}|{repo}@{branch}> on <{commit_url}|{commit[:7]}>.'
payload = {
'attachments': [
{
'text': message,
'fallback': 'New Github Action Run',
'pretext': 'New Github Action Run',
'color': color,
'mrkdwn_in': ['text'],
'footer': 'Developed by <https://www.ravsam.in|RavSam>',
}
]
}
payload = json.dumps(payload)
headers = {'Content-Type': 'application/json'}
if notify_when is None:
notify_when = 'success,failure,warnings'
if job_status in notify_when and not testing:
requests.post(url, data=payload, headers=headers)
def main():
job_status = os.getenv('INPUT_STATUS')
notify_when = os.getenv('INPUT_NOTIFY_WHEN')
notify_slack(job_status, notify_when)
if __name__ == '__main__':
try:
testing = True if sys.argv[1] == '--test' else False
except IndexError as e:
testing = False
main()