2018-12-25 04:48:26 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Releases
|
|
|
|
class CreateService < BaseService
|
|
|
|
include Releases::Concerns
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return error('Access Denied', 403) unless allowed?
|
|
|
|
return error('Release already exists', 409) if release
|
|
|
|
|
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
|
|
|
|
2018-12-30 22:05:11 -05:00
|
|
|
create_release(tag)
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
|
2019-04-09 02:52:15 -04:00
|
|
|
def find_or_build_release
|
|
|
|
release || build_release(existing_tag)
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
.execute(tag_name, ref, nil)
|
|
|
|
|
|
|
|
return result unless result[:status] == :success
|
|
|
|
|
|
|
|
result[:tag]
|
|
|
|
end
|
2018-12-25 04:48:26 -05:00
|
|
|
|
|
|
|
def allowed?
|
|
|
|
Ability.allowed?(current_user, :create_release, project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_release(tag)
|
2019-04-09 02:52:15 -04:00
|
|
|
release = build_release(tag)
|
|
|
|
|
|
|
|
release.save!
|
|
|
|
|
|
|
|
success(tag: tag, release: release)
|
|
|
|
rescue => e
|
|
|
|
error(e.message, 400)
|
|
|
|
end
|
|
|
|
|
|
|
|
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-01-02 05:56:03 -05:00
|
|
|
links_attributes: params.dig(:assets, 'links') || []
|
2018-12-25 04:48:26 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|