Commit a57c77f6 authored by Timothy Andrew's avatar Timothy Andrew

Add a spec for merge request metric caching while refreshing a merge request from a forked project.

parent 68e1b5bb
...@@ -145,7 +145,7 @@ module MergeRequests ...@@ -145,7 +145,7 @@ module MergeRequests
# If the merge requests closes any issues, save this information in the # If the merge requests closes any issues, save this information in the
# `MergeRequestsClosingIssues` model (as a performance optimization). # `MergeRequestsClosingIssues` model (as a performance optimization).
def cache_merge_requests_closing_issues def cache_merge_requests_closing_issues
merge_requests_for_source_branch.each do |merge_request| @project.merge_requests.where(source_branch: @branch_name).each do |merge_request|
merge_request.cache_merge_request_closes_issues!(@current_user) merge_request.cache_merge_request_closes_issues!(@current_user)
end end
end end
......
...@@ -108,7 +108,8 @@ describe MergeRequests::CreateService, services: true do ...@@ -108,7 +108,8 @@ 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(user)).to match_array([first_issue, second_issue]) issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to match_array([first_issue.id, second_issue.id])
end end
end end
end end
......
...@@ -174,11 +174,10 @@ describe MergeRequests::RefreshService, services: true do ...@@ -174,11 +174,10 @@ describe MergeRequests::RefreshService, services: true do
end end
end end
context 'push commits closing issues' do context 'merge request metrics' do
let(:issue) { create :issue, project: @project } let(:issue) { create :issue, project: @project }
let(:commit_author) { create :user } let(:commit_author) { create :user }
let(:commit) { project.commit } let(:commit) { project.commit }
let!(:merge_request) { create(:merge_request, target_branch: 'master', source_branch: 'feature', source_project: @project) }
before do before do
project.team << [commit_author, :developer] project.team << [commit_author, :developer]
...@@ -195,12 +194,35 @@ describe MergeRequests::RefreshService, services: true do ...@@ -195,12 +194,35 @@ describe MergeRequests::RefreshService, services: true do
allow_any_instance_of(MergeRequest).to receive(:commits).and_return([commit]) allow_any_instance_of(MergeRequest).to receive(:commits).and_return([commit])
end end
it 'creates a `MergeRequestsClosingIssues` record for each closed issue' do context 'when the merge request is sourced from the same project' do
it 'creates a `MergeRequestsClosingIssues` record for each issue closed by a commit' do
merge_request = create(:merge_request, target_branch: 'master', source_branch: 'feature', source_project: @project)
refresh_service = service.new(@project, @user) refresh_service = service.new(@project, @user)
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(@user)).to eq([issue]) issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to eq([issue.id])
end
end
context 'when the merge request is sourced from a different project' do
it 'creates a `MergeRequestsClosingIssues` record for each issue closed by a commit' do
forked_project = create(:project)
create(:forked_project_link, forked_to_project: forked_project, forked_from_project: @project)
merge_request = create(:merge_request,
target_branch: 'master',
source_branch: 'feature',
target_project: @project,
source_project: forked_project)
refresh_service = service.new(@project, @user)
allow(refresh_service).to receive(:execute_hooks)
refresh_service.execute(@oldrev, @newrev, 'refs/heads/feature')
issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to eq([issue.id])
end
end end
end end
......
...@@ -274,7 +274,8 @@ describe MergeRequests::UpdateService, services: true do ...@@ -274,7 +274,8 @@ 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(user)).to match_array([first_issue, second_issue]) issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to match_array([first_issue.id, second_issue.id])
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 +289,15 @@ describe MergeRequests::UpdateService, services: true do ...@@ -288,13 +289,15 @@ 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(user)).to match_array([first_issue, second_issue]) issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to match_array([first_issue.id, second_issue.id])
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(user)).to be_empty issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).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