clone: repo synchronization based on Gerrit topic
If the CI job was triggered by a Gerrit commit which carries a topic,
then the clone.sh script will attempt to synchronize other repositories
to the branch that matches the Gerrit topic name. [1]
[1]: https://linaro.atlassian.net/browse/TFC-75
Signed-off-by: Arthur She <arthur.she@linaro.org>
Change-Id: I7ed181cfb02669d03c84ab90ebdc38f57eaeda80
diff --git a/scripts/parse_refspec.py b/scripts/parse_refspec.py
new file mode 100755
index 0000000..83fd30e
--- /dev/null
+++ b/scripts/parse_refspec.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+#
+# Copyright (c) 2019-2022 Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+# This script parse the gerrit query results from the stdin
+# and return the correct refspec
+
+import sys
+import json
+
+def print_topic_tip(query_results):
+ patchsets = []
+ parents = []
+ project = query_results[0]["project"]
+ topic = query_results[0]["topic"]
+
+ # For each change, get its most recent patchset
+ for change in query_results:
+ patchsets.append(change["patchSets"][-1])
+
+ # For each patchset, get its parent commit
+ for patchset in patchsets:
+ parents.append(patchset["parents"][0])
+
+ # If a patchset's revision is NOT in the list of parents then it should
+ # be the tip commit
+ tips = list(filter(lambda x: x["revision"] not in parents, patchsets))
+
+ # There must be only one patchset remaining, otherwise the tip is ambiguous
+ if len(tips) > 1:
+ raise Exception("{} in {} has no unique tip commit.".format(topic, project))
+ if len(tips) == 0:
+ raise Exception("No tip commit found for {} in {}.".format(topic, project))
+ # Print the reference of the topic tip patchset
+ print(tips[0]["ref"])
+
+try:
+ changes = [json.loads(resp_line) for resp_line in sys.stdin]
+except:
+ raise Exception("Input error, it's not a JSON string!")
+
+# The last object is a summary; drop it as it's not of interest to us.
+changes.pop()
+
+if not changes:
+ raise Exception("Can not find anything.")
+
+if len(changes) > 1:
+ print_topic_tip(changes)
+else:
+ print(changes[0]["currentPatchSet"]["ref"])