Add script for bulk ACL removal from dist-git packages

Automates the common releng ticket pattern where a packager requests
removal from packages they have commit/collaborator access on but
cannot self-service remove.
This commit is contained in:
Samyak Jain 2026-09-03 12:33:23 +05:30
commit dccf6fad40

View file

@ -0,0 +1,185 @@
#!/usr/bin/python3
"""
Bulk-remove a user's ACL from a list of dist-git packages.
Handles the common releng ticket pattern where a packager requests removal
from specific packages they have commit or collaborator access on but cannot
self-service remove.
Usage:
python remove_user_acls.py salimma packages.txt
python remove_user_acls.py salimma --packages bsh clang15 gpgme2
python remove_user_acls.py salimma --packages "bsh (collaborator)" "gpgme2 (commit)"
The packages file supports one package per line, optionally with an access
level annotation in parentheses (ignored — the API removes whatever access
exists):
bsh (collaborator)
gpgme2 (commit)
jackson-core
Requires $PAGURE_TOKEN set to a privileged pagure token with modifyproject ACL.
"""
import argparse
import os
import re
import sys
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
DIST_GIT_BASE = "https://src.fedoraproject.org"
def retry_session():
session = requests.Session()
retry = Retry(
total=5,
read=5,
connect=5,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
def get_pagure_token():
token = os.environ.get("PAGURE_TOKEN")
if not token:
print(
"Error: $PAGURE_TOKEN must be set to a valid Pagure admin token",
file=sys.stderr,
)
sys.exit(1)
return token
def parse_package_line(line):
"""Extract package name from a line like 'bsh (collaborator)' or just 'bsh'."""
line = line.strip()
if not line or line.startswith("#"):
return None
match = re.match(r"^(\S+)", line)
return match.group(1) if match else None
def remove_access(session, token, namespace, package, username):
"""Remove the user's ACL from the specified package."""
url = f"{DIST_GIT_BASE}/api/0/{namespace}/{package}/git/modifyacls"
headers = {"Authorization": f"token {token}"}
data = {"user_type": "user", "name": username}
response = session.post(url, data=data, headers=headers, timeout=60)
if not response.ok:
print(f" FAILED: {namespace}/{package} — {response.status_code}: {response.text}")
return False
print(f" OK: removed {username} from {namespace}/{package}")
return True
def unwatch_package(session, token, namespace, package, username):
"""Reset the watch status of the user on the specified package."""
url = f"{DIST_GIT_BASE}/api/0/{namespace}/{package}/watchers/update"
headers = {"Authorization": f"token {token}"}
data = {"status": -1, "watcher": username}
response = session.post(url, data=data, headers=headers, timeout=60)
if not response.ok:
print(f" FAILED to unwatch: {namespace}/{package} — {response.status_code}")
return False
print(f" OK: unwatched {username} from {namespace}/{package}")
return True
def main():
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("username", help="FAS username to remove from packages")
parser.add_argument(
"packages_file",
nargs="?",
help="File containing package list (one per line, optional access level annotation)",
)
parser.add_argument(
"--packages",
nargs="+",
help="Package names directly on the command line (alternative to file)",
)
parser.add_argument(
"--namespace",
default="rpms",
help="Dist-git namespace (default: rpms)",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be done without making changes",
)
parser.add_argument(
"--skip-unwatch",
action="store_true",
help="Skip resetting watch status after ACL removal",
)
args = parser.parse_args()
if not args.packages_file and not args.packages:
parser.error("Provide either a packages file or --packages")
packages = []
if args.packages_file:
with open(args.packages_file) as f:
for line in f:
pkg = parse_package_line(line)
if pkg:
packages.append(pkg)
else:
for item in args.packages:
pkg = parse_package_line(item)
if pkg:
packages.append(pkg)
if not packages:
print("No packages found to process.", file=sys.stderr)
sys.exit(1)
token = get_pagure_token()
session = retry_session()
print(f"Removing {args.username} from {len(packages)} packages in {args.namespace}/")
if args.dry_run:
print("(dry run — no changes will be made)")
print()
succeeded = 0
failed = 0
for package in sorted(packages):
if args.dry_run:
print(f" Would remove {args.username} from {args.namespace}/{package}")
succeeded += 1
continue
if remove_access(session, token, args.namespace, package, args.username):
succeeded += 1
if not args.skip_unwatch:
unwatch_package(session, token, args.namespace, package, args.username)
else:
failed += 1
print()
print(f"Done: {succeeded} succeeded, {failed} failed out of {len(packages)} packages")
session.close()
sys.exit(1 if failed else 0)
if __name__ == "__main__":
main()