Skip to content

Instantly share code, notes, and snippets.

@goldmann
Last active June 5, 2019 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldmann/c13d796d89b70b4e3584eacb40f1da0e to your computer and use it in GitHub Desktop.
Save goldmann/c13d796d89b70b4e3584eacb40f1da0e to your computer and use it in GitHub Desktop.
A script to make it easier to orphan pacakges in Fedora
# MIT License
# Copyright (c) 2019 Marek Goldmann
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import click
import json
import requests
import sys
# Put your token from https://src.fedoraproject.org/settings#nav-api-tab here
API_KEY=""
headers = {"Authorization": "token {}".format(API_KEY)}
base_url = "https://src.fedoraproject.org/api/0/"
click.echo("Finding who we are")
# Get information about the user
try:
r = requests.post(base_url + "-/whoami", headers = headers)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
click.echo(r.text)
click.echo(err)
click.echo("Finding username failed, is your token valid?")
sys.exit(-1)
username = r.json()["username"]
click.echo("Getting list of projects we maintain")
# Get projects that are owned by us, no forks please
try:
r = requests.get(base_url + "projects", headers = headers, params={'owner': username, 'namespace': 'rpms', 'fork': 'false'})
r.raise_for_status()
except requests.exceptions.HTTPError as err:
click.echo(r.text)
click.echo(err)
click.echo("Getting projects failed")
sys.exit(-1)
projects = r.json()["projects"]
@click.command()
def cli():
for project in projects:
if not click.confirm("Do you want to orphan project '{}'".format(project['name'])):
continue
click.echo(" Obtaining token in the '{}' project to manage watch status".format(project['name']))
try:
r = requests.post(base_url + project['url_path'] + "/token/new", headers = headers, data = {'acls': ['update_watch_status'], 'description': "{}'s orphan automation, feel free to remove".format(username)})
r.raise_for_status()
except requests.exceptions.HTTPError as err:
click.echo(r.request.headers)
click.echo(r.request.body)
click.echo(r.text)
click.echo(err)
click.echo("Obtaining token failed")
sys.exit(-1)
project_headers = {"Authorization": "token {}".format(r.json()['token']['id'])}
click.echo(" Resetting watch status")
try:
r = requests.post(base_url + project['url_path'] + "/watchers/update", headers = project_headers, data = {'namespace': 'rpms', 'repo': project['name'], 'watcher': username, 'status': '0'})
r.raise_for_status()
except requests.exceptions.HTTPError as err:
click.echo(r.text)
click.echo(err)
click.echo("Resetting watch status failed")
sys.exit(-1)
click.echo(" Changing the owner to 'orphan'")
try:
r = requests.patch(base_url + project['url_path'], {'main_admin': 'orphan'}, headers = headers)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
click.echo(r.text)
click.echo(err)
click.echo("Orphaning project failed")
sys.exit(-1)
click.echo("Project '{}' orphaned".format(project['name']))
if __name__ == '__main__':
cli()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment