2121import requests
2222import json
2323import shutil
24+ import re
2425
2526from absl import logging
2627from requests .adapters import HTTPAdapter
3536REPO = 'firebase-cpp-sdk'
3637
3738BASE_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 )
3940logging .set_verbosity (logging .INFO )
4041
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+
4157def requests_retry_session (retries = RETRIES ,
4258 backoff_factor = BACKOFF ,
4359 status_forcelist = RETRY_STATUS ):
@@ -54,7 +70,7 @@ def requests_retry_session(retries=RETRIES,
5470
5571def create_issue (token , title , label , body ):
5672 """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'
5874 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
5975 data = {'title' : title , 'labels' : [label ], 'body' : body }
6076 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):
6480
6581def get_issue_body (token , issue_number ):
6682 """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 } '
6884 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
6985 with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
7086 logging .info ("get_issue_body: %s response: %s" , url , response )
@@ -73,7 +89,7 @@ def get_issue_body(token, issue_number):
7389
7490def update_issue (token , issue_number , data ):
7591 """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 } '
7793 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
7894 with requests_retry_session ().patch (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
7995 logging .info ("update_issue: %s response: %s" , url , response )
@@ -102,7 +118,7 @@ def search_issues_by_label(label):
102118
103119def list_comments (token , issue_number ):
104120 """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'
106122 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
107123 with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
108124 logging .info ("list_comments: %s response: %s" , url , response )
@@ -111,7 +127,7 @@ def list_comments(token, issue_number):
111127
112128def add_comment (token , issue_number , comment ):
113129 """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'
115131 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
116132 data = {'body' : comment }
117133 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):
120136
121137def update_comment (token , comment_id , comment ):
122138 """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 } '
124140 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
125141 data = {'body' : comment }
126142 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):
129145
130146def delete_comment (token , comment_id ):
131147 """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 } '
133149 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
134150 with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
135151 logging .info ("delete_comment: %s response: %s" , url , response )
136152
137153
138154def add_label (token , issue_number , label ):
139155 """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'
141157 headers = {}
142158 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
143159 data = [label ]
@@ -147,15 +163,15 @@ def add_label(token, issue_number, label):
147163
148164def delete_label (token , issue_number , label ):
149165 """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 } '
151167 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
152168 with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
153169 logging .info ("delete_label: %s response: %s" , url , response )
154170
155171
156172def list_artifacts (token , run_id ):
157173 """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'
159175 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
160176 with requests_retry_session ().get (url , headers = headers , timeout = TIMEOUT ) as response :
161177 logging .info ("list_artifacts: %s response: %s" , url , response )
@@ -164,7 +180,7 @@ def list_artifacts(token, run_id):
164180
165181def download_artifact (token , artifact_id , output_path ):
166182 """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'
168184 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
169185 with requests .get (url , headers = headers , stream = True , timeout = TIMEOUT ) as response :
170186 logging .info ("download_artifact: %s response: %s" , url , response )
@@ -174,17 +190,18 @@ def download_artifact(token, artifact_id, output_path):
174190
175191def dismiss_review (token , pull_number , review_id , message ):
176192 """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'
178194 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
179195 data = {'message' : message }
180196 with requests_retry_session ().put (url , headers = headers , data = json .dumps (data ),
181197 stream = True , timeout = TIMEOUT ) as response :
182198 logging .info ("dismiss_review: %s response: %s" , url , response )
183199 return response .json ()
184200
201+
185202def get_reviews (token , pull_number ):
186203 """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'
188205 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
189206 page = 1
190207 per_page = 100
@@ -203,19 +220,32 @@ def get_reviews(token, pull_number):
203220 return results
204221
205222
206- def workflow_dispatch (token , workflow_id , ref , inputs ):
223+ def create_workflow_dispatch (token , workflow_id , ref , inputs ):
207224 """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'
209226 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
210227 data = {'ref' : ref , 'inputs' : inputs }
211228 with requests .post (url , headers = headers , data = json .dumps (data ),
212229 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 ()
214244
215245
216246def create_pull_request (token , head , base , title , body , maintainer_can_modify ):
217247 """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'
219249 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
220250 data = {'head' : head , 'base' : base , 'title' : title , 'body' : body ,
221251 'maintainer_can_modify' : maintainer_can_modify }
@@ -224,9 +254,10 @@ def create_pull_request(token, head, base, title, body, maintainer_can_modify):
224254 logging .info ("create_pull_request: %s response: %s" , head , response )
225255 return True if response .status_code == 201 else False
226256
257+
227258def list_pull_requests (token , state , head , base ):
228259 """https://docs.github.com/en/rest/reference/pulls#list-pull-requests"""
229- url = f'{ FIREBASE_URL } /pulls'
260+ url = f'{ GITHUB_API_URL } /pulls'
230261 headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
231262 page = 1
232263 per_page = 100
0 commit comments