This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
generated from anoadragon453/nio-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
50 lines (39 loc) · 1.54 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
40
41
42
43
44
45
46
47
48
49
50
import requests
import json
from errors import BotException
import logging
logger = logging.getLogger(__name__)
class Github(object):
def __init__(self, repo_slug: str):
"""
Args:
repo_slug: The slug (user/repo_name) of the github repository
"""
# TODO: Add support for custom token
self.repo_slug = repo_slug
self.api_base = "https://api.github.com"
def get_info_for_issue_pr(self, num: int) -> dict:
"""Get the metadata of a github issue/PR
Args:
num: The issue/PR number
Returns:
dict[str, str]: Metadata about the issue/PR
Raises:
FileNotFoundError: The issue/PR was not found
"""
# Assume it's a PR. Query github's API
resp = requests.get(self.api_base + f"/repos/{self.repo_slug}/pulls/{num}")
if resp.status_code == 404 or not resp.content:
raise FileNotFoundError
# Load JSON
body = json.loads(resp.content)
if resp.status_code == 403:
# Check if this is a rate limit hit or an invalid token
if "message" in body:
logger.error(f"Rate-limit hit on {resp.url}. Consider using your own Github token.")
raise PermissionError("rate-limit hit")
logger.error(f"Forbidden on contacting {resp.url}. Check your access token.")
raise PermissionError("forbidden")
if resp.status_code != 200:
raise BotException(f"HTTP error ({resp.status_code})")
return body