Patrick Williams | 45c7017 | 2021-03-04 20:25:26 -0600 | [diff] [blame] | 1 | #!/usr/bin/env -S python3 -B |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 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 |
Patrick Williams | 0b047c4 | 2021-06-21 16:08:29 -0500 | [diff] [blame] | 11 | from obmc.reviewlist import ReviewList |
| 12 | from typing import cast, Callable, List, Optional |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 13 | import argparse |
| 14 | import os |
Patrick Williams | 2deb2e3 | 2021-06-21 15:15:20 -0500 | [diff] [blame] | 15 | import sh # type: ignore |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 16 | import sys |
| 17 | |
Patrick Williams | 2deb2e3 | 2021-06-21 15:15:20 -0500 | [diff] [blame] | 18 | git: Callable[..., str] = sh.git.bake() |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 19 | |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 20 | |
Patrick Williams | 404d301 | 2021-06-21 15:33:11 -0500 | [diff] [blame] | 21 | def get_reviewers( |
| 22 | root: Optional[str] = None, mname: str = "MAINTAINERS" |
Patrick Williams | 0b047c4 | 2021-06-21 16:08:29 -0500 | [diff] [blame] | 23 | ) -> ReviewList: |
| 24 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 25 | if not root: |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 26 | root = git("rev-parse", "--show-toplevel").strip() |
Patrick Williams | 951268d | 2021-06-21 15:53:10 -0500 | [diff] [blame] | 27 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 28 | mfile = os.path.join(root, mname) |
Patrick Williams | 951268d | 2021-06-21 15:53:10 -0500 | [diff] [blame] | 29 | if os.path.exists(mfile): |
| 30 | return maintainers.get_reviewers(mfile) |
| 31 | |
Patrick Williams | 0b047c4 | 2021-06-21 16:08:29 -0500 | [diff] [blame] | 32 | return ReviewList() |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 33 | |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 34 | |
| 35 | def gerrit_refspec_args( |
Patrick Williams | 404d301 | 2021-06-21 15:33:11 -0500 | [diff] [blame] | 36 | maintainers: Optional[List[str]] = None, |
| 37 | reviewers: Optional[List[str]] = None, |
| 38 | topic: str = None, |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 39 | ) -> str: |
Patrick Williams | 2deb2e3 | 2021-06-21 15:15:20 -0500 | [diff] [blame] | 40 | argl: List[str] = [] |
Patrick Williams | 404d301 | 2021-06-21 15:33:11 -0500 | [diff] [blame] | 41 | if maintainers: |
| 42 | argl.extend("r={}".format(addr) for addr in maintainers) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 43 | if reviewers: |
Patrick Williams | 404d301 | 2021-06-21 15:33:11 -0500 | [diff] [blame] | 44 | argl.extend("cc={}".format(addr) for addr in reviewers) |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 45 | if topic: |
Lei YU | 8e56c31 | 2020-07-30 10:14:47 +0000 | [diff] [blame] | 46 | argl.append("topic={}".format(topic)) |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 47 | return ",".join(argl) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 48 | |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 49 | |
Andrew Jeffery | 3512b93 | 2020-06-15 13:09:16 +0930 | [diff] [blame] | 50 | def decorate_refspec(refspec: str, topic: str) -> str: |
Patrick Williams | 0b047c4 | 2021-06-21 16:08:29 -0500 | [diff] [blame] | 51 | revlist = get_reviewers() |
| 52 | gargs = gerrit_refspec_args(revlist.maintainers, revlist.reviewers, topic) |
Andrew Jeffery | 39654b1 | 2018-05-22 12:38:53 +0930 | [diff] [blame] | 53 | if not gargs: |
| 54 | return refspec |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 55 | if "%" in refspec: |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 56 | return "{},{}".format(refspec, gargs) |
| 57 | return "{}%{}".format(refspec, gargs) |
| 58 | |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 59 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 60 | def do_push(args: argparse.Namespace) -> None: |
Patrick Williams | 2deb2e3 | 2021-06-21 15:15:20 -0500 | [diff] [blame] | 61 | git( |
| 62 | "push", |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 63 | args.remote, |
| 64 | decorate_refspec(args.refspec, args.topic), |
| 65 | _in=sys.stdin, |
| 66 | _out=sys.stdout, |
| 67 | _err=sys.stderr, |
| 68 | ) |
| 69 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 70 | |
Patrick Williams | 2d19834 | 2021-06-21 15:35:50 -0500 | [diff] [blame] | 71 | def do_reviewers(args: argparse.Namespace) -> None: |
Patrick Williams | 0b047c4 | 2021-06-21 16:08:29 -0500 | [diff] [blame] | 72 | revlist = get_reviewers() |
| 73 | print(gerrit_refspec_args(revlist.maintainers, revlist.reviewers)) |
Patrick Williams | 2d19834 | 2021-06-21 15:35:50 -0500 | [diff] [blame] | 74 | |
| 75 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 76 | parser = argparse.ArgumentParser() |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 77 | subbies = parser.add_subparsers(dest="subcommand") |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 78 | subbies.required = True |
Patrick Williams | 2d19834 | 2021-06-21 15:35:50 -0500 | [diff] [blame] | 79 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 80 | push = subbies.add_parser("push", help="Push changes to Gerrit with reviewers") |
| 81 | push.add_argument("remote") |
| 82 | push.add_argument("refspec") |
Patrick Williams | 9a01439 | 2021-06-21 14:57:29 -0500 | [diff] [blame] | 83 | push.add_argument("topic", nargs="?", default=None) |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 84 | push.set_defaults(func=do_push) |
| 85 | |
Patrick Williams | 2d19834 | 2021-06-21 15:35:50 -0500 | [diff] [blame] | 86 | reviewers = subbies.add_parser("reviewers", help="Get the reviewer list.") |
| 87 | reviewers.set_defaults(func=do_reviewers) |
| 88 | |
Andrew Jeffery | f4019fe | 2018-05-18 16:42:18 +0930 | [diff] [blame] | 89 | args = parser.parse_args() |
| 90 | args.func(args) |