diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-22 21:07:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-02-22 21:07:44 +0300 |
commit | d0f16d56f3716d4a60027eb261f12080094f8db3 (patch) | |
tree | f9ecf9f4bda6d761f612bc4a5efb701b7c6b3d2d /tooling | |
parent | 68aa32736b50c3609348f3bf740b81a2dfd1fb25 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'tooling')
-rw-r--r-- | tooling/danger/sidekiq_args.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/tooling/danger/sidekiq_args.rb b/tooling/danger/sidekiq_args.rb new file mode 100644 index 00000000000..d06bb92ca6d --- /dev/null +++ b/tooling/danger/sidekiq_args.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module Tooling + module Danger + module SidekiqArgs + include ::Danger::Helpers + + WORKER_FILES_REGEX = 'app/workers' + EE_PREFIX = 'ee/' + DEF_PERFORM = "def perform" + DEF_PERFORM_REGEX = /[\s+-]*def perform\((.*)\)/ + BEFORE_DEF_PERFORM_REGEX = /^[\s-]*def perform\b/ + AFTER_DEF_PERFORM_REGEX = /^[\s+]*def perform\b/ + + SUGGEST_MR_COMMENT = <<~SUGGEST_COMMENT + Please follow the [sidekiq development guidelines](https://docs.gitlab.com/ee/development/sidekiq/compatibility_across_updates.html#changing-the-arguments-for-a-worker) when changing sidekiq worker arguments. + SUGGEST_COMMENT + + def changed_worker_files(ee: :include) + changed_files = helper.all_changed_files + folder_prefix = + case ee + when :include + "(#{EE_PREFIX})?" + when :only + EE_PREFIX + when :exclude + nil + end + + changed_files.grep(%r{\A#{folder_prefix}#{WORKER_FILES_REGEX}}) + end + + def args_changed?(diff) + # Find the "before" and "after" versions of the perform method definition + before_def_perform = diff.find { |line| BEFORE_DEF_PERFORM_REGEX.match?(line) } + after_def_perform = diff.find { |line| AFTER_DEF_PERFORM_REGEX.match?(line) } + + # args are not changed if there is no before or after def perform method + return false unless before_def_perform && after_def_perform + + # Extract the perform method arguments from the "before" and "after" versions + before_args, after_args = diff.flat_map { |line| line.scan(DEF_PERFORM_REGEX) } + + before_args != after_args + end + + def add_comment_for_matched_line(filename) + diff = helper.changed_lines(filename) + return unless args_changed?(diff) + + file_lines = project_helper.file_lines(filename) + + perform_method_line = file_lines.index { |line| line.include?(DEF_PERFORM) } + markdown(format(SUGGEST_MR_COMMENT), file: filename, line: perform_method_line.succ) + end + end + end +end |