@@ -55,22 +55,40 @@ def get_pull_requests(self, updated_since):
55
55
url = '{}/search/issues?q=repo:{}/{}+is:pr+updated:>{}' .format (
56
56
self ._host , self ._org , self ._repo , window_start
57
57
)
58
+
59
+ logger .info (
60
+ 'Searching for pull requests updated since "{}"' .format (window_start )
61
+ )
62
+
58
63
data = request ('GET' , url )
64
+
59
65
if data ['incomplete_results' ]:
60
66
raise Exception ('Incomplete results' )
61
67
68
+ logger .info ('Found {} pull requests' .format (len (data ['items' ])))
69
+
62
70
return data ['items' ]
63
71
64
- def add_label (self , pull_request_number , name ):
72
+ def add_label (self , pull_request , name ):
73
+ number = pull_request ['number' ]
65
74
url = '{}/repos/{}/{}/issues/{}/labels' .format (
66
- self ._host , self ._org , self ._repo , pull_request_number
75
+ self ._host , self ._org , self ._repo , number
67
76
)
77
+
78
+ logger .info ('Adding label "{}" for pull request #{}"' .format (number , name ))
79
+
68
80
request ('POST' , url , {'labels' : [name ]})
69
81
82
+ def remove_label (self , pull_request , name ):
83
+ raise NotImplementedError ()
84
+
70
85
def create_deployment (self , ref ):
71
86
url = '{}/repos/{}/{}/deployments' .format (
72
87
self ._host , self ._org , self ._repo
73
88
)
89
+
90
+ logger .info ('Creating deployment for "{}"' .format (ref ))
91
+
74
92
request ('POST' , url , {'ref' : ref })
75
93
76
94
class Remote (object ):
@@ -108,12 +126,29 @@ def update_ref(self, refspec, revision):
108
126
raise NotImplementedError ()
109
127
110
128
def delete_ref (self , refspec ):
129
+ logger .info ('Deleting ref "{}"' .format (refspec ))
130
+
111
131
with self ._make_temp_repo () as temp_repo :
112
132
subprocess .check_call (
113
133
['git' , 'push' , self ._url , '--delete' , 'refs/{}' .format (refspec )],
114
134
cwd = temp_repo
115
135
)
116
136
137
+ def is_open (pull_request ):
138
+ return not pull_request ['closed_at' ]
139
+
140
+ def has_label (pull_request ):
141
+ for label in pull_request ['labels' ]:
142
+ if label ['name' ] == LABEL :
143
+ return True
144
+ return False
145
+
146
+ def should_be_mirrored (pull_request ):
147
+ return is_open (pull_request ) and (
148
+ pull_request ['author_association' ] == 'COLLABORATOR' or
149
+ has_label (pull_request )
150
+ )
151
+
117
152
def main (host , organization , repository ):
118
153
# > Accessing this endpoint does not count against your REST API rate limit.
119
154
#
@@ -136,66 +171,39 @@ def main(host, organization, repository):
136
171
time .gmtime (time .time () - FIVE_MINUTES )
137
172
)
138
173
139
- logger .info (
140
- 'Found {} pull requests modified in the past {} seconds' .format (
141
- len (pull_requests ), FIVE_MINUTES
142
- )
143
- )
144
-
145
174
for pull_request in pull_requests :
146
175
logger .info ('Processing pull request #{number}' .format (** pull_request ))
147
176
148
- has_label = any ([
149
- label ['name' ] == LABEL for label in pull_request ['labels' ]
150
- ])
151
177
refspec_labeled = 'prs-labeled-for-preview/{number}' .format (** pull_request )
152
178
refspec_open = 'prs-open/{number}' .format (** pull_request )
179
+ revision_latest = remote .get_revision (
180
+ 'pull/{number}/head' .format (** pull_request )
181
+ )
153
182
revision_labeled = remote .get_revision (refspec_labeled )
154
-
155
- if pull_request ['author_association' ] != 'COLLABORATOR' and not has_label :
156
- if revision_labeled :
157
- logger .info (
158
- 'Removing ref "{}" (was {})' .format (refspec_labeled , revision_labeled )
159
- )
160
- remote .delete_ref (refspec_labeled )
161
- else :
162
- logger .info ('No label and submitted by non-collaborator. Skipping.' )
163
-
164
- continue
165
-
166
- if not has_label :
167
- logger .info ('Automatically assigning GitHub pull request label' )
168
- project .add_label (pull_request ['number' ], LABEL )
169
-
170
183
revision_open = remote .get_revision (refspec_open )
171
184
172
- if pull_request [ 'closed_at' ] :
173
- logger .info ('Pull request is closed. ' )
185
+ if should_be_mirrored ( pull_request ) :
186
+ logger .info ('Pull request should be mirrored ' )
174
187
175
- if revision_open :
176
- logger .info (
177
- 'Removing ref "{}" (was {})' .format (refspec_open , revision_open )
178
- )
179
- remote .delete_ref (refspec_open )
188
+ if not has_label (pull_request ):
189
+ project .add_label (pull_request , LABEL )
180
190
181
- continue
191
+ if revision_labeled != latest_revision :
192
+ remote .update_ref (refspec_open , latest_revision )
182
193
183
- latest_revision = remote .get_revision ('pull/{number}/head' .format (** pull_request ))
194
+ if revision_open != latest_revision :
195
+ remote .update_ref (refspec_labeled , latest_revision )
196
+ else :
197
+ logger .info ('Pull request should not be mirrored' )
184
198
185
- if revision_labeled != latest_revision :
186
- logger .info (
187
- 'Updating ref "{}" to {}' .format (refspec_labeled , latest_revision )
188
- )
189
- remote .update_ref (refspec_labeled , latest_revision )
199
+ if has_label (pull_request ):
200
+ project .remove_label (pull_request , LABEL )
190
201
191
- logger . info ( 'Creating GitHub Deployment' )
192
- project . create_deployment ( latest_revision )
202
+ if revision_labeled != None :
203
+ remote . delete_ref ( refspec_labeled )
193
204
194
- if revision_open != latest_revision :
195
- logger .info (
196
- 'Updating ref "{}" to {}' .format (refspec_open , latest_revision )
197
- )
198
- remote .update_ref (refspec_open , latest_revision )
205
+ if revision_open != None and not is_open (pull_request ):
206
+ remote .delete_ref (refspec_labeled )
199
207
200
208
if __name__ == '__main__' :
201
209
parser = argparse .ArgumentParser ()
0 commit comments