#!/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 not os.path.exists(mfile):
        return (maints, reviewers)
    with open(mfile, "r") as mstream:
        maintainers.trash_preamble(mstream)
        block = maintainers.parse_block(mstream)
        if not block:
            return (maints, reviewers)
        mlist = cast(
            List[maintainers.Identity], block[maintainers.LineType.MAINTAINER]
        )
        maints.extend(i.email.address for i in mlist)
        if maintainers.LineType.REVIEWER in block:
            rlist = cast(
                List[maintainers.Identity], block[maintainers.LineType.REVIEWER]
            )
            reviewers.extend(i.email.address for i in rlist)
    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,
    )


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)

args = parser.parse_args()
args.func(args)
