debian-mirror-gitlab/app/services/milestones/find_or_create_service.rb

35 lines
871 B
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# 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