Skip to content

Commit

Permalink
FIX: Prevent duplicate approvals (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwaterworth committed Mar 29, 2021
1 parent 716e43a commit 2c58a36
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/discourse_code_review/state/commit_approval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,36 @@ def followed_up(followee_topic, follower_topic)
private

def ensure_approved_post(topic, approver)
post =
Post.where(
topic_id: topic.id,
user_id: approver.id,
action_code: "approved"
).first

unless post
old_highest_post_number = topic.highest_post_number
post =
topic.add_moderator_post(
approver,
nil,
bump: false,
post_type: Post.types[:small_action],
action_code: "approved"
)
DistributedMutex.synchronize("code-review:ensure-approved-post:#{topic.id}") do
ActiveRecord::Base.transaction(requires_new: true) do
post =
Post.where(
topic_id: topic.id,
user_id: approver.id,
action_code: "approved"
).first

unless post
old_highest_post_number = topic.highest_post_number
post =
topic.add_moderator_post(
approver,
nil,
bump: false,
post_type: Post.types[:small_action],
action_code: "approved"
)

PostTiming.pretend_read(
topic.id,
old_highest_post_number,
post.post_number
)
end

PostTiming.pretend_read(
topic.id,
old_highest_post_number,
post.post_number
)
post
end
end

post
end

def transition_to_approved(topic)
Expand Down

0 comments on commit 2c58a36

Please sign in to comment.