Commit 12dd22b3 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch...

Merge branch '235391-support-group-milestones-to-be-associated-with-project-releases-in-api-4' into 'master'

Refactor `Releases::Concerns` into `Releases::BaseService`

See merge request gitlab-org/gitlab!44413
parents b66c8721 9ef61ecb
# frozen_string_literal: true
module Releases
class BaseService
include BaseServiceUtility
include Gitlab::Utils::StrongMemoize
attr_accessor :project, :current_user, :params
def initialize(project, user = nil, params = {})
@project, @current_user, @params = project, user, params.dup
end
delegate :repository, to: :project
def tag_name
params[:tag]
end
def ref
params[:ref]
end
def name
params[:name] || tag_name
end
def description
params[:description]
end
def released_at
params[:released_at]
end
def release
strong_memoize(:release) do
project.releases.find_by_tag(tag_name)
end
end
def existing_tag
strong_memoize(:existing_tag) do
repository.find_tag(tag_name)
end
end
def tag_exist?
existing_tag.present?
end
def repository
strong_memoize(:repository) do
project.repository
end
end
def milestones
return [] unless param_for_milestone_titles_provided?
strong_memoize(:milestones) do
MilestonesFinder.new(
project: project,
current_user: current_user,
project_ids: Array(project.id),
state: 'all',
title: params[:milestones]
).execute
end
end
def inexistent_milestones
return [] unless param_for_milestone_titles_provided?
existing_milestone_titles = milestones.map(&:title)
Array(params[:milestones]) - existing_milestone_titles
end
def param_for_milestone_titles_provided?
params.key?(:milestones)
end
end
end
# frozen_string_literal: true
module Releases
module Concerns
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
included do
def tag_name
params[:tag]
end
def ref
params[:ref]
end
def name
params[:name] || tag_name
end
def description
params[:description]
end
def released_at
params[:released_at]
end
def release
strong_memoize(:release) do
project.releases.find_by_tag(tag_name)
end
end
def existing_tag
strong_memoize(:existing_tag) do
repository.find_tag(tag_name)
end
end
def tag_exist?
existing_tag.present?
end
def repository
strong_memoize(:repository) do
project.repository
end
end
def milestones
return [] unless param_for_milestone_titles_provided?
strong_memoize(:milestones) do
MilestonesFinder.new(
project: project,
current_user: current_user,
project_ids: Array(project.id),
state: 'all',
title: params[:milestones]
).execute
end
end
def inexistent_milestones
return [] unless param_for_milestone_titles_provided?
existing_milestone_titles = milestones.map(&:title)
Array(params[:milestones]) - existing_milestone_titles
end
def param_for_milestone_titles_provided?
params.key?(:milestones)
end
end
end
end
# frozen_string_literal: true # frozen_string_literal: true
module Releases module Releases
class CreateService < BaseService class CreateService < Releases::BaseService
include Releases::Concerns
def execute def execute
return error('Access Denied', 403) unless allowed? return error('Access Denied', 403) unless allowed?
return error('Release already exists', 409) if release return error('Release already exists', 409) if release
......
# frozen_string_literal: true # frozen_string_literal: true
module Releases module Releases
class DestroyService < BaseService class DestroyService < Releases::BaseService
include Releases::Concerns
def execute def execute
return error('Release does not exist', 404) unless release return error('Release does not exist', 404) unless release
return error('Access Denied', 403) unless allowed? return error('Access Denied', 403) unless allowed?
......
# frozen_string_literal: true # frozen_string_literal: true
module Releases module Releases
class UpdateService < BaseService class UpdateService < Releases::BaseService
include Releases::Concerns
def execute def execute
return error('Tag does not exist', 404) unless existing_tag return error('Tag does not exist', 404) unless existing_tag
return error('Release does not exist', 404) unless release return error('Release does not exist', 404) unless release
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment