create_release_service.rb 887 Bytes
Newer Older
1 2
# frozen_string_literal: true

3
class CreateReleaseService < BaseService
4
  # rubocop: disable CodeReuse/ActiveRecord
5 6 7 8 9 10
  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
11
      release = project.releases.find_by(tag: tag_name)
12

13
      if release
14 15
        error('Release already exists', 409)
      else
16 17 18 19 20 21 22
        release = project.releases.create!(
          tag: tag_name,
          name: tag_name,
          sha: existing_tag.dereferenced_target.sha,
          author: current_user,
          description: release_description
        )
23 24 25

        success(release)
      end
26
    else
27
      error('Tag does not exist', 404)
28 29
    end
  end
30
  # rubocop: enable CodeReuse/ActiveRecord
31 32

  def success(release)
33
    super().merge(release: release)
34 35
  end
end