From 39fa1b598749be0aad699032bbf31450b3ff0098 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 27 Apr 2020 09:09:51 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- lib/gitlab/import_export/project/import_export.yml | 3 ++ lib/gitlab/jira_import/handle_labels_service.rb | 39 ++++++++++++++++++++++ lib/gitlab/jira_import/issue_serializer.rb | 12 ++++++- lib/gitlab/jira_import/labels_importer.rb | 24 ++++++++++++- lib/gitlab/jira_import/metadata_collector.rb | 7 ---- lib/gitlab/request_context.rb | 5 +++ lib/tasks/file_hooks.rake | 5 +++ 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 lib/gitlab/jira_import/handle_labels_service.rb (limited to 'lib') diff --git a/lib/gitlab/import_export/project/import_export.yml b/lib/gitlab/import_export/project/import_export.yml index 3cbd0d144e6..24a48dfb187 100644 --- a/lib/gitlab/import_export/project/import_export.yml +++ b/lib/gitlab/import_export/project/import_export.yml @@ -190,6 +190,7 @@ excluded_attributes: - :merge_request_diff_id issues: - :milestone_id + - :sprint_id - :moved_to_id - :sent_notifications - :state_id @@ -197,6 +198,7 @@ excluded_attributes: - :promoted_to_epic_id merge_request: - :milestone_id + - :sprint_id - :ref_fetched - :merge_jid - :rebase_jid @@ -205,6 +207,7 @@ excluded_attributes: - :state_id merge_requests: - :milestone_id + - :sprint_id - :ref_fetched - :merge_jid - :rebase_jid 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') diff --git a/lib/gitlab/request_context.rb b/lib/gitlab/request_context.rb index 9da6732796a..560f6ac3130 100644 --- a/lib/gitlab/request_context.rb +++ b/lib/gitlab/request_context.rb @@ -24,6 +24,7 @@ module Gitlab end def ensure_deadline_not_exceeded! + return unless enabled? return unless request_deadline return if Gitlab::Metrics::System.real_time < request_deadline @@ -36,5 +37,9 @@ module Gitlab def max_request_duration_seconds Settings.gitlab.max_request_duration_seconds end + + def enabled? + !Rails.env.test? + end end end diff --git a/lib/tasks/file_hooks.rake b/lib/tasks/file_hooks.rake index 66d382db612..f767d63fe0d 100644 --- a/lib/tasks/file_hooks.rake +++ b/lib/tasks/file_hooks.rake @@ -4,6 +4,11 @@ namespace :file_hooks do puts 'Validating file hooks from /file_hooks and /plugins directories' Gitlab::FileHook.files.each do |file| + if File.dirname(file).ends_with?('plugins') + puts 'DEPRECATED: /plugins directory is deprecated and will be removed in 14.0. ' \ + 'Please move your files into /file_hooks directory.' + end + success, message = Gitlab::FileHook.execute(file, Gitlab::DataBuilder::Push::SAMPLE_DATA) if success -- cgit v1.2.3