23
23
import shutil
24
24
25
25
from absl import logging
26
+ from requests .adapters import HTTPAdapter
27
+ from requests .packages .urllib3 .util .retry import Retry
28
+
29
+ RETRIES = 3
30
+ BACKOFF = 5
31
+ RETRY_STATUS = (403 , 500 , 502 , 504 )
32
+ TIMEOUT = 5
26
33
27
34
OWNER = 'firebase'
28
35
REPO = 'firebase-cpp-sdk'
31
38
FIREBASE_URL = '%s/repos/%s/%s' % (BASE_URL , OWNER , REPO )
32
39
logging .set_verbosity (logging .INFO )
33
40
41
+ def requests_retry_session (retries = RETRIES ,
42
+ backoff_factor = BACKOFF ,
43
+ status_forcelist = RETRY_STATUS ):
44
+ session = requests .Session ()
45
+ retry = Retry (total = retries ,
46
+ read = retries ,
47
+ connect = retries ,
48
+ backoff_factor = backoff_factor ,
49
+ status_forcelist = status_forcelist )
50
+ adapter = HTTPAdapter (max_retries = retry )
51
+ session .mount ('http://' , adapter )
52
+ session .mount ('https://' , adapter )
53
+ return session
54
+
34
55
def create_issue (token , title , label ):
35
56
"""Create an issue: https://docs.github.com/en/rest/reference/issues#create-an-issue"""
36
57
url = f'{ FIREBASE_URL } /issues'
37
58
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
38
59
data = {'title' : title , 'labels' : [label ]}
39
- with requests .post (url , headers = headers , data = json .dumps (data )) as response :
60
+ with requests .post (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
40
61
logging .info ("create_issue: %s response: %s" , url , response )
41
62
return response .json ()
42
63
@@ -45,7 +66,7 @@ def update_issue(token, issue_number, data):
45
66
"""Update an issue: https://docs.github.com/en/rest/reference/issues#update-an-issue"""
46
67
url = f'{ FIREBASE_URL } /issues/{ issue_number } '
47
68
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
48
- with requests .patch (url , headers = headers , data = json .dumps (data )) as response :
69
+ with requests_retry_session () .patch (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
49
70
logging .info ("update_issue: %s response: %s" , url , response )
50
71
51
72
@@ -65,16 +86,16 @@ def search_issues_by_label(label):
65
86
"""https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests"""
66
87
url = f'{ BASE_URL } /search/issues?q=repo:{ OWNER } /{ REPO } +label:"{ label } "+is:issue'
67
88
headers = {'Accept' : 'application/vnd.github.v3+json' }
68
- with requests .get (url , headers = headers ) as response :
89
+ with requests_retry_session () .get (url , headers = headers , timeout = TIMEOUT ) as response :
69
90
logging .info ("search_issues_by_label: %s response: %s" , url , response )
70
91
return response .json ()["items" ]
71
92
72
93
73
- def list_comments (issue_number ):
94
+ def list_comments (token , issue_number ):
74
95
"""https://docs.github.com/en/rest/reference/issues#list-issue-comments"""
75
96
url = f'{ FIREBASE_URL } /issues/{ issue_number } /comments'
76
- headers = {'Accept' : 'application/vnd.github.v3+json' }
77
- with requests .get (url , headers = headers ) as response :
97
+ headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
98
+ with requests_retry_session () .get (url , headers = headers , timeout = TIMEOUT ) as response :
78
99
logging .info ("list_comments: %s response: %s" , url , response )
79
100
return response .json ()
80
101
@@ -84,7 +105,7 @@ def add_comment(token, issue_number, comment):
84
105
url = f'{ FIREBASE_URL } /issues/{ issue_number } /comments'
85
106
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
86
107
data = {'body' : comment }
87
- with requests .post (url , headers = headers , data = json .dumps (data )) as response :
108
+ with requests .post (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
88
109
logging .info ("add_comment: %s response: %s" , url , response )
89
110
90
111
@@ -93,15 +114,15 @@ def update_comment(token, comment_id, comment):
93
114
url = f'{ FIREBASE_URL } /issues/comments/{ comment_id } '
94
115
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
95
116
data = {'body' : comment }
96
- with requests .patch (url , headers = headers , data = json .dumps (data )) as response :
117
+ with requests_retry_session () .patch (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
97
118
logging .info ("update_comment: %s response: %s" , url , response )
98
119
99
120
100
121
def delete_comment (token , comment_id ):
101
122
"""https://docs.github.com/en/rest/reference/issues#delete-an-issue-comment"""
102
123
url = f'{ FIREBASE_URL } /issues/comments/{ comment_id } '
103
124
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
104
- with requests .delete (url , headers = headers ) as response :
125
+ with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
105
126
logging .info ("delete_comment: %s response: %s" , url , response )
106
127
107
128
@@ -111,23 +132,23 @@ def add_label(token, issue_number, label):
111
132
headers = {}
112
133
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
113
134
data = [label ]
114
- with requests .post (url , headers = headers , data = json .dumps (data )) as response :
135
+ with requests .post (url , headers = headers , data = json .dumps (data ), timeout = TIMEOUT ) as response :
115
136
logging .info ("add_label: %s response: %s" , url , response )
116
137
117
138
118
139
def delete_label (token , issue_number , label ):
119
140
"""https://docs.github.com/en/rest/reference/issues#delete-a-label"""
120
141
url = f'{ FIREBASE_URL } /issues/{ issue_number } /labels/{ label } '
121
142
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
122
- with requests .delete (url , headers = headers ) as response :
143
+ with requests .delete (url , headers = headers , timeout = TIMEOUT ) as response :
123
144
logging .info ("delete_label: %s response: %s" , url , response )
124
145
125
146
126
147
def list_artifacts (token , run_id ):
127
148
"""https://docs.github.com/en/rest/reference/actions#list-workflow-run-artifacts"""
128
149
url = f'{ FIREBASE_URL } /actions/runs/{ run_id } /artifacts'
129
150
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
130
- with requests .get (url , headers = headers ) as response :
151
+ with requests_retry_session () .get (url , headers = headers , timeout = TIMEOUT ) as response :
131
152
logging .info ("list_artifacts: %s response: %s" , url , response )
132
153
return response .json ()["artifacts" ]
133
154
@@ -136,7 +157,7 @@ def download_artifact(token, artifact_id, output_path):
136
157
"""https://docs.github.com/en/rest/reference/actions#download-an-artifact"""
137
158
url = f'{ FIREBASE_URL } /actions/artifacts/{ artifact_id } /zip'
138
159
headers = {'Accept' : 'application/vnd.github.v3+json' , 'Authorization' : f'token { token } ' }
139
- with requests .get (url , headers = headers , stream = True ) as response :
160
+ with requests .get (url , headers = headers , stream = True , timeout = TIMEOUT ) as response :
140
161
logging .info ("download_artifact: %s response: %s" , url , response )
141
162
with open (output_path , 'wb' ) as file :
142
163
shutil .copyfileobj (response .raw , file )
0 commit comments