From 0426f56edda47dfb8bac69ce1738b652a9a54bc1 Mon Sep 17 00:00:00 2001 From: Manoj M J <mmj@gitlab.com> Date: Tue, 9 Apr 2024 05:42:37 +0000 Subject: [PATCH] Housekeeper: Add support for `ci.skip` --- gems/gitlab-housekeeper/README.md | 3 +++ .../lib/gitlab/housekeeper/change.rb | 6 +++++- .../lib/gitlab/housekeeper/git.rb | 8 ++++++-- .../lib/gitlab/housekeeper/push_options.rb | 13 +++++++++++++ .../lib/gitlab/housekeeper/runner.rb | 7 +++++-- .../spec/gitlab/housekeeper/change_spec.rb | 1 + .../spec/gitlab/housekeeper/git_spec.rb | 17 +++++++++++++++-- .../gitlab/housekeeper/push_options_spec.rb | 13 +++++++++++++ .../spec/gitlab/housekeeper/runner_spec.rb | 10 +++++----- 9 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 gems/gitlab-housekeeper/lib/gitlab/housekeeper/push_options.rb create mode 100644 gems/gitlab-housekeeper/spec/gitlab/housekeeper/push_options_spec.rb diff --git a/gems/gitlab-housekeeper/README.md b/gems/gitlab-housekeeper/README.md index a4793aea9a62d..66c3f71578778 100644 --- a/gems/gitlab-housekeeper/README.md +++ b/gems/gitlab-housekeeper/README.md @@ -69,6 +69,9 @@ module Keeps change.changed_files = [file_name] + # to push changes without triggering a pipeline. + change.push_options.ci_skip = true + yield(change) end end diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/change.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/change.rb index 18551a9a2f875..f61377a841c41 100644 --- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/change.rb +++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/change.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'gitlab/housekeeper/push_options' + module Gitlab module Housekeeper class Change @@ -10,12 +12,14 @@ class Change :labels, :keep_class, :changelog_type, - :mr_web_url + :mr_web_url, + :push_options attr_reader :reviewers def initialize @labels = [] @reviewers = [] + @push_options = PushOptions.new end def reviewers=(reviewers) diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb index ab9d2555b76e1..8970eb9e5b7cb 100644 --- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb +++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/git.rb @@ -2,6 +2,7 @@ require 'logger' require 'gitlab/housekeeper/shell' +require 'gitlab/housekeeper/push_options' module Gitlab module Housekeeper @@ -46,8 +47,11 @@ def create_commit(change) Shell.execute("git", "commit", "-m", change.commit_message) end - def push(branch_name) - Shell.execute('git', 'push', '-f', housekeeper_remote, "#{branch_name}:#{branch_name}") + def push(branch_name, push_options = PushOptions.new) + push_command = ['git', 'push', '-f', housekeeper_remote, "#{branch_name}:#{branch_name}"] + push_command << '-o ci.skip' if push_options.ci_skip + + Shell.execute(*push_command) end private diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/push_options.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/push_options.rb new file mode 100644 index 0000000000000..ad3c3466e6e55 --- /dev/null +++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/push_options.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Gitlab + module Housekeeper + class PushOptions + attr_accessor :ci_skip + + def initialize + @ci_skip = false + end + end + end +end diff --git a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb index 3ce4da9a278b1..bd3624b56202a 100644 --- a/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb +++ b/gems/gitlab-housekeeper/lib/gitlab/housekeeper/runner.rb @@ -105,7 +105,10 @@ def require_keeps end def print_change_details(change, branch_name) - puts "Merge request URL: #{change.mr_web_url || '(known after create)'}, on branch #{branch_name}".yellowish + base_message = "Merge request URL: #{change.mr_web_url || '(known after create)'}, on branch #{branch_name}." + base_message << " CI skipped." if change.push_options.ci_skip + + puts base_message.yellowish puts "=> #{change.identifiers.join(': ')}".purple puts '=> Title:'.purple @@ -137,7 +140,7 @@ def create(change, branch_name) target_project_id: housekeeper_target_project_id ) - git.push(branch_name) unless non_housekeeper_changes.include?(:code) + git.push(branch_name, change.push_options) unless non_housekeeper_changes.include?(:code) gitlab_client.create_or_update_merge_request( change: change, diff --git a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/change_spec.rb b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/change_spec.rb index a4a2ed678d8a6..6bd99dde1a2ec 100644 --- a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/change_spec.rb +++ b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/change_spec.rb @@ -17,6 +17,7 @@ expect(change.labels).to eq([]) expect(change.reviewers).to eq([]) + expect(change.push_options.ci_skip).to eq(false) end end diff --git a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb index 3489b002d6e67..38e567468900a 100644 --- a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb +++ b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/git_spec.rb @@ -130,6 +130,8 @@ def setup_and_checkout_another_branch end describe '#push' do + let(:push_options) { ::Gitlab::Housekeeper::PushOptions.new } + before do # Needed to check if remote exists allow(::Gitlab::Housekeeper::Shell).to receive(:execute) @@ -152,7 +154,7 @@ def setup_and_checkout_another_branch expect(::Gitlab::Housekeeper::Shell).to receive(:execute) .with('git', 'push', '-f', 'housekeeper', 'the-branch-name:the-branch-name') - git.push('the-branch-name') + git.push('the-branch-name', push_options) end end @@ -160,7 +162,18 @@ def setup_and_checkout_another_branch expect(::Gitlab::Housekeeper::Shell).to receive(:execute) .with('git', 'push', '-f', 'origin', 'the-branch-name:the-branch-name') - git.push('the-branch-name') + git.push('the-branch-name', push_options) + end + + context 'with push options defined' do + it 'pushes with push options set' do + expect(::Gitlab::Housekeeper::Shell).to receive(:execute) + .with('git', 'push', '-f', 'origin', 'the-branch-name:the-branch-name', '-o ci.skip') + + push_options.ci_skip = true + + git.push('the-branch-name', push_options) + end end end end diff --git a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/push_options_spec.rb b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/push_options_spec.rb new file mode 100644 index 0000000000000..6dbb1274b77da --- /dev/null +++ b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/push_options_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'gitlab/housekeeper/push_options' + +RSpec.describe ::Gitlab::Housekeeper::PushOptions do + describe '#initialize' do + it 'sets ci_skip to false by default' do + push_options = described_class.new + expect(push_options.ci_skip).to be false + end + end +end diff --git a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/runner_spec.rb b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/runner_spec.rb index 6acbf64a45d83..d0b7521879aee 100644 --- a/gems/gitlab-housekeeper/spec/gitlab/housekeeper/runner_spec.rb +++ b/gems/gitlab-housekeeper/spec/gitlab/housekeeper/runner_spec.rb @@ -95,12 +95,12 @@ .with('git', '--no-pager', 'diff', '--color=always', 'master', 'the-identifier-for-the-first-change', '--', 'change1.txt', 'change2.txt') expect(git).to receive(:push) - .with('the-identifier-for-the-first-change') + .with('the-identifier-for-the-first-change', change1.push_options) expect(::Gitlab::Housekeeper::Shell).to receive(:execute) .with('git', '--no-pager', 'diff', '--color=always', 'master', 'the-identifier-for-the-second-change', '--', 'change1.txt', 'change2.txt') expect(git).to receive(:push) - .with('the-identifier-for-the-second-change') + .with('the-identifier-for-the-second-change', change2.push_options) # Merge requests get created expect(gitlab_client).to receive(:create_or_update_merge_request) @@ -163,7 +163,7 @@ .with('git', '--no-pager', 'diff', '--color=always', 'master', 'the-identifier-for-the-second-change', '--', 'change1.txt', 'change2.txt') expect(git).to receive(:push) - .with('the-identifier-for-the-second-change') + .with('the-identifier-for-the-second-change', change2.push_options) # Merge requests get created expect(gitlab_client).to receive(:create_or_update_merge_request) @@ -207,9 +207,9 @@ expect(::Gitlab::Housekeeper::Substitutor).to receive(:perform).with(change2) expect(git).not_to receive(:push) - .with('the-identifier-for-the-first-change') + .with('the-identifier-for-the-first-change', change1.push_options) expect(git).to receive(:push) - .with('the-identifier-for-the-second-change') + .with('the-identifier-for-the-second-change', change2.push_options) expect(gitlab_client).to receive(:create_or_update_merge_request) .with( -- GitLab