-
Notifications
You must be signed in to change notification settings - Fork 3
/
set-status
executable file
·42 lines (32 loc) · 1.27 KB
/
set-status
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
#!/usr/bin/env python3
import os
import sys
import argparse
import urllib.request
import json
parser = argparse.ArgumentParser()
parser.add_argument("gh_token", type=str)
parser.add_argument("revision", type=str)
parser.add_argument("status", choices=["success", "pending", "failure", "error", "cancelled"])
if os.environ.get("GITHUB_ACTIONS", "false") != "true":
print("This command works only in Github Actions environment.\n", file=sys.stderr)
parser.print_help(file=sys.stderr)
exit(1)
args = parser.parse_args()
if args.status == "cancelled":
args.status = "error"
buildurl = "{GITHUB_SERVER_URL}/{GITHUB_REPOSITORY}/actions/runs/{GITHUB_RUN_ID}".format(**os.environ)
context = "{GITHUB_WORKFLOW} ({GITHUB_EVENT_NAME})".format(**os.environ)
data = {
"context": context,
"state": args.status,
"target_url": buildurl
}
jsondata = json.dumps(data).encode("utf-8")
url = f"https://api.github.com/repos/{os.environ['GITHUB_REPOSITORY']}/statuses/{args.revision}"
req = urllib.request.Request(url)
req.add_header("Authorization", f"Bearer {args.gh_token}")
req.add_header("Content-Type", "application/json; charset=utf-8")
req.add_header("Accept", "application/vnd.github.v3+json")
req.add_header("Content-Length", len(jsondata))
urllib.request.urlopen(req, jsondata)