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