diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-27 12:09:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-27 12:09:51 +0300 |
commit | 39fa1b598749be0aad699032bbf31450b3ff0098 (patch) | |
tree | 15a4c28989d58f9315e58458a3a494ff8cfc1525 /lib/gitlab/jira_import | |
parent | a59d305223365cb31bb670f134383d6ff316a13e (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/gitlab/jira_import')
-rw-r--r-- | lib/gitlab/jira_import/handle_labels_service.rb | 39 | ||||
-rw-r--r-- | lib/gitlab/jira_import/issue_serializer.rb | 12 | ||||
-rw-r--r-- | lib/gitlab/jira_import/labels_importer.rb | 24 | ||||
-rw-r--r-- | lib/gitlab/jira_import/metadata_collector.rb | 7 |
4 files changed, 73 insertions, 9 deletions
diff --git a/lib/gitlab/jira_import/handle_labels_service.rb b/lib/gitlab/jira_import/handle_labels_service.rb new file mode 100644 index 00000000000..1b00515cced --- /dev/null +++ b/lib/gitlab/jira_import/handle_labels_service.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Gitlab + module JiraImport + class HandleLabelsService + def initialize(project, jira_labels) + @project = project + @jira_labels = jira_labels + end + + def execute + return if jira_labels.blank? + + existing_labels = LabelsFinder.new(nil, project: project, title: jira_labels) + .execute(skip_authorization: true).select(:id, :name) + new_labels = create_missing_labels(existing_labels) + + label_ids = existing_labels.map(&:id) + label_ids += new_labels if new_labels.present? + label_ids + end + + private + + attr_reader :project, :jira_labels + + def create_missing_labels(existing_labels) + labels_to_create = jira_labels - existing_labels.map(&:name) + return if labels_to_create.empty? + + new_labels_hash = labels_to_create.map do |title| + { project_id: project.id, title: title, type: 'ProjectLabel' } + end + + Label.insert_all(new_labels_hash).rows.flatten + end + end + end +end diff --git a/lib/gitlab/jira_import/issue_serializer.rb b/lib/gitlab/jira_import/issue_serializer.rb index 75056d87d99..f05b36e54da 100644 --- a/lib/gitlab/jira_import/issue_serializer.rb +++ b/lib/gitlab/jira_import/issue_serializer.rb @@ -21,7 +21,8 @@ module Gitlab state_id: map_status(jira_issue.status.statusCategory), updated_at: jira_issue.updated, created_at: jira_issue.created, - author_id: project.creator_id # TODO: map actual author: https://gitlab.com/gitlab-org/gitlab/-/issues/210580 + author_id: project.creator_id, # TODO: map actual author: https://gitlab.com/gitlab-org/gitlab/-/issues/210580 + label_ids: label_ids } end @@ -49,6 +50,15 @@ module Gitlab Issuable::STATE_ID_MAP[:opened] end end + + # We already create labels in Gitlab::JiraImport::LabelsImporter stage but + # there is a possibility it may fail or + # new labels were created on the Jira in the meantime + def label_ids + return if jira_issue.fields['labels'].blank? + + Gitlab::JiraImport::HandleLabelsService.new(project, jira_issue.fields['labels']).execute + end end end end diff --git a/lib/gitlab/jira_import/labels_importer.rb b/lib/gitlab/jira_import/labels_importer.rb index 555a08d2c40..f1f5708865a 100644 --- a/lib/gitlab/jira_import/labels_importer.rb +++ b/lib/gitlab/jira_import/labels_importer.rb @@ -5,6 +5,8 @@ module Gitlab class LabelsImporter < BaseImporter attr_reader :job_waiter + MAX_LABELS = 500 + def initialize(project) super @job_waiter = JobWaiter.new @@ -25,9 +27,29 @@ module Gitlab end def import_jira_labels - # todo: import jira labels, see https://gitlab.com/gitlab-org/gitlab/-/issues/212651 + start_at = 0 + loop do + break if process_jira_page(start_at) + + start_at += MAX_LABELS + end + job_waiter end + + def process_jira_page(start_at) + request = "/rest/api/2/label?maxResults=#{MAX_LABELS}&startAt=#{start_at}" + response = JSON.parse(client.get(request)) + + return true if response['values'].blank? + return true unless response.key?('isLast') + + Gitlab::JiraImport::HandleLabelsService.new(project, response['values']).execute + + response['isLast'] + rescue => e + Gitlab::ErrorTracking.track_exception(e, project_id: project.id, request: request) + end end end end diff --git a/lib/gitlab/jira_import/metadata_collector.rb b/lib/gitlab/jira_import/metadata_collector.rb index 5e873ab3ef4..4551f38ba98 100644 --- a/lib/gitlab/jira_import/metadata_collector.rb +++ b/lib/gitlab/jira_import/metadata_collector.rb @@ -13,7 +13,6 @@ module Gitlab def execute 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 @@ -33,12 +32,6 @@ module Gitlab metadata << "- #{field_label}: #{value}" end - def add_labels - return if fields['labels'].blank? || !fields['labels'].is_a?(Array) - - metadata << "- Labels: #{fields['labels'].join(', ')}" - end - def add_parent parent_issue_key = fields.dig('parent', 'key') |