2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
2021-01-03 14:25:43 +05:30
|
|
|
class CreateService < Releases::BaseService
|
2019-02-15 15:39:39 +05:30
|
|
|
def execute
|
|
|
|
return error('Access Denied', 403) unless allowed?
|
|
|
|
return error('Release already exists', 409) if release
|
2019-12-04 20:38:33 +05:30
|
|
|
return error("Milestone(s) not found: #{inexistent_milestones.join(', ')}", 400) if inexistent_milestones.any?
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
# 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
|
2021-02-22 17:27:13 +05:30
|
|
|
evidence_pipeline = Releases::EvidencePipelineFinder.new(project, params).execute
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
tag = ensure_tag
|
|
|
|
|
|
|
|
return tag unless tag.is_a?(Gitlab::Git::Tag)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
create_release(tag, evidence_pipeline)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def find_or_build_release
|
|
|
|
release || build_release(existing_tag)
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_tag
|
|
|
|
existing_tag || create_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_tag
|
|
|
|
return error('Ref is not specified', 422) unless ref
|
|
|
|
|
|
|
|
result = Tags::CreateService
|
|
|
|
.new(project, current_user)
|
2022-07-23 23:45:48 +05:30
|
|
|
.execute(tag_name, ref, tag_message)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
return result unless result[:status] == :success
|
|
|
|
|
|
|
|
result[:tag]
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed?
|
2021-09-30 23:02:18 +05:30
|
|
|
Ability.allowed?(current_user, :create_release, project) && can_create_tag?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_tag?
|
|
|
|
::Gitlab::UserAccess.new(current_user, container: project).can_create_tag?(tag_name)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_release(tag, evidence_pipeline)
|
2019-07-31 22:56:46 +05:30
|
|
|
release = build_release(tag)
|
|
|
|
|
|
|
|
release.save!
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
notify_create_release(release)
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
execute_hooks(release, 'create')
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
create_evidence!(release, evidence_pipeline)
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
success(tag: tag, release: release)
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2019-07-31 22:56:46 +05:30
|
|
|
error(e.message, 400)
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def notify_create_release(release)
|
|
|
|
NotificationService.new.async.send_new_release_notifications(release)
|
|
|
|
end
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
def build_release(tag)
|
|
|
|
project.releases.build(
|
2019-02-15 15:39:39 +05:30
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
author: current_user,
|
|
|
|
tag: tag.name,
|
|
|
|
sha: tag.dereferenced_target.sha,
|
2019-09-30 21:07:59 +05:30
|
|
|
released_at: released_at,
|
2019-12-04 20:38:33 +05:30
|
|
|
links_attributes: params.dig(:assets, 'links') || [],
|
|
|
|
milestones: milestones
|
2019-02-15 15:39:39 +05:30
|
|
|
)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def create_evidence!(release, pipeline)
|
2021-02-22 17:27:13 +05:30
|
|
|
return if release.historical_release? || release.upcoming_release?
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
::Releases::CreateEvidenceWorker.perform_async(release.id, pipeline&.id)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|