build-unit-test-docker: enable topic-based testing
There are cases when a phosphor-dbus-interfaces change is necessary
before (or at the same time) as a commit in another repository.
Maintainers will sometimes ignore review of a commit with failing CI
which prevents getting feedback necessary for the reviewer to move
forward with the entire set of changes. Enable topic-based testing
in CI, which will prime the Docker container dependencies from a topic
on Gerrit.
In order to simplify this implementation and avoid having to figure out
commit application order (potentially across splits in the Git tree),
the topic must only have one commit from each repository.
Tested:
Confirmed both phosphor-dbus-interfaces and phosphor-logging selected
commits from the topic when building the Docker container with:
```
$ GERRIT_TOPIC=logging-create-with-path scripts/build-unit-test-docker
```
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I71831d992ec79a9d820456bd4a70e02d9000ebc2
diff --git a/scripts/build-unit-test-docker b/scripts/build-unit-test-docker
index 4af1f8d..56dd211 100755
--- a/scripts/build-unit-test-docker
+++ b/scripts/build-unit-test-docker
@@ -20,10 +20,12 @@
# http_proxy The HTTP address of the proxy server to connect to.
# Default: "", proxy is not setup if this is not set
+import json
import os
import re
import sys
import threading
+import urllib.request
from datetime import date
from hashlib import sha256
@@ -407,12 +409,66 @@
depcache += "%s:%s," % (pkg, cls.packages[pkg]["rev"])
return depcache
+ def _check_gerrit_topic(self) -> bool:
+ if not gerrit_topic:
+ return False
+ if not self.package.startswith("openbmc/"):
+ return False
+ if gerrit_project == self.package and gerrit_rev:
+ return False
+
+ try:
+ commits = json.loads(
+ urllib.request.urlopen(
+ f"https://gerrit.openbmc.org/changes/?q=status:open+project:{self.package}+topic:{gerrit_topic}"
+ )
+ .read()
+ .splitlines()[-1]
+ )
+
+ if len(commits) == 0:
+ return False
+ if len(commits) > 1:
+ print(
+ f"{self.package} has more than 1 commit under {gerrit_topic}; using lastest upstream: {len(commits)}",
+ file=sys.stderr,
+ )
+ return False
+
+ change_id = commits[0]["id"]
+
+ commit = json.loads(
+ urllib.request.urlopen(
+ f"https://gerrit.openbmc.org/changes/{change_id}/revisions/current/commit"
+ )
+ .read()
+ .splitlines()[-1]
+ )["commit"]
+
+ print(
+ f"Using {commit} from {gerrit_topic} for {self.package}",
+ file=sys.stderr,
+ )
+ self.pkg_def["rev"] = commit
+ return True
+
+ except urllib.error.HTTPError as e:
+ print(
+ f"Error loading topic {gerrit_topic} for {self.package}: ",
+ e,
+ file=sys.stderr,
+ )
+ return False
+
def _update_rev(self) -> None:
"""Look up the HEAD for missing a static rev."""
if "rev" in self.pkg_def:
return
+ if self._check_gerrit_topic():
+ return
+
# Check if Jenkins/Gerrit gave us a revision and use it.
if gerrit_project == self.package and gerrit_rev:
print(
@@ -648,6 +704,7 @@
gerrit_project = os.environ.get("GERRIT_PROJECT")
gerrit_rev = os.environ.get("GERRIT_PATCHSET_REVISION")
+gerrit_topic = os.environ.get("GERRIT_TOPIC")
# Ensure appropriate docker build output to see progress and identify
# any issues