blob: c6636e544540ad501a40eca53cb6918a865a3456 [file] [log] [blame]
#!/usr/bin/env -S python3 -B
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2018 IBM Corp.
#
# Push changes to Gerrit, automatically adding reviewers to the patches by
# parsing the OpenBMC-style MAINTAINERS file in the root of the repository (if
# it exists).
from obmc import maintainers
from obmc.reviewlist import ReviewList
from typing import cast, Callable, List, Optional
import argparse
import os
import sh # type: ignore
import sys
git: Callable[..., str] = sh.git.bake()
def get_reviewers(
root: Optional[str] = None, mname: str = "MAINTAINERS"
) -> ReviewList:
if not root:
root = git("rev-parse", "--show-toplevel").strip()
mfile = os.path.join(root, mname)
if os.path.exists(mfile):
return maintainers.get_reviewers(mfile)
return ReviewList()
def gerrit_refspec_args(
maintainers: Optional[List[str]] = None,
reviewers: Optional[List[str]] = None,
topic: str = None,
) -> str:
argl: List[str] = []
if maintainers:
argl.extend("r={}".format(addr) for addr in maintainers)
if reviewers:
argl.extend("cc={}".format(addr) for addr in reviewers)
if topic:
argl.append("topic={}".format(topic))
return ",".join(argl)
def decorate_refspec(refspec: str, topic: str) -> str:
revlist = get_reviewers()
gargs = gerrit_refspec_args(revlist.maintainers, revlist.reviewers, topic)
if not gargs:
return refspec
if "%" in refspec:
return "{},{}".format(refspec, gargs)
return "{}%{}".format(refspec, gargs)
def do_push(args: argparse.Namespace) -> None:
git(
"push",
args.remote,
decorate_refspec(args.refspec, args.topic),
_in=sys.stdin,
_out=sys.stdout,
_err=sys.stderr,
)
def do_reviewers(args: argparse.Namespace) -> None:
revlist = get_reviewers()
print(gerrit_refspec_args(revlist.maintainers, revlist.reviewers))
parser = argparse.ArgumentParser()
subbies = parser.add_subparsers(dest="subcommand")
subbies.required = True
push = subbies.add_parser("push", help="Push changes to Gerrit with reviewers")
push.add_argument("remote")
push.add_argument("refspec")
push.add_argument("topic", nargs="?", default=None)
push.set_defaults(func=do_push)
reviewers = subbies.add_parser("reviewers", help="Get the reviewer list.")
reviewers.set_defaults(func=do_reviewers)
args = parser.parse_args()
args.func(args)