-
Notifications
You must be signed in to change notification settings - Fork 0
/
PR-github.5m.py
132 lines (114 loc) Β· 4.48 KB
/
PR-github.5m.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
#!/opt/homebrew/bin/python3
# -*- coding: utf-8 -*-
import requests
import xml.etree.ElementTree as ET
import datetime
import time
from requests.auth import HTTPBasicAuth
import json
from base64 import b64encode
# ######## CONFIG ########################################################################################
CACHE_FILE="not/PR-github.cache"
GITHUB_APIKEY=""
AUTHOR=""
ORG=""
GITHUB_TEAMMEMBERS=["teamMember1", "teamMember2"]
###########################################################################################################
if not GITHUB_APIKEY or not AUTHOR or not ORG:
print("PR π")
print("---")
print("Missing variables. π")
exit(0)
CLOSED_MYKINSTA16_PRS=f"https://github.com/{ORG}/mykinsta16/pulls?q=is%3Apr+author:{AUTHOR}+is:closed"
CLOSED_BILLING_PRS=f"https://github.com/{ORG}/mk-billing-service/pulls?q=is%3Apr+author:{AUTHOR}+is:closed"
PR_SCRIPT_OUTPUT = []
PR_QUERY=f"?q=type:pr+is:open+author:{AUTHOR}+sort:updated-desc"
ASSIGNED_QUERY=f"?q=type:pr+is:open+assignee:{AUTHOR}+sort:updated-desc"
TEAM_REVIEW_QUERY=f"?q=is%3Apr+review-requested:{AUTHOR}+is:open+archived:false+{''.join(str(f'author:{x}+') for x in GITHUB_TEAMMEMBERS)}"
REVIEW_QUERY=f"?q=is%3Apr+review-requested:{AUTHOR}+is:open+archived:false"
def fallback():
with open(CACHE_FILE, "r") as f:
stored_feed = f.read()
if(len(stored_feed) > 0):
print(stored_feed)
else:
print ("No cache...")
# Authorization token: we need to base 64 encode it
# and then decode it to acsii as python 3 stores it as a byte string
def basic_auth(username, password):
token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii")
return f'Basic {token}'
def put(array, string):
array.append("%s" % (string))
def createRequest(url):
result = []
headers = {
'Accept': 'application/vnd.github.v3+json',
'Connection': 'keep-alive',
'Authorization': f"{basic_auth(GITHUB_APIKEY, 'x-oauth-basic')}",
'User-Agent': 'Python'
}
try:
response = requests.request("GET", url, headers=headers)
responseJSON = json.loads(response.text)
except:
print("PR π")
print("---")
print("Couldn't parse response. π")
print(f"url: {url}. π")
# print(f"message: {response.text}. π")
fallback()
exit(0)
for line in responseJSON['items']:
put(result,f"{line['title']} :: ({line['repository_url'].rsplit('/', 1)[-1]}) | href='{line['html_url']}'")
return result
def createdPRQuery():
url=f'https://api.github.com/search/issues{PR_QUERY}'
return createRequest(url)
def assignedQuery():
url=f'https://api.github.com/search/issues{ASSIGNED_QUERY}'
return createRequest(url)
def teamReviewQuery():
url=f'https://api.github.com/search/issues{TEAM_REVIEW_QUERY[:-1]}'
return createRequest(url)
def reviewQuery():
url=f'https://api.github.com/search/issues{REVIEW_QUERY}'
return createRequest(url)
PRArray = createdPRQuery()
assignedArray = assignedQuery()
teamReviewArray = teamReviewQuery()
reviewArray = reviewQuery()
PR_SCRIPT_OUTPUT = [ f"π±PR ({len(teamReviewArray)})" ]
put(PR_SCRIPT_OUTPUT, '---')
put(PR_SCRIPT_OUTPUT, f"Updated on: {time.ctime()}")
put(PR_SCRIPT_OUTPUT, f"Closed Mykinsta16 PRs |href={CLOSED_MYKINSTA16_PRS}")
put(PR_SCRIPT_OUTPUT, f"Closed billing PRs |href={CLOSED_BILLING_PRS}")
put(PR_SCRIPT_OUTPUT, 'Refresh... | refresh=true')
# ##### Created PRs #########################################
put(PR_SCRIPT_OUTPUT, "---")
put(PR_SCRIPT_OUTPUT, "π author:")
put(PR_SCRIPT_OUTPUT, "---")
PR_SCRIPT_OUTPUT += PRArray
# ##### Assigned PRs ########################################
put(PR_SCRIPT_OUTPUT, '---')
put(PR_SCRIPT_OUTPUT, f"assignee ({len(assignedArray)}): | href=https://github.com/issues/{ASSIGNED_QUERY}")
put(PR_SCRIPT_OUTPUT, "---")
PR_SCRIPT_OUTPUT += assignedArray
# ##### Waiting for teammate review ##################################
put(PR_SCRIPT_OUTPUT, '---')
put(PR_SCRIPT_OUTPUT, f"πͺ Waiting for team review ({len(teamReviewArray)}): | href=https://github.com/issues/{TEAM_REVIEW_QUERY[:-1]}")
put(PR_SCRIPT_OUTPUT, "---")
PR_SCRIPT_OUTPUT += teamReviewArray
# ##### Waiting for review ##################################
put(PR_SCRIPT_OUTPUT, '---')
put(PR_SCRIPT_OUTPUT, f"π Waiting for review ({len(reviewArray)}): | href=https://github.com/issues/{REVIEW_QUERY}")
put(PR_SCRIPT_OUTPUT, "---")
PR_SCRIPT_OUTPUT += reviewArray
content = '\n'.join(PR_SCRIPT_OUTPUT)
print(content)
with open(CACHE_FILE, "w+") as f:
f.write(content)
f.write("\n---\n")
f.write("cached\n")
f.write(time.ctime())
f.close()