Class: Tags::CreateService

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

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

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

This class inherits a constructor from BaseService

Instance Method Details

#execute(tag_name, target, message) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/tags/create_service.rb', line 5

def execute(tag_name, target, message)
  valid_tag = Gitlab::GitRefValidator.validate(tag_name)
  return error('Tag name invalid', 400) unless valid_tag

  repository = project.repository
  message = message&.strip

  new_tag = nil

  begin
    new_tag = repository.add_tag(current_user, tag_name, target, message)
  rescue Gitlab::Git::Repository::TagExistsError
    return error("Tag #{tag_name} already exists", 409)
  rescue Gitlab::Git::PreReceiveError => ex
    return error(ex.message)
  end

  if new_tag
    repository.expire_tags_cache

    success.merge(tag: new_tag)
  else
    error("Target #{target} is invalid", 400)
  end
end