summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorAlessio Caiazza <acaiazza@gitlab.com>2018-12-21 17:40:14 +0100
committerShinya Maeda <shinya@gitlab.com>2018-12-31 14:34:15 +0900
commit6a2decf5454922441606fce1560389acbbd9eff1 (patch)
tree1434716df04a5a8623943c85bd5b074b53320cee /app/services
parenta7aaad96f3cca5be2886bf3e18c81a7b06e5129f (diff)
downloadgitlab-ce-6a2decf5454922441606fce1560389acbbd9eff1.tar.gz
Refactor Release services
CreateReleaseService and UpdateReleaseService now takes all the release attributes as constructor parameters. This will simplify attribute expansion
Diffstat (limited to 'app/services')
-rw-r--r--app/services/create_release_service.rb54
-rw-r--r--app/services/update_release_service.rb36
2 files changed, 30 insertions, 60 deletions
diff --git a/app/services/create_release_service.rb b/app/services/create_release_service.rb
index 223670a935d..facf2a729ad 100644
--- a/app/services/create_release_service.rb
+++ b/app/services/create_release_service.rb
@@ -1,50 +1,40 @@
# frozen_string_literal: true
class CreateReleaseService < BaseService
- def execute(tag_name, release_description, name: nil, ref: nil)
- repository = project.repository
- tag = repository.find_tag(tag_name)
+ def execute(ref = nil)
+ return error('Unauthorized', 401) unless Ability.allowed?(current_user, :create_release, project)
- if tag.blank? && ref.present?
- result = create_tag(tag_name, ref)
- return result unless result[:status] == :success
+ tag_result = find_or_create_tag(ref)
+ return tag_result if tag_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
-
- def success(release)
- super().merge(release: release)
+ create_release(tag_result[:tag])
end
private
- def create_release(tag, name, description)
- release = project.releases.find_by(tag: tag.name) # rubocop: disable CodeReuse/ActiveRecord
+ def find_or_create_tag(ref)
+ tag = repository.find_tag(params[:tag])
+ return success(tag: tag) if tag
+ return error('Tag does not exist', 404) if ref.blank?
+
+ Tags::CreateService.new(project, current_user).execute(params[:tag], ref, nil)
+ end
+
+ def create_release(tag)
+ release = Release.by_tag(project, tag.name)
if release
error('Release already exists', 409)
else
- release = project.releases.create!(
- tag: tag.name,
- name: name || tag.name,
- sha: tag.dereferenced_target.sha,
+ create_params = {
author: current_user,
- description: description
- )
+ name: tag.name,
+ sha: tag.dereferenced_target.sha
+ }.merge(params)
- success(release)
- end
- end
+ release = project.releases.create!(create_params)
- def create_tag(tag_name, ref)
- Tags::CreateService.new(project, current_user)
- .execute(tag_name, ref, nil)
+ success(tag: tag, release: release)
+ end
end
end
diff --git a/app/services/update_release_service.rb b/app/services/update_release_service.rb
index b51e72c7bfc..f1d5d023100 100644
--- a/app/services/update_release_service.rb
+++ b/app/services/update_release_service.rb
@@ -1,38 +1,18 @@
# 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
- repository = project.repository
- existing_tag = repository.find_tag(@tag_name)
+ return error('Unauthorized', 401) unless Ability.allowed?(current_user, :update_release, project)
- if existing_tag
- release = project.releases.find_by(tag: @tag_name)
+ tag_name = params[:tag]
+ release = Release.by_tag(project, tag_name)
- if 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
+ return error('Release does not exist', 404) if release.blank?
+
+ if release.update(params)
+ success(release: release)
else
- error('Tag does not exist', 404)
+ error(release.errors.messages || '400 Bad request', 400)
end
end
- # rubocop: enable CodeReuse/ActiveRecord
-
- def success(release)
- super().merge(release: release)
- end
end