From e22b96e7b313f0e7c7bf164d2c2f6c22108e7055 Mon Sep 17 00:00:00 2001
From: Felipe Santos <felipecassiors@gmail.com>
Date: Sat, 30 Nov 2024 06:49:26 -0300
Subject: [PATCH] fix(gerrit): `getBranchStatus` not returning `red` for failed
 checks (#32812)

---
 lib/modules/platform/gerrit/client.ts     |  2 +-
 lib/modules/platform/gerrit/index.spec.ts | 28 +++++++++++++++++++++++
 lib/modules/platform/gerrit/index.ts      | 14 +++++++++---
 lib/modules/platform/gerrit/types.ts      |  2 ++
 4 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/lib/modules/platform/gerrit/client.ts b/lib/modules/platform/gerrit/client.ts
index 4f14d46919..fbc392765b 100644
--- a/lib/modules/platform/gerrit/client.ts
+++ b/lib/modules/platform/gerrit/client.ts
@@ -18,7 +18,7 @@ const QUOTES_REGEX = regEx('"', 'g');
 class GerritClient {
   private requestDetails = [
     'SUBMITTABLE', //include the submittable field in ChangeInfo, which can be used to tell if the change is reviewed and ready for submit.
-    'CHECK', // include potential problems with the change.
+    'CHECK', // include potential consistency problems with the change (not related to labels)
     'MESSAGES',
     'DETAILED_ACCOUNTS',
     'LABELS',
diff --git a/lib/modules/platform/gerrit/index.spec.ts b/lib/modules/platform/gerrit/index.spec.ts
index 480c39e7ae..3b9c456f18 100644
--- a/lib/modules/platform/gerrit/index.spec.ts
+++ b/lib/modules/platform/gerrit/index.spec.ts
@@ -453,6 +453,26 @@ describe('modules/platform/gerrit/index', () => {
         gerrit.getBranchStatus('renovate/dependency-1.x'),
       ).resolves.toBe('red');
     });
+
+    it('getBranchStatus() - branchname/changes found and hasBlockingLabels but no problems => red', async () => {
+      const submittableChange = partial<GerritChange>({
+        submittable: true,
+        problems: [],
+      });
+      const changeWithProblems = { ...submittableChange };
+      changeWithProblems.submittable = false;
+      changeWithProblems.problems = [];
+      changeWithProblems.labels = {
+        Verified: { blocking: true },
+      };
+      clientMock.findChanges.mockResolvedValueOnce([
+        changeWithProblems,
+        submittableChange,
+      ]);
+      await expect(
+        gerrit.getBranchStatus('renovate/dependency-1.x'),
+      ).resolves.toBe('red');
+    });
   });
 
   describe('getBranchStatusCheck()', () => {
@@ -496,6 +516,14 @@ describe('modules/platform/gerrit/index', () => {
           labelValue: { approved: partial<GerritAccountInfo>({}) },
           expectedState: 'green' as BranchStatus,
         },
+        {
+          label: 'Renovate-Merge-Confidence',
+          labelValue: {
+            approved: partial<GerritAccountInfo>({}),
+            rejected: partial<GerritAccountInfo>({}),
+          },
+          expectedState: 'red' as BranchStatus,
+        },
       ])('$ctx/$labels', async ({ label, labelValue, expectedState }) => {
         const change = partial<GerritChange>({
           labels: {
diff --git a/lib/modules/platform/gerrit/index.ts b/lib/modules/platform/gerrit/index.ts
index 06a756b82d..234d996563 100644
--- a/lib/modules/platform/gerrit/index.ts
+++ b/lib/modules/platform/gerrit/index.ts
@@ -258,6 +258,13 @@ export async function getBranchStatus(
     if (hasProblems) {
       return 'red';
     }
+    const hasBlockingLabels =
+      changes.filter((change) =>
+        Object.values(change.labels ?? {}).some((label) => label.blocking),
+      ).length > 0;
+    if (hasBlockingLabels) {
+      return 'red';
+    }
   }
   return 'yellow';
 }
@@ -284,12 +291,13 @@ export async function getBranchStatusCheck(
     if (change) {
       const labelRes = change.labels?.[context];
       if (labelRes) {
-        if (labelRes.approved) {
-          return 'green';
-        }
+        // Check for rejected first, as a label could have both rejected and approved
         if (labelRes.rejected) {
           return 'red';
         }
+        if (labelRes.approved) {
+          return 'green';
+        }
       }
     }
   }
diff --git a/lib/modules/platform/gerrit/types.ts b/lib/modules/platform/gerrit/types.ts
index 0d1b5d90fe..a6ddf07fb4 100644
--- a/lib/modules/platform/gerrit/types.ts
+++ b/lib/modules/platform/gerrit/types.ts
@@ -77,6 +77,8 @@ export interface GerritChangeMessageInfo {
 export interface GerritLabelInfo {
   approved?: GerritAccountInfo;
   rejected?: GerritAccountInfo;
+  /** If true, the label blocks submit operation. If not set, the default is false. */
+  blocking?: boolean;
 }
 
 export interface GerritActionInfo {
-- 
GitLab