From b973fc4abdba680bd408d6ccc02309cfc2ca73ac Mon Sep 17 00:00:00 2001 From: Marin Jankovski Date: Thu, 5 Feb 2015 12:36:51 -0800 Subject: [PATCH] Add note cross reference specs, remove repetition. --- app/models/project_services/jira_service.rb | 8 +- spec/models/note_spec.rb | 89 ++++++++++++++++++--- spec/services/git_push_service_spec.rb | 12 +-- spec/support/repo_helpers.rb | 9 +++ 4 files changed, 98 insertions(+), 20 deletions(-) diff --git a/app/models/project_services/jira_service.rb b/app/models/project_services/jira_service.rb index 347fb28b17..000cf159e2 100644 --- a/app/models/project_services/jira_service.rb +++ b/app/models/project_services/jira_service.rb @@ -57,7 +57,7 @@ class JiraService < IssueTrackerService def create_cross_reference_note(mentioned, noteable, author) issue_name = mentioned.id 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 else @@ -161,7 +161,7 @@ class JiraService < IssueTrackerService "#{self.class.name} ERROR: #{result}. Hostname: #{url}." else case result.code - when 201 + when 201, 200 "#{self.class.name} SUCCESS 201: Sucessfully posted to #{url}." when 401 "#{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 end end - Rails.logger.info(message) - message + Rails.logger.info(message) + message end def server_url diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index 6ab7162c15..2413368aab 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -209,26 +209,80 @@ describe Note do let(:issue) { create(:issue, project: project) } let(:mergereq) { create(:merge_request, :simple, target_project: project, source_project: project) } 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 # roles, to ensure that the correct information can be inferred from any argument. 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}_" } + 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 + + 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, mergereq, author, project) } + + it { should == jira_status_message } + end end 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 } - its(:noteable) { should == issue } - its(:note) { should == "_mentioned in commit #{commit.sha}_" } + it { should be_valid } + its(:noteable) { should == issue } + 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 context 'merge request from an issue' do @@ -386,4 +440,19 @@ describe Note do let(:backref_text) { issue.gfm_reference } let(:set_mentionable_text) { ->(txt) { subject.note = txt } } 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 diff --git a/spec/services/git_push_service_spec.rb b/spec/services/git_push_service_spec.rb index 3b02fea7d7..47f7908186 100644 --- a/spec/services/git_push_service_spec.rb +++ b/spec/services/git_push_service_spec.rb @@ -270,15 +270,15 @@ describe GitPushService do it "should initiate one api call to jira server to close the issue" do message = { - 'update' => { - 'comment' => [{ - 'add' => { - 'body' => "Issue solved with http://localhost/#{project.path_with_namespace}/commit/#{closing_commit.id}" + update: { + comment: [{ + add: { + body: "Issue solved with http://localhost/#{project.path_with_namespace}/commit/#{closing_commit.id}" } }] }, - 'transition' => { - 'id' => '2' + transition: { + id: '2' } }.to_json diff --git a/spec/support/repo_helpers.rb b/spec/support/repo_helpers.rb index 4c4775da69..6d0a6bff81 100644 --- a/spec/support/repo_helpers.rb +++ b/spec/support/repo_helpers.rb @@ -96,4 +96,13 @@ eos commits: commits ) 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