blob: e7e8f6a364e782fc756c1685a1825ea6dc109046 [file] [log] [blame]
Andrew Jefferyf4019fe2018-05-18 16:42:18 +09301#!/usr/bin/env python3
2#
3# SPDX-License-Identifier: Apache-2.0
4# Copyright (C) 2018 IBM Corp.
5#
6# Push changes to Gerrit, automatically adding reviewers to the patches by
7# parsing the OpenBMC-style MAINTAINERS file in the root of the repository (if
8# it exists).
9
10from obmc import maintainers
11from typing import cast, List, Optional
12import argparse
13import os
14import sh
15import sys
16
17git = sh.git.bake()
18
19def get_reviewers(root: Optional[str]=None, mname: str='MAINTAINERS') -> List[str]:
Andrew Jeffery382f2862018-05-23 17:05:50 +093020 reviewers = cast(List[str], list())
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093021 if not root:
22 root = git('rev-parse', '--show-toplevel').strip()
23 mfile = os.path.join(root, mname)
Andrew Jeffery39654b12018-05-22 12:38:53 +093024 if not os.path.exists(mfile):
25 return reviewers
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093026 with open(mfile, 'r') as mstream:
27 maintainers.trash_preamble(mstream)
28 block = maintainers.parse_block(mstream)
29 if not block:
30 return reviewers
31 mlist = cast(List[maintainers.Identity],
32 block[maintainers.LineType.MAINTAINER])
33 reviewers.extend(i.email.address for i in mlist)
34 if maintainers.LineType.REVIEWER in block:
35 rlist = cast(List[maintainers.Identity],
36 block[maintainers.LineType.REVIEWER])
37 reviewers.extend(i.email.address for i in rlist)
38 return reviewers
39
40def gerrit_refspec_args(reviewers: Optional[List[str]]=None) -> str:
41 args = ""
42 if reviewers:
43 args += ",".join("r={}".format(addr) for addr in reviewers)
44 return args
45
46def decorate_refspec(refspec: str) -> str:
47 gargs = gerrit_refspec_args(get_reviewers())
Andrew Jeffery39654b12018-05-22 12:38:53 +093048 if not gargs:
49 return refspec
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093050 if '%' in refspec:
51 return "{},{}".format(refspec, gargs)
52 return "{}%{}".format(refspec, gargs)
53
54def do_push(args: argparse.Namespace) -> None:
55 git.push(args.remote, decorate_refspec(args.refspec),
56 _in=sys.stdin, _out=sys.stdout, _err=sys.stderr)
57
58parser = argparse.ArgumentParser()
59subbies = parser.add_subparsers(dest='subcommand')
60subbies.required = True
61push = subbies.add_parser("push", help="Push changes to Gerrit with reviewers")
62push.add_argument("remote")
63push.add_argument("refspec")
64push.set_defaults(func=do_push)
65
66args = parser.parse_args()
67args.func(args)