|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright Contributors to the Packit project. |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +# /// script |
| 7 | +# dependencies = [ |
| 8 | +# "ruamel.yaml", |
| 9 | +# "requests", |
| 10 | +# ] |
| 11 | +# /// |
| 12 | + |
| 13 | +import argparse |
| 14 | +import copy |
| 15 | + |
| 16 | +import requests |
| 17 | +import ruamel.yaml |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +# Constants |
| 21 | +SCRIPTS_DIR = Path(__file__).parent |
| 22 | +ROOT_DIR = SCRIPTS_DIR.parent |
| 23 | +packit_service_file = ROOT_DIR / "secrets/packit/prod/packit-service.yaml.j2" |
| 24 | +SKIP_JINJA_LINES = 32 |
| 25 | +DIST_GIT_FORMAT = r"https://src.fedoraproject.org/rpms/{}" |
| 26 | +PAGURE_BZ = "https://src.fedoraproject.org/extras/pagure_bz.json" |
| 27 | + |
| 28 | +parser = argparse.ArgumentParser( |
| 29 | + description="Provide a comma-separated list of FAS maintainers/groups to bulk add to the packit-service.yaml.j2.", |
| 30 | +) |
| 31 | +parser.add_argument( |
| 32 | + "maintainers", |
| 33 | + type=str, |
| 34 | + nargs="+", |
| 35 | + help="a comma-separated list of FAS maintainers", |
| 36 | +) |
| 37 | +args = parser.parse_args() |
| 38 | +maintainers = {maintainer.strip() for maintainer in args.maintainers[0].split(",")} |
| 39 | +print(f"Onboarding packages for {maintainers}") |
| 40 | + |
| 41 | +# Using ruamel.yaml to preserve comments and format |
| 42 | +packit_service_yaml = ruamel.yaml.YAML() |
| 43 | +packit_service_yaml.indent(mapping=2, sequence=4, offset=2) |
| 44 | + |
| 45 | +# Get the current packit-service.yaml.j2 file |
| 46 | +with packit_service_file.open("r") as f: |
| 47 | + jinja_lines = [] |
| 48 | + for _ in range(SKIP_JINJA_LINES): |
| 49 | + jinja_lines.append(next(f)) |
| 50 | + packit_service = packit_service_yaml.load(f.read()) |
| 51 | + |
| 52 | +# Get all active |
| 53 | +response = requests.get(PAGURE_BZ) |
| 54 | +maintainers_data = response.json() |
| 55 | +maintainers_projects = { |
| 56 | + pkg_name |
| 57 | + for pkg_name, pkg_maintainers in maintainers_data["rpms"].items() |
| 58 | + if any(m in maintainers for m in pkg_maintainers) |
| 59 | +} |
| 60 | + |
| 61 | +# Onboard user's projects |
| 62 | +fedora_ci_projects = copy.copy(packit_service["enabled_projects_for_fedora_ci"]) |
| 63 | +previous_count = len(fedora_ci_projects) |
| 64 | +for project in maintainers_projects: |
| 65 | + dist_git_url = DIST_GIT_FORMAT.format(project) |
| 66 | + if dist_git_url not in fedora_ci_projects: |
| 67 | + fedora_ci_projects.append(dist_git_url) |
| 68 | +new_count = len(fedora_ci_projects) |
| 69 | + |
| 70 | +# Update the packit-service.yaml.j2 file |
| 71 | +print(f"Number of projects added: {new_count - previous_count}") |
| 72 | +packit_service["enabled_projects_for_fedora_ci"] = sorted(fedora_ci_projects) |
| 73 | +with packit_service_file.open("w") as f: |
| 74 | + for line in jinja_lines: |
| 75 | + f.write(line) |
| 76 | + packit_service_yaml.dump(packit_service, f) |
0 commit comments