2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-11-21 12:08:45 -05:00
|
|
|
class CreateReleaseService < BaseService
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2015-11-21 12:08:45 -05:00
|
|
|
def execute(tag_name, release_description)
|
|
|
|
repository = project.repository
|
|
|
|
existing_tag = repository.find_tag(tag_name)
|
|
|
|
|
|
|
|
# Only create a release if the tag exists
|
|
|
|
if existing_tag
|
2015-11-21 16:24:34 -05:00
|
|
|
release = project.releases.find_by(tag: tag_name)
|
2015-11-21 12:08:45 -05:00
|
|
|
|
2015-11-21 16:34:53 -05:00
|
|
|
if release
|
2015-11-21 16:24:34 -05:00
|
|
|
error('Release already exists', 409)
|
|
|
|
else
|
2018-12-12 04:56:43 -05:00
|
|
|
release = project.releases.create!(
|
|
|
|
tag: tag_name,
|
|
|
|
name: tag_name,
|
|
|
|
sha: existing_tag.dereferenced_target.sha,
|
|
|
|
author: current_user,
|
|
|
|
description: release_description
|
|
|
|
)
|
2015-11-21 16:24:34 -05:00
|
|
|
|
|
|
|
success(release)
|
|
|
|
end
|
2015-11-21 12:08:45 -05:00
|
|
|
else
|
2015-11-21 16:24:34 -05:00
|
|
|
error('Tag does not exist', 404)
|
2015-11-21 12:08:45 -05:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2015-11-21 12:08:45 -05:00
|
|
|
|
|
|
|
def success(release)
|
2016-07-06 09:26:59 -04:00
|
|
|
super().merge(release: release)
|
2015-11-21 12:08:45 -05:00
|
|
|
end
|
|
|
|
end
|