Skip to content

Instantly share code, notes, and snippets.

@ralphbean
Created August 3, 2017 14:25
Show Gist options
  • Save ralphbean/1407cd4ecdc7282854e4b5bd431360cc to your computer and use it in GitHub Desktop.
Save ralphbean/1407cd4ecdc7282854e4b5bd431360cc to your computer and use it in GitHub Desktop.
This is a quick script to migrate the Bugzilla default assignees data from PkgDB to a YAML format.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is a quick script to migrate the Bugzilla default assignees data from
PkgDB to a YAML format.
"""
import argparse
import requests
import yaml
import os
def get_yaml_default_assignees(destination_dir, namespace, name):
yaml_file_path = os.path.join(destination_dir, namespace, name)
if not os.path.exists(yaml_file_path):
return None
with open(yaml_file_path, 'r') as yaml_file_stream:
data = yaml.safe_load(yaml_file_stream)
return data.get('bugzilla_contact')
def write_yaml_default_assignees(destination_dir, namespace, name, values):
yaml_namespace_dir = os.path.join(destination_dir, namespace)
yaml_file_path = os.path.join(yaml_namespace_dir, name)
print('Writing {0}'.format(yaml_file_path))
if not os.path.isdir(yaml_namespace_dir):
os.mkdir(yaml_namespace_dir)
with open(yaml_file_path, 'w') as yaml_file_stream:
values = dict(bugzilla_contact=values)
yaml.dump(values, yaml_file_stream, default_flow_style=False)
def delete_yaml_file(destination_dir, namespace, name):
yaml_file_path = os.path.join(destination_dir, namespace, name)
os.remove(yaml_file_path)
parser = argparse.ArgumentParser()
parser.add_argument('--pkgdburl', help='Base URL to PkgDB',
default='https://admin.fedoraproject.org/pkgdb/')
parser.add_argument('--pagureurl', help='Base URL to Pagure over dist-git',
default='https://src.fedoraproject.org/')
parser.add_argument('destinationdir',
help='Directory for the repository structure')
args = parser.parse_args()
namespace_to_bz_keys = {
'rpms': ['Fedora', 'Fedora EPEL'],
'container': ['Fedora Container'],
'modules': ['Fedora Modules']
}
pkgdb_bz_url = '{0}/api/bugzilla/?format=json'.format(
args.pkgdburl.rstrip('/'))
pkgdb_bz_info = requests.get(pkgdb_bz_url, timeout=600).json()
pagure_projects_url = '{0}/api/0/projects?page=1&per_page=100'.format(
args.pagureurl.rstrip('/'))
while True:
pagure_projects_rv = requests.get(pagure_projects_url, timeout=60).json()
for project in pagure_projects_rv['projects']:
print('Looking at {0}'.format(project['fullname']))
# If it's not a namespace we know about, skip it
if project['namespace'] not in namespace_to_bz_keys:
continue
for bz_key in namespace_to_bz_keys[project['namespace']]:
pkgdb_bz_owner = pkgdb_bz_info['bugzillaAcls'][bz_key]\
.get(project['name'], {}).get('owner')
if not pkgdb_bz_owner:
continue
pagure_owner = project['access_users']['owner'][0]
if pkgdb_bz_owner == pagure_owner:
yaml_assignees = get_yaml_default_assignees(
args.destinationdir, project['namespace'], project['name'])
# The owners are the same in Pagure and Bugzilla, make sure
# there isn't an override in the YAML file
if yaml_assignees and yaml_assignees.get(bz_key):
yaml_assignees.pop(bz_key)
if yaml_assignees:
write_yaml_default_assignees(
args.destinationdir, project['namespace'],
project['name'], yaml_assignees)
else:
# Delete the YAML file if after removing the current
# overridee there are no more overrides in the file
delete_yaml_file(
args.destinationdir, project['namespace'],
project['name'])
else:
yaml_assignees = get_yaml_default_assignees(
args.destinationdir, project['namespace'], project['name'])
if yaml_assignees:
# If the value is already, set just continue on to the next
if yaml_assignees.get(bz_key) \
and yaml_assignees[bz_key] == pkgdb_bz_owner:
continue
yaml_assignees[bz_key] = str(pkgdb_bz_owner)
else:
yaml_assignees = {bz_key: str(pkgdb_bz_owner)}
write_yaml_default_assignees(
args.destinationdir, project['namespace'], project['name'],
yaml_assignees)
if pagure_projects_rv['pagination']['next']:
pagure_projects_url = pagure_projects_rv['pagination']['next']
else:
break
print('All done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment