-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
91 lines (75 loc) · 2.72 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
"""
Interface with the Github API
"""
import requests
import json
from .utils import filter_by_claimed
class API(object):
def __init__(self, token=None, useragent="GHI Assist"):
self.token = token
self.useragent = useragent
def _call(self, api_url, content=None, method="PUT"):
"""
Convenience method which calls the Github API with default arguments.
Args:
api_url: URL of API endpoint.
content (optional): dictionary of content.
method (optional): HTTP method. Defaults to "PUT".
Returns:
The response object.
"""
headers = {
'User-Agent': self.useragent,
'Authorization': "token %s" % self.token,
}
response = requests.request(method, api_url, headers=headers, data=json.dumps(content))
return response.json()
def assign_issue(self, issue_url=None, assignee=None):
"""
Assigns an issue to the given user.
Args:
issue_url: API endpoint for this issue.
assignee: String with the user's login username.
"""
return self._call(issue_url, content={"assignee": assignee}, method="PATCH")
def label_claimed(self, issue_url=None, labels=None):
"""
Replace the list of labels if we've changed the issue's claimed status
"""
new_labels, replace = filter_by_claimed(labels, claimed=True)
if replace:
return self._call("%s/labels" % issue_url, content=new_labels)
def issue(self, issue_url=None):
"""
Get issue data.
Args:
issue_url: url for the issue we're getting data for.
Returns:
The issue data as a dictionary.
"""
return self._call(issue_url, method="GET")
def add_labels(self, issue_url, labels=None):
"""
Add issue labels (while keeping existing ones).
Args:
issue_url: API endpoint for this issue.
labels: list of labels to add.
"""
if len(labels) > 0:
return self._call("%s/labels" % issue_url, content=labels, method="POST")
def replace_labels(self, issue_url=None, labels=None):
"""
Replace issue labels.
Args:
issue_url: API endpoint for this issue. Taken from previous API response.
labels: list of labels to use.
"""
if len(labels) > 0:
return self._call("%s/labels" % issue_url, content=labels)
def get_repo_labels(self, labels_url=None):
"""
Get labels for a repository.
Args:
labels_url: API endpoint for labels for this repository
"""
return self._call(labels_url, method="GET")