21
21
import requests
22
22
import json
23
23
import shutil
24
+ import re
24
25
25
26
from absl import logging
26
27
from requests .adapters import HTTPAdapter
35
36
REPO = 'firebase-cpp-sdk'
36
37
37
38
BASE_URL = 'https://api.github.com'
38
- FIREBASE_URL = '%s/repos/%s/%s' % (BASE_URL , OWNER , REPO )
39
+ GITHUB_API_URL = '%s/repos/%s/%s' % (BASE_URL , OWNER , REPO )
39
40
logging .set_verbosity (logging .INFO )
40
41
42
+
43
+ def set_repo_url (repo ):
44
+ match = re .match (r'https://github\.com/([^/]+)/([^/.]+)' , repo )
45
+ if not match :
46
+ logging .info ('Error, only pattern https://github.com/\{repo_owner\}/\{repo_name\} are allowed.' )
47
+ return False
48
+
49
+ (repo_owner , repo_name ) = match .groups ()
50
+ global OWNER , REPO , GITHUB_API_URL
51
+ OWNER = repo_owner
52
+ REPO = repo_name
53
+ GITHUB_API_URL = '%s/repos/%s/%s' % (BASE_URL , OWNER , REPO )
54
+ return True
55
+
56
+
41
57
def requests_retry_session (retries = RETRIES ,
42
58
backoff_factor = BACKOFF ,
43
59
status_forcelist = RETRY_STATUS ):
@@ -54,7 +70,7 @@ def requests_retry_session(retries=RETRIES,
54
70
55
71
def create_issue (token , title , label , body ):
56
72
"""Create an issue: https://docs.github.com/en/rest/reference/issues#create-an-issue"""
57
- url = f'{ FIREBASE_URL } /issues'
73
+ url = f'{ GITHUB_API_URL } /issues'
58
74
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
59
75
data = {'title' : title , 'labels' : [label ], 'body' : body }
60
76
with requests .post (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
@@ -64,7 +80,7 @@ def create_issue(token, title, label, body):
64
80
65
81
def get_issue_body (token , issue_number ):
66
82
"""https://docs.github.com/en/rest/reference/issues#get-an-issue-comment"""
67
- url = f'{ FIREBASE_URL } /issues/{ issue_number } '
83
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } '
68
84
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
69
85
with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
70
86
logging .info ("get_issue_body: %s response: %s" , url , response )
@@ -73,7 +89,7 @@ def get_issue_body(token, issue_number):
73
89
74
90
def update_issue (token , issue_number , data ):
75
91
"""Update an issue: https://docs.github.com/en/rest/reference/issues#update-an-issue"""
76
- url = f'{ FIREBASE_URL } /issues/{ issue_number } '
92
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } '
77
93
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
78
94
with requests_retry_session ().patch (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
79
95
logging .info ("update_issue: %s response: %s" , url , response )
@@ -102,7 +118,7 @@ def search_issues_by_label(label):
102
118
103
119
def list_comments (token , issue_number ):
104
120
"""https://docs.github.com/en/rest/reference/issues#list-issue-comments"""
105
- url = f'{ FIREBASE_URL } /issues/{ issue_number } /comments'
121
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } /comments'
106
122
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
107
123
with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
108
124
logging .info ("list_comments: %s response: %s" , url , response )
@@ -111,7 +127,7 @@ def list_comments(token, issue_number):
111
127
112
128
def add_comment (token , issue_number , comment ):
113
129
"""https://docs.github.com/en/rest/reference/issues#create-an-issue-comment"""
114
- url = f'{ FIREBASE_URL } /issues/{ issue_number } /comments'
130
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } /comments'
115
131
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
116
132
data = {'body' : comment }
117
133
with requests .post (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
@@ -120,7 +136,7 @@ def add_comment(token, issue_number, comment):
120
136
121
137
def update_comment (token , comment_id , comment ):
122
138
"""https://docs.github.com/en/rest/reference/issues#update-an-issue-comment"""
123
- url = f'{ FIREBASE_URL } /issues/comments/{ comment_id } '
139
+ url = f'{ GITHUB_API_URL } /issues/comments/{ comment_id } '
124
140
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
125
141
data = {'body' : comment }
126
142
with requests_retry_session ().patch (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
@@ -129,15 +145,15 @@ def update_comment(token, comment_id, comment):
129
145
130
146
def delete_comment (token , comment_id ):
131
147
"""https://docs.github.com/en/rest/reference/issues#delete-an-issue-comment"""
132
- url = f'{ FIREBASE_URL } /issues/comments/{ comment_id } '
148
+ url = f'{ GITHUB_API_URL } /issues/comments/{ comment_id } '
133
149
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
134
150
with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
135
151
logging .info ("delete_comment: %s response: %s" , url , response )
136
152
137
153
138
154
def add_label (token , issue_number , label ):
139
155
"""https://docs.github.com/en/rest/reference/issues#add-labels-to-an-issue"""
140
- url = f'{ FIREBASE_URL } /issues/{ issue_number } /labels'
156
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } /labels'
141
157
headers = {}
142
158
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
143
159
data = [label ]
@@ -147,15 +163,15 @@ def add_label(token, issue_number, label):
147
163
148
164
def delete_label (token , issue_number , label ):
149
165
"""https://docs.github.com/en/rest/reference/issues#delete-a-label"""
150
- url = f'{ FIREBASE_URL } /issues/{ issue_number } /labels/{ label } '
166
+ url = f'{ GITHUB_API_URL } /issues/{ issue_number } /labels/{ label } '
151
167
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
152
168
with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
153
169
logging .info ("delete_label: %s response: %s" , url , response )
154
170
155
171
156
172
def list_artifacts (token , run_id ):
157
173
"""https://docs.github.com/en/rest/reference/actions#list-workflow-run-artifacts"""
158
- url = f'{ FIREBASE_URL } /actions/runs/{ run_id } /artifacts'
174
+ url = f'{ GITHUB_API_URL } /actions/runs/{ run_id } /artifacts'
159
175
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
160
176
with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
161
177
logging .info ("list_artifacts: %s response: %s" , url , response )
@@ -164,7 +180,7 @@ def list_artifacts(token, run_id):
164
180
165
181
def download_artifact (token , artifact_id , output_path ):
166
182
"""https://docs.github.com/en/rest/reference/actions#download-an-artifact"""
167
- url = f'{ FIREBASE_URL } /actions/artifacts/{ artifact_id } /zip'
183
+ url = f'{ GITHUB_API_URL } /actions/artifacts/{ artifact_id } /zip'
168
184
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
169
185
with requests .get (url , headers = headers , stream = True , timeout = TIMEOUT ) as response :
170
186
logging .info ("download_artifact: %s response: %s" , url , response )
@@ -174,17 +190,18 @@ def download_artifact(token, artifact_id, output_path):
174
190
175
191
def dismiss_review (token , pull_number , review_id , message ):
176
192
"""https://docs.github.com/en/rest/reference/pulls#dismiss-a-review-for-a-pull-request"""
177
- url = f'{ FIREBASE_URL } /pulls/{ pull_number } /reviews/{ review_id } /dismissals'
193
+ url = f'{ GITHUB_API_URL } /pulls/{ pull_number } /reviews/{ review_id } /dismissals'
178
194
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
179
195
data = {'message' : message }
180
196
with requests_retry_session ().put (url , headers = headers , data = json .dumps (data ),
181
197
stream = True , timeout = TIMEOUT ) as response :
182
198
logging .info ("dismiss_review: %s response: %s" , url , response )
183
199
return response .json ()
184
200
201
+
185
202
def get_reviews (token , pull_number ):
186
203
"""https://docs.github.com/en/rest/reference/pulls#list-reviews-for-a-pull-request"""
187
- url = f'{ FIREBASE_URL } /pulls/{ pull_number } /reviews'
204
+ url = f'{ GITHUB_API_URL } /pulls/{ pull_number } /reviews'
188
205
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
189
206
page = 1
190
207
per_page = 100
@@ -203,19 +220,32 @@ def get_reviews(token, pull_number):
203
220
return results
204
221
205
222
206
- def workflow_dispatch (token , workflow_id , ref , inputs ):
223
+ def create_workflow_dispatch (token , workflow_id , ref , inputs ):
207
224
"""https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event"""
208
- url = f'{ FIREBASE_URL } /actions/workflows/{ workflow_id } /dispatches'
225
+ url = f'{ GITHUB_API_URL } /actions/workflows/{ workflow_id } /dispatches'
209
226
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
210
227
data = {'ref' : ref , 'inputs' : inputs }
211
228
with requests .post (url , headers = headers , data = json .dumps (data ),
212
229
stream = True , timeout = TIMEOUT ) as response :
213
- logging .info ("workflow_dispatch: %s response: %s" , url , response )
230
+ logging .info ("create_workflow_dispatch: %s response: %s" , url , response )
231
+ # Response Status: 204 No Content
232
+ return True if response .status_code == 204 else False
233
+
234
+
235
+ def list_workflows (token , workflow_id , branch ):
236
+ """https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository"""
237
+ url = f'{ GITHUB_API_URL } /actions/workflows/{ workflow_id } /runs'
238
+ headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
239
+ data = {'event' : 'workflow_dispatch' , 'branch' : branch }
240
+ with requests .get (url , headers = headers , data = json .dumps (data ),
241
+ stream = True , timeout = TIMEOUT ) as response :
242
+ logging .info ("list_workflows: %s response: %s" , url , response )
243
+ return response .json ()
214
244
215
245
216
246
def create_pull_request (token , head , base , title , body , maintainer_can_modify ):
217
247
"""https://docs.github.com/en/rest/reference/pulls#create-a-pull-request"""
218
- url = f'{ FIREBASE_URL } /pulls'
248
+ url = f'{ GITHUB_API_URL } /pulls'
219
249
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
220
250
data = {'head' : head , 'base' : base , 'title' : title , 'body' : body ,
221
251
'maintainer_can_modify' : maintainer_can_modify }
@@ -224,9 +254,10 @@ def create_pull_request(token, head, base, title, body, maintainer_can_modify):
224
254
logging .info ("create_pull_request: %s response: %s" , head , response )
225
255
return True if response .status_code == 201 else False
226
256
257
+
227
258
def list_pull_requests (token , state , head , base ):
228
259
"""https://docs.github.com/en/rest/reference/pulls#list-pull-requests"""
229
- url = f'{ FIREBASE_URL } /pulls'
260
+ url = f'{ GITHUB_API_URL } /pulls'
230
261
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
231
262
page = 1
232
263
per_page = 100
0 commit comments