2018-12-25 04:48:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
2020-10-06 11:08:33 -04:00
|
|
|
class CreateService < Releases::BaseService
|
2018-12-25 04:48:26 -05:00
|
|
|
def execute
|
|
|
|
return error('Access Denied', 403) unless allowed?
|
2022-09-14 11:12:56 -04:00
|
|
|
return error('You are not allowed to create this tag as it is protected.', 403) unless can_create_tag?
|
2018-12-25 04:48:26 -05:00
|
|
|
return error('Release already exists', 409) if release
|
2019-09-16 11:06:26 -04:00
|
|
|
return error("Milestone(s) not found: #{inexistent_milestones.join(', ')}", 400) if inexistent_milestones.any?
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
# should be found before the creation of new tag
|
|
|
|
# because tag creation can spawn new pipeline
|
|
|
|
# which won't have any data for evidence yet
|
2020-11-18 16:09:32 -05:00
|
|
|
evidence_pipeline = Releases::EvidencePipelineFinder.new(project, params).execute
|
2020-06-16 11:08:32 -04:00
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
tag = ensure_tag
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
return tag unless tag.is_a?(Gitlab::Git::Tag)
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
create_release(tag, evidence_pipeline)
|
2018-12-30 22:05:11 -05:00
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
private
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
def ensure_tag
|
|
|
|
existing_tag || create_tag
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
def create_tag
|
|
|
|
return error('Ref is not specified', 422) unless ref
|
|
|
|
|
|
|
|
result = Tags::CreateService
|
|
|
|
.new(project, current_user)
|
2022-05-19 02:08:48 -04:00
|
|
|
.execute(tag_name, ref, tag_message)
|
2018-12-30 22:05:11 -05:00
|
|
|
|
|
|
|
return result unless result[:status] == :success
|
|
|
|
|
|
|
|
result[:tag]
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
|
|
|
|
def allowed?
|
2022-09-14 11:12:56 -04:00
|
|
|
Ability.allowed?(current_user, :create_release, project)
|
2021-07-06 23:08:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_tag?
|
|
|
|
::Gitlab::UserAccess.new(current_user, container: project).can_create_tag?(tag_name)
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
def create_release(tag, evidence_pipeline)
|
2019-04-09 02:52:15 -04:00
|
|
|
release = build_release(tag)
|
|
|
|
|
|
|
|
release.save!
|
|
|
|
|
2020-05-19 14:08:11 -04:00
|
|
|
notify_create_release(release)
|
|
|
|
|
2020-11-06 19:08:58 -05:00
|
|
|
execute_hooks(release, 'create')
|
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
create_evidence!(release, evidence_pipeline)
|
2020-05-21 20:08:07 -04:00
|
|
|
|
2019-04-09 02:52:15 -04:00
|
|
|
success(tag: tag, release: release)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2019-04-09 02:52:15 -04:00
|
|
|
error(e.message, 400)
|
|
|
|
end
|
|
|
|
|
2020-05-19 14:08:11 -04:00
|
|
|
def notify_create_release(release)
|
|
|
|
NotificationService.new.async.send_new_release_notifications(release)
|
|
|
|
end
|
|
|
|
|
2019-04-09 02:52:15 -04:00
|
|
|
def build_release(tag)
|
|
|
|
project.releases.build(
|
2018-12-25 04:48:26 -05:00
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
author: current_user,
|
|
|
|
tag: tag.name,
|
2019-01-01 20:40:33 -05:00
|
|
|
sha: tag.dereferenced_target.sha,
|
2019-07-03 05:12:15 -04:00
|
|
|
released_at: released_at,
|
2019-09-03 05:38:59 -04:00
|
|
|
links_attributes: params.dig(:assets, 'links') || [],
|
2019-09-16 11:06:26 -04:00
|
|
|
milestones: milestones
|
2018-12-25 04:48:26 -05:00
|
|
|
)
|
|
|
|
end
|
2020-05-21 20:08:07 -04:00
|
|
|
|
2020-06-16 11:08:32 -04:00
|
|
|
def create_evidence!(release, pipeline)
|
2020-11-18 16:09:32 -05:00
|
|
|
return if release.historical_release? || release.upcoming_release?
|
2020-05-21 20:08:07 -04:00
|
|
|
|
2020-11-18 16:09:32 -05:00
|
|
|
::Releases::CreateEvidenceWorker.perform_async(release.id, pipeline&.id)
|
2020-05-21 20:08:07 -04:00
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
end
|
|
|
|
end
|