Depends-on: take into consideration the git branch for query and default
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Fri, 18 Feb 2022 19:43:01 +0000 (14:43 -0500)
committerJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Fri, 18 Feb 2022 20:51:19 +0000 (15:51 -0500)
Since gerrit cherry-picks have the same change-id the current query
returns multiples objects which is not supported. To alleviate this, we
use the GERRIT_BRANCH to refine the query. This is only valid for
project with the same version/name scheme at the branch level
(lttng-ust, lttng-modules). For now we need to handle this mostly for
those projects, it is quite rare that we need to handle this for
babeltrace and userspace-rcu.

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Change-Id: I5171d89083cbbb94e168158a11c69edebd4a766e

scripts/lttng-tools/gerrit-depends-on.sh

index b917c23aecf77a57d7f2e79617f8077a0aa8ec4e..33fd9d48ddc0e6ecd6e7b6683ac2d29fdf9e5130 100755 (executable)
@@ -24,9 +24,9 @@ WORKSPACE=${WORKSPACE:-}
 conf=${conf:-}
 
 gerrit_url="https://${GERRIT_NAME}"
-gerrit_query="?o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS"
-gerrit_json_query=".revisions[.current_revision].ref"
-gerrit_json_query_status=".status"
+gerrit_query="&o=CURRENT_REVISION&o=DOWNLOAD_COMMANDS"
+gerrit_json_query=".[0].revisions[.[0].current_revision].ref"
+gerrit_json_query_status=".[0].status"
 
 possible_depends_on="lttng-ust|lttng-modules|userspace-rcu"
 re="Depends-on: (${possible_depends_on}): ([^'$'\n'']*)"
@@ -75,23 +75,52 @@ git rev-list --format=%B --max-count=1 HEAD | while read -r line; do
     echo "${project_sanitize^^}_RUN_TESTS=no" >> "$property_file"
 
     # Get the change latest ref
-    ref=$(curl "${gerrit_url}/changes/${gerrit_id}${gerrit_query}" | tail -n+2 | jq -r "$gerrit_json_query")
-    change_status=$(curl "${gerrit_url}/changes/${gerrit_id}${gerrit_query}" | tail -n+2 | jq -r "$gerrit_json_query_status")
+    case $project in
+        lttng-*)
+            # This is necessary since a cherry pick can have the same change id
+            # across branches. Still this is only valid for projects where the
+            # branch name fits the same branch name style of the lttng-tools
+            # project.
+            # We will need to be much more clever if the situation arise where
+            # we need to depends-on a cherry picked change id for the
+            # userspace-rcu or babeltrace project. Until then let's use this
+            # hack. The quick solution to this is to tell the committer to change
+            # the change id. We could also be clever and require that the
+            # "branch name" be included in the `Depends-on` clause.
+            local_query="${gerrit_url}/changes/?q=change:${gerrit_id}+branch:${GERRIT_BRANCH}${gerrit_query}"
+           default_branch="${GERRIT_BRANCH}"
+            ;;
+        *)
+            local_query="${gerrit_url}/changes/?q=change:${gerrit_id}${gerrit_query}"
+           default_branch="master"
+            ;;
+    esac
+
+    json_doc=$(curl "$local_query" | tail -n+2)
+    count=$(jq -r '. | length' <<< "$json_doc")
+    if [ "$count" != "1" ]; then
+           echo "Expected an array of size 1 got $count"
+           exit 1
+    fi
+
+    ref=$(jq -r "$gerrit_json_query" <<< "$json_doc")
+    change_status=$(jq -r "$gerrit_json_query_status" <<< "$json_doc")
     if [ "$change_status" == "MERGED" ]; then
            # When the change we depends on is merged use master as the ref.
            # This is not ideal CI time wise since we do not reuse artifacts.
            # This solve a tricky situation where we actually want to use master
            # instead of the change available on gerrit. Intermediary changes
            # present in master could have an impact on the change under test.
-           ref="refs/heads/master"
+           echo "Depends-on change is MERGED. Defaulting to ${default_branch}"
+           ref="refs/heads/$default_branch"
     elif [ "$change_status" == "ABANDONED" ]; then
            # We have a situation where the "HEAD" commit for feature branch are
            # not merged and abandoned. Default to the master branch and hope
            # for the best. This is far from ideal but we need might also need
            # to find a better way to handle feature branch here. In the
            # meantime use master for such cases.
-           echo "Depends-on change is ABANDONED. Defaulting to master"
-           ref="refs/heads/master"
+           echo "Depends-on change is ABANDONED. Defaulting to ${default_branch}"
+           ref="refs/heads/${default_branch}"
     fi
 
     # The build.sh script from userspace-rcu expects the source to be located in
This page took 0.02422 seconds and 4 git commands to generate.