debian-mirror-gitlab/spec/services/system_note_service_spec.rb

824 lines
25 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2015-09-11 14:41:01 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe SystemNoteService do
include Gitlab::Routing
2018-03-17 18:26:18 +05:30
include RepoHelpers
2018-11-08 19:23:39 +05:30
include AssetsHelpers
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
set(:group) { create(:group) }
set(:project) { create(:project, :repository, group: group) }
set(:author) { create(:user) }
2015-09-11 14:41:01 +05:30
let(:noteable) { create(:issue, project: project) }
2017-08-17 22:00:37 +05:30
let(:issue) { noteable }
2015-09-11 14:41:01 +05:30
describe '.add_commits' do
2019-12-21 20:55:43 +05:30
let(:new_commits) { double }
let(:old_commits) { double }
let(:oldrev) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls CommitService' do
expect_next_instance_of(::SystemNotes::CommitService) do |service|
expect(service).to receive(:add_commits).with(new_commits, old_commits, oldrev)
2015-09-11 14:41:01 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.add_commits(noteable, project, author, new_commits, old_commits, oldrev)
2015-09-11 14:41:01 +05:30
end
end
2018-11-20 20:47:30 +05:30
describe '.tag_commit' do
2019-12-21 20:55:43 +05:30
let(:tag_name) { double }
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
it 'calls CommitService' do
expect_next_instance_of(::SystemNotes::CommitService) do |service|
expect(service).to receive(:tag_commit).with(tag_name)
end
2018-11-20 20:47:30 +05:30
2019-12-21 20:55:43 +05:30
described_class.tag_commit(noteable, project, author, tag_name)
2018-11-20 20:47:30 +05:30
end
end
2015-09-11 14:41:01 +05:30
describe '.change_assignee' do
2019-12-21 20:55:43 +05:30
let(:assignee) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_assignee).with(assignee)
2015-09-11 14:41:01 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.change_assignee(noteable, project, author, assignee)
2015-09-11 14:41:01 +05:30
end
end
2019-07-31 22:56:46 +05:30
describe '.change_issuable_assignees' do
2019-12-21 20:55:43 +05:30
let(:assignees) { [double, double] }
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_issuable_assignees).with(assignees)
2019-12-04 20:38:33 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.change_issuable_assignees(noteable, project, author, assignees)
2019-12-04 20:38:33 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-09-11 14:41:01 +05:30
describe '.change_milestone' do
2019-12-21 20:55:43 +05:30
let(:milestone) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_milestone).with(milestone)
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.change_milestone(noteable, project, author, milestone)
2015-09-11 14:41:01 +05:30
end
end
2018-11-20 20:47:30 +05:30
describe '.change_due_date' do
subject { described_class.change_due_date(noteable, project, author, due_date) }
let(:due_date) { Date.today }
2019-07-31 22:56:46 +05:30
it_behaves_like 'a note with overridable created_at'
2018-11-20 20:47:30 +05:30
it_behaves_like 'a system note' do
let(:action) { 'due_date' }
end
context 'when due date added' do
it 'sets the note text' do
expect(subject.note).to eq "changed due date to #{Date.today.to_s(:long)}"
end
end
context 'when due date removed' do
let(:due_date) { nil }
it 'sets the note text' do
expect(subject.note).to eq 'removed due date'
end
end
end
2015-09-11 14:41:01 +05:30
describe '.change_status' do
2019-12-21 20:55:43 +05:30
let(:status) { double }
let(:source) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_status).with(status, source)
2017-08-17 22:00:37 +05:30
end
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
described_class.change_status(noteable, project, author, status, source)
2015-09-11 14:41:01 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe '.merge_when_pipeline_succeeds' do
let(:pipeline) { build(:ci_pipeline_without_jobs )}
let(:noteable) do
create(:merge_request, source_project: project, target_project: project)
end
2015-12-23 02:04:40 +05:30
2019-09-30 21:07:59 +05:30
subject { described_class.merge_when_pipeline_succeeds(noteable, project, author, pipeline.sha) }
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
it_behaves_like 'a system note' do
let(:action) { 'merge' }
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
it "posts the 'merge when pipeline succeeds' system note" do
2018-03-17 18:26:18 +05:30
expect(subject.note).to match(%r{enabled an automatic merge when the pipeline for (\w+/\w+@)?\h{40} succeeds})
2015-12-23 02:04:40 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe '.cancel_merge_when_pipeline_succeeds' do
let(:noteable) do
create(:merge_request, source_project: project, target_project: project)
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
subject { described_class.cancel_merge_when_pipeline_succeeds(noteable, project, author) }
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
it_behaves_like 'a system note' do
let(:action) { 'merge' }
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
it "posts the 'merge when pipeline succeeds' system note" do
2018-12-05 23:21:45 +05:30
expect(subject.note).to eq "canceled the automatic merge"
2015-12-23 02:04:40 +05:30
end
end
2019-09-30 21:07:59 +05:30
describe '.abort_merge_when_pipeline_succeeds' do
let(:noteable) do
create(:merge_request, source_project: project, target_project: project)
end
subject { described_class.abort_merge_when_pipeline_succeeds(noteable, project, author, 'merge request was closed') }
it_behaves_like 'a system note' do
let(:action) { 'merge' }
end
it "posts the 'merge when pipeline succeeds' system note" do
expect(subject.note).to eq "aborted the automatic merge because merge request was closed"
end
end
2015-09-11 14:41:01 +05:30
describe '.change_title' do
2019-12-21 20:55:43 +05:30
let(:title) { double }
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_title).with(title)
2017-08-17 22:00:37 +05:30
end
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
described_class.change_title(noteable, project, author, title)
2017-08-17 22:00:37 +05:30
end
end
describe '.change_description' do
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_description)
2017-08-17 22:00:37 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.change_description(noteable, project, author)
2015-09-11 14:41:01 +05:30
end
2016-06-02 11:05:42 +05:30
end
2015-09-11 14:41:01 +05:30
2016-06-02 11:05:42 +05:30
describe '.change_issue_confidentiality' do
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_issue_confidentiality)
2017-08-17 22:00:37 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.change_issue_confidentiality(noteable, project, author)
2015-09-11 14:41:01 +05:30
end
end
describe '.change_branch' do
subject { described_class.change_branch(noteable, project, author, 'target', old_branch, new_branch) }
2017-08-17 22:00:37 +05:30
2015-09-11 14:41:01 +05:30
let(:old_branch) { 'old_branch'}
let(:new_branch) { 'new_branch'}
2017-08-17 22:00:37 +05:30
it_behaves_like 'a system note' do
let(:action) { 'branch' }
end
2015-09-11 14:41:01 +05:30
context 'when target branch name changed' do
it 'sets the note text' do
2017-08-17 22:00:37 +05:30
expect(subject.note).to eq "changed target branch from `#{old_branch}` to `#{new_branch}`"
2015-09-11 14:41:01 +05:30
end
end
end
2015-10-24 18:46:33 +05:30
describe '.change_branch_presence' do
subject { described_class.change_branch_presence(noteable, project, author, :source, 'feature', :delete) }
2017-08-17 22:00:37 +05:30
it_behaves_like 'a system note' do
let(:action) { 'branch' }
end
2015-10-24 18:46:33 +05:30
context 'when source branch deleted' do
it 'sets the note text' do
2017-08-17 22:00:37 +05:30
expect(subject.note).to eq "deleted source branch `feature`"
2015-10-24 18:46:33 +05:30
end
end
end
2016-06-02 11:05:42 +05:30
describe '.new_issue_branch' do
2019-09-30 21:07:59 +05:30
let(:branch) { '1-mepmep' }
2016-06-02 11:05:42 +05:30
2019-09-30 21:07:59 +05:30
subject { described_class.new_issue_branch(noteable, project, author, branch, branch_project: branch_project) }
2016-06-02 11:05:42 +05:30
2019-09-30 21:07:59 +05:30
shared_examples_for 'a system note for new issue branch' do
it_behaves_like 'a system note' do
let(:action) { 'branch' }
2016-06-02 11:05:42 +05:30
end
2019-09-30 21:07:59 +05:30
context 'when a branch is created from the new branch button' do
it 'sets the note text' do
expect(subject.note).to start_with("created branch [`#{branch}`]")
end
end
end
context 'branch_project is set' do
let(:branch_project) { create(:project, :repository) }
it_behaves_like 'a system note for new issue branch'
end
context 'branch_project is not set' do
let(:branch_project) { nil }
it_behaves_like 'a system note for new issue branch'
2016-06-02 11:05:42 +05:30
end
end
2019-02-15 15:39:39 +05:30
describe '.new_merge_request' do
subject { described_class.new_merge_request(noteable, project, author, merge_request) }
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
it_behaves_like 'a system note' do
let(:action) { 'merge' }
end
it 'sets the new merge request note text' do
2019-09-30 21:07:59 +05:30
expect(subject.note).to eq("created merge request #{merge_request.to_reference(project)} to address this issue")
2019-02-15 15:39:39 +05:30
end
end
2019-10-12 21:52:04 +05:30
describe '.zoom_link_added' do
2019-12-21 20:55:43 +05:30
it 'calls ZoomService' do
expect_next_instance_of(::SystemNotes::ZoomService) do |service|
expect(service).to receive(:zoom_link_added)
end
2019-10-12 21:52:04 +05:30
2019-12-21 20:55:43 +05:30
described_class.zoom_link_added(noteable, project, author)
2019-10-12 21:52:04 +05:30
end
end
describe '.zoom_link_removed' do
2019-12-21 20:55:43 +05:30
it 'calls ZoomService' do
expect_next_instance_of(::SystemNotes::ZoomService) do |service|
expect(service).to receive(:zoom_link_removed)
end
2019-10-12 21:52:04 +05:30
2019-12-21 20:55:43 +05:30
described_class.zoom_link_removed(noteable, project, author)
2019-10-12 21:52:04 +05:30
end
end
2015-09-11 14:41:01 +05:30
describe '.cross_reference' do
2019-12-21 20:55:43 +05:30
let(:mentioner) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:cross_reference).with(mentioner)
2017-08-17 22:00:37 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.cross_reference(double, mentioner, double)
2015-09-11 14:41:01 +05:30
end
end
describe '.cross_reference_disallowed?' do
2019-12-21 20:55:43 +05:30
let(:mentioner) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:cross_reference_disallowed?).with(mentioner)
2015-09-11 14:41:01 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.cross_reference_disallowed?(double, mentioner)
2015-09-11 14:41:01 +05:30
end
end
describe '.cross_reference_exists?' do
2019-12-21 20:55:43 +05:30
let(:mentioner) { double }
2015-09-11 14:41:01 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:cross_reference_exists?).with(mentioner)
2015-09-11 14:41:01 +05:30
end
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
described_class.cross_reference_exists?(double, mentioner)
2016-04-02 18:10:28 +05:30
end
2015-09-11 14:41:01 +05:30
end
2015-12-23 02:04:40 +05:30
2016-06-02 11:05:42 +05:30
describe '.noteable_moved' do
2019-12-21 20:55:43 +05:30
let(:noteable_ref) { double }
let(:direction) { double }
2016-06-02 11:05:42 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:noteable_moved).with(noteable_ref, direction)
2016-06-02 11:05:42 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.noteable_moved(double, double, noteable_ref, double, direction: direction)
2016-06-02 11:05:42 +05:30
end
end
2019-09-30 21:07:59 +05:30
describe 'Jira integration' do
2017-08-17 22:00:37 +05:30
include JiraServiceHelper
2017-09-10 17:25:29 +05:30
let(:project) { create(:jira_project, :repository) }
2017-08-17 22:00:37 +05:30
let(:author) { create(:user) }
let(:issue) { create(:issue, project: project) }
let(:merge_request) { create(:merge_request, :simple, target_project: project, source_project: project) }
let(:jira_issue) { ExternalIssue.new("JIRA-1", project)}
let(:jira_tracker) { project.jira_service }
let(:commit) { project.commit }
let(:comment_url) { jira_api_comment_url(jira_issue.id) }
2018-11-20 20:47:30 +05:30
let(:success_message) { "SUCCESS: Successfully posted to http://jira.example.net." }
2017-08-17 22:00:37 +05:30
before do
stub_jira_urls(jira_issue.id)
jira_service_settings
end
2017-09-10 17:25:29 +05:30
def cross_reference(type, link_exists = false)
noteable = type == 'commit' ? commit : merge_request
links = []
if link_exists
url = if type == 'commit'
"#{Settings.gitlab.base_url}/#{project.namespace.path}/#{project.path}/commit/#{commit.id}"
else
"#{Settings.gitlab.base_url}/#{project.namespace.path}/#{project.path}/merge_requests/#{merge_request.iid}"
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
link = double(object: { 'url' => url })
links << link
expect(link).to receive(:save!)
end
allow(JIRA::Resource::Remotelink).to receive(:all).and_return(links)
described_class.cross_reference(jira_issue, noteable, author)
end
2017-08-17 22:00:37 +05:30
noteable_types = %w(merge_requests commit)
noteable_types.each do |type|
context "when noteable is a #{type}" do
it "blocks cross reference when #{type.underscore}_events is false" do
jira_tracker.update("#{type}_events" => false)
2017-09-10 17:25:29 +05:30
expect(cross_reference(type)).to eq("Events for #{type.pluralize.humanize.downcase} are disabled.")
2017-08-17 22:00:37 +05:30
end
2018-05-09 12:01:36 +05:30
it "creates cross reference when #{type.underscore}_events is true" do
2017-08-17 22:00:37 +05:30
jira_tracker.update("#{type}_events" => true)
2017-09-10 17:25:29 +05:30
expect(cross_reference(type)).to eq(success_message)
end
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
context 'when a new cross reference is created' do
it 'creates a new comment and remote link' do
cross_reference(type)
expect(WebMock).to have_requested(:post, jira_api_comment_url(jira_issue))
expect(WebMock).to have_requested(:post, jira_api_remote_link_url(jira_issue))
end
end
context 'when a link exists' do
it 'updates a link but does not create a new comment' do
expect(WebMock).not_to have_requested(:post, jira_api_comment_url(jira_issue))
cross_reference(type, true)
2017-08-17 22:00:37 +05:30
end
2015-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
describe "new reference" do
2018-11-08 19:23:39 +05:30
let(:favicon_path) { "http://localhost/assets/#{find_asset('favicon.png').digest_path}" }
2017-09-10 17:25:29 +05:30
before do
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
end
2017-08-17 22:00:37 +05:30
context 'for commits' do
it "creates comment" do
result = described_class.cross_reference(jira_issue, commit, author)
expect(result).to eq(success_message)
2015-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
it "creates remote link" do
described_class.cross_reference(jira_issue, commit, author)
expect(WebMock).to have_requested(:post, jira_api_remote_link_url(jira_issue)).with(
body: hash_including(
GlobalID: "GitLab",
2019-07-07 11:18:12 +05:30
relationship: 'mentioned on',
2017-08-17 22:00:37 +05:30
object: {
2017-09-10 17:25:29 +05:30
url: project_commit_url(project, commit),
2019-07-07 11:18:12 +05:30
title: "Commit - #{commit.title}",
2018-11-08 19:23:39 +05:30
icon: { title: "GitLab", url16x16: favicon_path },
2017-08-17 22:00:37 +05:30
status: { resolved: false }
}
)
).once
end
end
context 'for issues' do
2019-03-02 22:35:43 +05:30
let(:issue) { create(:issue, project: project) }
2017-08-17 22:00:37 +05:30
it "creates comment" do
result = described_class.cross_reference(jira_issue, issue, author)
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
expect(result).to eq(success_message)
end
it "creates remote link" do
described_class.cross_reference(jira_issue, issue, author)
expect(WebMock).to have_requested(:post, jira_api_remote_link_url(jira_issue)).with(
body: hash_including(
GlobalID: "GitLab",
2019-07-07 11:18:12 +05:30
relationship: 'mentioned on',
2017-08-17 22:00:37 +05:30
object: {
2017-09-10 17:25:29 +05:30
url: project_issue_url(project, issue),
2019-07-07 11:18:12 +05:30
title: "Issue - #{issue.title}",
2018-11-08 19:23:39 +05:30
icon: { title: "GitLab", url16x16: favicon_path },
2017-08-17 22:00:37 +05:30
status: { resolved: false }
}
)
).once
end
2015-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
context 'for snippets' do
let(:snippet) { create(:snippet, project: project) }
it "creates comment" do
result = described_class.cross_reference(jira_issue, snippet, author)
expect(result).to eq(success_message)
2015-12-23 02:04:40 +05:30
end
2017-08-17 22:00:37 +05:30
it "creates remote link" do
described_class.cross_reference(jira_issue, snippet, author)
expect(WebMock).to have_requested(:post, jira_api_remote_link_url(jira_issue)).with(
body: hash_including(
GlobalID: "GitLab",
2019-07-07 11:18:12 +05:30
relationship: 'mentioned on',
2017-08-17 22:00:37 +05:30
object: {
2017-09-10 17:25:29 +05:30
url: project_snippet_url(project, snippet),
2019-07-07 11:18:12 +05:30
title: "Snippet - #{snippet.title}",
2018-11-08 19:23:39 +05:30
icon: { title: "GitLab", url16x16: favicon_path },
2017-08-17 22:00:37 +05:30
status: { resolved: false }
}
)
).once
end
2015-12-23 02:04:40 +05:30
end
end
2017-08-17 22:00:37 +05:30
describe "existing reference" do
before do
2017-09-10 17:25:29 +05:30
allow(JIRA::Resource::Remotelink).to receive(:all).and_return([])
message = "[#{author.name}|http://localhost/#{author.username}] mentioned this issue in [a commit of #{project.full_path}|http://localhost/#{project.full_path}/commit/#{commit.id}]:\n'#{commit.title.chomp}'"
2017-08-17 22:00:37 +05:30
allow_any_instance_of(JIRA::Resource::Issue).to receive(:comments).and_return([OpenStruct.new(body: message)])
end
it "does not return success message" do
result = described_class.cross_reference(jira_issue, commit, author)
expect(result).not_to eq(success_message)
end
it 'does not try to create comment and remote link' do
subject
expect(WebMock).not_to have_requested(:post, jira_api_comment_url(jira_issue))
expect(WebMock).not_to have_requested(:post, jira_api_remote_link_url(jira_issue))
end
end
end
2019-07-31 22:56:46 +05:30
describe '.change_time_estimate' do
subject { described_class.change_time_estimate(noteable, project, author) }
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
end
context 'with a time estimate' do
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
expect(subject.note).to eq "changed time estimate to 1w 4d 5h"
end
2019-09-30 21:07:59 +05:30
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
end
it 'sets the note text' do
noteable.update_attribute(:time_estimate, 277200)
expect(subject.note).to eq "changed time estimate to 77h"
end
end
2019-07-31 22:56:46 +05:30
end
context 'without a time estimate' do
it 'sets the note text' do
expect(subject.note).to eq "removed time estimate"
end
end
end
2017-08-17 22:00:37 +05:30
describe '.discussion_continued_in_issue' do
2018-03-17 18:26:18 +05:30
let(:discussion) { create(:diff_note_on_merge_request, project: project).to_discussion }
2017-08-17 22:00:37 +05:30
let(:merge_request) { discussion.noteable }
let(:issue) { create(:issue, project: project) }
def reloaded_merge_request
MergeRequest.find(merge_request.id)
end
subject { described_class.discussion_continued_in_issue(discussion, project, author, issue) }
it_behaves_like 'a system note' do
let(:expected_noteable) { discussion.first_note.noteable }
let(:action) { 'discussion' }
end
it 'creates a new note in the discussion' do
# we need to completely rebuild the merge request object, or the `@discussions` on the merge request are not reloaded.
expect { subject }.to change { reloaded_merge_request.discussions.first.notes.size }.by(1)
end
it 'mentions the created issue in the system note' do
expect(subject.note).to include(issue.to_reference)
end
end
describe '.change_time_spent' do
# We need a custom noteable in order to the shared examples to be green.
let(:noteable) do
mr = create(:merge_request, source_project: project)
2018-03-17 18:26:18 +05:30
mr.spend_time(duration: 360000, user_id: author.id)
2017-08-17 22:00:37 +05:30
mr.save!
mr
end
subject do
described_class.change_time_spent(noteable, project, author)
end
it_behaves_like 'a system note' do
let(:action) { 'time_tracking' }
end
context 'when time was added' do
it 'sets the note text' do
spend_time!(277200)
expect(subject.note).to eq "added 1w 4d 5h of time spent"
end
end
context 'when time was subtracted' do
it 'sets the note text' do
spend_time!(-277200)
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
expect(subject.note).to eq "subtracted 1w 4d 5h of time spent"
end
end
2015-12-23 02:04:40 +05:30
2017-08-17 22:00:37 +05:30
context 'when time was removed' do
it 'sets the note text' do
spend_time!(:reset)
expect(subject.note).to eq "removed time spent"
2015-12-23 02:04:40 +05:30
end
end
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
context 'when time_tracking_limit_to_hours setting is true' do
before do
stub_application_setting(time_tracking_limit_to_hours: true)
end
it 'sets the note text' do
spend_time!(277200)
expect(subject.note).to eq "added 77h of time spent"
end
end
2017-08-17 22:00:37 +05:30
def spend_time!(seconds)
2018-03-17 18:26:18 +05:30
noteable.spend_time(duration: seconds, user_id: author.id)
2017-08-17 22:00:37 +05:30
noteable.save!
end
end
2018-03-17 18:26:18 +05:30
describe '.handle_merge_request_wip' do
context 'adding wip note' do
let(:noteable) { create(:merge_request, source_project: project, title: 'WIP Lorem ipsum') }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
subject { described_class.handle_merge_request_wip(noteable, project, author) }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
it_behaves_like 'a system note' do
let(:action) { 'title' }
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
it 'sets the note text' do
expect(subject.note).to eq 'marked as a **Work In Progress**'
end
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
context 'removing wip note' do
let(:noteable) { create(:merge_request, source_project: project, title: 'Lorem ipsum') }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
subject { described_class.handle_merge_request_wip(noteable, project, author) }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
it_behaves_like 'a system note' do
let(:action) { 'title' }
end
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
it 'sets the note text' do
expect(subject.note).to eq 'unmarked as a **Work In Progress**'
end
2017-08-17 22:00:37 +05:30
end
end
describe '.add_merge_request_wip_from_commit' do
let(:noteable) do
create(:merge_request, source_project: project, target_project: project)
end
subject do
described_class.add_merge_request_wip_from_commit(
noteable,
project,
author,
noteable.diff_head_commit
)
end
it_behaves_like 'a system note' do
let(:action) { 'title' }
end
it "posts the 'marked as a Work In Progress from commit' system note" do
expect(subject.note).to match(
/marked as a \*\*Work In Progress\*\* from #{Commit.reference_pattern}/
)
end
end
describe '.change_task_status' do
2019-12-21 20:55:43 +05:30
let(:new_task) { double }
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:change_task_status).with(new_task)
end
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
described_class.change_task_status(noteable, project, author, new_task)
2017-08-17 22:00:37 +05:30
end
end
describe '.resolve_all_discussions' do
let(:noteable) { create(:merge_request, source_project: project, target_project: project) }
subject { described_class.resolve_all_discussions(noteable, project, author) }
it_behaves_like 'a system note' do
let(:action) { 'discussion' }
end
it 'sets the note text' do
2019-09-30 21:07:59 +05:30
expect(subject.note).to eq 'resolved all threads'
2017-08-17 22:00:37 +05:30
end
2015-12-23 02:04:40 +05:30
end
2017-09-10 17:25:29 +05:30
describe '.diff_discussion_outdated' do
2018-03-17 18:26:18 +05:30
let(:discussion) { create(:diff_note_on_merge_request, project: project).to_discussion }
2017-09-10 17:25:29 +05:30
let(:merge_request) { discussion.noteable }
let(:change_position) { discussion.position }
def reloaded_merge_request
MergeRequest.find(merge_request.id)
end
subject { described_class.diff_discussion_outdated(discussion, project, author, change_position) }
it_behaves_like 'a system note' do
let(:expected_noteable) { discussion.first_note.noteable }
let(:action) { 'outdated' }
end
2018-03-17 18:26:18 +05:30
context 'when the change_position is valid for the discussion' do
it 'creates a new note in the discussion' do
# we need to completely rebuild the merge request object, or the `@discussions` on the merge request are not reloaded.
expect { subject }.to change { reloaded_merge_request.discussions.first.notes.size }.by(1)
end
it 'links to the diff in the system note' do
diff_id = merge_request.merge_request_diff.id
line_code = change_position.line_code(project.repository)
2019-09-30 21:07:59 +05:30
link = diffs_project_merge_request_path(project, merge_request, diff_id: diff_id, anchor: line_code)
expect(subject.note).to eq("changed this line in [version 1 of the diff](#{link})")
end
context 'discussion is on an image' do
let(:discussion) { create(:image_diff_note_on_merge_request, project: project).to_discussion }
it 'links to the diff in the system note' do
diff_id = merge_request.merge_request_diff.id
file_hash = change_position.file_hash
link = diffs_project_merge_request_path(project, merge_request, diff_id: diff_id, anchor: file_hash)
expect(subject.note).to eq("changed this file in [version 1 of the diff](#{link})")
end
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
end
2019-09-30 21:07:59 +05:30
context 'when the change_position does not point to a valid version' do
before do
allow(merge_request).to receive(:version_params_for).and_return(nil)
end
2018-03-17 18:26:18 +05:30
it 'creates a new note in the discussion' do
# we need to completely rebuild the merge request object, or the `@discussions` on the merge request are not reloaded.
expect { subject }.to change { reloaded_merge_request.discussions.first.notes.size }.by(1)
end
2017-09-10 17:25:29 +05:30
2018-03-17 18:26:18 +05:30
it 'does not create a link' do
expect(subject.note).to eq('changed this line in version 1 of the diff')
end
2017-09-10 17:25:29 +05:30
end
end
describe '.mark_duplicate_issue' do
2019-12-21 20:55:43 +05:30
let(:canonical_issue) { double }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:mark_duplicate_issue).with(canonical_issue)
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.mark_duplicate_issue(noteable, project, author, canonical_issue)
2017-09-10 17:25:29 +05:30
end
end
describe '.mark_canonical_issue_of_duplicate' do
2019-12-21 20:55:43 +05:30
let(:duplicate_issue) { double }
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:mark_canonical_issue_of_duplicate).with(duplicate_issue)
2017-09-10 17:25:29 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.mark_canonical_issue_of_duplicate(noteable, project, author, duplicate_issue)
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe '.discussion_lock' do
2019-12-21 20:55:43 +05:30
let(:issuable) { double }
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
before do
allow(issuable).to receive(:project).and_return(double)
2018-03-17 18:26:18 +05:30
end
2019-12-21 20:55:43 +05:30
it 'calls IssuableService' do
expect_next_instance_of(::SystemNotes::IssuablesService) do |service|
expect(service).to receive(:discussion_lock)
2018-03-17 18:26:18 +05:30
end
2019-12-21 20:55:43 +05:30
described_class.discussion_lock(issuable, double)
2018-03-17 18:26:18 +05:30
end
end
2015-09-11 14:41:01 +05:30
end