Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 4 | # |
| 5 | # SPDX-License-Identifier: BSD-3-Clause |
| 6 | # |
| 7 | |
| 8 | # This scripts translates certain accepted refspec schemes to something that can |
| 9 | # be used on git command line. For example, given the refspec 'topic:foo/bar' |
| 10 | # for a given project, this script translates and prints the full commit hash. |
| 11 | # |
| 12 | # If a scheme is not recognized, print the received refspec unchanged. |
| 13 | |
| 14 | import argparse |
| 15 | import gerrit |
| 16 | import sys |
| 17 | |
| 18 | # Gerrit servers we care about. |
| 19 | gerrit_arm = gerrit.GerritServer("gerrit.oss.arm.com") |
| 20 | gerrit_tforg = gerrit.GerritServer("review.trustedfirmware.org") |
| 21 | |
| 22 | # Trusted Firmware-A and associated projects. |
| 23 | # Different projects are hosted on different Gerrit servers. |
| 24 | projects = { |
| 25 | # Projects hosted on Arm Gerrit server. |
| 26 | "arm": { |
| 27 | "trusted-firmware": gerrit.GerritProject("pdcs-platforms/ap/tf-topics", gerrit_arm), |
| 28 | "trusted-firmware-tf": gerrit.GerritProject("trusted-firmware/tf-a-tests", gerrit_arm), |
| 29 | "trusted-firmware-ci": gerrit.GerritProject("pdswinf/ci/pdcs-platforms/platform-ci", gerrit_arm), |
Zelalem | 219df41 | 2020-05-17 19:21:20 -0500 | [diff] [blame] | 30 | "cc_plugin": gerrit.GerritProject("tests/lava/test-definitions.git", gerrit_arm), |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 31 | "scp": gerrit.GerritProject("scp/firmware", gerrit_arm), |
| 32 | }, |
| 33 | |
| 34 | # Projects hosted on trustedfirmware.org Gerrit server. |
| 35 | "tforg": { |
| 36 | "trusted-firmware": gerrit.GerritProject("TF-A/trusted-firmware-a", gerrit_tforg), |
| 37 | "trusted-firmware-tf": gerrit.GerritProject("TF-A/tf-a-tests", gerrit_tforg), |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | # Argument setup |
| 42 | parser = argparse.ArgumentParser() |
| 43 | parser.add_argument("--project", "-p", |
| 44 | help="Gerrit project identifier this refspec belongs to") |
| 45 | parser.add_argument("--server", "-s", help="Gerrit server hosting this project", |
| 46 | choices=["arm", "tforg"]) |
| 47 | parser.add_argument("--user", "-u", |
| 48 | help="Username to use to query the Gerrit server") |
| 49 | parser.add_argument("--key", "-k", |
| 50 | help="SSH private key to use to authenticate with the Gerrit server") |
| 51 | parser.add_argument("refspec", help="Refspec to translate") |
| 52 | opts = parser.parse_args() |
| 53 | |
| 54 | project = projects[opts.server][opts.project] |
| 55 | |
| 56 | # Default action: print refspec and exit |
| 57 | def do_default(): |
| 58 | print(opts.refspec) |
| 59 | sys.exit(0) |
| 60 | |
| 61 | def print_topic_tip(query_results): |
| 62 | patchsets = [] |
| 63 | parents = [] |
| 64 | |
| 65 | # For each change, get its most recent patchset |
| 66 | for change in query_results: |
| 67 | patchsets.append(change["patchSets"][-1]) |
| 68 | |
| 69 | # For each patchset, get its parent commit |
| 70 | for patchset in patchsets: |
| 71 | parents.append(patchset["parents"][0]) |
| 72 | |
| 73 | # If a patchset's revision is NOT in the list of parents then it should |
| 74 | # be the tip commit |
| 75 | tips = list(filter(lambda x: x["revision"] not in parents, patchsets)) |
| 76 | |
| 77 | # There must be only one patchset remaining, otherwise the tip is ambiguous |
| 78 | if len(tips) > 1: |
| 79 | raise Exception("{} in {} has no unique tip commit.".format(opts.refspec, |
| 80 | opts.project)) |
| 81 | if len(tips) == 0: |
| 82 | raise Exception("No tip commit found for {} in {}.".format(opts.refspec, |
| 83 | opts.project)) |
| 84 | # Print the reference of the topic tip patchset |
| 85 | print(tips[0]["ref"]) |
| 86 | |
| 87 | query = ["status:open"] |
| 88 | |
| 89 | # If we don't understand the refspec, that's OK. We don't translate it, but |
| 90 | # print it as is. |
| 91 | try: |
| 92 | scheme, rest = opts.refspec.split(":") |
| 93 | if scheme == "topic": |
| 94 | query += ["topic:" + rest] |
| 95 | elif scheme == "change": |
| 96 | query += [opts.refspec] |
| 97 | else: |
| 98 | do_default() |
| 99 | except: |
| 100 | do_default() |
| 101 | |
| 102 | changes = project.query(query, username=opts.user, keyfile=opts.key) |
| 103 | |
| 104 | # The last object is a summary; drop it as it's not of interest to us. |
| 105 | changes.pop() |
| 106 | |
| 107 | if not changes: |
| 108 | raise Exception("{} for {} resolved to nothing.".format(opts.refspec, |
| 109 | opts.project)) |
| 110 | |
| 111 | if scheme == "topic": |
| 112 | if len(changes) > 1: |
| 113 | print_topic_tip(changes) |
| 114 | else: |
| 115 | print(changes[0]["currentPatchSet"]["ref"]) |
| 116 | elif scheme == "change": |
| 117 | if len(changes) > 1: |
| 118 | # When querying for a specific change there must be just a single result |
| 119 | raise Exception("{} for {} did not resolve uniquely.".format(opts.refspec, |
| 120 | opts.project)) |
| 121 | print(changes[0]["currentPatchSet"]["revision"]) |