gitlab-org--gitlab-foss/app/services/milestones/find_or_create_service.rb
Eugenia Grieff 7a3234c68f Add service to transfer group milestones
- Add new service that transfers milestones
from a group to a project
- Include new service in Projects transfer service
- Include FromUnion module in Milestone model
to use in transfer service
- Add specs for new milestones service
- Add specs for transferring milestones in
project transfer service
2019-09-04 16:19:31 +00:00

34 lines
871 B
Ruby

# frozen_string_literal: true
module Milestones
class FindOrCreateService
attr_accessor :project, :current_user, :params
def initialize(project, user, params = {})
@project, @current_user, @params = project, user, params.dup
end
def execute
find_milestone || create_milestone
end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_milestone
groups = project.group&.self_and_ancestors_ids
Milestone.for_projects_and_groups([project.id], groups).find_by(title: params["title"])
end
# rubocop: enable CodeReuse/ActiveRecord
def create_milestone
return unless current_user.can?(:admin_milestone, project)
new_milestone if new_milestone.persisted?
end
def new_milestone
@new_milestone ||= CreateService.new(project, current_user, params).execute
end
end
end