#!/usr/local/bin/python3 """ Script for cleaning up old Wikimedia repos """ import os import shutil import urllib from git import Repo from github import Github from pygerrit2.rest import GerritRestAPI from pygerrit2.rest.auth import HTTPDigestAuthFromNetrc GERRIT_URL = 'https://gerrit.wikimedia.org/r/' def mark_as_obsolete(repo_to_archive, alternative=None): """ Clone the repo from Gerrit, remove files, create an OBSOLETE file, commit :param repo_to_archive: The repo we're archiving :param alternative: If there's another repo, provide the URL here """ # Clone it clone_dir = os.path.join(os.getcwd(), 'deleteme') git = Repo.clone_from(GERRIT_URL + '/' + repo_to_archive, clone_dir).git # Remove all the files git.rm('.', '-r') # Make up some excuse message_to_the_future = 'This repository has been marked as obsolete' if alternative: message_to_the_future += ', please see {}'.format(alternative) obsolete = open(os.path.join(clone_dir, 'OBSOLETE'), 'w') obsolete.write(message_to_the_future) obsolete.close() # Add our new file, commit everything git.add('OBSOLETE') git.commit('-a', '-m' 'Marking repository as obsolete') # Push and clear our repo out #git.push('--force') shutil.rmtree(clone_dir) def archive_in_gerrit(repo_to_archive): """ Now that we have the repo all tidied up, mark it as obsolete in Gerrit! """ rest = GerritRestAPI(url=GERRIT_URL, auth=HTTPDigestAuthFromNetrc(url=GERRIT_URL)) escaped = urllib.quote(repo_to_archive) # Update the description and mark inactive desc = rest.get('/projects/{}/description'.format(escaped)) config_data = { 'description': '[ARCHIVED] {}'.format(desc), 'state': 'READ_ONLY' } rest.put('/projects/{}/config'.format(escaped), data=config_data) def archive_in_phab(repo_to_archive): """ Mark the repo as archived in Phabricator :param repo_to_archive: The repo we're deleting """ pass def delete_from_github(repo_to_archive): """ Delete the github repo outright :param repo_to_archive: The repo we're deleting """ org = Github(login_or_token='').get_organization('wikimedia') org.get_repo(repo_to_archive).delete() if __name__ == '__main__': SOME_REPO = 'foo' SOME_CALLSIGN = 'FOO' mark_as_obsolete(SOME_REPO) archive_in_phab(SOME_CALLSIGN) delete_from_github(SOME_REPO.replace('/', '-'))