blob: b666652b336002a52f49b3fea32e86196874c50e [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 typing import cast, Callable, List, Optional, Tuple
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"
) -> Tuple[List[str], List[str]]:
maints: List[str] = list()
reviewers: List[str] = list()
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 (maints, reviewers)
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:
(maintainers, reviewers) = get_reviewers()
gargs = gerrit_refspec_args(maintainers, 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:
(maintainers, reviewers) = get_reviewers()
print(gerrit_refspec_args(maintainers, 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)