From 8a5138ed7d38ccff8b5ca2fe0f7bbb77f8fdaad3 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 14 Apr 2023 12:08:53 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- tooling/bin/find_only_js_changes | 2 +- tooling/lib/tooling/find_changes.rb | 39 ++++++++++++++++++----------- tooling/lib/tooling/helpers/file_handler.rb | 8 +++--- tooling/lib/tooling/predictive_tests.rb | 11 ++++++-- 4 files changed, 39 insertions(+), 21 deletions(-) (limited to 'tooling') diff --git a/tooling/bin/find_only_js_changes b/tooling/bin/find_only_js_changes index 67e3d3c6ff5..a69ee64fe14 100755 --- a/tooling/bin/find_only_js_changes +++ b/tooling/bin/find_only_js_changes @@ -3,7 +3,7 @@ require_relative '../lib/tooling/find_changes' -if Tooling::FindChanges.new.only_js_files_changed +if Tooling::FindChanges.new(from: :api).only_js_files_changed puts "Only JS files were changed" exit 0 else diff --git a/tooling/lib/tooling/find_changes.rb b/tooling/lib/tooling/find_changes.rb index b4439bd4abe..d8373d11245 100755 --- a/tooling/lib/tooling/find_changes.rb +++ b/tooling/lib/tooling/find_changes.rb @@ -9,8 +9,15 @@ module Tooling include Helpers::FileHandler def initialize( - changed_files_pathname = nil, predictive_tests_pathname = nil, frontend_fixtures_mapping_pathname = nil + from:, + changed_files_pathname: nil, + predictive_tests_pathname: nil, + frontend_fixtures_mapping_pathname: nil ) + + raise ArgumentError, ':from can only be :api or :changed_files' unless + %i[api changed_files].include?(from) + @gitlab_token = ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] || '' @gitlab_endpoint = ENV['CI_API_V4_URL'] @mr_project_path = ENV['CI_MERGE_REQUEST_PROJECT_PATH'] @@ -18,20 +25,23 @@ module Tooling @changed_files_pathname = changed_files_pathname @predictive_tests_pathname = predictive_tests_pathname @frontend_fixtures_mapping_pathname = frontend_fixtures_mapping_pathname + @from = from end def execute if changed_files_pathname.nil? - raise ArgumentError, "A path to the changed files file must be given as first argument." + raise ArgumentError, "A path to the changed files file must be given as :changed_files_pathname" end - add_frontend_fixture_files! - write_array_to_file(changed_files_pathname, file_changes, overwrite: true) + case @from + when :api + write_array_to_file(changed_files_pathname, file_changes + frontend_fixture_files, append: false) + else + write_array_to_file(changed_files_pathname, frontend_fixture_files, append: true) + end end def only_js_files_changed - @changed_files_pathname = nil # We ensure that we'll get the diff from the MR directly, not from a file. - file_changes.any? && file_changes.all? { |file| file.end_with?('.js') } end @@ -55,25 +65,26 @@ module Tooling predictive_tests_pathname && frontend_fixtures_mapping_pathname end - def add_frontend_fixture_files! - return unless add_frontend_fixture_files? - + def frontend_fixture_files # If we have a `test file -> JSON frontend fixture` mapping file, we add the files JSON frontend fixtures # files to the list of changed files so that Jest can automatically run the dependent tests # using --findRelatedTests flag. - test_files.each do |test_file| - file_changes.concat(frontend_fixtures_mapping[test_file]) if frontend_fixtures_mapping.key?(test_file) + empty = [].freeze + + test_files.flat_map do |test_file| + frontend_fixtures_mapping[test_file] || empty end end def file_changes @file_changes ||= - if changed_files_pathname && File.exist?(changed_files_pathname) - read_array_from_file(changed_files_pathname) - else + case @from + when :api mr_changes.changes.flat_map do |change| change.to_h.values_at('old_path', 'new_path') end.uniq + else + read_array_from_file(changed_files_pathname) end end diff --git a/tooling/lib/tooling/helpers/file_handler.rb b/tooling/lib/tooling/helpers/file_handler.rb index 2778bb1ffbc..88248e31df2 100644 --- a/tooling/lib/tooling/helpers/file_handler.rb +++ b/tooling/lib/tooling/helpers/file_handler.rb @@ -11,17 +11,17 @@ module Tooling File.read(file).split(' ') end - def write_array_to_file(file, content_array, overwrite: false) + def write_array_to_file(file, content_array, append: true) FileUtils.touch file # We sort the array to make it easier to read the output file content_array.sort! output_content = - if overwrite - content_array.join(' ') + if append + [File.read(file), *content_array].join(' ').lstrip else - (File.read(file).split(' ') + content_array).join(' ') + content_array.join(' ') end File.write(file, output_content) diff --git a/tooling/lib/tooling/predictive_tests.rb b/tooling/lib/tooling/predictive_tests.rb index 2691e1ba56d..503426b5520 100644 --- a/tooling/lib/tooling/predictive_tests.rb +++ b/tooling/lib/tooling/predictive_tests.rb @@ -32,7 +32,10 @@ module Tooling end def execute - Tooling::FindChanges.new(rspec_changed_files_path).execute + Tooling::FindChanges.new( + from: :api, + changed_files_pathname: rspec_changed_files_path + ).execute Tooling::FindTests.new(rspec_changed_files_path, rspec_matching_tests_path).execute Tooling::Mappings::PartialToViewsMappings.new( rspec_changed_files_path, rspec_views_including_partials_path).execute @@ -41,7 +44,11 @@ module Tooling Tooling::Mappings::GraphqlBaseTypeMappings.new(rspec_changed_files_path, rspec_matching_tests_path).execute Tooling::Mappings::ViewToSystemSpecsMappings.new(rspec_changed_files_path, rspec_matching_tests_path).execute Tooling::FindChanges.new( - rspec_changed_files_path, rspec_matching_tests_path, frontend_fixtures_mapping_path).execute + from: :changed_files, + changed_files_pathname: rspec_changed_files_path, + predictive_tests_pathname: rspec_matching_tests_path, + frontend_fixtures_mapping_pathname: frontend_fixtures_mapping_path + ).execute Tooling::Mappings::ViewToJsMappings.new(rspec_changed_files_path, rspec_matching_js_files_path).execute end -- cgit v1.2.1