Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/qa/tasks
diff options
context:
space:
mode:
Diffstat (limited to 'qa/tasks')
-rw-r--r--qa/tasks/ci.rake61
-rw-r--r--qa/tasks/helpers/util.rb49
2 files changed, 110 insertions, 0 deletions
diff --git a/qa/tasks/ci.rake b/qa/tasks/ci.rake
new file mode 100644
index 00000000000..44a794d9f94
--- /dev/null
+++ b/qa/tasks/ci.rake
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+require_relative "helpers/util"
+
+# rubocop:disable Rails/RakeEnvironment
+namespace :ci do
+ include Task::Helpers::Util
+
+ desc "Detect changes and populate test variables for selective test execution and feature flag testing"
+ task :detect_changes, [:env_file] do |_, args|
+ env_file = args[:env_file]
+ abort("ERROR: Path for environment file must be provided") unless env_file
+
+ diff = mr_diff
+ labels = mr_labels
+
+ qa_changes = QA::Tools::Ci::QaChanges.new(diff, labels)
+ logger = qa_changes.logger
+
+ logger.info("Analyzing merge request changes")
+ # skip running tests when only quarantine changes detected
+ if qa_changes.quarantine_changes?
+ logger.info(" merge request contains only quarantine changes, e2e test execution will be skipped!")
+ append_to_file(env_file, <<~TXT)
+ QA_SKIP_ALL_TESTS=true
+ TXT
+ next
+ end
+
+ tests = qa_changes.qa_tests
+ if qa_changes.framework_changes? # run all tests when framework changes detected
+ logger.info(" merge request contains qa framework changes, full test suite will be executed")
+ append_to_file(env_file, <<~TXT)
+ QA_FRAMEWORK_CHANGES=true
+ TXT
+ elsif tests
+ logger.info(" detected following specs to execute: '#{tests}'")
+ else
+ logger.info(" no specific specs to execute detected")
+ end
+
+ # always check all test suites in case a suite is defined but doesn't have any runnable specs
+ suites = QA::Tools::Ci::NonEmptySuites.new(tests).fetch
+ append_to_file(env_file, <<~TXT)
+ QA_TESTS='#{tests}'
+ QA_SUITES='#{suites}'
+ TXT
+
+ # check if mr contains feature flag changes
+ feature_flags = QA::Tools::Ci::FfChanges.new(diff).fetch
+ append_to_file(env_file, <<~TXT)
+ QA_FEATURE_FLAGS='#{feature_flags}'
+ TXT
+ end
+
+ desc "Download test results from downstream pipeline"
+ task :download_test_results, [:trigger_name, :test_report_job_name, :report_path] do |_, args|
+ QA::Tools::Ci::TestResults.get(args[:trigger_name], args[:test_report_job_name], args[:report_path])
+ end
+end
+# rubocop:enable Rails/RakeEnvironment
diff --git a/qa/tasks/helpers/util.rb b/qa/tasks/helpers/util.rb
new file mode 100644
index 00000000000..f8eb9b02f72
--- /dev/null
+++ b/qa/tasks/helpers/util.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module Task
+ module Helpers
+ module Util
+ include ::QA::Support::API
+
+ # Append text to file
+ #
+ # @param [String] path
+ # @param [String] text
+ # @return [void]
+ def append_to_file(path, text)
+ File.open(path, "a") { |f| f.write(text) }
+ end
+
+ # Merge request labels
+ #
+ # @return [Array]
+ def mr_labels
+ ENV["CI_MERGE_REQUEST_LABELS"]&.split(',') || []
+ end
+
+ # Merge request changes
+ #
+ # @return [Array<Hash>]
+ def mr_diff
+ mr_iid = ENV["CI_MERGE_REQUEST_IID"]
+ return [] unless mr_iid
+
+ gitlab_endpoint = ENV["CI_API_V4_URL"]
+ gitlab_token = ENV["PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE"]
+ project_id = ENV["CI_MERGE_REQUEST_PROJECT_ID"]
+
+ response = get(
+ "#{gitlab_endpoint}/projects/#{project_id}/merge_requests/#{mr_iid}/changes",
+ headers: { "PRIVATE-TOKEN" => gitlab_token }
+ )
+
+ parse_body(response).fetch(:changes, []).map do |change|
+ {
+ path: change[:new_path],
+ **change.slice(:new_file, :deleted_file, :diff)
+ }
+ end
+ end
+ end
+ end
+end