diff options
author | Alessio Caiazza <acaiazza@gitlab.com> | 2018-12-13 14:08:53 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-12-31 06:05:56 +0300 |
commit | 1ea2d9faa518f6b769b05a71a1f93b4ea0226bc1 (patch) | |
tree | 291cd8d7a6c6113e75219dc873e24fb414df119d /app/services | |
parent | c07bf1abf2a5c3751e575ee5926e9a052fa0b341 (diff) |
Add releases API
This commit introduces Releases API under /api/v4/projects/:id/releases
* We are introducing release policies at project level.
* We are deprecating releases changes from tags, both api and web
interface.
* Tags::CreateService no longer create a release
This feature is controlled by :releases_page feature flag
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/commits/tag_service.rb | 3 | ||||
-rw-r--r-- | app/services/create_release_service.rb | 59 | ||||
-rw-r--r-- | app/services/tags/create_service.rb | 7 | ||||
-rw-r--r-- | app/services/update_release_service.rb | 22 |
4 files changed, 56 insertions, 35 deletions
diff --git a/app/services/commits/tag_service.rb b/app/services/commits/tag_service.rb index 7961ba4d3c4..bb8cfb63f98 100644 --- a/app/services/commits/tag_service.rb +++ b/app/services/commits/tag_service.rb @@ -9,11 +9,10 @@ module Commits tag_name = params[:tag_name] message = params[:tag_message] - release_description = nil result = Tags::CreateService .new(commit.project, current_user) - .execute(tag_name, commit.sha, message, release_description) + .execute(tag_name, commit.sha, message) if result[:status] == :success tag = result[:tag] diff --git a/app/services/create_release_service.rb b/app/services/create_release_service.rb index ab2dc5337aa..223670a935d 100644 --- a/app/services/create_release_service.rb +++ b/app/services/create_release_service.rb @@ -1,35 +1,50 @@ # frozen_string_literal: true class CreateReleaseService < BaseService - # rubocop: disable CodeReuse/ActiveRecord - def execute(tag_name, release_description) + def execute(tag_name, release_description, name: nil, ref: nil) repository = project.repository - existing_tag = repository.find_tag(tag_name) - - # Only create a release if the tag exists - if existing_tag - release = project.releases.find_by(tag: tag_name) - - if release - error('Release already exists', 409) - else - release = project.releases.create!( - tag: tag_name, - name: tag_name, - sha: existing_tag.dereferenced_target.sha, - author: current_user, - description: release_description - ) - - success(release) - end + tag = repository.find_tag(tag_name) + + if tag.blank? && ref.present? + result = create_tag(tag_name, ref) + return result unless result[:status] == :success + + tag = result[:tag] + end + + if tag.present? + create_release(tag, name, release_description) else error('Tag does not exist', 404) end end - # rubocop: enable CodeReuse/ActiveRecord def success(release) super().merge(release: release) end + + private + + def create_release(tag, name, description) + release = project.releases.find_by(tag: tag.name) # rubocop: disable CodeReuse/ActiveRecord + + if release + error('Release already exists', 409) + else + release = project.releases.create!( + tag: tag.name, + name: name || tag.name, + sha: tag.dereferenced_target.sha, + author: current_user, + description: description + ) + + success(release) + end + end + + def create_tag(tag_name, ref) + Tags::CreateService.new(project, current_user) + .execute(tag_name, ref, nil) + end end diff --git a/app/services/tags/create_service.rb b/app/services/tags/create_service.rb index 6bb9bb3988e..4de6b2d2774 100644 --- a/app/services/tags/create_service.rb +++ b/app/services/tags/create_service.rb @@ -2,7 +2,7 @@ module Tags class CreateService < BaseService - def execute(tag_name, target, message, release_description = nil) + def execute(tag_name, target, message) valid_tag = Gitlab::GitRefValidator.validate(tag_name) return error('Tag name invalid') unless valid_tag @@ -20,10 +20,7 @@ module Tags end if new_tag - if release_description.present? - CreateReleaseService.new(@project, @current_user) - .execute(tag_name, release_description) - end + repository.expire_tags_cache success.merge(tag: new_tag) else diff --git a/app/services/update_release_service.rb b/app/services/update_release_service.rb index e2228ca026c..b51e72c7bfc 100644 --- a/app/services/update_release_service.rb +++ b/app/services/update_release_service.rb @@ -1,18 +1,28 @@ # frozen_string_literal: true class UpdateReleaseService < BaseService + attr_accessor :tag_name + + def initialize(project, user, tag_name, params) + super(project, user, params) + + @tag_name = tag_name + end + # rubocop: disable CodeReuse/ActiveRecord - def execute(tag_name, release_description) + def execute repository = project.repository - existing_tag = repository.find_tag(tag_name) + existing_tag = repository.find_tag(@tag_name) if existing_tag - release = project.releases.find_by(tag: tag_name) + release = project.releases.find_by(tag: @tag_name) if release - release.update(description: release_description) - - success(release) + if release.update(params) + success(release) + else + error(release.errors.messages || '400 Bad request', 400) + end else error('Release does not exist', 404) end |