Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 1 | #!/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 | |
| 10 | from obmc import maintainers |
| 11 | from typing import cast, List, Optional |
| 12 | import argparse |
| 13 | import os |
| 14 | import sh |
| 15 | import sys |
| 16 | |
| 17 | git = sh.git.bake() |
| 18 | |
| 19 | def get_reviewers(root: Optional[str]=None, mname: str='MAINTAINERS') -> List[str]: |
Andrew Jeffery | 382f286 | 2018-05-23 17:05:50 +0930 | [diff] [blame] | 20 | reviewers = cast(List[str], list()) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 21 | if not root: |
| 22 | root = git('rev-parse', '--show-toplevel').strip() |
| 23 | mfile = os.path.join(root, mname) |
Andrew Jeffery | 39654b1 | 2018-05-22 12:38:53 +0930 | [diff] [blame] | 24 | if not os.path.exists(mfile): |
| 25 | return reviewers |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 26 | 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 Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 40 | def gerrit_refspec_args(reviewers: Optional[List[str]]=None, topic: str=None) -> str: |
| 41 | argl = [] |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 42 | if reviewers: |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 43 | argl.extend("r={}".format(addr) for addr in reviewers) |
| 44 | if topic: |
Lei YU | 8e56c31 | 2020-07-30 10:14:47 +0000 | [diff] [blame] | 45 | argl.append("topic={}".format(topic)) |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 46 | return ",".join(argl) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 47 | |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 48 | def decorate_refspec(refspec: str, topic: str) -> str: |
| 49 | gargs = gerrit_refspec_args(get_reviewers(), topic) |
Andrew Jeffery | 39654b1 | 2018-05-22 12:38:53 +0930 | [diff] [blame] | 50 | if not gargs: |
| 51 | return refspec |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 52 | if '%' in refspec: |
| 53 | return "{},{}".format(refspec, gargs) |
| 54 | return "{}%{}".format(refspec, gargs) |
| 55 | |
| 56 | def do_push(args: argparse.Namespace) -> None: |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 57 | git.push(args.remote, decorate_refspec(args.refspec, args.topic), |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 58 | _in=sys.stdin, _out=sys.stdout, _err=sys.stderr) |
| 59 | |
| 60 | parser = argparse.ArgumentParser() |
| 61 | subbies = parser.add_subparsers(dest='subcommand') |
| 62 | subbies.required = True |
| 63 | push = subbies.add_parser("push", help="Push changes to Gerrit with reviewers") |
| 64 | push.add_argument("remote") |
| 65 | push.add_argument("refspec") |
Lei YU | 8e56c31 | 2020-07-30 10:14:47 +0000 | [diff] [blame] | 66 | push.add_argument("topic", nargs='?', default=None) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 67 | push.set_defaults(func=do_push) |
| 68 | |
| 69 | args = parser.parse_args() |
| 70 | args.func(args) |