From 825124f463eb4cf123ad0196fd8d69b81fd8de77 Mon Sep 17 00:00:00 2001 From: Mark Lapierre Date: Thu, 4 Nov 2021 11:29:56 +0000 Subject: [PATCH] Skip package-and-qa if an MR only quarantines tests --- .gitlab/ci/qa.gitlab-ci.yml | 23 +++++++++++----- .gitlab/ci/rules.gitlab-ci.yml | 2 +- .rubocop.yml | 2 +- tooling/bin/find_change_diffs | 27 +++++++++++++++++++ .../bin/qa/check_if_only_quarantined_specs | 18 +++++++++++++ 5 files changed, 64 insertions(+), 8 deletions(-) create mode 100755 tooling/bin/find_change_diffs create mode 100755 tooling/bin/qa/check_if_only_quarantined_specs diff --git a/.gitlab/ci/qa.gitlab-ci.yml b/.gitlab/ci/qa.gitlab-ci.yml index c3bba1d8c5c2..d19300f8e8d9 100644 --- a/.gitlab/ci/qa.gitlab-ci.yml +++ b/.gitlab/ci/qa.gitlab-ci.yml @@ -55,8 +55,14 @@ update-qa-cache: before_script: - source scripts/utils.sh - install_gitlab_gem + - tooling/bin/find_change_diffs ${CHANGES_DIFFS_DIR} script: - - ./scripts/trigger-build omnibus + - | + if tooling/bin/qa/check_if_only_quarantined_specs ${CHANGES_DIFFS_DIR}; then + exit 0 + else + ./scripts/trigger-build omnibus + fi # These jobs often time out, so temporarily use private runners and a long timeout: https://gitlab.com/gitlab-org/gitlab/-/issues/238563 tags: - prm @@ -66,14 +72,19 @@ update-qa-cache: artifacts: false - job: build-assets-image artifacts: false + - detect-tests + artifacts: + expire_in: 7d + paths: + - ${CHANGES_FILE} + - ${CHANGES_DIFFS_DIR}/* + variables: + CHANGES_FILE: tmp/changed_files.txt + CHANGES_DIFFS_DIR: tmp/diffs .package-and-qa-ff-base: - needs: - - detect-tests - variables: - CHANGED_FILES: tmp/changed_files.txt script: - - export GITLAB_QA_OPTIONS="--set-feature-flags $(scripts/changed-feature-flags --files $(cat $CHANGED_FILES | tr ' ' ',') --state $QA_FF_STATE)" + - export GITLAB_QA_OPTIONS="--set-feature-flags $(scripts/changed-feature-flags --files $(cat $CHANGES_FILE | tr ' ' ',') --state $QA_FF_STATE)" - echo $GITLAB_QA_OPTIONS - ./scripts/trigger-build omnibus diff --git a/.gitlab/ci/rules.gitlab-ci.yml b/.gitlab/ci/rules.gitlab-ci.yml index fc68b8aa3547..be987bc7e7e9 100644 --- a/.gitlab/ci/rules.gitlab-ci.yml +++ b/.gitlab/ci/rules.gitlab-ci.yml @@ -1258,7 +1258,7 @@ .rails:rules:detect-tests: rules: - - changes: *code-backstage-patterns + - changes: *code-backstage-qa-patterns - <<: *if-merge-request-labels-run-all-rspec .rails:rules:detect-previous-failed-tests: diff --git a/.rubocop.yml b/.rubocop.yml index 9b5ed0a38c30..0dbdd706ef26 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -123,7 +123,7 @@ Naming/FileName: - 'ee/lib/generators/**/*' - 'scripts/**/*' - 'spec/**/*' - - 'tooling/bin/*' + - 'tooling/bin/**/*' - 'ee/spec/**/*' - 'jh/spec/**/*' - 'qa/bin/*' diff --git a/tooling/bin/find_change_diffs b/tooling/bin/find_change_diffs new file mode 100755 index 000000000000..8ac26d564762 --- /dev/null +++ b/tooling/bin/find_change_diffs @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'gitlab' +require 'pathname' + +# This script saves the diffs of changes in an MR to the directory specified as the first argument + +gitlab_token = ENV.fetch('PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE') +gitlab_endpoint = ENV.fetch('CI_API_V4_URL') +mr_project_path = ENV.fetch('CI_MERGE_REQUEST_PROJECT_PATH') +mr_iid = ENV.fetch('CI_MERGE_REQUEST_IID') + +abort("ERROR: Please specify a directory to write MR diffs into.") if ARGV.empty? +output_diffs_dir = Pathname.new(ARGV.shift).expand_path + +Gitlab.configure do |config| + config.endpoint = gitlab_endpoint + config.private_token = gitlab_token +end + +Gitlab.merge_request_changes(mr_project_path, mr_iid).changes.each do |change| + next if change['diff'].empty? + + output_diffs_dir.join(File.dirname(change['new_path'])).mkpath + output_diffs_dir.join("#{change['new_path']}.diff").write(change['diff']) +end diff --git a/tooling/bin/qa/check_if_only_quarantined_specs b/tooling/bin/qa/check_if_only_quarantined_specs new file mode 100755 index 000000000000..8a36761c58a3 --- /dev/null +++ b/tooling/bin/qa/check_if_only_quarantined_specs @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'pathname' + +# This script assumes the first argument is a directory of files containing diffs of changes from an MR. It exits with a +# success code if all diffs add a line that quarantines a test. If any diffs are not specs, or they are specs that don't +# quarantine a test, it exits with code 1 to indicate failure (i.e., there was _not_ only quarantined specs). + +abort("ERROR: Please specify the directory containing MR diffs.") if ARGV.empty? +diffs_dir = Pathname.new(ARGV.shift).expand_path + +diffs_dir.glob('**/*').each do |path| + next if path.directory? + + exit 1 unless path.to_s.end_with?('_spec.rb.diff') + exit 1 unless path.read.match?(/^\+.*, quarantine:/) +end