Commit f0bffb9e authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '280951-add-method-to-create-issue-in-jira' into 'master'

Add method to JiraService to create issue in Jira using API

See merge request gitlab-org/gitlab!48434
parents 878b844f bd1c71e4
......@@ -42,6 +42,26 @@ module EE
"#{url}/secure/CreateIssueDetails!init.jspa?pid=#{jira_project_id}&issuetype=#{vulnerabilities_issuetype}&summary=#{escaped_summary}&description=#{escaped_description}"[0..MAX_URL_LENGTH]
end
def create_issue(summary, description)
return if client_url.blank?
jira_request do
issue = client.Issue.build
issue.save!(
fields: {
project: { id: jira_project_id },
issuetype: { id: vulnerabilities_issuetype },
summary: summary,
description: description
}
)
issue
rescue JIRA::HTTPError => e
issue.attrs[:errors] = ::Gitlab::Json.parse(e.response.read_body)
issue
end
end
def jira_project_id
strong_memoize(:jira_project_id) do
client_url.present? ? jira_request { client.Project.find(project_key).id } : nil
......
......@@ -143,6 +143,48 @@ RSpec.describe JiraService do
end
end
describe '#create_issue' do
let(:jira_service) { described_class.new(options) }
let(:issue_info) { { 'id': '10000' } }
before do
allow(jira_service).to receive(:jira_project_id).and_return('11223')
allow(jira_service).to receive(:vulnerabilities_issuetype).and_return('10001')
end
context 'when there is no issues in Jira API' do
before do
WebMock.stub_request(:post, 'http://jira.example.com/rest/api/2/issue').with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(body: issue_info.to_json)
end
it 'creates issue in Jira API' do
issue = jira_service.create_issue("Special Summary!?", "*ID*: 2\n_Issue_: !")
expect(WebMock).to have_requested(:post, 'http://jira.example.com/rest/api/2/issue').with(
body: { fields: { project: { id: '11223' }, issuetype: { id: '10001' }, summary: 'Special Summary!?', description: "*ID*: 2\n_Issue_: !" } }.to_json
).once
expect(issue.id).to eq('10000')
end
end
context 'when there is an error in Jira' do
let(:errors) { { 'errorMessages' => [], 'errors' => { 'summary' => 'You must specify a summary of the issue.' } } }
before do
WebMock.stub_request(:post, 'http://jira.example.com/rest/api/2/issue').with(basic_auth: %w(gitlab_jira_username gitlab_jira_password)).to_return(status: [400, 'Bad Request'], body: errors.to_json)
end
it 'returns issue with errors' do
issue = jira_service.create_issue('', "*ID*: 2\n_Issue_: !")
expect(WebMock).to have_requested(:post, 'http://jira.example.com/rest/api/2/issue').with(
body: { fields: { project: { id: '11223' }, issuetype: { id: '10001' }, summary: '', description: "*ID*: 2\n_Issue_: !" } }.to_json
).once
expect(issue.attrs[:errors]).to eq(errors)
end
end
end
describe '#new_issue_url_with_predefined_fields' do
before do
allow(jira_service).to receive(:jira_project_id).and_return('11223')
......
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