blob: eba7844723e05e32988e220bc97f86ee893a568e [file] [log] [blame]
Fathi Boudra422bf772019-12-02 11:10:16 +02001#!/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
14import argparse
15import gerrit
16import sys
17
18# Gerrit servers we care about.
19gerrit_arm = gerrit.GerritServer("gerrit.oss.arm.com")
20gerrit_tforg = gerrit.GerritServer("review.trustedfirmware.org")
21
22# Trusted Firmware-A and associated projects.
23# Different projects are hosted on different Gerrit servers.
24projects = {
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),
Zelalem219df412020-05-17 19:21:20 -050030 "cc_plugin": gerrit.GerritProject("tests/lava/test-definitions.git", gerrit_arm),
Fathi Boudra422bf772019-12-02 11:10:16 +020031 "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),
Zelalem11ea6e02020-08-18 14:49:51 -050038 "trusted-firmware-ci": gerrit.GerritProject("ci/tf-a-ci-scripts", gerrit_tforg),
Fathi Boudra422bf772019-12-02 11:10:16 +020039 },
40}
41
42# Argument setup
43parser = argparse.ArgumentParser()
44parser.add_argument("--project", "-p",
45 help="Gerrit project identifier this refspec belongs to")
46parser.add_argument("--server", "-s", help="Gerrit server hosting this project",
47 choices=["arm", "tforg"])
48parser.add_argument("--user", "-u",
49 help="Username to use to query the Gerrit server")
50parser.add_argument("--key", "-k",
51 help="SSH private key to use to authenticate with the Gerrit server")
52parser.add_argument("refspec", help="Refspec to translate")
53opts = parser.parse_args()
54
55project = projects[opts.server][opts.project]
56
57# Default action: print refspec and exit
58def do_default():
59 print(opts.refspec)
60 sys.exit(0)
61
62def print_topic_tip(query_results):
63 patchsets = []
64 parents = []
65
66 # For each change, get its most recent patchset
67 for change in query_results:
68 patchsets.append(change["patchSets"][-1])
69
70 # For each patchset, get its parent commit
71 for patchset in patchsets:
72 parents.append(patchset["parents"][0])
73
74 # If a patchset's revision is NOT in the list of parents then it should
75 # be the tip commit
76 tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
77
78 # There must be only one patchset remaining, otherwise the tip is ambiguous
79 if len(tips) > 1:
80 raise Exception("{} in {} has no unique tip commit.".format(opts.refspec,
81 opts.project))
82 if len(tips) == 0:
83 raise Exception("No tip commit found for {} in {}.".format(opts.refspec,
84 opts.project))
85 # Print the reference of the topic tip patchset
86 print(tips[0]["ref"])
87
88query = ["status:open"]
89
90# If we don't understand the refspec, that's OK. We don't translate it, but
91# print it as is.
92try:
93 scheme, rest = opts.refspec.split(":")
94 if scheme == "topic":
95 query += ["topic:" + rest]
96 elif scheme == "change":
97 query += [opts.refspec]
98 else:
99 do_default()
100except:
101 do_default()
102
103changes = project.query(query, username=opts.user, keyfile=opts.key)
104
105# The last object is a summary; drop it as it's not of interest to us.
106changes.pop()
107
108if not changes:
109 raise Exception("{} for {} resolved to nothing.".format(opts.refspec,
110 opts.project))
111
112if scheme == "topic":
113 if len(changes) > 1:
114 print_topic_tip(changes)
115 else:
116 print(changes[0]["currentPatchSet"]["ref"])
117elif scheme == "change":
118 if len(changes) > 1:
119 # When querying for a specific change there must be just a single result
120 raise Exception("{} for {} did not resolve uniquely.".format(opts.refspec,
121 opts.project))
122 print(changes[0]["currentPatchSet"]["revision"])