Commit bfc42fc7 authored by Rubén Dávila's avatar Rubén Dávila

Stop passing push_rule by making it an attribute

parent c0f35d7e
...@@ -13,6 +13,8 @@ module EE ...@@ -13,6 +13,8 @@ module EE
push_rule_committer_not_allowed: "You cannot push commits for '%{committer_email}'. You can only push commits that were committed with one of your own verified emails." push_rule_committer_not_allowed: "You cannot push commits for '%{committer_email}'. You can only push commits that were committed with one of your own verified emails."
}.freeze }.freeze
attr_reader :push_rule
override :exec override :exec
def exec def exec
return true if skip_authorization return true if skip_authorization
...@@ -29,23 +31,23 @@ module EE ...@@ -29,23 +31,23 @@ module EE
def push_rule_check def push_rule_check
return unless newrev && oldrev && project.feature_available?(:push_rules) return unless newrev && oldrev && project.feature_available?(:push_rules)
push_rule = project.push_rule @push_rule = project.push_rule
if tag_name if tag_name
push_rule_tag_check(push_rule) push_rule_tag_check
else else
push_rule_branch_check(push_rule) push_rule_branch_check
end end
end end
def push_rule_tag_check(push_rule) def push_rule_tag_check
if tag_deletion_denied_by_push_rule?(push_rule) if tag_deletion_denied_by_push_rule?
raise ::Gitlab::GitAccess::UnauthorizedError, 'You cannot delete a tag' raise ::Gitlab::GitAccess::UnauthorizedError, 'You cannot delete a tag'
end end
end end
def push_rule_branch_check(push_rule) def push_rule_branch_check
unless branch_name_allowed_by_push_rule?(push_rule) unless branch_name_allowed_by_push_rule?
message = ERROR_MESSAGES[:push_rule_branch_name] % { branch_name_regex: push_rule.branch_name_regex } message = ERROR_MESSAGES[:push_rule_branch_name] % { branch_name_regex: push_rule.branch_name_regex }
raise ::Gitlab::GitAccess::UnauthorizedError.new(message) raise ::Gitlab::GitAccess::UnauthorizedError.new(message)
end end
...@@ -57,40 +59,40 @@ module EE ...@@ -57,40 +59,40 @@ module EE
# n+1: https://gitlab.com/gitlab-org/gitlab-ee/issues/3593 # n+1: https://gitlab.com/gitlab-org/gitlab-ee/issues/3593
::Gitlab::GitalyClient.allow_n_plus_1_calls do ::Gitlab::GitalyClient.allow_n_plus_1_calls do
commits.each do |commit| commits.each do |commit|
push_rule_commit_check(commit, push_rule) push_rule_commit_check(commit)
end end
end end
rescue ::PushRule::MatchError => e rescue ::PushRule::MatchError => e
raise ::Gitlab::GitAccess::UnauthorizedError, e.message raise ::Gitlab::GitAccess::UnauthorizedError, e.message
end end
def branch_name_allowed_by_push_rule?(push_rule) def branch_name_allowed_by_push_rule?
return true if skip_branch_name_push_rule?(push_rule) return true if skip_branch_name_push_rule?
push_rule.branch_name_allowed?(branch_name) push_rule.branch_name_allowed?(branch_name)
end end
def skip_branch_name_push_rule?(push_rule) def skip_branch_name_push_rule?
push_rule.nil? || push_rule.nil? ||
deletion? || deletion? ||
branch_name.blank? || branch_name.blank? ||
branch_name == project.default_branch branch_name == project.default_branch
end end
def tag_deletion_denied_by_push_rule?(push_rule) def tag_deletion_denied_by_push_rule?
push_rule.try(:deny_delete_tag) && push_rule.try(:deny_delete_tag) &&
!updated_from_web? && !updated_from_web? &&
deletion? && deletion? &&
tag_exists? tag_exists?
end end
def push_rule_commit_check(commit, push_rule) def push_rule_commit_check(commit)
if push_rule.try(:commit_validation?) if push_rule.try(:commit_validation?)
error = check_commit(commit, push_rule) error = check_commit(commit)
raise ::Gitlab::GitAccess::UnauthorizedError, error if error raise ::Gitlab::GitAccess::UnauthorizedError, error if error
end end
if error = check_commit_diff(commit, push_rule) if error = check_commit_diff(commit)
raise ::Gitlab::GitAccess::UnauthorizedError, error raise ::Gitlab::GitAccess::UnauthorizedError, error
end end
end end
...@@ -98,7 +100,7 @@ module EE ...@@ -98,7 +100,7 @@ module EE
# If commit does not pass push rule validation the whole push should be rejected. # If commit does not pass push rule validation the whole push should be rejected.
# This method should return nil if no error found or a string if error. # This method should return nil if no error found or a string if error.
# In case of errors - all other checks will be canceled and push will be rejected. # In case of errors - all other checks will be canceled and push will be rejected.
def check_commit(commit, push_rule) def check_commit(commit)
unless push_rule.commit_message_allowed?(commit.safe_message) unless push_rule.commit_message_allowed?(commit.safe_message)
return "Commit message does not follow the pattern '#{push_rule.commit_message_regex}'" return "Commit message does not follow the pattern '#{push_rule.commit_message_regex}'"
end end
...@@ -111,7 +113,7 @@ module EE ...@@ -111,7 +113,7 @@ module EE
return "Author's email '#{commit.author_email}' does not follow the pattern '#{push_rule.author_email_regex}'" return "Author's email '#{commit.author_email}' does not follow the pattern '#{push_rule.author_email_regex}'"
end end
committer_error_message = committer_check(commit, push_rule) committer_error_message = committer_check(commit)
return committer_error_message if committer_error_message return committer_error_message if committer_error_message
if !updated_from_web? && !push_rule.commit_signature_allowed?(commit) if !updated_from_web? && !push_rule.commit_signature_allowed?(commit)
...@@ -134,7 +136,7 @@ module EE ...@@ -134,7 +136,7 @@ module EE
nil nil
end end
def committer_check(commit, push_rule) def committer_check(commit)
unless push_rule.committer_allowed?(commit.committer_email, user_access.user) unless push_rule.committer_allowed?(commit.committer_email, user_access.user)
committer_is_current_user = commit.committer == user_access.user committer_is_current_user = commit.committer == user_access.user
...@@ -146,8 +148,8 @@ module EE ...@@ -146,8 +148,8 @@ module EE
end end
end end
def check_commit_diff(commit, push_rule) def check_commit_diff(commit)
validations = validations_for_commit(commit, push_rule) validations = validations_for_commit(commit)
return if validations.empty? return if validations.empty?
...@@ -162,12 +164,12 @@ module EE ...@@ -162,12 +164,12 @@ module EE
nil nil
end end
def validations_for_commit(commit, push_rule) def validations_for_commit(commit)
validations = base_validations validations = base_validations
return validations unless push_rule return validations unless push_rule
validations << file_name_validation(push_rule) validations << file_name_validation
if push_rule.max_file_size > 0 if push_rule.max_file_size > 0
validations << file_size_validation(commit, push_rule.max_file_size) validations << file_size_validation(commit, push_rule.max_file_size)
...@@ -200,7 +202,7 @@ module EE ...@@ -200,7 +202,7 @@ module EE
end end
end end
def file_name_validation(push_rule) def file_name_validation
lambda do |diff| lambda do |diff|
if (diff.renamed_file || diff.new_file) && blacklisted_regex = push_rule.filename_blacklisted?(diff.new_path) if (diff.renamed_file || diff.new_file) && blacklisted_regex = push_rule.filename_blacklisted?(diff.new_path)
return nil unless blacklisted_regex.present? return nil unless blacklisted_regex.present?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment