From 7c8e3121af8a8276dfa14ab108f9fa145a00bad7 Mon Sep 17 00:00:00 2001
From: Vasilii Iakliushin <viakliushin@gitlab.com>
Date: Wed, 17 Apr 2024 15:55:14 +0200
Subject: [PATCH] Add missing handler for protected branches Deploy key

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/452117

**Problem**

We don't provide an ID of the modified deploy key. Because of that
backend tries to create a deploy key, but fails due to uniqueness check.

**Solution**

Return id for deploy key elements

Changelog: fixed
---
 .../protected_branch_edit.js                  |  8 ++++++++
 .../protected_branch_edit_spec.js             | 20 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/app/assets/javascripts/protected_branches/protected_branch_edit.js b/app/assets/javascripts/protected_branches/protected_branch_edit.js
index 67ae33e1fc83..22973ae318bc 100644
--- a/app/assets/javascripts/protected_branches/protected_branch_edit.js
+++ b/app/assets/javascripts/protected_branches/protected_branch_edit.js
@@ -182,6 +182,14 @@ export default class ProtectedBranchEdit {
           persisted: true,
         };
       }
+      if (currentItem.deploy_key_id) {
+        return {
+          id: currentItem.id,
+          deploy_key_id: currentItem.deploy_key_id,
+          type: LEVEL_TYPES.DEPLOY_KEY,
+          persisted: true,
+        };
+      }
 
       return {
         id: currentItem.id,
diff --git a/spec/frontend/protected_branches/protected_branch_edit_spec.js b/spec/frontend/protected_branches/protected_branch_edit_spec.js
index 301b0e8e1572..463354ab7e1e 100644
--- a/spec/frontend/protected_branches/protected_branch_edit_spec.js
+++ b/spec/frontend/protected_branches/protected_branch_edit_spec.js
@@ -43,6 +43,11 @@ const response = {
       group_id: null,
       deploy_key_id: null,
     },
+    {
+      id: 39,
+      access_level: 40,
+      deploy_key_id: 45,
+    },
   ],
 };
 
@@ -193,6 +198,21 @@ describe('ProtectedBranchEdit', () => {
         expect(dropdown.preselected[0].id).toBe(response.push_access_levels[0].id);
       });
 
+      it('updates deploy key on save for enabled dropdowns', async () => {
+        const selectedValue = [{ deploy_key_id: 45 }];
+        const ProtectedBranchEditInstance = create({});
+        const dropdown = ProtectedBranchEditInstance.push_access_levels_dropdown;
+        dropdown.$emit('select', selectedValue);
+        dropdown.$emit('hidden');
+        await waitForPromises();
+        expect(dropdown.preselected[1]).toEqual({
+          deploy_key_id: 45,
+          id: 39,
+          persisted: true,
+          type: 'deploy_key',
+        });
+      });
+
       it('does not update selected item on save for disabled dropdowns', async () => {
         const selectedValue = [{ access_level: 40 }];
         const ProtectedBranchEditInstance = create({ pushDisabled: '' });
-- 
GitLab