Class: Gb::CreateTag

Inherits:
SubCommand show all
Defined in:
lib/commands/create_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) ⇒ CreateTag

Returns a new instance of CreateTag.



25
26
27
28
29
30
# File 'lib/commands/create_tag.rb', line 25

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

Class Method Details

.optionsObject



19
20
21
22
23
# File 'lib/commands/create_tag.rb', line 19

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

Instance Method Details

#gitlab_search_project(project_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/commands/create_tag.rb', line 85

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



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
78
79
80
81
82
83
# File 'lib/commands/create_tag.rb', line 42

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

  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.tag(gitlab_project.id, @tag_name)
    rescue Gitlab::Error::NotFound => error
      tag = nil
    rescue Gitlab::Error::Error => error
      raise(error)
    end

    if tag.nil?
      Gitlab.create_tag(gitlab_project.id, @tag_name, @branch)
      info "create tag '#{@tag_name}' success"
    else
      if @force
        info "tag '#{@tag_name}' exist, skip."
      else
        help! "tag '#{@tag_name}' exist."
      end
    end

    puts
  end
end

#validate!Object



32
33
34
35
36
37
38
39
40
# File 'lib/commands/create_tag.rb', line 32

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