diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-11 18:10:08 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-11 18:10:08 +0300 |
commit | 9dde2726710184f066387d044fce4ae2b3684210 (patch) | |
tree | 141da0dfc25da6b1724329a3d5cf2d51c7d45937 /app/services | |
parent | 03b5d94c2c145491bd493837ec50a36e5d1d2612 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/award_emojis/copy_service.rb | 30 | ||||
-rw-r--r-- | app/services/incident_management/create_issue_service.rb | 21 | ||||
-rw-r--r-- | app/services/issuable/clone/base_service.rb | 30 | ||||
-rw-r--r-- | app/services/issuable/clone/content_rewriter.rb | 74 | ||||
-rw-r--r-- | app/services/markdown_content_rewriter_service.rb | 28 | ||||
-rw-r--r-- | app/services/notes/copy_service.rb | 70 | ||||
-rw-r--r-- | app/services/packages/maven/find_or_create_package_service.rb | 39 |
7 files changed, 192 insertions, 100 deletions
diff --git a/app/services/award_emojis/copy_service.rb b/app/services/award_emojis/copy_service.rb new file mode 100644 index 00000000000..2e500d4c697 --- /dev/null +++ b/app/services/award_emojis/copy_service.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# This service copies AwardEmoji from one Awardable to another. +# +# It expects the calling code to have performed the necessary authorization +# checks in order to allow the copy to happen. +module AwardEmojis + class CopyService + def initialize(from_awardable, to_awardable) + raise ArgumentError, 'Awardables must be different' if from_awardable == to_awardable + + @from_awardable = from_awardable + @to_awardable = to_awardable + end + + def execute + from_awardable.award_emoji.find_each do |award| + new_award = award.dup + new_award.awardable = to_awardable + new_award.save! + end + + ServiceResponse.success + end + + private + + attr_accessor :from_awardable, :to_awardable + end +end diff --git a/app/services/incident_management/create_issue_service.rb b/app/services/incident_management/create_issue_service.rb index 34c70b002b6..d1c5c6752d4 100644 --- a/app/services/incident_management/create_issue_service.rb +++ b/app/services/incident_management/create_issue_service.rb @@ -5,13 +5,16 @@ module IncidentManagement include Gitlab::Utils::StrongMemoize include IncidentManagement::Settings - def initialize(project, params) - super(project, User.alert_bot, params) + attr_reader :alert + + def initialize(project, alert) + super(project, User.alert_bot) + @alert = alert end def execute return error('setting disabled') unless incident_management_setting.create_issue? - return error('invalid alert') unless alert.valid? + return error('invalid alert') unless alert_presenter.valid? result = create_incident return error(result.message, result.payload[:issue]) unless result.success? @@ -31,7 +34,7 @@ module IncidentManagement end def issue_title - alert.full_title + alert_presenter.full_title end def issue_description @@ -45,16 +48,16 @@ module IncidentManagement end def alert_summary - alert.issue_summary_markdown + alert_presenter.issue_summary_markdown end def alert_markdown - alert.alert_markdown + alert_presenter.alert_markdown end - def alert - strong_memoize(:alert) do - Gitlab::Alerting::Alert.new(project: project, payload: params).present + def alert_presenter + strong_memoize(:alert_presenter) do + Gitlab::Alerting::Alert.for_alert_management_alert(project: project, alert: alert).present end end diff --git a/app/services/issuable/clone/base_service.rb b/app/services/issuable/clone/base_service.rb index 0d1640924e5..639423ed4bf 100644 --- a/app/services/issuable/clone/base_service.rb +++ b/app/services/issuable/clone/base_service.rb @@ -24,12 +24,34 @@ module Issuable private + def copy_award_emoji + AwardEmojis::CopyService.new(original_entity, new_entity).execute + end + + def copy_notes + Notes::CopyService.new(current_user, original_entity, new_entity).execute + end + def update_new_entity - rewriters = [ContentRewriter, AttributesRewriter] + update_new_entity_description + update_new_entity_attributes + copy_award_emoji + copy_notes + end - rewriters.each do |rewriter| - rewriter.new(current_user, original_entity, new_entity).execute - end + def update_new_entity_description + rewritten_description = MarkdownContentRewriterService.new( + current_user, + original_entity.description, + original_entity.project, + new_entity.project + ).execute + + new_entity.update!(description: rewritten_description) + end + + def update_new_entity_attributes + AttributesRewriter.new(current_user, original_entity, new_entity).execute end def update_old_entity diff --git a/app/services/issuable/clone/content_rewriter.rb b/app/services/issuable/clone/content_rewriter.rb deleted file mode 100644 index 67d2f9fd3fe..00000000000 --- a/app/services/issuable/clone/content_rewriter.rb +++ /dev/null @@ -1,74 +0,0 @@ -# frozen_string_literal: true - -module Issuable - module Clone - class ContentRewriter < ::Issuable::Clone::BaseService - def initialize(current_user, original_entity, new_entity) - @current_user = current_user - @original_entity = original_entity - @new_entity = new_entity - @project = original_entity.project - end - - def execute - rewrite_description - rewrite_award_emoji(original_entity, new_entity) - rewrite_notes - end - - private - - def rewrite_description - new_entity.update(description: rewrite_content(original_entity.description)) - end - - def rewrite_notes - new_discussion_ids = {} - original_entity.notes_with_associations.find_each do |note| - new_note = note.dup - new_discussion_ids[note.discussion_id] ||= Discussion.discussion_id(new_note) - new_params = { - project: new_entity.project, - noteable: new_entity, - discussion_id: new_discussion_ids[note.discussion_id], - note: rewrite_content(new_note.note), - note_html: nil, - created_at: note.created_at, - updated_at: note.updated_at - } - - if note.system_note_metadata - new_params[:system_note_metadata] = note.system_note_metadata.dup - - # TODO: Implement copying of description versions when an issue is moved - # https://gitlab.com/gitlab-org/gitlab/issues/32300 - new_params[:system_note_metadata].description_version = nil - end - - new_note.update(new_params) - - rewrite_award_emoji(note, new_note) - end - end - - def rewrite_content(content) - return unless content - - rewriters = [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter] - - rewriters.inject(content) do |text, klass| - rewriter = klass.new(text, old_project, current_user) - rewriter.rewrite(new_parent) - end - end - - def rewrite_award_emoji(old_awardable, new_awardable) - old_awardable.award_emoji.each do |award| - new_award = award.dup - new_award.awardable = new_awardable - new_award.save - end - end - end - end -end diff --git a/app/services/markdown_content_rewriter_service.rb b/app/services/markdown_content_rewriter_service.rb new file mode 100644 index 00000000000..f945990a1b4 --- /dev/null +++ b/app/services/markdown_content_rewriter_service.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# This service passes Markdown content through our GFM rewriter classes +# which rewrite references to GitLab objects and uploads within the content +# based on their visibility by the `target_parent`. +class MarkdownContentRewriterService + REWRITERS = [Gitlab::Gfm::ReferenceRewriter, Gitlab::Gfm::UploadsRewriter].freeze + + def initialize(current_user, content, source_parent, target_parent) + @current_user = current_user + @content = content.presence + @source_parent = source_parent + @target_parent = target_parent + end + + def execute + return unless content + + REWRITERS.inject(content) do |text, klass| + rewriter = klass.new(text, source_parent, current_user) + rewriter.rewrite(target_parent) + end + end + + private + + attr_reader :current_user, :content, :source_parent, :target_parent +end diff --git a/app/services/notes/copy_service.rb b/app/services/notes/copy_service.rb new file mode 100644 index 00000000000..31aaa219922 --- /dev/null +++ b/app/services/notes/copy_service.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +# This service copies Notes from one Noteable to another. +# +# It expects the calling code to have performed the necessary authorization +# checks in order to allow the copy to happen. +module Notes + class CopyService + def initialize(current_user, from_noteable, to_noteable) + raise ArgumentError, 'Noteables must be different' if from_noteable == to_noteable + + @current_user = current_user + @from_noteable = from_noteable + @to_noteable = to_noteable + @from_project = from_noteable.project + @to_project = to_noteable.project + @new_discussion_ids = {} + end + + def execute + from_noteable.notes_with_associations.find_each do |note| + copy_note(note) + end + + ServiceResponse.success + end + + private + + attr_reader :from_noteable, :to_noteable, :from_project, :to_project, + :current_user, :new_discussion_ids + + def copy_note(note) + new_note = note.dup + new_params = params_from_note(note, new_note) + new_note.update!(new_params) + + copy_award_emoji(note, new_note) + end + + def params_from_note(note, new_note) + new_discussion_ids[note.discussion_id] ||= Discussion.discussion_id(new_note) + rewritten_note = MarkdownContentRewriterService.new(current_user, note.note, from_project, to_project).execute + + new_params = { + project: to_noteable.project, + noteable: to_noteable, + discussion_id: new_discussion_ids[note.discussion_id], + note: rewritten_note, + note_html: nil, + created_at: note.created_at, + updated_at: note.updated_at + } + + if note.system_note_metadata + new_params[:system_note_metadata] = note.system_note_metadata.dup + + # TODO: Implement copying of description versions when an issue is moved + # https://gitlab.com/gitlab-org/gitlab/issues/32300 + new_params[:system_note_metadata].description_version = nil + end + + new_params + end + + def copy_award_emoji(from_note, to_note) + AwardEmojis::CopyService.new(from_note, to_note).execute + end + end +end diff --git a/app/services/packages/maven/find_or_create_package_service.rb b/app/services/packages/maven/find_or_create_package_service.rb index 50a008843ad..505f45a7b21 100644 --- a/app/services/packages/maven/find_or_create_package_service.rb +++ b/app/services/packages/maven/find_or_create_package_service.rb @@ -3,21 +3,33 @@ module Packages module Maven class FindOrCreatePackageService < BaseService MAVEN_METADATA_FILE = 'maven-metadata.xml'.freeze + SNAPSHOT_TERM = '-SNAPSHOT'.freeze def execute - package = ::Packages::Maven::PackageFinder - .new(params[:path], current_user, project: project).execute + package = + ::Packages::Maven::PackageFinder.new(params[:path], current_user, project: project) + .execute unless package - if params[:file_name] == MAVEN_METADATA_FILE - # Maven uploads several files during `mvn deploy` in next order: - # - my-company/my-app/1.0-SNAPSHOT/my-app.jar - # - my-company/my-app/1.0-SNAPSHOT/my-app.pom - # - my-company/my-app/1.0-SNAPSHOT/maven-metadata.xml - # - my-company/my-app/maven-metadata.xml - # - # The last xml file does not have VERSION in URL because it contains - # information about all versions. + # Maven uploads several files during `mvn deploy` in next order: + # - my-company/my-app/1.0-SNAPSHOT/my-app.jar + # - my-company/my-app/1.0-SNAPSHOT/my-app.pom + # - my-company/my-app/1.0-SNAPSHOT/maven-metadata.xml + # - my-company/my-app/maven-metadata.xml + # + # The last xml file does not have VERSION in URL because it contains + # information about all versions. When uploading such file, we create + # a package with a version set to `nil`. The xml file with a version + # is only created and uploaded for snapshot versions. + # + # Gradle has a different upload order: + # - my-company/my-app/1.0-SNAPSHOT/maven-metadata.xml + # - my-company/my-app/1.0-SNAPSHOT/my-app.jar + # - my-company/my-app/1.0-SNAPSHOT/my-app.pom + # - my-company/my-app/maven-metadata.xml + # + # The first upload has to create the proper package (the one with the version set). + if params[:file_name] == MAVEN_METADATA_FILE && !params[:path]&.ends_with?(SNAPSHOT_TERM) package_name, version = params[:path], nil else package_name, _, version = params[:path].rpartition('/') @@ -30,8 +42,9 @@ module Packages build: params[:build] } - package = ::Packages::Maven::CreatePackageService - .new(project, current_user, package_params).execute + package = + ::Packages::Maven::CreatePackageService.new(project, current_user, package_params) + .execute end package |