From 1554cc68d7c0f7b32fe1b48af0d23036841b152b Mon Sep 17 00:00:00 2001
From: Simon Le Bourdais-Cabana <simonlbc@gmail.com>
Date: Fri, 15 Mar 2024 04:43:38 -0400
Subject: [PATCH] fix(azure): Support exact branch merge policies. (#27918)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 .../platform/azure/azure-helper.spec.ts       | 34 ++++++++++++++++++-
 lib/modules/platform/azure/azure-helper.ts    |  2 +-
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/lib/modules/platform/azure/azure-helper.spec.ts b/lib/modules/platform/azure/azure-helper.spec.ts
index 1e7a722ab3..2d4430dd37 100644
--- a/lib/modules/platform/azure/azure-helper.spec.ts
+++ b/lib/modules/platform/azure/azure-helper.spec.ts
@@ -1,5 +1,8 @@
 import { Readable } from 'node:stream';
-import { GitPullRequestMergeStrategy } from 'azure-devops-node-api/interfaces/GitInterfaces.js';
+import type { IPolicyApi } from 'azure-devops-node-api/PolicyApi';
+import { GitPullRequestMergeStrategy } from 'azure-devops-node-api/interfaces/GitInterfaces';
+import type { PolicyConfiguration } from 'azure-devops-node-api/interfaces/PolicyInterfaces';
+import { partial } from '../../../../test/util';
 
 jest.mock('./azure-got-wrapper');
 
@@ -238,6 +241,35 @@ describe('modules/platform/azure/azure-helper', () => {
       );
     });
 
+    it('should return Squash when Project wide exact branch policy exists', async () => {
+      const refMock = 'refs/heads/ding';
+
+      azureApi.policyApi.mockResolvedValueOnce(
+        partial<IPolicyApi>({
+          getPolicyConfigurations: jest.fn(() =>
+            Promise.resolve([
+              partial<PolicyConfiguration>({
+                settings: {
+                  allowSquash: true,
+                  scope: [
+                    {
+                      // null here means project wide
+                      repositoryId: null,
+                      matchKind: 'Exact',
+                      refName: refMock,
+                    },
+                  ],
+                },
+              }),
+            ]),
+          ),
+        }),
+      );
+      expect(await azureHelper.getMergeMethod('', '', refMock)).toEqual(
+        GitPullRequestMergeStrategy.Squash,
+      );
+    });
+
     it('should return default branch policy', async () => {
       azureApi.policyApi.mockImplementationOnce(
         () =>
diff --git a/lib/modules/platform/azure/azure-helper.ts b/lib/modules/platform/azure/azure-helper.ts
index e215c7a7c8..ff63ce2843 100644
--- a/lib/modules/platform/azure/azure-helper.ts
+++ b/lib/modules/platform/azure/azure-helper.ts
@@ -135,7 +135,7 @@ export async function getMergeMethod(
     ) {
       return true;
     }
-    if (scope.repositoryId !== repoId) {
+    if (scope.repositoryId !== repoId && scope.repositoryId !== null) {
       return false;
     }
     if (!branchRef) {
-- 
GitLab