openSUSE-release-tools/osclib/repair_command.py

72 lines
2.5 KiB
Python
Raw Normal View History

2019-11-08 18:04:40 +01:00
from urllib.error import HTTPError
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)
if not req:
2024-05-07 17:55:17 +02:00
raise oscerr.WrongArgs(f'Request {reqid} not found')
if req.state.name != 'review':
2024-05-07 17:55:17 +02:00
print(f'Request "{reqid}" is not in review state')
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:
raise oscerr.WrongArgs(
2024-05-07 17:55:17 +02:00
f'Request {reqid} had multiple review opened by different staging project')
else:
2024-05-07 17:55:17 +02:00
raise oscerr.WrongArgs(f'Request {reqid} is not for staging project')
staging_project = reviews[0]
try:
2019-11-25 07:36:37 +01:00
data = self.api.project_status(staging_project)
except HTTPError as e:
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:
2024-05-07 17:55:17 +02:00
print(f'Request "{reqid}" had the good setup in "{staging_project}"')
return
else:
2019-11-25 07:36:37 +01:00
# this situation should only happen on adi staging
2024-05-07 17:55:17 +02:00
print(f'Project is not exist, re-creating "{staging_project}"')
2022-02-18 17:01:38 +01:00
self.api.create_adi_project(staging_project)
# a bad request setup found
2024-05-07 17:55:17 +02:00
print(f'Repairing "{reqid}"')
change_review_state(self.api.apiurl, reqid, newstate='accepted',
message='Re-evaluation needed', by_project=staging_project)
self.api.add_review(reqid, by_group=self.api.cstaging_group, msg='Requesting new staging review')
def perform(self, packages, cleanup=False):
"""
Repair request in staging project or move it out
:param packages: packages/requests to repair in staging projects
"""
if cleanup:
untracked = self.api.project_status_requests('untracked')
if len(untracked) > 0:
2024-05-07 17:55:17 +02:00
print(f'Cleanup {len(untracked)} untracked requests')
packages += tuple(untracked)
for reqid in RequestFinder.find_sr(packages, self.api):
self.repair(reqid)