blob: f285f6172f297a46b369837da31252a99d498380 [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
Andrew Jeffery3512b932020-06-15 13:09:16 +093040def gerrit_refspec_args(reviewers: Optional[List[str]]=None, topic: str=None) -> str:
41 argl = []
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093042 if reviewers:
Andrew Jeffery3512b932020-06-15 13:09:16 +093043 argl.extend("r={}".format(addr) for addr in reviewers)
44 if topic:
Lei YU8e56c312020-07-30 10:14:47 +000045 argl.append("topic={}".format(topic))
Andrew Jeffery3512b932020-06-15 13:09:16 +093046 return ",".join(argl)
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093047
Andrew Jeffery3512b932020-06-15 13:09:16 +093048def decorate_refspec(refspec: str, topic: str) -> str:
49 gargs = gerrit_refspec_args(get_reviewers(), topic)
Andrew Jeffery39654b12018-05-22 12:38:53 +093050 if not gargs:
51 return refspec
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093052 if '%' in refspec:
53 return "{},{}".format(refspec, gargs)
54 return "{}%{}".format(refspec, gargs)
55
56def do_push(args: argparse.Namespace) -> None:
Andrew Jeffery3512b932020-06-15 13:09:16 +093057 git.push(args.remote, decorate_refspec(args.refspec, args.topic),
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093058 _in=sys.stdin, _out=sys.stdout, _err=sys.stderr)
59
60parser = argparse.ArgumentParser()
61subbies = parser.add_subparsers(dest='subcommand')
62subbies.required = True
63push = subbies.add_parser("push", help="Push changes to Gerrit with reviewers")
64push.add_argument("remote")
65push.add_argument("refspec")
Lei YU8e56c312020-07-30 10:14:47 +000066push.add_argument("topic", nargs='?', default=None)
Andrew Jefferyf4019fe2018-05-18 16:42:18 +093067push.set_defaults(func=do_push)
68
69args = parser.parse_args()
70args.func(args)