1
|
|
|
import attr |
2
|
|
|
import requests |
3
|
|
|
import uritemplate |
4
|
|
|
from pathlib import Path |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
EXT_TO_MIME_TYPE = { |
8
|
|
|
'.gz': 'application/x-gzip', |
9
|
|
|
'.whl': 'application/zip', |
10
|
|
|
'.zip': 'application/zip', |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
@attr.s |
15
|
|
|
class GitHub(object): |
16
|
|
|
ISSUE_ENDPOINT = ( |
17
|
|
|
'https://api.github.com/repos{/owner}{/repo}/issues{/number}' |
18
|
|
|
) |
19
|
|
|
LABELS_ENDPOINT = ( |
20
|
|
|
'https://api.github.com/repos{/owner}{/repo}/labels' |
21
|
|
|
) |
22
|
|
|
RELEASES_ENDPOINT = ( |
23
|
|
|
'https://api.github.com/repos{/owner}{/repo}/releases' |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
repository = attr.ib() |
27
|
|
|
|
28
|
|
|
@property |
29
|
|
|
def owner(self): |
30
|
|
|
return self.repository.owner |
31
|
|
|
|
32
|
|
|
@property |
33
|
|
|
def repo(self): |
34
|
|
|
return self.repository.repo |
35
|
|
|
|
36
|
|
|
@property |
37
|
|
|
def auth_token(self): |
38
|
|
|
return self.repository.auth_token |
39
|
|
|
|
40
|
|
|
@property |
41
|
|
|
def headers(self): |
42
|
|
|
# TODO: requests.Session |
43
|
|
|
return { |
44
|
|
|
'Authorization': 'token {}'.format(self.auth_token) |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
def pull_request(self, pr_num): |
48
|
|
|
pull_request_api_url = uritemplate.expand( |
49
|
|
|
self.ISSUE_ENDPOINT, |
50
|
|
|
dict( |
51
|
|
|
owner=self.owner, |
52
|
|
|
repo=self.repo, |
53
|
|
|
number=pr_num |
54
|
|
|
), |
55
|
|
|
) |
56
|
|
|
|
57
|
|
|
return requests.get( |
58
|
|
|
pull_request_api_url, |
59
|
|
|
headers=self.headers, |
60
|
|
|
).json() |
61
|
|
|
|
62
|
|
|
def labels(self): |
63
|
|
|
labels_api_url = uritemplate.expand( |
64
|
|
|
self.LABELS_ENDPOINT, |
65
|
|
|
dict( |
66
|
|
|
owner=self.owner, |
67
|
|
|
repo=self.repo, |
68
|
|
|
), |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
return requests.get( |
72
|
|
|
labels_api_url, |
73
|
|
|
headers=self.headers, |
74
|
|
|
).json() |
75
|
|
|
|
76
|
|
|
def create_release(self, release, uploads=None): |
77
|
|
|
params = { |
78
|
|
|
'tag_name': release.version, |
79
|
|
|
'name': release.name, |
80
|
|
|
'body': release.description, |
81
|
|
|
# 'prerelease': True, |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
releases_api_url = uritemplate.expand( |
85
|
|
|
self.RELEASES_ENDPOINT, |
86
|
|
|
dict( |
87
|
|
|
owner=self.owner, |
88
|
|
|
repo=self.repo, |
89
|
|
|
) |
90
|
|
|
) |
91
|
|
|
|
92
|
|
|
response = requests.post( |
93
|
|
|
releases_api_url, |
94
|
|
|
headers=self.headers, |
95
|
|
|
json=params, |
96
|
|
|
).json() |
97
|
|
|
|
98
|
|
|
upload_url = response['upload_url'] |
99
|
|
|
upload_responses = ( |
100
|
|
|
[self.create_upload(upload_url, Path(upload)) for upload in uploads] |
101
|
|
|
if uploads |
102
|
|
|
else [] |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
return response, upload_responses |
106
|
|
|
|
107
|
|
|
def create_upload(self, upload_url, upload_path): |
108
|
|
|
requests.post( |
109
|
|
|
uritemplate.expand( |
110
|
|
|
upload_url, |
111
|
|
|
{'name': upload_path.name}, |
112
|
|
|
), |
113
|
|
|
headers=dict(**self.headers, **{ |
114
|
|
|
'content-type': EXT_TO_MIME_TYPE[upload_path.ext], |
115
|
|
|
}), |
116
|
|
|
data=upload_path.read_bytes(), |
117
|
|
|
verify=False, |
118
|
|
|
) |
119
|
|
|
|