diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-01 06:08:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-01 06:08:54 +0300 |
commit | c50b444ebb2a379a8b30528c634d02afd58b7b41 (patch) | |
tree | ce507b043e0a5952be7bb2845195abed29239831 /tooling | |
parent | bd98eab3fac88fdde059fafc59bc59b150b56d43 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'tooling')
-rwxr-xr-x | tooling/bin/find_changes | 84 | ||||
-rwxr-xr-x | tooling/bin/find_only_js_changes | 12 | ||||
-rwxr-xr-x | tooling/lib/tooling/find_changes.rb | 89 |
3 files changed, 109 insertions, 76 deletions
diff --git a/tooling/bin/find_changes b/tooling/bin/find_changes index 38e1f363dd9..331fae45a53 100755 --- a/tooling/bin/find_changes +++ b/tooling/bin/find_changes @@ -1,82 +1,14 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'gitlab' +require_relative '../lib/tooling/find_changes' -class FindChanges # rubocop:disable Gitlab/NamespacedClass - def initialize(output_file:, matched_tests_file: nil, frontend_fixtures_mapping_path: nil) - @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') - @output_file = output_file - @matched_tests_file = matched_tests_file - @frontend_fixtures_mapping_path = frontend_fixtures_mapping_path - end - - def execute - add_frontend_fixture_files! - - File.write(output_file, file_changes.join(' ')) - end - - private - - def add_frontend_fixture_files? - matched_tests_file && frontend_fixtures_mapping_path - end - - def add_frontend_fixture_files! - return unless add_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 thanks to --findRelatedTests - test_files.each do |test_file| - file_changes.concat(frontend_fixtures_mapping[test_file]) if frontend_fixtures_mapping.key?(test_file) - end - end - - def file_changes - @file_changes ||= - if File.exist?(output_file) - File.read(output_file).split(' ') - else - Gitlab.configure do |config| - config.endpoint = gitlab_endpoint - config.private_token = gitlab_token - end - - mr_changes.changes.flat_map do |change| - change.to_h.values_at('old_path', 'new_path') - end.uniq - end - end - - def mr_changes - @mr_changes ||= Gitlab.merge_request_changes(mr_project_path, mr_iid) - end - - def test_files - return [] if !matched_tests_file || !File.exist?(matched_tests_file) - - File.read(matched_tests_file).split(' ') - end - - def frontend_fixtures_mapping - return {} if !frontend_fixtures_mapping_path || !File.exist?(frontend_fixtures_mapping_path) - - JSON.parse(File.read(frontend_fixtures_mapping_path)) # rubocop:disable Gitlab/Json - end - - attr_reader :gitlab_token, :gitlab_endpoint, :mr_project_path, :mr_iid, :output_file, :matched_tests_file, :frontend_fixtures_mapping_path -end - -output_file = ARGV.shift -raise ArgumentError, "An path to an output file must be given as first argument of #{__FILE__}." if output_file.nil? - -matched_tests_file = ARGV.shift +output_file = ARGV.shift +matched_tests_file = ARGV.shift frontend_fixtures_mapping_path = ARGV.shift -FindChanges - .new(output_file: output_file, matched_tests_file: matched_tests_file, frontend_fixtures_mapping_path: frontend_fixtures_mapping_path) - .execute +Tooling::FindChanges + .new( + output_file: output_file, + matched_tests_file: matched_tests_file, + frontend_fixtures_mapping_path: frontend_fixtures_mapping_path).execute diff --git a/tooling/bin/find_only_js_changes b/tooling/bin/find_only_js_changes new file mode 100755 index 00000000000..67e3d3c6ff5 --- /dev/null +++ b/tooling/bin/find_only_js_changes @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require_relative '../lib/tooling/find_changes' + +if Tooling::FindChanges.new.only_js_files_changed + puts "Only JS files were changed" + exit 0 +else + puts "Changes were made to files other than JS files" + exit 1 +end diff --git a/tooling/lib/tooling/find_changes.rb b/tooling/lib/tooling/find_changes.rb new file mode 100755 index 00000000000..e151734e41c --- /dev/null +++ b/tooling/lib/tooling/find_changes.rb @@ -0,0 +1,89 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'gitlab' + +module Tooling + class FindChanges + def initialize(output_file: nil, matched_tests_file: nil, frontend_fixtures_mapping_path: nil) + @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'] + @mr_iid = ENV['CI_MERGE_REQUEST_IID'] + @output_file = output_file + @matched_tests_file = matched_tests_file + @frontend_fixtures_mapping_path = frontend_fixtures_mapping_path + end + + def execute + raise ArgumentError, "An path to an output file must be given as first argument." if output_file.nil? + + add_frontend_fixture_files! + File.write(output_file, file_changes.join(' ')) + end + + def only_js_files_changed + @output_file = 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 + + private + + attr_reader :gitlab_token, :gitlab_endpoint, :mr_project_path, + :mr_iid, :output_file, :matched_tests_file, :frontend_fixtures_mapping_path + + def gitlab + @gitlab ||= begin + Gitlab.configure do |config| + config.endpoint = gitlab_endpoint + config.private_token = gitlab_token + end + + Gitlab + end + end + + def add_frontend_fixture_files? + matched_tests_file && frontend_fixtures_mapping_path + end + + def add_frontend_fixture_files! + return unless add_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) + end + end + + def file_changes + @file_changes ||= + if output_file && File.exist?(output_file) + File.read(output_file).split(' ') + else + mr_changes.changes.flat_map do |change| + change.to_h.values_at('old_path', 'new_path') + end.uniq + end + end + + def mr_changes + @mr_changes ||= gitlab.merge_request_changes(mr_project_path, mr_iid) + end + + def test_files + return [] if !matched_tests_file || !File.exist?(matched_tests_file) + + File.read(matched_tests_file).split(' ') + end + + def frontend_fixtures_mapping + return {} if !frontend_fixtures_mapping_path || !File.exist?(frontend_fixtures_mapping_path) + + JSON.parse(File.read(frontend_fixtures_mapping_path)) # rubocop:disable Gitlab/Json + end + end +end |