Skip to content

Commit

Permalink
validate a PR after every PR update / prevent loops
Browse files Browse the repository at this point in the history
This change will trigger a sidekiq job after every update on a PR. With
two exceptions: If eligible_for_merge_comment or eligible_for_ci_comment
got updated, we won't triger a job. Those attribute are *only* set in
the validation methods.
  • Loading branch information
bastelfreak committed May 10, 2020
1 parent a3e2ecb commit 801a290
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-05-07 22:11:39 +0200 using RuboCop version 0.82.0.
# on 2020-05-10 12:20:17 +0200 using RuboCop version 0.82.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 13
# Offense count: 12
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 42
Max: 41

# Offense count: 2
# Configuration parameters: CountComments, ExcludedMethods.
Expand All @@ -20,9 +20,9 @@ Metrics/BlockLength:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 123
Max: 122

# Offense count: 2
# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 7
Expand Down
21 changes: 9 additions & 12 deletions app/models/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,8 @@ def validate(saved_changes)
# If we're running in development mode, we try to run read-only and won't modify PRs
return if Rails.env.development?

# Don't run through the validaten, if only the eligible_for_merge_comment attribute got updated
if saved_changes.keys.sort != %w[updated_at eligible_for_merge_comment].sort && mergeable.nil?
# check merge status and do work if required
validate_mergeable
end

# Don't run through the validaten, if only the eligible_for_ci_comment attribute got updated
return unless saved_changes.keys.sort != %w[updated_at eligible_for_ci_comment].sort
# check merge status and do work if required
validate_mergeable

# check CI status and do work if required
validate_status(saved_changes)
Expand All @@ -176,11 +170,14 @@ def validate(saved_changes)
private

##
# Since we want to be a fancy responsive application we to all the validation
# stuff which might result in some new querys and api requests asyncronously.

# Queue a job into sidekiq that runs the validate() method above
# validate() might use update() to change attributes which would trigger a new job
# To prevent loops, we filter `saved_changed` of those attributes and won't create new job if those are the only changed attributes
def queue_validation
ValidatePullRequestWorker.perform_async(id, saved_changes) if saved_changes? || mergeable.nil?
attributes = %w[eligible_for_merge_comment eligible_for_ci_comment]
return unless [saved_changes.keys & attributes].empty?

ValidatePullRequestWorker.perform_async(id, saved_changes)
end

def validate_mergeable
Expand Down

0 comments on commit 801a290

Please sign in to comment.