2020-03-16 11:42:52 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
from shutil import copyfile
|
|
|
|
import subprocess
|
|
|
|
from os.path import basename
|
|
|
|
import glob
|
|
|
|
from openqa_client.client import OpenQA_Client
|
2022-02-18 18:29:27 +01:00
|
|
|
from openqa_client.exceptions import RequestError
|
2020-03-16 11:42:52 +01:00
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
def old_filename(state):
|
|
|
|
return f'{args.repos}/{state}.yaml'
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
def new_filename(state):
|
|
|
|
return f'{args.to}/{state}.yaml'
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
def file_changed(state):
|
|
|
|
with open(old_filename(state), 'r') as old_file:
|
|
|
|
old_content = old_file.read()
|
|
|
|
try:
|
|
|
|
with open(new_filename(state), 'r') as new_file:
|
|
|
|
new_content = new_file.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return True
|
|
|
|
return old_content != new_content
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
def notify_project(openqa, state):
|
|
|
|
project, repository = state.split('_-_')
|
|
|
|
if not file_changed(state):
|
2020-03-17 07:17:31 +01:00
|
|
|
logger.debug(f'{state} did not change')
|
2020-03-16 11:42:52 +01:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
openqa.openqa_request('PUT', 'obs_rsync/{}/runs?repository={}'.format(project, repository), retries=0)
|
|
|
|
except RequestError as e:
|
|
|
|
logger.info("Got exception on syncing repository: {}".format(e))
|
|
|
|
return
|
|
|
|
copyfile(old_filename(state), new_filename(state))
|
|
|
|
subprocess.run(f'cd {args.to} && git add . && git commit -m "Update of {project}/{repository}" && git push', shell=True, check=True)
|
|
|
|
|
2022-02-18 17:11:46 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Bot to sync openQA status to OBS')
|
|
|
|
parser.add_argument('--openqa', type=str, required=True, help='OpenQA URL')
|
|
|
|
parser.add_argument('--repos', type=str, required=True, help='Directory to read from')
|
|
|
|
parser.add_argument('--to', type=str, required=True, help='Directory to commit into')
|
|
|
|
|
|
|
|
global args
|
|
|
|
args = parser.parse_args()
|
|
|
|
global logger
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-03-18 13:52:29 +01:00
|
|
|
openqa = OpenQA_Client(server=args.openqa)
|
|
|
|
|
2022-03-05 08:35:27 +01:00
|
|
|
# make sure we avoid a race between gocd polling the notifications repo and
|
|
|
|
# scheduling a notify job because of other changes. In that case gocd schedules
|
|
|
|
# a new job on outdated notifications repo and we can't push
|
2022-03-05 15:28:05 +01:00
|
|
|
subprocess.run(f'cd {args.to} && git pull', shell=True, check=True)
|
2022-03-05 08:35:27 +01:00
|
|
|
|
2020-03-17 07:17:31 +01:00
|
|
|
interesting_repos = dict()
|
2020-03-18 13:52:29 +01:00
|
|
|
list = openqa.openqa_request('GET', 'obs_rsync')
|
|
|
|
for repopair in list:
|
|
|
|
project, repository = repopair
|
|
|
|
interesting_repos[f'{project}_-_{repository}'] = 1
|
2020-03-17 07:17:31 +01:00
|
|
|
|
2020-03-16 11:42:52 +01:00
|
|
|
openqa = OpenQA_Client(server=args.openqa)
|
|
|
|
for state in glob.glob('{}/*.yaml'.format(args.repos)):
|
|
|
|
state = basename(state).replace('.yaml', '')
|
2021-09-21 14:20:08 +02:00
|
|
|
if state not in interesting_repos:
|
2020-03-16 11:42:52 +01:00
|
|
|
continue
|
|
|
|
notify_project(openqa, state)
|