/
github.py
39 lines (35 loc) · 1.51 KB
/
github.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
import urllib.parse
import json
from collections import defaultdict
import urllib.request
from typing import Any, DefaultDict, Dict, Optional, Set
class GithubClient:
def __init__(self, api_token: Optional[str]) -> None:
self.api_token = api_token
def get(self, path: str) -> Any:
url = urllib.parse.urljoin("https://api.github.com/", path)
req = urllib.request.Request(url)
if self.api_token:
req.add_header("Authorization", f"token {self.api_token}")
return json.loads(urllib.request.urlopen(req).read())
def get_borg_eval_gist(self, pr: Dict[str, Any]) -> Optional[Dict[str, Set[str]]]:
packages_per_system: DefaultDict[str, Set[str]] = defaultdict(set)
statuses = self.get(pr["statuses_url"])
for status in statuses:
url = status.get("target_url", "")
if (
status["description"] == "^.^!"
and status["creator"]["login"] == "ofborg[bot]"
and url != ""
):
url = urllib.parse.urlparse(url)
raw_gist_url = (
f"https://gist.githubusercontent.com/GrahamcOfBorg{url.path}/raw/"
)
for line in urllib.request.urlopen(raw_gist_url):
if line == b"":
break
system, attribute = line.decode("utf-8").split()
packages_per_system[system].add(attribute)
return packages_per_system
return None