From bfa6c49b69d16087dcaa4101d63e18a2b8618f5f Mon Sep 17 00:00:00 2001
From: Jerry Seto <jseto@gitlab.com>
Date: Mon, 18 Sep 2023 12:43:44 +0000
Subject: [PATCH] Allow using only mirror param for updates

Setting the `mirror` parameter to false is sufficient to remove pull
mirroring from a project.  This change is to allow that parameter to
be used in the absence of other parameters

Contributes to: https://gitlab.com/gitlab-org/gitlab/-/issues/356154
---
 doc/api/projects.md                       | 30 +++++++++++++++++++++++
 ee/lib/ee/api/helpers/projects_helpers.rb |  1 +
 ee/spec/requests/api/projects_spec.rb     | 12 +++++++++
 3 files changed, 43 insertions(+)

diff --git a/doc/api/projects.md b/doc/api/projects.md
index 385c3eaf468a8..7ae9c599ce24e 100644
--- a/doc/api/projects.md
+++ b/doc/api/projects.md
@@ -3226,6 +3226,36 @@ with the API scope enabled.
 | `only_mirror_protected_branches`| boolean | No | Limits mirroring to only protected branches when set to `true`. |
 | `mirror_branch_regex`            | String  | No | Contains a regular expression. Only branches with names matching the regex are mirrored. Requires `only_mirror_protected_branches` to be disabled. |
 
+Example creating a project with pull mirroring:
+
+```shell
+curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
+ --header "Content-Type: application/json" \
+ --data '{
+  "name": "new_project",
+  "namespace_id": "1",
+  "mirror": true,
+  "import_url": "https://username:token@gitlab.example.com/group/project.git"
+ }' \
+ --url "https://gitlab.example.com/api/v4/projects/"
+```
+
+Example adding pull mirroring:
+
+```shell
+curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" \
+ --url "https://gitlab.example.com/api/v4/projects/:id" \
+ --data "mirror=true&import_url=https://username:token@gitlab.example.com/group/project.git"
+```
+
+Example removing pull mirroring:
+
+```shell
+curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" \
+ --url "https://gitlab.example.com/api/v4/projects/:id"  \
+ --data "mirror=false"
+```
+
 ## Start the pull mirroring process for a Project **(PREMIUM ALL)**
 
 > Moved to GitLab Premium in 13.9.
diff --git a/ee/lib/ee/api/helpers/projects_helpers.rb b/ee/lib/ee/api/helpers/projects_helpers.rb
index eb4e53550ed12..99ad272eda1ae 100644
--- a/ee/lib/ee/api/helpers/projects_helpers.rb
+++ b/ee/lib/ee/api/helpers/projects_helpers.rb
@@ -62,6 +62,7 @@ def update_params_at_least_one_of
               :fallback_approvals_required,
               :import_url,
               :issues_template,
+              :mirror,
               :merge_requests_template,
               :merge_pipelines_enabled,
               :merge_trains_enabled,
diff --git a/ee/spec/requests/api/projects_spec.rb b/ee/spec/requests/api/projects_spec.rb
index c7a9a2ac0c1d5..b785c50c1594a 100644
--- a/ee/spec/requests/api/projects_spec.rb
+++ b/ee/spec/requests/api/projects_spec.rb
@@ -1310,6 +1310,18 @@
         )
       end
 
+      context 'when only disabling pull mirror' do
+        let(:project) { create(:project, mirror: true, import_url: import_url, mirror_user: user, namespace: user.namespace) }
+        let(:project_params) do
+          { mirror: false }
+        end
+
+        it 'updates mirror to false' do
+          expect { subject }.to change { project.reload.mirror }.from(true).to(false)
+          expect(response).to have_gitlab_http_status(:ok)
+        end
+      end
+
       it 'updates project without mirror attributes when the project is unable to set up repository mirroring' do
         stub_licensed_features(repository_mirrors: false)
 
-- 
GitLab