Commit a51e68cf authored by Patrick Bajao's avatar Patrick Bajao

Merge branch 'ph/fixAttentionRequestedRaceConditions' into 'master'

Fixes a bug with attention requested state

See merge request gitlab-org/gitlab!75136
parents f2c6c39a 7646f84c
...@@ -282,7 +282,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -282,7 +282,7 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
if merge_request.errors.present? if merge_request.errors.present?
render json: @merge_request.errors, status: :bad_request render json: @merge_request.errors, status: :bad_request
else else
render json: serializer.represent(@merge_request, serializer: 'basic') render json: serializer.represent(@merge_request, serializer: params[:serializer] || 'basic')
end end
end end
end end
......
...@@ -15,11 +15,5 @@ module MergeRequestReviewerState ...@@ -15,11 +15,5 @@ module MergeRequestReviewerState
inclusion: { in: self.states.keys } inclusion: { in: self.states.keys }
after_initialize :set_state, unless: :persisted? after_initialize :set_state, unless: :persisted?
def set_state
if Feature.enabled?(:mr_attention_requests, self.merge_request&.project, default_enabled: :yaml)
self.state = :attention_requested
end
end
end end
end end
...@@ -10,6 +10,12 @@ class MergeRequestAssignee < ApplicationRecord ...@@ -10,6 +10,12 @@ class MergeRequestAssignee < ApplicationRecord
scope :in_projects, ->(project_ids) { joins(:merge_request).where(merge_requests: { target_project_id: project_ids }) } scope :in_projects, ->(project_ids) { joins(:merge_request).where(merge_requests: { target_project_id: project_ids }) }
def set_state
if Feature.enabled?(:mr_attention_requests, self.merge_request&.project, default_enabled: :yaml)
self.state = MergeRequestReviewer.find_by(user_id: self.user_id, merge_request_id: self.merge_request_id)&.state || :attention_requested
end
end
def cache_key def cache_key
[model_name.cache_key, id, state, assignee.cache_key] [model_name.cache_key, id, state, assignee.cache_key]
end end
......
...@@ -6,6 +6,12 @@ class MergeRequestReviewer < ApplicationRecord ...@@ -6,6 +6,12 @@ class MergeRequestReviewer < ApplicationRecord
belongs_to :merge_request belongs_to :merge_request
belongs_to :reviewer, class_name: 'User', foreign_key: :user_id, inverse_of: :merge_request_reviewers belongs_to :reviewer, class_name: 'User', foreign_key: :user_id, inverse_of: :merge_request_reviewers
def set_state
if Feature.enabled?(:mr_attention_requests, self.merge_request&.project, default_enabled: :yaml)
self.state = MergeRequestAssignee.find_by(user_id: self.user_id, merge_request_id: self.merge_request_id)&.state || :attention_requested
end
end
def cache_key def cache_key
[model_name.cache_key, id, state, reviewer.cache_key] [model_name.cache_key, id, state, reviewer.cache_key]
end end
......
...@@ -367,7 +367,8 @@ RSpec.describe Projects::MergeRequestsController do ...@@ -367,7 +367,8 @@ RSpec.describe Projects::MergeRequestsController do
namespace_id: project.namespace, namespace_id: project.namespace,
project_id: project, project_id: project,
id: merge_request.iid, id: merge_request.iid,
merge_request: mr_params merge_request: mr_params,
serializer: 'basic'
}.merge(additional_params) }.merge(additional_params)
put :update, params: params put :update, params: params
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe MergeRequestAssignee do RSpec.describe MergeRequestAssignee do
let(:assignee) { create(:user) }
let(:merge_request) { create(:merge_request) } let(:merge_request) { create(:merge_request) }
subject { merge_request.merge_request_assignees.build(assignee: create(:user)) } subject { merge_request.merge_request_assignees.build(assignee: assignee) }
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:merge_request).class_name('MergeRequest') } it { is_expected.to belong_to(:merge_request).class_name('MergeRequest') }
...@@ -41,4 +42,13 @@ RSpec.describe MergeRequestAssignee do ...@@ -41,4 +42,13 @@ RSpec.describe MergeRequestAssignee do
it_behaves_like 'having unique enum values' it_behaves_like 'having unique enum values'
it_behaves_like 'having reviewer state' it_behaves_like 'having reviewer state'
describe 'syncs to reviewer state' do
before do
reviewer = merge_request.merge_request_reviewers.build(reviewer: assignee)
reviewer.update!(state: :reviewed)
end
it { is_expected.to have_attributes(state: 'reviewed') }
end
end end
...@@ -3,14 +3,24 @@ ...@@ -3,14 +3,24 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe MergeRequestReviewer do RSpec.describe MergeRequestReviewer do
let(:reviewer) { create(:user) }
let(:merge_request) { create(:merge_request) } let(:merge_request) { create(:merge_request) }
subject { merge_request.merge_request_reviewers.build(reviewer: create(:user)) } subject { merge_request.merge_request_reviewers.build(reviewer: reviewer) }
it_behaves_like 'having unique enum values' it_behaves_like 'having unique enum values'
it_behaves_like 'having reviewer state' it_behaves_like 'having reviewer state'
describe 'syncs to assignee state' do
before do
assignee = merge_request.merge_request_assignees.build(assignee: reviewer)
assignee.update!(state: :reviewed)
end
it { is_expected.to have_attributes(state: 'reviewed') }
end
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:merge_request).class_name('MergeRequest') } it { is_expected.to belong_to(:merge_request).class_name('MergeRequest') }
it { is_expected.to belong_to(:reviewer).class_name('User').inverse_of(:merge_request_reviewers) } it { is_expected.to belong_to(:reviewer).class_name('User').inverse_of(:merge_request_reviewers) }
......
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