2019-11-08 18:04:40 +01:00
|
|
|
from urllib.error import HTTPError
|
2016-04-18 19:26:47 +08:00
|
|
|
from osc import oscerr
|
|
|
|
from osc.core import change_review_state
|
|
|
|
from osc.core import get_request
|
|
|
|
from osclib.request_finder import RequestFinder
|
|
|
|
|
|
|
|
|
|
|
|
class RepairCommand(object):
|
|
|
|
|
|
|
|
def __init__(self, api):
|
|
|
|
self.api = api
|
|
|
|
|
|
|
|
def repair(self, request):
|
|
|
|
reviews = []
|
|
|
|
reqid = str(request)
|
|
|
|
req = get_request(self.api.apiurl, reqid)
|
|
|
|
|
2017-10-17 09:10:17 +02:00
|
|
|
if not req:
|
2016-04-18 19:26:47 +08:00
|
|
|
raise oscerr.WrongArgs('Request {} not found'.format(reqid))
|
|
|
|
|
|
|
|
if req.state.name != 'review':
|
|
|
|
print('Request "{}" is not in review state'.format(reqid))
|
|
|
|
return
|
|
|
|
|
|
|
|
reviews = [r.by_project for r in req.reviews if ':Staging:' in str(r.by_project) and r.state == 'new']
|
|
|
|
|
|
|
|
if reviews:
|
|
|
|
if len(reviews) > 1:
|
2022-02-18 17:44:32 +01:00
|
|
|
raise oscerr.WrongArgs(
|
|
|
|
'Request {} had multiple review opened by different staging project'.format(reqid))
|
2016-04-18 19:26:47 +08:00
|
|
|
else:
|
|
|
|
raise oscerr.WrongArgs('Request {} is not for staging project'.format(reqid))
|
|
|
|
|
|
|
|
staging_project = reviews[0]
|
2017-02-02 15:44:45 +08:00
|
|
|
try:
|
2019-11-25 07:36:37 +01:00
|
|
|
data = self.api.project_status(staging_project)
|
2018-11-05 15:46:50 +01:00
|
|
|
except HTTPError as e:
|
2017-02-02 15:44:45 +08:00
|
|
|
if e.code == 404:
|
|
|
|
data = None
|
|
|
|
|
|
|
|
# Pre-check and pre-setup
|
2019-11-25 07:36:37 +01:00
|
|
|
if data is not None:
|
|
|
|
for request in data.findall('staged_requests/requests'):
|
|
|
|
if request.get('id') == reqid:
|
2017-02-02 15:44:45 +08:00
|
|
|
print('Request "{}" had the good setup in "{}"'.format(reqid, staging_project))
|
|
|
|
return
|
|
|
|
else:
|
2019-11-25 07:36:37 +01:00
|
|
|
# this situation should only happen on adi staging
|
2017-02-02 15:44:45 +08:00
|
|
|
print('Project is not exist, re-creating "{}"'.format(staging_project))
|
2022-02-18 17:01:38 +01:00
|
|
|
self.api.create_adi_project(staging_project)
|
2016-04-18 19:26:47 +08:00
|
|
|
|
|
|
|
# a bad request setup found
|
|
|
|
print('Repairing "{}"'.format(reqid))
|
2022-02-18 17:44:32 +01:00
|
|
|
change_review_state(self.api.apiurl, reqid, newstate='accepted',
|
|
|
|
message='Re-evaluation needed', by_project=staging_project)
|
2016-04-18 19:26:47 +08:00
|
|
|
self.api.add_review(reqid, by_group=self.api.cstaging_group, msg='Requesting new staging review')
|
|
|
|
|
2017-03-18 17:59:50 -05:00
|
|
|
def perform(self, packages, cleanup=False):
|
2016-04-18 19:26:47 +08:00
|
|
|
"""
|
|
|
|
Repair request in staging project or move it out
|
|
|
|
:param packages: packages/requests to repair in staging projects
|
|
|
|
"""
|
|
|
|
|
2017-03-18 17:59:50 -05:00
|
|
|
if cleanup:
|
|
|
|
untracked = self.api.project_status_requests('untracked')
|
|
|
|
if len(untracked) > 0:
|
|
|
|
print('Cleanup {} untracked requests'.format(len(untracked)))
|
|
|
|
packages += tuple(untracked)
|
|
|
|
|
2016-04-18 19:26:47 +08:00
|
|
|
for reqid in RequestFinder.find_sr(packages, self.api):
|
|
|
|
self.repair(reqid)
|