Class: Gb::DeleteTag

Inherits:
SubCommand show all
Defined in:
lib/commands/delete_tag.rb

Instance Attribute Summary

Attributes inherited from SubCommand

#gb_config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SubCommand

#check_uncommit, #run, #save_workspace_config, #workspace_config

Methods inherited from Command

#error, handle_exception, #info, report_error, run

Constructor Details

#initialize(argv) ⇒ DeleteTag

Returns a new instance of DeleteTag.



24
25
26
27
28
# File 'lib/commands/delete_tag.rb', line 24

def initialize(argv)
  @tag_name = argv.shift_argument
  @force = argv.flag?('force')
  super
end

Class Method Details

.optionsObject



18
19
20
21
22
# File 'lib/commands/delete_tag.rb', line 18

def self.options
  [
      ["--force", "忽略tag是否存在,强制执行"],
  ].concat(super)
end

Instance Method Details

#gitlab_search_project(project_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/commands/delete_tag.rb', line 79

def gitlab_search_project(project_name)
  projects = Gitlab.project_search(project_name)
  if projects.size > 1
    info "find #{projects.size} project named #{project_name}. you means which one?"
    projects.each do |project|
      print project.name + '  '
    end
    print "\n"
    raise Error.new("find #{projects.size} project named #{project_name}")

  elsif projects.size == 1
    project = projects[0];
  else
    raise Error.new("can't find project named '#{project_name}'.")
  end
  project
end

#run_in_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/commands/delete_tag.rb', line 37

def run_in_config
  # api: https://www.rubydoc.info/gems/gitlab/toplevel
  # document: https://narkoz.github.io/gitlab/cli

  Gitlab.configure do |config|
    # set an API endpoint
    # API endpoint URL, default: ENV['GITLAB_API_ENDPOINT']
    config.endpoint = self.gb_config.gitlab.endpoint

    # set a user private token
    # user's private token or OAuth2 access token, default: ENV['GITLAB_API_PRIVATE_TOKEN']
    config.private_token = self.gb_config.gitlab.private_token

    # user agent
    config.user_agent = "gb ruby gem[#{VERSION}"
  end

  info "ready delete tag '#{@tag_name}'.\n"

  self.gb_config.projects.each do |project|
    gitlab_project = gitlab_search_project(project.name)
    info "find project #{gitlab_project.name} on #{gitlab_project.web_url}."

    begin
      tag = Gitlab.delete_tag(gitlab_project.id, @tag_name)
    rescue Gitlab::Error::NotFound => error
      tag = nil
      if @force
        raise(error)
      else
        info "tag '#{@tag_name}' not found, skip."
      end
    rescue Gitlab::Error::Error => error
      raise(error)
    end
    if tag
      info "delete tag '#{@tag_name}' success.\n"
    end
    puts
  end
end

#validate!Object



30
31
32
33
34
35
# File 'lib/commands/delete_tag.rb', line 30

def validate!
  super
  if @tag_name.nil?
    help! 'tag_name is required.'
  end
end