2015-04-30 23:16:19 -04:00
require 'spec_helper'
2015-12-09 05:55:49 -05:00
describe SystemNoteService , services : true do
2016-11-09 16:55:21 -05:00
include Gitlab :: Routing . url_helpers
2015-04-30 23:16:19 -04:00
let ( :project ) { create ( :project ) }
let ( :author ) { create ( :user ) }
let ( :noteable ) { create ( :issue , project : project ) }
shared_examples_for 'a system note' do
2015-05-10 23:52:46 -04:00
it 'is valid' do
expect ( subject ) . to be_valid
end
2015-04-30 23:16:19 -04:00
it 'sets the noteable model' do
expect ( subject . noteable ) . to eq noteable
end
it 'sets the project' do
expect ( subject . project ) . to eq project
end
it 'sets the author' do
expect ( subject . author ) . to eq author
end
it 'is a system note' do
expect ( subject ) . to be_system
end
end
2015-05-09 18:18:50 -04:00
describe '.add_commits' do
2015-05-10 23:52:46 -04:00
subject { described_class . add_commits ( noteable , project , author , new_commits , old_commits , oldrev ) }
2015-05-09 18:18:50 -04:00
let ( :noteable ) { create ( :merge_request , source_project : project ) }
let ( :new_commits ) { noteable . commits }
let ( :old_commits ) { [ ] }
let ( :oldrev ) { nil }
2015-04-30 23:16:19 -04:00
it_behaves_like 'a system note'
2015-05-09 18:18:50 -04:00
describe 'note body' do
let ( :note_lines ) { subject . note . split ( " \n " ) . reject ( & :blank? ) }
2015-04-30 23:16:19 -04:00
2016-09-29 21:51:33 -04:00
describe 'comparison diff link line' do
2016-09-30 11:00:03 -04:00
it 'adds the comparison text' do
expect ( note_lines [ 2 ] ) . to match " [Compare with previous version] "
2016-09-29 21:51:33 -04:00
end
end
2015-05-09 18:18:50 -04:00
context 'without existing commits' do
it 'adds a message header' do
2016-11-23 01:55:23 -05:00
expect ( note_lines [ 0 ] ) . to eq " added #{ new_commits . size } commits "
2015-05-09 18:18:50 -04:00
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'adds a message line for each commit' do
new_commits . each_with_index do | commit , i |
# Skip the header
2016-10-03 08:11:16 -04:00
expect ( HTMLEntities . new . decode ( note_lines [ i + 1 ] ) ) . to eq " * #{ commit . short_id } - #{ commit . title } "
2015-05-09 18:18:50 -04:00
end
end
2015-04-30 23:16:19 -04:00
end
2015-05-09 18:18:50 -04:00
describe 'summary line for existing commits' do
2016-09-30 11:00:03 -04:00
let ( :summary_line ) { note_lines [ 1 ] }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'with one existing commit' do
let ( :old_commits ) { [ noteable . commits . last ] }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'includes the existing commit' do
expect ( summary_line ) . to eq " * #{ old_commits . first . short_id } - 1 commit from branch `feature` "
end
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'with multiple existing commits' do
let ( :old_commits ) { noteable . commits [ 3 .. - 1 ] }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'with oldrev' do
let ( :oldrev ) { noteable . commits [ 2 ] . id }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'includes a commit range' do
expect ( summary_line ) . to start_with " * #{ Commit . truncate_sha ( oldrev ) } ... #{ old_commits . last . short_id } "
2015-04-30 23:16:19 -04:00
end
2015-05-09 18:18:50 -04:00
it 'includes a commit count' do
2016-10-03 08:11:16 -04:00
expect ( summary_line ) . to end_with " - 26 commits from branch `feature` "
2015-04-30 23:16:19 -04:00
end
end
2015-05-09 18:18:50 -04:00
context 'without oldrev' do
it 'includes a commit range' do
expect ( summary_line ) . to start_with " * #{ old_commits [ 0 ] . short_id } .. #{ old_commits [ - 1 ] . short_id } "
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'includes a commit count' do
2016-10-03 08:11:16 -04:00
expect ( summary_line ) . to end_with " - 26 commits from branch `feature` "
2015-04-30 23:16:19 -04:00
end
end
2015-05-09 18:18:50 -04:00
context 'on a fork' do
before do
expect ( noteable ) . to receive ( :for_fork? ) . and_return ( true )
end
it 'includes the project namespace' do
expect ( summary_line ) . to end_with " ` #{ noteable . target_project_namespace } :feature` "
2015-04-30 23:16:19 -04:00
end
end
end
end
end
end
2015-05-09 18:18:50 -04:00
describe '.change_assignee' do
subject { described_class . change_assignee ( noteable , project , author , assignee ) }
2015-05-10 23:52:46 -04:00
let ( :assignee ) { create ( :user ) }
2015-05-09 18:18:50 -04:00
it_behaves_like 'a system note'
context 'when assignee added' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " assigned to @ #{ assignee . username } "
2015-05-09 18:18:50 -04:00
end
end
context 'when assignee removed' do
let ( :assignee ) { nil }
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq 'removed assignee'
2015-05-09 18:18:50 -04:00
end
end
end
describe '.change_label' do
2015-05-10 23:52:46 -04:00
subject { described_class . change_label ( noteable , project , author , added , removed ) }
2015-04-30 23:16:19 -04:00
let ( :labels ) { create_list ( :label , 2 ) }
let ( :added ) { [ ] }
let ( :removed ) { [ ] }
it_behaves_like 'a system note'
context 'with added labels' do
let ( :added ) { labels }
let ( :removed ) { [ ] }
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " added ~ #{ labels [ 0 ] . id } ~ #{ labels [ 1 ] . id } labels "
2015-04-30 23:16:19 -04:00
end
end
context 'with removed labels' do
let ( :added ) { [ ] }
let ( :removed ) { labels }
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " removed ~ #{ labels [ 0 ] . id } ~ #{ labels [ 1 ] . id } labels "
2015-04-30 23:16:19 -04:00
end
end
context 'with added and removed labels' do
let ( :added ) { [ labels [ 0 ] ] }
let ( :removed ) { [ labels [ 1 ] ] }
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " added ~ #{ labels [ 0 ] . id } and removed ~ #{ labels [ 1 ] . id } labels "
2015-04-30 23:16:19 -04:00
end
end
end
2015-05-09 18:18:50 -04:00
describe '.change_milestone' do
subject { described_class . change_milestone ( noteable , project , author , milestone ) }
2015-04-30 23:16:19 -04:00
2015-05-10 23:52:46 -04:00
let ( :milestone ) { create ( :milestone , project : project ) }
2015-04-30 23:16:19 -04:00
it_behaves_like 'a system note'
context 'when milestone added' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " changed milestone to #{ milestone . to_reference } "
2015-04-30 23:16:19 -04:00
end
end
context 'when milestone removed' do
let ( :milestone ) { nil }
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq 'removed milestone'
2015-04-30 23:16:19 -04:00
end
end
end
2015-05-09 18:18:50 -04:00
describe '.change_status' do
2015-05-10 23:52:46 -04:00
subject { described_class . change_status ( noteable , project , author , status , source ) }
2015-05-09 18:18:50 -04:00
let ( :status ) { 'new_status' }
let ( :source ) { nil }
2015-04-30 23:16:19 -04:00
it_behaves_like 'a system note'
2015-05-09 18:18:50 -04:00
context 'with a source' do
let ( :source ) { double ( 'commit' , gfm_reference : 'commit 123456' ) }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " #{ status } via commit 123456 "
2015-05-09 18:18:50 -04:00
end
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'without a source' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq status
2015-04-30 23:16:19 -04:00
end
2015-05-09 18:18:50 -04:00
end
end
2015-04-30 23:16:19 -04:00
2015-11-18 05:17:41 -05:00
describe '.merge_when_build_succeeds' do
2016-06-03 10:22:26 -04:00
let ( :pipeline ) { build ( :ci_pipeline_without_jobs ) }
2016-04-26 07:52:18 -04:00
let ( :noteable ) do
create ( :merge_request , source_project : project , target_project : project )
end
2015-11-18 05:17:41 -05:00
2016-06-20 12:48:04 -04:00
subject { described_class . merge_when_build_succeeds ( noteable , project , author , noteable . diff_head_commit ) }
2015-11-18 05:17:41 -05:00
it_behaves_like 'a system note'
2016-11-21 04:55:54 -05:00
it " posts the 'merge when pipeline succeeds' system note " do
2016-11-25 05:05:34 -05:00
expect ( subject . note ) . to match / enabled an automatic merge when the pipeline for ( \ w+ \/ \ w+@)? \ h{40} succeeds /
2015-11-24 08:59:02 -05:00
end
end
describe '.cancel_merge_when_build_succeeds' do
2016-04-26 07:52:18 -04:00
let ( :noteable ) do
create ( :merge_request , source_project : project , target_project : project )
end
2015-11-24 08:59:02 -05:00
subject { described_class . cancel_merge_when_build_succeeds ( noteable , project , author ) }
2015-11-18 05:17:41 -05:00
2015-11-24 08:59:02 -05:00
it_behaves_like 'a system note'
2016-11-21 04:55:54 -05:00
it " posts the 'merge when pipeline succeeds' system note " do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " canceled the automatic merge "
2015-11-18 05:17:41 -05:00
end
end
2015-05-26 21:49:04 -04:00
describe '.change_title' do
subject { described_class . change_title ( noteable , project , author , 'Old title' ) }
context 'when noteable responds to `title`' do
it_behaves_like 'a system note'
it 'sets the note text' do
expect ( subject . note ) .
2016-11-23 01:55:23 -05:00
to eq " changed title from **{-Old title-}** to **{+ #{ noteable . title } +}** "
2015-05-26 21:49:04 -04:00
end
end
end
2016-05-18 13:56:13 -04:00
describe '.change_issue_confidentiality' do
subject { described_class . change_issue_confidentiality ( noteable , project , author ) }
2016-04-20 18:41:11 -04:00
context 'when noteable responds to `confidential`' do
it_behaves_like 'a system note'
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq 'made the issue visible to everyone'
2016-04-20 18:41:11 -04:00
end
end
end
2015-05-28 21:00:37 -04:00
describe '.change_branch' do
2015-10-16 03:25:19 -04:00
subject { described_class . change_branch ( noteable , project , author , 'target' , old_branch , new_branch ) }
2015-05-28 21:00:37 -04:00
let ( :old_branch ) { 'old_branch' }
let ( :new_branch ) { 'new_branch' }
it_behaves_like 'a system note'
context 'when target branch name changed' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " changed target branch from ` #{ old_branch } ` to ` #{ new_branch } ` "
2015-05-28 21:00:37 -04:00
end
end
end
2015-10-15 04:41:46 -04:00
describe '.change_branch_presence' do
2015-10-16 03:25:19 -04:00
subject { described_class . change_branch_presence ( noteable , project , author , :source , 'feature' , :delete ) }
2015-10-15 04:41:46 -04:00
it_behaves_like 'a system note'
context 'when source branch deleted' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " deleted source branch `feature` "
2015-10-15 04:41:46 -04:00
end
end
end
2016-02-17 01:11:48 -05:00
describe '.new_issue_branch' do
subject { described_class . new_issue_branch ( noteable , project , author , " 1-mepmep " ) }
it_behaves_like 'a system note'
context 'when a branch is created from the new branch button' do
it 'sets the note text' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to match / \ Acreated branch [`1-mepmep`] /
2016-02-17 01:11:48 -05:00
end
end
end
2015-05-09 18:18:50 -04:00
describe '.cross_reference' do
subject { described_class . cross_reference ( noteable , mentioner , author ) }
2015-04-30 23:16:19 -04:00
2015-05-10 23:52:46 -04:00
let ( :mentioner ) { create ( :issue , project : project ) }
2015-05-09 18:18:50 -04:00
it_behaves_like 'a system note'
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'when cross-reference disallowed' do
before do
expect ( described_class ) . to receive ( :cross_reference_disallowed? ) . and_return ( true )
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'returns nil' do
expect ( subject ) . to be_nil
end
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'when cross-reference allowed' do
before do
expect ( described_class ) . to receive ( :cross_reference_disallowed? ) . and_return ( false )
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
describe 'note_body' do
context 'cross-project' do
let ( :project2 ) { create ( :project ) }
let ( :mentioner ) { create ( :issue , project : project2 ) }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'from Commit' do
let ( :mentioner ) { project2 . repository . commit }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'references the mentioning commit' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " mentioned in commit #{ mentioner . to_reference ( project ) } "
2015-04-30 23:16:19 -04:00
end
end
2015-05-09 18:18:50 -04:00
context 'from non-Commit' do
it 'references the mentioning object' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " mentioned in issue #{ mentioner . to_reference ( project ) } "
2015-04-30 23:16:19 -04:00
end
end
end
2015-05-10 23:52:46 -04:00
context 'within the same project' do
2015-05-09 18:18:50 -04:00
context 'from Commit' do
let ( :mentioner ) { project . repository . commit }
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
it 'references the mentioning commit' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " mentioned in commit #{ mentioner . to_reference } "
2015-05-09 18:18:50 -04:00
end
end
2015-04-30 23:16:19 -04:00
2015-05-09 18:18:50 -04:00
context 'from non-Commit' do
it 'references the mentioning object' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to eq " mentioned in issue #{ mentioner . to_reference } "
2015-05-09 18:18:50 -04:00
end
end
end
2015-04-30 23:16:19 -04:00
end
end
end
describe '.cross_reference?' do
it 'is truthy when text begins with expected text' do
2016-11-23 01:55:23 -05:00
expect ( described_class . cross_reference? ( 'mentioned in something' ) ) . to be_truthy
end
it 'is truthy when text begins with legacy capitalized expected text' do
expect ( described_class . cross_reference? ( 'mentioned in something' ) ) . to be_truthy
2015-04-30 23:16:19 -04:00
end
it 'is falsey when text does not begin with expected text' do
expect ( described_class . cross_reference? ( 'this is a note' ) ) . to be_falsey
end
end
2015-05-10 23:51:49 -04:00
describe '.cross_reference_disallowed?' do
context 'when mentioner is not a MergeRequest' do
it 'is falsey' do
mentioner = noteable . dup
expect ( described_class . cross_reference_disallowed? ( noteable , mentioner ) ) .
to be_falsey
end
end
context 'when mentioner is a MergeRequest' do
let ( :mentioner ) { create ( :merge_request , :simple , source_project : project ) }
let ( :noteable ) { project . commit }
it 'is truthy when noteable is in commits' do
expect ( mentioner ) . to receive ( :commits ) . and_return ( [ noteable ] )
expect ( described_class . cross_reference_disallowed? ( noteable , mentioner ) ) .
to be_truthy
end
it 'is falsey when noteable is not in commits' do
expect ( mentioner ) . to receive ( :commits ) . and_return ( [ ] )
expect ( described_class . cross_reference_disallowed? ( noteable , mentioner ) ) .
to be_falsey
end
end
2015-06-14 11:41:11 -04:00
context 'when notable is an ExternalIssue' do
let ( :noteable ) { ExternalIssue . new ( 'EXT-1234' , project ) }
it 'is truthy' do
mentioner = noteable . dup
expect ( described_class . cross_reference_disallowed? ( noteable , mentioner ) ) .
to be_truthy
end
end
2015-05-10 23:51:49 -04:00
end
2015-04-30 23:16:19 -04:00
describe '.cross_reference_exists?' do
let ( :commit0 ) { project . commit }
let ( :commit1 ) { project . commit ( 'HEAD~2' ) }
context 'issue from commit' do
before do
# Mention issue (noteable) from commit0
described_class . cross_reference ( noteable , commit0 , author )
end
it 'is truthy when already mentioned' do
expect ( described_class . cross_reference_exists? ( noteable , commit0 ) ) .
to be_truthy
end
it 'is falsey when not already mentioned' do
expect ( described_class . cross_reference_exists? ( noteable , commit1 ) ) .
to be_falsey
end
2016-11-23 01:55:23 -05:00
context 'legacy capitalized cross reference' do
before do
# Mention issue (noteable) from commit0
system_note = described_class . cross_reference ( noteable , commit0 , author )
system_note . update ( note : system_note . note . capitalize )
end
it 'is truthy when already mentioned' do
expect ( described_class . cross_reference_exists? ( noteable , commit0 ) ) .
to be_truthy
end
end
2015-04-30 23:16:19 -04:00
end
context 'commit from commit' do
before do
# Mention commit1 from commit0
described_class . cross_reference ( commit0 , commit1 , author )
end
it 'is truthy when already mentioned' do
expect ( described_class . cross_reference_exists? ( commit0 , commit1 ) ) .
to be_truthy
end
it 'is falsey when not already mentioned' do
expect ( described_class . cross_reference_exists? ( commit1 , commit0 ) ) .
to be_falsey
end
2016-11-23 01:55:23 -05:00
context 'legacy capitalized cross reference' do
before do
# Mention commit1 from commit0
system_note = described_class . cross_reference ( commit0 , commit1 , author )
system_note . update ( note : system_note . note . capitalize )
end
it 'is truthy when already mentioned' do
expect ( described_class . cross_reference_exists? ( commit0 , commit1 ) ) .
to be_truthy
end
end
2015-04-30 23:16:19 -04:00
end
2016-02-04 12:36:16 -05:00
2016-02-12 12:01:47 -05:00
context 'commit with cross-reference from fork' do
2016-09-19 14:28:41 -04:00
let ( :author2 ) { create ( :project_member , :reporter , user : create ( :user ) , project : project ) . user }
2016-02-04 12:36:16 -05:00
let ( :forked_project ) { Projects :: ForkService . new ( project , author2 ) . execute }
let ( :commit2 ) { forked_project . commit }
before do
2016-02-12 12:01:47 -05:00
described_class . cross_reference ( noteable , commit0 , author2 )
2016-02-04 12:36:16 -05:00
end
2016-02-12 12:01:47 -05:00
it 'is true when a fork mentions an external issue' do
2016-02-05 11:43:05 -05:00
expect ( described_class . cross_reference_exists? ( noteable , commit2 ) ) .
2016-02-12 12:01:47 -05:00
to be true
2016-02-04 12:36:16 -05:00
end
2016-11-23 01:55:23 -05:00
context 'legacy capitalized cross reference' do
before do
system_note = described_class . cross_reference ( noteable , commit0 , author2 )
system_note . update ( note : system_note . note . capitalize )
end
it 'is true when a fork mentions an external issue' do
expect ( described_class . cross_reference_exists? ( noteable , commit2 ) ) .
to be true
end
end
2016-02-04 12:36:16 -05:00
end
2015-04-30 23:16:19 -04:00
end
2015-12-17 17:08:14 -05:00
2016-02-16 05:47:00 -05:00
describe '.noteable_moved' do
let ( :new_project ) { create ( :project ) }
let ( :new_noteable ) { create ( :issue , project : new_project ) }
2016-02-15 09:14:57 -05:00
subject do
2016-03-16 05:24:44 -04:00
described_class . noteable_moved ( noteable , project , new_noteable , author , direction : direction )
2016-02-15 09:14:57 -05:00
end
2016-02-16 05:47:00 -05:00
shared_examples 'cross project mentionable' do
include GitlabMarkdownHelper
2016-08-01 11:00:44 -04:00
it 'contains cross reference to new noteable' do
2016-02-16 05:47:00 -05:00
expect ( subject . note ) . to include cross_project_reference ( new_project , new_noteable )
end
2016-08-01 11:00:44 -04:00
it 'mentions referenced noteable' do
2016-02-16 05:47:00 -05:00
expect ( subject . note ) . to include new_noteable . to_reference
end
2016-08-01 11:00:44 -04:00
it 'mentions referenced project' do
2016-11-02 19:49:13 -04:00
expect ( subject . note ) . to include new_project . path_with_namespace
2016-02-16 05:47:00 -05:00
end
end
context 'moved to' do
let ( :direction ) { :to }
it_behaves_like 'cross project mentionable'
2016-02-15 09:14:57 -05:00
2016-08-01 11:00:44 -04:00
it 'notifies about noteable being moved to' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to match / moved to /
2016-02-16 05:47:00 -05:00
end
2016-02-15 09:14:57 -05:00
end
2016-02-16 05:47:00 -05:00
context 'moved from' do
let ( :direction ) { :from }
it_behaves_like 'cross project mentionable'
2016-08-01 11:00:44 -04:00
it 'notifies about noteable being moved from' do
2016-11-23 01:55:23 -05:00
expect ( subject . note ) . to match / moved from /
2016-02-16 05:47:00 -05:00
end
2016-02-15 09:14:57 -05:00
end
2016-02-16 05:47:00 -05:00
context 'invalid direction' do
2016-02-17 09:59:25 -05:00
let ( :direction ) { :invalid }
2016-02-16 05:47:00 -05:00
2016-08-01 11:00:44 -04:00
it 'raises error' do
2016-02-16 05:47:00 -05:00
expect { subject } . to raise_error StandardError , / Invalid direction /
end
2016-02-15 09:14:57 -05:00
end
end
2016-05-07 11:41:10 -04:00
describe '.new_commit_summary' do
it 'escapes HTML titles' do
commit = double ( title : '<pre>This is a test</pre>' , short_id : '12345678' )
escaped = '* 12345678 - <pre>This is a test</pre>'
expect ( described_class . new_commit_summary ( [ commit ] ) ) . to eq ( [ escaped ] )
end
end
2015-12-17 17:08:14 -05:00
include JiraServiceHelper
describe 'JIRA integration' do
2016-09-29 17:11:32 -04:00
let ( :project ) { create ( :jira_project ) }
let ( :author ) { create ( :user ) }
let ( :issue ) { create ( :issue , project : project ) }
2016-11-14 16:30:01 -05:00
let ( :merge_request ) { create ( :merge_request , :simple , target_project : project , source_project : project ) }
2016-09-29 17:11:32 -04:00
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 ) }
let ( :success_message ) { " JiraService SUCCESS: Successfully posted to http://jira.example.net. " }
2016-11-17 15:46:31 -05:00
before do
stub_jira_urls ( jira_issue . id )
jira_service_settings
end
2015-12-17 17:08:14 -05:00
2016-11-14 16:30:01 -05:00
noteable_types = [ " 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 )
noteable = type == " commit " ? commit : merge_request
result = described_class . cross_reference ( jira_issue , noteable , author )
expect ( result ) . to eq ( " Events for #{ noteable . class . to_s . underscore . humanize . pluralize . downcase } are disabled. " )
end
it " blocks cross reference when #{ type . underscore } _events is true " do
jira_tracker . update ( " #{ type } _events " = > true )
noteable = type == " commit " ? commit : merge_request
result = described_class . cross_reference ( jira_issue , noteable , author )
2015-12-17 17:08:14 -05:00
2016-11-14 16:30:01 -05:00
expect ( result ) . to eq ( success_message )
end
end
end
2015-12-17 17:08:14 -05:00
2016-11-17 15:46:31 -05:00
describe " new reference " do
context 'for commits' do
it " creates comment " do
result = described_class . cross_reference ( jira_issue , commit , author )
2015-12-17 17:08:14 -05:00
2016-11-17 15:46:31 -05:00
expect ( result ) . to eq ( success_message )
end
2016-11-09 16:55:21 -05:00
it " creates remote link " do
2016-11-17 15:46:31 -05:00
described_class . cross_reference ( jira_issue , commit , author )
2016-11-09 16:55:21 -05:00
expect ( WebMock ) . to have_requested ( :post , jira_api_remote_link_url ( jira_issue ) ) . with (
body : hash_including (
GlobalID : " GitLab " ,
object : {
url : namespace_project_commit_url ( project . namespace , project , commit ) ,
title : " GitLab: Mentioned on commit - #{ commit . title } " ,
icon : { title : " GitLab " , url16x16 : " https://gitlab.com/favicon.ico " } ,
status : { resolved : false }
}
)
) . once
end
2015-12-17 17:08:14 -05:00
end
2016-11-17 15:46:31 -05:00
context 'for issues' do
let ( :issue ) { create ( :issue , project : project ) }
2015-12-17 17:08:14 -05:00
2016-11-17 15:46:31 -05:00
it " creates comment " do
result = described_class . cross_reference ( jira_issue , issue , author )
2015-12-17 17:08:14 -05:00
2016-11-17 15:46:31 -05:00
expect ( result ) . to eq ( success_message )
end
2016-11-09 16:55:21 -05:00
it " creates remote link " do
2016-11-17 15:46:31 -05:00
described_class . cross_reference ( jira_issue , issue , author )
2016-11-09 16:55:21 -05:00
expect ( WebMock ) . to have_requested ( :post , jira_api_remote_link_url ( jira_issue ) ) . with (
body : hash_including (
GlobalID : " GitLab " ,
object : {
url : namespace_project_issue_url ( project . namespace , project , issue ) ,
title : " GitLab: Mentioned on issue - #{ issue . title } " ,
icon : { title : " GitLab " , url16x16 : " https://gitlab.com/favicon.ico " } ,
status : { resolved : false }
}
)
) . once
end
2016-09-29 17:11:32 -04:00
end
2016-11-17 15:46:31 -05:00
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 )
end
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 " ,
object : {
url : namespace_project_snippet_url ( project . namespace , project , snippet ) ,
title : " GitLab: Mentioned on snippet - #{ snippet . title } " ,
icon : { title : " GitLab " , url16x16 : " https://gitlab.com/favicon.ico " } ,
status : { resolved : false }
}
)
) . once
end
end
2016-09-29 17:11:32 -04:00
end
describe " existing reference " do
before do
2016-11-04 15:36:37 -04:00
message = " [ #{ author . name } |http://localhost/ #{ author . username } ] mentioned this issue in [a commit of #{ project . path_with_namespace } |http://localhost/ #{ project . path_with_namespace } /commit/ #{ commit . id } ]: \n ' #{ commit . title . chomp } ' "
2016-09-29 17:11:32 -04:00
allow_any_instance_of ( JIRA :: Resource :: Issue ) . to receive ( :comments ) . and_return ( [ OpenStruct . new ( body : message ) ] )
2015-12-17 17:08:14 -05:00
end
2016-09-29 17:11:32 -04:00
2016-11-17 15:46:31 -05:00
it " does not return success message " do
result = described_class . cross_reference ( jira_issue , commit , author )
2016-09-29 17:11:32 -04:00
2016-11-17 15:46:31 -05:00
expect ( result ) . not_to eq ( success_message )
end
2016-11-09 16:55:21 -05:00
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
2015-12-17 17:08:14 -05:00
end
end
2016-10-26 17:21:50 -04:00
describe '.discussion_continued_in_issue' do
let ( :discussion ) { Discussion . for_diff_notes ( [ create ( :diff_note_on_merge_request ) ] ) . first }
let ( :merge_request ) { discussion . noteable }
let ( :project ) { merge_request . source_project }
let ( :issue ) { create ( :issue , project : project ) }
let ( :user ) { create ( :user ) }
def reloaded_merge_request
MergeRequest . find ( merge_request . id )
end
before do
project . team << [ user , :developer ]
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 { SystemNoteService . discussion_continued_in_issue ( discussion , project , user , issue ) } .
to change { reloaded_merge_request . discussions . first . notes . size } . by ( 1 )
end
it 'mentions the created issue in the system note' do
note = SystemNoteService . discussion_continued_in_issue ( discussion , project , user , issue )
expect ( note . note ) . to include ( issue . to_reference )
end
end
2015-04-30 23:16:19 -04:00
end