-
Notifications
You must be signed in to change notification settings - Fork 13
/
get_installations.py
57 lines (45 loc) · 1.61 KB
/
get_installations.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
import os
import argparse
from datetime import datetime, timedelta
import requests
import jwt
def get_private_pem(file_path):
key = os.environ.get("PRIVATE_KEY", "")
if not key:
default_path = os.path.join(os.path.dirname(__file__), file_path)
if os.path.exists(default_path):
with open(default_path, "r", encoding="utf-8") as f:
key = f.readlines()
key = "".join(key)
return key
parser = argparse.ArgumentParser(description="Get Installations count of your App.")
parser.add_argument("app_id", help="app_id of Your apps")
parser.add_argument("--pem", default="", help="path to pem file")
if __name__ == "__main__":
url = "https://api.github.com/app/installations"
args = parser.parse_args()
pem_path = args.pem
if not pem_path:
path = os.path.abspath(os.path.dirname(__file__))
app_name = os.path.basename(path)
pem_path = app_name + ".pem"
if not os.path.exists(pem_path):
raise Exception("Pem file {} does not exist".format(pem_path))
utcnow = datetime.utcnow() + timedelta(seconds=-5)
duration = timedelta(seconds=10)
payload = {
"iat": utcnow,
"exp": utcnow + duration,
"iss": args.app_id
}
pem = get_private_pem(pem_path)
encoded = jwt.encode(payload, pem, "RS256")
headers = {
"Authorization": "Bearer " + encoded.decode("utf-8"),
"Accept": "application/vnd.github.machine-man-preview+json"
}
r = requests.get(url, headers=headers)
if r.ok:
print(len(r.json()))
else:
r.raise_for_status()