Commit b973fc4a authored by Marin Jankovski's avatar Marin Jankovski

Add note cross reference specs, remove repetition.

parent a7ae2679
...@@ -57,7 +57,7 @@ class JiraService < IssueTrackerService ...@@ -57,7 +57,7 @@ class JiraService < IssueTrackerService
def create_cross_reference_note(mentioned, noteable, author) def create_cross_reference_note(mentioned, noteable, author)
issue_name = mentioned.id issue_name = mentioned.id
project = self.project project = self.project
noteable_name = noteable.class.name.downcase.to_sym noteable_name = noteable.class.name.underscore.downcase.to_sym
noteable_id = if noteable.is_a?(Commit) noteable_id = if noteable.is_a?(Commit)
noteable.id noteable.id
else else
...@@ -161,7 +161,7 @@ class JiraService < IssueTrackerService ...@@ -161,7 +161,7 @@ class JiraService < IssueTrackerService
"#{self.class.name} ERROR: #{result}. Hostname: #{url}." "#{self.class.name} ERROR: #{result}. Hostname: #{url}."
else else
case result.code case result.code
when 201 when 201, 200
"#{self.class.name} SUCCESS 201: Sucessfully posted to #{url}." "#{self.class.name} SUCCESS 201: Sucessfully posted to #{url}."
when 401 when 401
"#{self.class.name} ERROR 401: Unauthorized. Check the #{self.username} credentials and JIRA access permissions and try again." "#{self.class.name} ERROR 401: Unauthorized. Check the #{self.username} credentials and JIRA access permissions and try again."
...@@ -170,8 +170,8 @@ class JiraService < IssueTrackerService ...@@ -170,8 +170,8 @@ class JiraService < IssueTrackerService
end end
end end
Rails.logger.info(message) Rails.logger.info(message)
message message
end end
def server_url def server_url
......
...@@ -209,26 +209,80 @@ describe Note do ...@@ -209,26 +209,80 @@ describe Note do
let(:issue) { create(:issue, project: project) } let(:issue) { create(:issue, project: project) }
let(:mergereq) { create(:merge_request, :simple, target_project: project, source_project: project) } let(:mergereq) { create(:merge_request, :simple, target_project: project, source_project: project) }
let(:commit) { project.repository.commit } let(:commit) { project.repository.commit }
let(:jira_issue) { JiraIssue.new("JIRA-1", project)}
let(:jira_tracker) { project.create_jira_service if project.jira_service.nil? }
let(:api_mention_url) { 'http://jira.example/rest/api/2/issue/JIRA-1/comment' }
# Test all of {issue, merge request, commit} in both the referenced and referencing # Test all of {issue, merge request, commit} in both the referenced and referencing
# roles, to ensure that the correct information can be inferred from any argument. # roles, to ensure that the correct information can be inferred from any argument.
context 'issue from a merge request' do context 'issue from a merge request' do
subject { Note.create_cross_reference_note(issue, mergereq, author, project) } context 'in default issue tracker' do
subject { Note.create_cross_reference_note(issue, mergereq, author, project) }
it { should be_valid }
its(:noteable) { should == issue }
its(:project) { should == issue.project }
its(:author) { should == author }
its(:note) { should == "_mentioned in merge request !#{mergereq.iid}_" }
end
it { should be_valid } context 'in JIRA issue tracker' do
its(:noteable) { should == issue } before do
its(:project) { should == issue.project } jira_service_settings
its(:author) { should == author } WebMock.stub_request(:post, api_mention_url)
its(:note) { should == "_mentioned in merge request !#{mergereq.iid}_" } end
after do
jira_tracker.destroy!
end
subject { Note.create_cross_reference_note(jira_issue, mergereq, author, project) }
it { should == jira_status_message }
end
end end
context 'issue from a commit' do context 'issue from a commit' do
subject { Note.create_cross_reference_note(issue, commit, author, project) } context 'in default issue tracker' do
subject { Note.create_cross_reference_note(issue, commit, author, project) }
it { should be_valid } it { should be_valid }
its(:noteable) { should == issue } its(:noteable) { should == issue }
its(:note) { should == "_mentioned in commit #{commit.sha}_" } its(:note) { should == "_mentioned in commit #{commit.sha}_" }
end
context 'in JIRA issue tracker' do
before do
jira_service_settings
WebMock.stub_request(:post, api_mention_url)
end
after do
jira_tracker.destroy!
end
subject { Note.create_cross_reference_note(jira_issue, commit, author, project) }
it { should == jira_status_message }
end
end
context 'issue from an isue' do
context 'in JIRA issue tracker' do
before do
jira_service_settings
WebMock.stub_request(:post, api_mention_url)
end
after do
jira_tracker.destroy!
end
subject { Note.create_cross_reference_note(jira_issue, issue, author, project) }
it { should == jira_status_message }
end
end end
context 'merge request from an issue' do context 'merge request from an issue' do
...@@ -386,4 +440,19 @@ describe Note do ...@@ -386,4 +440,19 @@ describe Note do
let(:backref_text) { issue.gfm_reference } let(:backref_text) { issue.gfm_reference }
let(:set_mentionable_text) { ->(txt) { subject.note = txt } } let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
end end
def jira_service_settings
properties = {
"title"=>"JIRA tracker",
"project_url"=>"http://jira.example/issues/?jql=project=A",
"issues_url"=>"http://jira.example/browse/JIRA-1",
"new_issue_url"=>"http://jira.example/secure/CreateIssue.jspa"
}
jira_tracker.update_attributes(properties: properties, active: true)
end
def jira_status_message
"JiraService SUCCESS 201: Sucessfully posted to #{api_mention_url}."
end
end end
...@@ -270,15 +270,15 @@ describe GitPushService do ...@@ -270,15 +270,15 @@ describe GitPushService do
it "should initiate one api call to jira server to close the issue" do it "should initiate one api call to jira server to close the issue" do
message = { message = {
'update' => { update: {
'comment' => [{ comment: [{
'add' => { add: {
'body' => "Issue solved with http://localhost/#{project.path_with_namespace}/commit/#{closing_commit.id}" body: "Issue solved with http://localhost/#{project.path_with_namespace}/commit/#{closing_commit.id}"
} }
}] }]
}, },
'transition' => { transition: {
'id' => '2' id: '2'
} }
}.to_json }.to_json
......
...@@ -96,4 +96,13 @@ eos ...@@ -96,4 +96,13 @@ eos
commits: commits commits: commits
) )
end end
def jira_properties
{
"title"=>"JIRA tracker",
"project_url"=>"http://jira.example/issues/?jql=project=A",
"issues_url"=>"http://jira.example/browse/JIRA-1",
"new_issue_url"=>"http://jira.example/secure/CreateIssue.jspa"
}
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