Class: IssuableLinks::CreateService

Inherits:
BaseService show all
Defined in:
app/services/issuable_links/create_service.rb

Instance Attribute Summary collapse

Attributes inherited from BaseService

#project

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(issuable, user, params) ⇒ CreateService

Returns a new instance of CreateService.



7
8
9
10
11
12
# File 'app/services/issuable_links/create_service.rb', line 7

def initialize(issuable, user, params)
  @issuable = issuable
  @current_user = user
  @params = params.dup
  @errors = []
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



5
6
7
# File 'app/services/issuable_links/create_service.rb', line 5

def current_user
  @current_user
end

#issuableObject (readonly)

Returns the value of attribute issuable.



5
6
7
# File 'app/services/issuable_links/create_service.rb', line 5

def issuable
  @issuable
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'app/services/issuable_links/create_service.rb', line 5

def params
  @params
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/issuable_links/create_service.rb', line 14

def execute
  # If ALL referenced issues are already assigned to the given epic it renders a conflict status,
  # otherwise create issue links for the issues which
  # are still not assigned and return success message.
  if render_conflict_error?
    return error(issuables_already_assigned_message, 409)
  end

  if render_no_permission_error?
    return error(issuables_no_permission_error_message, 403)
  end

  if render_not_found_error?
    return error(issuables_not_found_message, 404)
  end

  references = create_links

  if @errors.present?
    return error(@errors.join('. '), 422)
  end

  track_event

  success(created_references: references)
end

#relate_issuables(referenced_issuable) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/issuable_links/create_service.rb', line 42

def relate_issuables(referenced_issuable)
  link = link_class.find_or_initialize_by(source: issuable, target: referenced_issuable)

  set_link_type(link)

  if link.changed? && link.save
    create_notes(link)
  end

  link
end