-
Notifications
You must be signed in to change notification settings - Fork 8
/
notify.py
executable file
·44 lines (40 loc) · 1.68 KB
/
notify.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
import time
import hmac
import hashlib
import base64
import urllib
import requests
import os
def dingtalk_send(token, secret, json_data=""):
url = "https://oapi.dingtalk.com/robot/send"
ts = int(round(time.time() * 1000))
str = '{}\n{}'.format(ts, secret)
hmc = hmac.new(secret.encode('utf-8'), str.encode('utf-8'), digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmc))
endpoint = '{}?access_token={}×tamp={}&sign={}'.format(url, token, ts, sign)
headers = {'Content-Type': 'application/json'}
resp = requests.post(url=endpoint, headers=headers, json=json_data)
print(resp.text)
if __name__ == '__main__':
token = os.getenv('DINGTALK_ACCESS_TOKEN')
secret = os.getenv('DINGTALK_SECRET')
job_status = os.getenv('JOB_STATE')
github_ref = os.getenv('GITHUB_REF')
github_run_id = os.getenv('GITHUB_RUN_ID')
runner_os = os.getenv('RUNNER_OS')
github_repository = os.getenv('GITHUB_REPOSITORY')
github_workflow = os.getenv('GITHUB_WORKFLOW')
if job_status == 'success':
color = "#1ce43f"
elif job_status == 'cancelled':
color = "#c1c1c1"
else:
color = "#ff4a83"
message = {
"msgtype": "markdown",
"markdown": {
"title": github_workflow,
"text": "### " + github_workflow + " #" + github_run_id + "\n\n> status: **<font color=" + color + ">" + str(job_status).upper() + "</font>** \n\n> head ref: **" + github_ref + "** \n\n> runner os: **" + runner_os + "** \n\n> actions url: [" + github_repository + "](https://github.com/" + github_repository + "/actions)\n\n"
}
}
dingtalk_send(token, secret, json_data=message)