Commit 9611953a authored by Nick Thomas's avatar Nick Thomas

Merge branch '209792-jira-issue-meta' into 'master'

Map jira metadata fields to gitlab issue

See merge request gitlab-org/gitlab!28321
parents e68bf142 b5fc6a2a
...@@ -4,12 +4,14 @@ module Gitlab ...@@ -4,12 +4,14 @@ module Gitlab
module JiraImport module JiraImport
class IssueSerializer class IssueSerializer
attr_reader :jira_issue, :project, :params, :formatter attr_reader :jira_issue, :project, :params, :formatter
attr_accessor :metadata
def initialize(project, jira_issue, params = {}) def initialize(project, jira_issue, params = {})
@jira_issue = jira_issue @jira_issue = jira_issue
@project = project @project = project
@params = params @params = params
@formatter = Gitlab::ImportFormatter.new @formatter = Gitlab::ImportFormatter.new
@metadata = []
end end
def execute def execute
...@@ -36,6 +38,7 @@ module Gitlab ...@@ -36,6 +38,7 @@ module Gitlab
body << formatter.author_line(jira_issue.reporter.displayName) body << formatter.author_line(jira_issue.reporter.displayName)
body << formatter.assignee_line(jira_issue.assignee.displayName) if jira_issue.assignee body << formatter.assignee_line(jira_issue.assignee.displayName) if jira_issue.assignee
body << jira_issue.description body << jira_issue.description
body << add_metadata
body.join body.join
end end
...@@ -48,6 +51,50 @@ module Gitlab ...@@ -48,6 +51,50 @@ module Gitlab
Issuable::STATE_ID_MAP[:opened] Issuable::STATE_ID_MAP[:opened]
end end
end end
def add_metadata
add_field(%w(issuetype name), 'Issue type')
add_field(%w(priority name), 'Priority')
add_labels
add_field('environment', 'Environment')
add_field('duedate', 'Due date')
add_parent
add_versions
return if metadata.empty?
metadata.join("\n").prepend("\n\n---\n\n**Issue metadata**\n\n")
end
def add_field(keys, field_label)
value = fields.dig(*keys)
return if value.blank?
metadata << "- #{field_label}: #{value}"
end
def add_labels
return if fields['labels'].blank?
metadata << "- Labels: #{fields['labels'].join(', ')}"
end
def add_parent
parent_issue_key = fields.dig('parent', 'key')
return if parent_issue_key.blank?
metadata << "- Parent issue: [#{parent_issue_key}] #{fields['parent']['fields']['summary']}"
end
def add_versions
return if fields['fixVersions'].blank?
metadata << "- Fix versions: #{fields['fixVersions'].map { |version| version['name'] }.join(', ')}"
end
def fields
jira_issue.fields
end
end end
end end
end end
...@@ -14,6 +14,29 @@ describe Gitlab::JiraImport::IssueSerializer do ...@@ -14,6 +14,29 @@ describe Gitlab::JiraImport::IssueSerializer do
let(:updated_at) { '2020-01-10 20:00:00' } let(:updated_at) { '2020-01-10 20:00:00' }
let(:assignee) { double(displayName: 'Solver') } let(:assignee) { double(displayName: 'Solver') }
let(:jira_status) { 'new' } let(:jira_status) { 'new' }
let(:parent_field) do
{ 'key' => 'FOO-2', 'id' => '1050', 'fields' => { 'summary' => 'parent issue FOO' } }
end
let(:issue_type_field) { { 'name' => 'Task' } }
let(:fix_versions_field) { [{ 'name' => '1.0' }, { 'name' => '1.1' }] }
let(:priority_field) { { 'name' => 'Medium' } }
let(:labels_field) { %w(bug backend) }
let(:environment_field) { 'staging' }
let(:duedate_field) { '2020-03-01' }
let(:fields) do
{
'parent' => parent_field,
'issuetype' => issue_type_field,
'fixVersions' => fix_versions_field,
'priority' => priority_field,
'labels' => labels_field,
'environment' => environment_field,
'duedate' => duedate_field
}
end
let(:jira_issue) do let(:jira_issue) do
double( double(
id: '1234', id: '1234',
...@@ -24,11 +47,15 @@ describe Gitlab::JiraImport::IssueSerializer do ...@@ -24,11 +47,15 @@ describe Gitlab::JiraImport::IssueSerializer do
updated: updated_at, updated: updated_at,
assignee: assignee, assignee: assignee,
reporter: double(displayName: 'Reporter'), reporter: double(displayName: 'Reporter'),
status: double(statusCategory: { 'key' => jira_status }) status: double(statusCategory: { 'key' => jira_status }),
fields: fields
) )
end end
let(:params) { { iid: iid } } let(:params) { { iid: iid } }
subject { described_class.new(project, jira_issue, params).execute }
let(:expected_description) do let(:expected_description) do
<<~MD <<~MD
*Created by: Reporter* *Created by: Reporter*
...@@ -36,11 +63,21 @@ describe Gitlab::JiraImport::IssueSerializer do ...@@ -36,11 +63,21 @@ describe Gitlab::JiraImport::IssueSerializer do
*Assigned to: Solver* *Assigned to: Solver*
basic description basic description
---
**Issue metadata**
- Issue type: Task
- Priority: Medium
- Labels: bug, backend
- Environment: staging
- Due date: 2020-03-01
- Parent issue: [FOO-2] parent issue FOO
- Fix versions: 1.0, 1.1
MD MD
end end
subject { described_class.new(project, jira_issue, params).execute }
context 'attributes setting' do context 'attributes setting' do
it 'sets the basic attributes' do it 'sets the basic attributes' do
expect(subject).to eq( expect(subject).to eq(
...@@ -54,20 +91,45 @@ describe Gitlab::JiraImport::IssueSerializer do ...@@ -54,20 +91,45 @@ describe Gitlab::JiraImport::IssueSerializer do
author_id: project.creator_id author_id: project.creator_id
) )
end end
end
context 'with done status' do context 'when some metadata fields are missing' do
let(:jira_status) { 'done' } let(:assignee) { nil }
let(:parent_field) { nil }
let(:fix_versions_field) { [] }
let(:labels_field) { [] }
let(:environment_field) { nil }
let(:duedate_field) { '2020-03-01' }
it 'maps the status to closed' do it 'skips the missing fields' do
expect(subject[:state_id]).to eq(2) expected_description = <<~MD
*Created by: Reporter*
basic description
---
**Issue metadata**
- Issue type: Task
- Priority: Medium
- Due date: 2020-03-01
MD
expect(subject[:description]).to eq(expected_description.strip)
end end
end end
context 'without the assignee' do context 'when all metadata fields are missing' do
let(:assignee) { nil } let(:assignee) { nil }
let(:parent_field) { nil }
it 'does not include assignee in the description' do let(:issue_type_field) { nil }
let(:fix_versions_field) { [] }
let(:priority_field) { nil }
let(:labels_field) { [] }
let(:environment_field) { nil }
let(:duedate_field) { nil }
it 'skips the whole metadata secction' do
expected_description = <<~MD expected_description = <<~MD
*Created by: Reporter* *Created by: Reporter*
...@@ -77,6 +139,15 @@ describe Gitlab::JiraImport::IssueSerializer do ...@@ -77,6 +139,15 @@ describe Gitlab::JiraImport::IssueSerializer do
expect(subject[:description]).to eq(expected_description.strip) expect(subject[:description]).to eq(expected_description.strip)
end end
end end
end
context 'with done status' do
let(:jira_status) { 'done' }
it 'maps the status to closed' do
expect(subject[:state_id]).to eq(2)
end
end
context 'without the iid' do context 'without the iid' do
let(:params) { {} } let(:params) { {} }
......
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