-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_api.py
78 lines (66 loc) · 2.54 KB
/
github_api.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
import requests
import json
import os
import sys
def parse_creds(path):
if not os.path.exists(path):
print(f"ERROR: path `{path}` not found")
sys.exit(1)
try:
with open(path,"r") as f:
json_content = json.load(f)
return json_content
except Exception as e:
print()
print(f"ERROR: Failed reading json `{path}`")
print(e)
print(e.args)
print()
sys.exit(1)
def validate_creds_existence(cred_file_path,creds_dct):
try:
assert "user" in creds_dct , "ERROR: " + f"`user` key must be present in `{cred_file_path}`"
assert creds_dct["user"] != "" , "ERROR: " + "`user` value cannot be ''"
assert "repo" in creds_dct , "ERROR: " + f"`repo` key must be present in `{cred_file_path}`"
assert creds_dct["repo"] != "" , "ERROR: " + "`repo` value cannot be ''"
assert "auth_token" in creds_dct , "ERROR: " + f"`auth_token` key must be present in `{cred_file_path}`"
assert creds_dct["auth_token"] != "" , "ERROR: " + "`auth_token` value cannot be ''"
except Exception as e:
print(e)
sys.exit(1)
def create_gh_issue(creds_dct,todo):
user = creds_dct["user"]
repo = creds_dct["repo"]
auth_token = creds_dct["auth_token"]
title = todo["title"]
body = todo["body"] if "body" in todo else ""
typ = todo["type"] if "type" in todo else "todo"
header_con = {"Authorization" : f"token {auth_token.strip()}"}
data = {
"owner": user,
"repo": repo,
"title": title,
"body": body,
"labels": [typ]
}
url = f"https://api.github.com/repos/{user}/{repo}/issues"
try:
r = requests.post(url,headers=header_con,data=json.dumps(data))
res_dct = json.loads(r.text)
if r.status_code != 201:
print()
print(f"ERROR: {res_dct['message']}")
print(res_dct)
if "errors" in res_dct:
print(res_dct["errors"])
print()
else:
print(f"Issue {res_dct['number']}: `{title}` created successfully")
except Exception as e:
print(e)
print(e.args)
def gh_main_runner(cred_file_path="creds.json",todo={}):
creds_dct = parse_creds(cred_file_path)
validate_creds_existence(cred_file_path=cred_file_path,creds_dct=creds_dct)
assert todo["title"] != "" , "Todo title cannot be blank"
create_gh_issue(creds_dct=creds_dct,todo=todo)