forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin-manager.py
executable file
·264 lines (212 loc) · 11.8 KB
/
origin-manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/python3
from typing import Optional, Tuple, Union
try:
from typing import Literal
except ImportError:
from typing_extensions import Literal
from osclib.core import devel_project_get
from osclib.core import package_source_hash
from osclib.core import package_kind
from osclib.core import package_role_expand
from osclib.origin import origin_annotation_dump
from osclib.origin import origin_devel_projects
from osclib.origin import origin_workaround_strip
from osclib.origin import config_load
from osclib.origin import config_origin_list
from osclib.origin import devel_project_simulate
from osclib.origin import devel_project_simulate_exception
from osclib.origin import origin_find
from osclib.origin import policy_evaluate
from osclib.origin import PolicyResult
import ReviewBot
import sys
class OriginManager(ReviewBot.ReviewBot):
def __init__(self, *args, **kwargs):
ReviewBot.ReviewBot.__init__(self, *args, **kwargs)
# ReviewBot options.
# Younger than default splitter-request-age-threshold to allow for quick
# strategy to still be useful which requires a completed review.
self.request_age_min_default = 30 * 60
self.request_default_return = True
self.override_allow = False
def check_action_change_devel(self, request, action):
advance, result = self.config_validate(action.tgt_project)
if not advance:
return result
source_hash = package_source_hash(self.apiurl, action.tgt_project, action.tgt_package)
origin_info_old = origin_find(self.apiurl, action.tgt_project, action.tgt_package, source_hash, True)
with devel_project_simulate(self.apiurl, action.tgt_project, action.tgt_package,
action.src_project, action.src_package):
origin_info_new = origin_find(self.apiurl, action.tgt_project, action.tgt_package, source_hash)
result = policy_evaluate(self.apiurl, action.tgt_project, action.tgt_package,
origin_info_new, origin_info_old,
source_hash, source_hash)
reviews = {}
# Remove all additional_reviews as there are no source changes.
for key, comment in result.reviews.items():
if key in ('fallback', 'maintainer'):
reviews[key] = comment
if result.accept:
config = config_load(self.apiurl, action.tgt_project)
if request.creator == config['review-user']:
# Remove all reviews since the request was generated via
# origin_update() which indicates it was approved already. Acts
# as workaround for to lack of set devel on a submit request.
reviews = {}
if len(reviews) != len(result.reviews):
result = PolicyResult(result.wait, result.accept, reviews, result.comments)
return self.policy_result_handle(action.tgt_project, action.tgt_package, origin_info_new, origin_info_old, result)
def check_action_delete_package(self, request, action):
advance, result = self.config_validate(action.tgt_project)
if not advance:
return result
origin_info_old = origin_find(self.apiurl, action.tgt_project, action.tgt_package)
reviews = {'fallback': 'Delete requests require fallback review.'}
self.policy_result_reviews_add(action.tgt_project, action.tgt_package,
reviews, origin_info_old, origin_info_old)
return True
def check_source_submission(self, src_project, src_package, src_rev, tgt_project, tgt_package) -> Optional[bool]:
kind = package_kind(self.apiurl, tgt_project, tgt_package)
if not (kind is None or kind == 'source'):
self.review_messages['accepted'] = f'skipping {kind} package since not source'
return True
advance, result = self.config_validate(tgt_project)
if not advance:
return result
if self.request_age_wait():
# Allow for parallel submission to be created.
return None
source_hash_new = package_source_hash(self.apiurl, src_project, src_package, src_rev)
origin_info_new = origin_find(self.apiurl, tgt_project, tgt_package, source_hash_new)
source_hash_old = package_source_hash(self.apiurl, tgt_project, tgt_package)
origin_info_old = origin_find(self.apiurl, tgt_project, tgt_package, source_hash_old, True)
# Check if simulating the devel project is appropriate.
devel_project, reason = self.devel_project_simulate_check(src_project, tgt_project)
if devel_project and (reason.startswith('change_devel command') or origin_info_new is None):
self.logger.debug(f'reevaluate considering {devel_project} as devel since {reason}')
try:
with devel_project_simulate(self.apiurl, tgt_project, tgt_package, src_project, src_package):
# Recurse with simulated devel project.
ret = self.check_source_submission(
src_project, src_package, src_rev, tgt_project, tgt_package)
self.review_messages['accepted']['comment'] = reason
return ret
except devel_project_simulate_exception:
# Invalid infinite recursion so fallback to normal behavior.
pass
result = policy_evaluate(self.apiurl, tgt_project, tgt_package,
origin_info_new, origin_info_old,
source_hash_new, source_hash_old)
return self.policy_result_handle(tgt_project, tgt_package, origin_info_new, origin_info_old, result)
def config_validate(self, target_project: str) -> Tuple[bool, bool]:
"""Check the settings ``OSRT:OriginConfig`` of the target project and
return a tuple of booleans. The first boolean indicates whether the
action should proceed and the second whether the config is valid.
This function checks whether the value
``OSRT:OriginConfig.fallback-group`` is present and whether
``OSRT:OriginConfig.review-user`` matches the configured review_user.
"""
config = config_load(self.apiurl, target_project)
if not config:
# No perfect solution for lack of a config. For normal projects a
# decline seems best, but in the event of failure to return proper
# config no good behavior. For maintenance the situation is further
# complicated since multiple actions some of which are not intended
# to be reviewed, but not always guaranteed to see multiple actions.
self.review_messages['accepted'] = 'skipping since no OSRT:OriginConfig'
return False, True
if not config.get('fallback-group'):
self.review_messages['declined'] = 'OSRT:OriginConfig.fallback-group missing'
return False, False
if not self.dryrun and config['review-user'] != self.review_user:
self.logger.warning(
'OSRT:OriginConfig.review-user ({}) does not match ReviewBot.review_user ({})'.format(
config['review-user'], self.review_user))
return True, True
def devel_project_simulate_check(
self,
source_project: str,
target_project: str
) -> Union[Tuple[str, str], Tuple[Literal[False], Literal[None]]]:
config = config_load(self.apiurl, target_project)
origin_list = config_origin_list(config, self.apiurl, target_project, skip_workarounds=True)
if '<devel>' not in origin_list:
return False, None
if len(origin_list) == 1:
return source_project, 'only devel origin allowed'
if source_project in origin_devel_projects(self.apiurl, target_project):
return source_project, 'familiar devel origin'
override, who = self.devel_project_simulate_check_command(source_project, target_project)
if override:
return override, f'change_devel command by {who}'
return False, None
def devel_project_simulate_check_command(
self,
source_project: str,
target_project: str
) -> Union[Tuple[str, Optional[str]], Tuple[Literal[False], Literal[None]]]:
who_allowed = self.request_override_check_users(target_project)
if self.request.creator not in who_allowed:
who_allowed.append(self.request.creator)
for args, who in self.request_commands('change_devel', who_allowed):
override = args[1] if len(args) >= 2 else source_project
return override, who
return False, None
def policy_result_handle(self, project, package, origin_info_new, origin_info_old, result: PolicyResult) -> Optional[bool]:
if result.wait and not result.accept:
result.comments.append(f'Decision may be overridden via `@{self.review_user} override`.')
self.policy_result_reviews_add(project, package, result.reviews, origin_info_new, origin_info_old)
self.policy_result_comment_add(project, package, result.comments)
if result.wait:
# Allow overriding a policy wait by accepting as workaround with the
# hope that pending request will be accepted.
override = self.request_override_check(True)
if override:
self.review_messages['accepted'] = origin_annotation_dump(
origin_info_new, origin_info_old, self.review_messages['accepted'], raw=True)
return override
else:
if result.accept:
self.review_messages['accepted'] = origin_annotation_dump(
origin_info_new, origin_info_old, raw=True)
return result.accept
return None
def policy_result_reviews_add(self, project, package, reviews, origin_info_new, origin_info_old):
for key, comment in reviews.items():
if key == 'maintainer':
self.origin_maintainer_review_ensure(origin_info_new, package, message=comment)
elif key == 'fallback':
fallback_group = config_load(self.apiurl, project).get('fallback-group')
comment += '\n\n' + origin_annotation_dump(origin_info_new, origin_info_old)
self.add_review(self.request, by_group=fallback_group, msg=comment)
else:
self.add_review(self.request, by_group=key, msg=comment)
def origin_maintainer_review_ensure(self, origin_info, package, message, request=None):
if not request:
request = self.request
origin = origin_workaround_strip(origin_info.project)
# Check if the origin package is actually developed elsewhere and add
# the maintainer review for the development location.
devel_project, _ = devel_project_get(self.apiurl, origin, package)
if devel_project:
origin = devel_project
users = package_role_expand(self.apiurl, origin, package, 'maintainer')
if request.creator not in users:
self.add_review(request, by_project=origin, by_package=package, msg=message)
def policy_result_comment_add(self, project, package, comments):
message = '\n\n'.join(comments)
if len(self.request.actions) > 1:
message = f'## {project}/{package}\n\n{message}'
suffix = '::'.join([project, package])
else:
suffix = None
only_replace = False
if not len(comments):
message = 'Previous comment no longer relevant.'
only_replace = True
self.comment_write(state='seen', message=message, identical=True,
only_replace=only_replace, bot_name_suffix=suffix)
if __name__ == '__main__':
app = ReviewBot.CommandLineInterface()
app.clazz = OriginManager
sys.exit(app.main())