Commit a7ae2679 authored by Marin Jankovski's avatar Marin Jankovski

Put the call for external tracker mention in the same place as gitlab default...

Put the call for external tracker mention in the same place as gitlab default mention. Add git push service spec.
parent 03f16a95
...@@ -90,8 +90,13 @@ class Note < ActiveRecord::Base ...@@ -90,8 +90,13 @@ class Note < ActiveRecord::Base
note_options.merge!(noteable: noteable) note_options.merge!(noteable: noteable)
end end
if noteable.is_a?(ExternalIssue)
project.issues_tracker.create_cross_reference_note(noteable, mentioner, author)
else
create(note_options) unless cross_reference_disallowed?(noteable, mentioner) create(note_options) unless cross_reference_disallowed?(noteable, mentioner)
end end
end
def create_milestone_change_note(noteable, project, author, milestone) def create_milestone_change_note(noteable, project, author, milestone)
body = if milestone.nil? body = if milestone.nil?
...@@ -169,9 +174,7 @@ class Note < ActiveRecord::Base ...@@ -169,9 +174,7 @@ class Note < ActiveRecord::Base
# eg. mentioning a commit in MR comments which exists inside a MR # eg. mentioning a commit in MR comments which exists inside a MR
# should not create "mentioned in" note. # should not create "mentioned in" note.
def cross_reference_disallowed?(noteable, mentioner) def cross_reference_disallowed?(noteable, mentioner)
if noteable.is_a?(ExternalIssue) if mentioner.kind_of?(MergeRequest)
true
elsif mentioner.kind_of?(MergeRequest)
mentioner.commits.map(&:id).include? noteable.id mentioner.commits.map(&:id).include? noteable.id
end end
end end
......
...@@ -54,8 +54,15 @@ class JiraService < IssueTrackerService ...@@ -54,8 +54,15 @@ class JiraService < IssueTrackerService
close_issue(push, issue.id) if issue close_issue(push, issue.id) if issue
end end
def create_cross_reference_note(mentioned, noteable, author, project) def create_cross_reference_note(mentioned, noteable, author)
issue_name = mentioned.id issue_name = mentioned.id
project = self.project
noteable_name = noteable.class.name.downcase.to_sym
noteable_id = if noteable.is_a?(Commit)
noteable.id
else
noteable.iid
end
data = { data = {
user: { user: {
...@@ -68,7 +75,7 @@ class JiraService < IssueTrackerService ...@@ -68,7 +75,7 @@ class JiraService < IssueTrackerService
}, },
entity: { entity: {
name: noteable.class.name.underscore.humanize.downcase, name: noteable.class.name.underscore.humanize.downcase,
url: resource_url(polymorphic_url([project, noteable], routing_type: :path)) url: resource_url(polymorphic_url([project, noteable_name], id: noteable_id, routing_type: :path))
} }
} }
......
...@@ -15,14 +15,10 @@ module Notes ...@@ -15,14 +15,10 @@ module Notes
# Create a cross-reference note if this Note contains GFM that names an # Create a cross-reference note if this Note contains GFM that names an
# issue, merge request, or commit. # issue, merge request, or commit.
note.references.each do |mentioned| note.references.each do |mentioned|
if mentioned.is_a?(ExternalIssue)
note.project.issues_tracker.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
else
Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project) Note.create_cross_reference_note(mentioned, note.noteable, note.author, note.project)
end end
end end
end end
end
note note
end end
......
...@@ -238,7 +238,8 @@ describe GitPushService do ...@@ -238,7 +238,8 @@ describe GitPushService do
end end
context "for jira issue tracker" do context "for jira issue tracker" do
let(:api_url) { 'http://jira.example/rest/api/2/issue/JIRA-1/transitions' } let(:api_transition_url) { 'http://jira.example/rest/api/2/issue/JIRA-1/transitions' }
let(:api_mention_url) { 'http://jira.example/rest/api/2/issue/JIRA-1/comment' }
let(:jira_tracker) { project.create_jira_service if project.jira_service.nil? } let(:jira_tracker) { project.create_jira_service if project.jira_service.nil? }
before do before do
...@@ -250,7 +251,8 @@ describe GitPushService do ...@@ -250,7 +251,8 @@ describe GitPushService do
} }
jira_tracker.update_attributes(properties: properties, active: true) jira_tracker.update_attributes(properties: properties, active: true)
WebMock.stub_request(:post, api_url) WebMock.stub_request(:post, api_transition_url)
WebMock.stub_request(:post, api_mention_url)
closing_commit.stub({ closing_commit.stub({
issue_closing_regex: Regexp.new(Gitlab.config.gitlab.issue_closing_pattern), issue_closing_regex: Regexp.new(Gitlab.config.gitlab.issue_closing_pattern),
...@@ -266,7 +268,7 @@ describe GitPushService do ...@@ -266,7 +268,7 @@ describe GitPushService do
jira_tracker.destroy! jira_tracker.destroy!
end end
it "should initiate one api call to jira server" do it "should initiate one api call to jira server to close the issue" do
message = { message = {
'update' => { 'update' => {
'comment' => [{ 'comment' => [{
...@@ -281,11 +283,19 @@ describe GitPushService do ...@@ -281,11 +283,19 @@ describe GitPushService do
}.to_json }.to_json
service.execute(project, user, @oldrev, @newrev, @ref) service.execute(project, user, @oldrev, @newrev, @ref)
WebMock.should have_requested(:post, api_url).with( WebMock.should have_requested(:post, api_transition_url).with(
body: message body: message
).once ).once
end end
it "should initiate one api call to jira server to mention the issue" do
service.execute(project, user, @oldrev, @newrev, @ref)
WebMock.should have_requested(:post, api_mention_url).with(
body: /mentioned JIRA-1 in/
).once
end
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