From 2c8f9e779d689cc31d0a0f7a3f72ed9597ff9dfe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9my=20Coutable?= <remy@rymai.me>
Date: Mon, 22 Apr 2024 09:25:25 +0200
Subject: [PATCH] Don't checkout master when running gitlab-housekeeper
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Rémy Coutable <remy@rymai.me>
---
 gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb  | 10 +++-------
 .../lib/gitlab/housekeeper/runner.rb                   |  4 ++--
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
index 8970eb9e5b7cb..ed2970cb9e82b 100644
--- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
+++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb
@@ -15,21 +15,17 @@ def initialize(logger:, branch_from: 'master')
         @branch_from = branch_from
       end
 
-      def with_clean_state
+      def with_clean_state(&block)
         result = Shell.execute('git', 'stash')
         stashed = !result.include?('No local changes to save')
 
-        with_return_to_current_branch(stashed: stashed) do
-          checkout_branch(@branch_from)
-
-          yield
-        end
+        with_return_to_current_branch(stashed: stashed, &block)
       end
 
       def create_branch(change)
         branch_name = branch_name(change.identifiers)
 
-        Shell.execute("git", "branch", "-f", branch_name)
+        Shell.execute("git", "branch", "-f", branch_name, @branch_from)
 
         branch_name
       end
diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb
index 15d881d59fa71..5c9c91f174864 100644
--- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb
+++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb
@@ -68,9 +68,9 @@ def run
 
               # If no merge request exists yet, create an empty one to allow keeps to use the web URL.
               unless @dry_run
-                merge_reqeust = get_existing_merge_request(branch_name) || create(change, branch_name)
+                merge_request = get_existing_merge_request(branch_name) || create(change, branch_name)
 
-                change.mr_web_url = merge_reqeust['web_url']
+                change.mr_web_url = merge_request['web_url']
               end
 
               git.in_branch(branch_name) do
-- 
GitLab