From 74f9d472bfb778e2afdc3c2cbe8e18c84eba5595 Mon Sep 17 00:00:00 2001
From: Jennifer Li <jli@gitlab.com>
Date: Fri, 8 Mar 2024 12:11:52 +0000
Subject: [PATCH] This reverts merge request !144691

---
 .gitlab/ci/rules.gitlab-ci.yml                |  9 --
 .gitlab/ci/setup.gitlab-ci.yml                | 12 ---
 scripts/check_default_pipeline_status.rb      | 97 -------------------
 .../check_default_pipeline_status_spec.rb     | 93 ------------------
 4 files changed, 211 deletions(-)
 delete mode 100755 scripts/check_default_pipeline_status.rb
 delete mode 100644 spec/scripts/check_default_pipeline_status_spec.rb

diff --git a/.gitlab/ci/rules.gitlab-ci.yml b/.gitlab/ci/rules.gitlab-ci.yml
index 3a51e040e0f0d..7f80b042cade2 100644
--- a/.gitlab/ci/rules.gitlab-ci.yml
+++ b/.gitlab/ci/rules.gitlab-ci.yml
@@ -2761,15 +2761,6 @@
   rules:
     - if: '$CI_FETCH_REPO_GIT_STRATEGY == "none"'
 
-.setup:rules:check-default-pipeline-status:
-  rules:
-    - <<: *if-merge-request-labels-pipeline-expedite
-      when: never
-    - if: '$CI_MERGE_REQUEST_LABELS =~ /pipeline:ignore-master-status/'
-      when: never
-    - <<: *if-default-refs
-      allow_failure: true
-
 .setup:rules:gitlab_git_test:
   rules:
     - <<: *if-merge-request-labels-pipeline-expedite
diff --git a/.gitlab/ci/setup.gitlab-ci.yml b/.gitlab/ci/setup.gitlab-ci.yml
index fa6707bd8516c..e2e9508200154 100644
--- a/.gitlab/ci/setup.gitlab-ci.yml
+++ b/.gitlab/ci/setup.gitlab-ci.yml
@@ -57,18 +57,6 @@ clone-gitlab-repo:
       - '*'
     expire_in: '12 hours'
 
-master-pipeline-status:
-  extends:
-    - .default-retry
-    - .setup:rules:check-default-pipeline-status
-  stage: sync
-  image: ${GITLAB_DEPENDENCY_PROXY_ADDRESS}ruby:${RUBY_VERSION}-slim
-  before_script:
-    - source ./scripts/utils.sh
-    - install_gitlab_gem
-  script:
-    - scripts/check_default_pipeline_status.rb
-
 gitlab_git_test:
   extends:
     - .predictive-job
diff --git a/scripts/check_default_pipeline_status.rb b/scripts/check_default_pipeline_status.rb
deleted file mode 100755
index 510eab148639f..0000000000000
--- a/scripts/check_default_pipeline_status.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-#!/usr/bin/env ruby
-
-# frozen_string_literal: true
-
-require 'gitlab'
-require 'optparse'
-
-require_relative 'api/default_options'
-
-class CheckDefaultPipelineStatus
-  def initialize(options)
-    @options = options
-    @project = @options.fetch(:project)
-    api_token = @options.fetch(:api_token, '')
-
-    warn "No API token given." if api_token.empty?
-
-    @api_client = ::Gitlab.client(
-      endpoint: options.fetch(:endpoint),
-      private_token: api_token
-    )
-  end
-
-  def execute
-    return unless last_completed_default_pipeline&.status == 'failed' && jobs_expected_to_fail.any?
-
-    warn error_message
-
-    exit 1
-  end
-
-  private
-
-  attr_reader :project, :api_client, :options
-
-  def last_completed_default_pipeline
-    @last_completed_default_pipeline ||= api_client.pipelines(
-      project,
-      ref: 'master',
-      scope: 'finished',
-      per_page: 1
-    ).first
-  end
-
-  def jobs_expected_to_fail
-    @jobs_expected_to_fail ||= failed_jobs_from_last_completed_default_pipeline.select do |failed_job|
-      !failed_job.allow_failure && job_names_in_current_pipeline.include?(failed_job.name)
-    end
-  end
-
-  def failed_jobs_from_last_completed_default_pipeline
-    @failed_jobs_from_last_completed_default_pipeline ||= api_client.pipeline_jobs(
-      project,
-      last_completed_default_pipeline.id,
-      scope: 'failed'
-    ).auto_paginate
-  end
-
-  def job_names_in_current_pipeline
-    @job_names_in_current_pipeline ||= begin
-      jobs = api_client.pipeline_jobs(
-        project,
-        current_pipeline_id
-      ).auto_paginate
-
-      jobs.map(&:name)
-    end
-  end
-
-  def current_pipeline_id
-    ENV['CI_PIPELINE_ID']
-  end
-
-  def error_message
-    <<~TEXT
-      ******************************************************************************************
-      We are failing this job to warn you that you may be impacted by a master broken incident.
-      Jobs below may fail in your pipeline:
-      #{jobs_expected_to_fail.map(&:name).join("\n")}
-      Check if the failures are also present in the master pipeline: #{last_completed_default_pipeline&.web_url}
-      Reach out to #master-broken for assistance if you think you are blocked.
-      Apply ~"pipeline:ignore-master-status" to skip this job if you don't think this is helpful.
-      ******************************************************************************************
-    TEXT
-  end
-end
-
-if $PROGRAM_NAME == __FILE__
-  OptionParser.new do |opts|
-    opts.on("-h", "--help", "Prints this help") do
-      puts opts
-      exit
-    end
-  end.parse!
-
-  CheckDefaultPipelineStatus.new(API::DEFAULT_OPTIONS).execute
-end
diff --git a/spec/scripts/check_default_pipeline_status_spec.rb b/spec/scripts/check_default_pipeline_status_spec.rb
deleted file mode 100644
index 57ccd5fb17492..0000000000000
--- a/spec/scripts/check_default_pipeline_status_spec.rb
+++ /dev/null
@@ -1,93 +0,0 @@
-# frozen_string_literal: true
-
-# rubocop:disable RSpec/VerifiedDoubles -- generic double is sufficient
-
-require 'fast_spec_helper'
-
-require_relative '../../scripts/check_default_pipeline_status'
-
-RSpec.describe CheckDefaultPipelineStatus, feature_category: :tooling do
-  let(:api_client_double)      { double('Client') }
-  let(:master_pipeline_status) { 'failed' }
-  let(:allowed_to_fail)        { false }
-  let(:failed_job_double)      { double('Job', name: 'job_name', allow_failure: allowed_to_fail) }
-  let(:pipeline_jobs_double)   { double('pipeline_jobs', auto_paginate: [failed_job_double]) }
-
-  subject(:master_pipeline_status_checker) do
-    described_class.new({ project: '1', pipeline_id: '100', api_token: 'api-token', endpoint: '' })
-  end
-
-  before do
-    allow(Gitlab).to receive(:client).and_return(api_client_double)
-
-    allow(api_client_double).to receive(:pipelines).with(
-      '1',
-      ref: 'master',
-      scope: 'finished',
-      per_page: 1
-    ).and_return([double('Pipeline', id: '1', web_url: 'master_web_url', status: master_pipeline_status)])
-
-    allow(api_client_double).to receive(:pipeline_jobs).and_return(
-      double(
-        'master_pipeline_jobs',
-        auto_paginate: [
-          double('Job', name: 'job1', allow_failure: true),
-          failed_job_double
-        ]
-      ),
-      pipeline_jobs_double
-    )
-  end
-
-  shared_examples 'exits successfully' do
-    it 'returns' do
-      expect(master_pipeline_status_checker.execute).to be nil
-    end
-  end
-
-  describe 'execute' do
-    context 'when the latest master pipeline succeeded' do
-      let(:master_pipeline_status) { 'success' }
-
-      it_behaves_like 'exits successfully'
-    end
-
-    context 'when the latest master pipeline failed' do
-      context 'when the current pipeline does not contain any failed job from master' do
-        let(:pipeline_jobs_double) do
-          double('pipeline_jobs', auto_paginate: [double('Job', name: 'new-job', allow_failure: true)])
-        end
-
-        it_behaves_like 'exits successfully'
-      end
-
-      context 'when the current pipeline contains a failed job from master' do
-        context 'when the matching job is allowed to fail' do
-          let(:allowed_to_fail) { true }
-
-          it_behaves_like 'exits successfully'
-        end
-
-        context 'when the matching job is not allowed to fail' do
-          it 'raises system exit error' do
-            expect(master_pipeline_status_checker).to receive(:warn).with(
-              <<~TEXT
-              ******************************************************************************************
-              We are failing this job to warn you that you may be impacted by a master broken incident.
-              Jobs below may fail in your pipeline:
-              job_name
-              Check if the failures are also present in the master pipeline: master_web_url
-              Reach out to #master-broken for assistance if you think you are blocked.
-              Apply ~"pipeline:ignore-master-status" to skip this job if you don't think this is helpful.
-              ******************************************************************************************
-              TEXT
-            )
-            expect { master_pipeline_status_checker.execute }.to raise_error(SystemExit)
-          end
-        end
-      end
-    end
-  end
-end
-
-# rubocop:enable RSpec/VerifiedDoubles
-- 
GitLab