Commit 68e1b5bb authored by Timothy Andrew's avatar Timothy Andrew

Use the `IssuableBaseService` lifecycle hooks to cache `MergeRequestsClosingIssues`

- Instead of overriding `create` and `update` in `MergeRequests::BaseService`
- Get all merge request service specs passing
parent 918e589c
...@@ -23,7 +23,7 @@ class Issue < ActiveRecord::Base ...@@ -23,7 +23,7 @@ class Issue < ActiveRecord::Base
has_many :events, as: :target, dependent: :destroy has_many :events, as: :target, dependent: :destroy
has_many :merge_requests_closing_issues, class_name: 'MergeRequestsClosingIssues' has_many :merge_requests_closing_issues, class_name: 'MergeRequestsClosingIssues', dependent: :delete_all
validates :project, presence: true validates :project, presence: true
......
...@@ -16,7 +16,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -16,7 +16,7 @@ class MergeRequest < ActiveRecord::Base
has_many :events, as: :target, dependent: :destroy has_many :events, as: :target, dependent: :destroy
has_many :merge_requests_closing_issues, class_name: 'MergeRequestsClosingIssues' has_many :merge_requests_closing_issues, class_name: 'MergeRequestsClosingIssues', dependent: :delete_all
serialize :merge_params, Hash serialize :merge_params, Hash
......
...@@ -157,6 +157,10 @@ class IssuableBaseService < BaseService ...@@ -157,6 +157,10 @@ class IssuableBaseService < BaseService
# To be overridden by subclasses # To be overridden by subclasses
end end
def after_update(issuable)
# To be overridden by subclasses
end
def update_issuable(issuable, attributes) def update_issuable(issuable, attributes)
issuable.with_transaction_returning_status do issuable.with_transaction_returning_status do
issuable.update(attributes.merge(updated_by: current_user)) issuable.update(attributes.merge(updated_by: current_user))
...@@ -182,6 +186,7 @@ class IssuableBaseService < BaseService ...@@ -182,6 +186,7 @@ class IssuableBaseService < BaseService
end end
handle_changes(issuable, old_labels: old_labels, old_mentioned_users: old_mentioned_users) handle_changes(issuable, old_labels: old_labels, old_mentioned_users: old_mentioned_users)
after_update(issuable)
issuable.create_new_cross_references!(current_user) issuable.create_new_cross_references!(current_user)
execute_hooks(issuable, 'update') execute_hooks(issuable, 'update')
end end
......
...@@ -35,18 +35,6 @@ module MergeRequests ...@@ -35,18 +35,6 @@ module MergeRequests
end end
end end
def create(merge_request)
merge_request = super(merge_request)
merge_request.cache_merge_request_closes_issues!(current_user)
merge_request
end
def update(merge_request)
merge_request = super(merge_request)
merge_request.cache_merge_request_closes_issues!(current_user)
merge_request
end
private private
def filter_params def filter_params
......
...@@ -20,6 +20,7 @@ module MergeRequests ...@@ -20,6 +20,7 @@ module MergeRequests
event_service.open_mr(issuable, current_user) event_service.open_mr(issuable, current_user)
notification_service.new_merge_request(issuable, current_user) notification_service.new_merge_request(issuable, current_user)
todo_service.new_merge_request(issuable, current_user) todo_service.new_merge_request(issuable, current_user)
issuable.cache_merge_request_closes_issues!(current_user)
end end
end end
end end
...@@ -77,5 +77,9 @@ module MergeRequests ...@@ -77,5 +77,9 @@ module MergeRequests
def close_service def close_service
MergeRequests::CloseService MergeRequests::CloseService
end end
def after_update(issuable)
issuable.cache_merge_request_closes_issues!(current_user)
end
end end
end end
...@@ -108,7 +108,7 @@ describe MergeRequests::CreateService, services: true do ...@@ -108,7 +108,7 @@ describe MergeRequests::CreateService, services: true do
allow(service).to receive(:execute_hooks) allow(service).to receive(:execute_hooks)
merge_request = service.execute merge_request = service.execute
expect(merge_request.reload.closes_issues).to match_array([first_issue, second_issue]) expect(merge_request.reload.closes_issues(user)).to match_array([first_issue, second_issue])
end end
end end
end end
......
...@@ -200,7 +200,7 @@ describe MergeRequests::RefreshService, services: true do ...@@ -200,7 +200,7 @@ describe MergeRequests::RefreshService, services: true do
allow(refresh_service).to receive(:execute_hooks) allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/feature') refresh_service.execute(@oldrev, @newrev, 'refs/heads/feature')
expect(merge_request.reload.closes_issues).to eq([issue]) expect(merge_request.reload.closes_issues(@user)).to eq([issue])
end end
end end
......
...@@ -274,7 +274,7 @@ describe MergeRequests::UpdateService, services: true do ...@@ -274,7 +274,7 @@ describe MergeRequests::UpdateService, services: true do
allow(service).to receive(:execute_hooks) allow(service).to receive(:execute_hooks)
service.execute(merge_request) service.execute(merge_request)
expect(merge_request.reload.closes_issues).to match_array([first_issue, second_issue]) expect(merge_request.reload.closes_issues(user)).to match_array([first_issue, second_issue])
end end
it 'removes `MergeRequestsClosingIssues` records when issues are not closed anymore' do it 'removes `MergeRequestsClosingIssues` records when issues are not closed anymore' do
...@@ -288,13 +288,13 @@ describe MergeRequests::UpdateService, services: true do ...@@ -288,13 +288,13 @@ describe MergeRequests::UpdateService, services: true do
merge_request = MergeRequests::CreateService.new(project, user, opts).execute merge_request = MergeRequests::CreateService.new(project, user, opts).execute
expect(merge_request.reload.closes_issues).to match_array([first_issue, second_issue]) expect(merge_request.reload.closes_issues(user)).to match_array([first_issue, second_issue])
service = described_class.new(project, user, description: "not closing any issues") service = described_class.new(project, user, description: "not closing any issues")
allow(service).to receive(:execute_hooks) allow(service).to receive(:execute_hooks)
service.execute(merge_request.reload) service.execute(merge_request.reload)
expect(merge_request.reload.closes_issues).to be_empty expect(merge_request.reload.closes_issues(user)).to be_empty
end end
end end
end end
......
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