-
Notifications
You must be signed in to change notification settings - Fork 20
/
gh_utils.py
69 lines (57 loc) · 1.77 KB
/
gh_utils.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
import json
import logging
import requests
import sys
from requests.auth import HTTPBasicAuth
# from gh_oauth_token import retrieve_token
from bot_config import API_BASE_URL, GH_USER_TOKEN, GH_USER
log = logging.getLogger(__name__)
def make_github_rest_api_call(api_path, method='GET', params=None):
"""Send API call to Github using a personal token.
Use this function to make API calls to the GitHub REST api
For example:
`GET` the current user
---
```py
me = make_github_rest_api_call('login')
```
`POST` to create a comment on a PR
---
```py
new_comment = make_github_rest_api_call(
'repos/my_org/my_repo/issues/31/comments',
'POST', {
'body': "Hello there, thanks for creating a new Pull Request!"
}
)
```
"""
# token = retrieve_token()
token = GH_USER_TOKEN
# Required headers.
headers = {'Accept': 'application/vnd.github.antiope-preview+json',
'Content-Type': 'application/json',
# 'Authorization': f'Bearer {token}'
}
# API url
url = f'{API_BASE_URL}/{api_path}'
log.info(
f'sending {method.upper()} request to {url} w/ data {json.dumps(params)}')
try:
if method.upper() == 'POST':
response = requests.post(
url,
headers=headers,
data=json.dumps(params),
auth=HTTPBasicAuth(GH_USER, GH_USER_TOKEN) # basic auth
)
elif method.upper() == 'GET':
response = requests.get(
url,
headers=headers,
auth=HTTPBasicAuth(GH_USER, GH_USER_TOKEN) # basic auth
)
else:
raise Exception('Invalid Request Method.')
except:
log.exception("Could not make a successful API call to GitHub.")