Class: Gb::Merge

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

Returns a new instance of Merge.



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

def initialize(argv)
  @from_branch = argv.shift_argument
  @to_branch = argv.shift_argument
  @assignee = argv.option('assignee')
  @title = argv.option('title')
  @show_diff = argv.flag?('show-diff')
  super
end

Class Method Details

.optionsObject



20
21
22
23
24
25
26
# File 'lib/commands/merge.rb', line 20

def self.options
  [
      ["--assignee=[user name]", "指定review用户名称"],
      ["--title", "merge request标题"],
      ["--show-diff", "review前是否显示变更"],
  ].concat(super)
end

Instance Method Details

#gitlab_get_team_members(project_id) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/commands/merge.rb', line 234

def gitlab_get_team_members(project_id)
  users = Gitlab.project_usesrs(project_id).delete_if { |user|
    user.username == 'root'
  }
  if users.size > 0
    info "Find user to assign."
    users.each_with_index do |user, index|
      puts "#{index + 1}#{user.username}#{user.name})".green
    end
  else
    raise Error.new("Can't find members in project '#{project_id}''.")
  end
  users
end

#gitlab_search_project(project_name) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/commands/merge.rb', line 216

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

#gitlab_search_user(assignee) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/commands/merge.rb', line 199

def gitlab_search_user(assignee)
  users = Gitlab.user_search(assignee)
  if users.size > 1
    info "Find more than one user. you means which one?"
    users.each do |user|
      print user.name + '  '
    end
    info ""
    raise Error.new("Find #{users.size} user named #{project.name}")
  elsif users.size == 1
    user = users[0]
  else
    raise Error.new("Can't find user #{assignee}.")
  end
  user
end

#run_in_configObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/commands/merge.rb', line 47

def run_in_config
  remote = "origin"

  # 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

  user = nil
  if !@assignee.nil?
    user = gitlab_search_user(@assignee)
  end

  self.gb_config.projects.each_with_index do |project, index|

    gitlab_project = gitlab_search_project(project.name)
    info "Find project #{gitlab_project.name} on #{gitlab_project.web_url}."

    begin
      Gitlab.branch(gitlab_project.id, @from_branch)
    rescue Gitlab::Error::NotFound => error
      raise Error.new("Branch '#{@from_branch}' not exist in remote '#{remote}'.")
    rescue Gitlab::Error::Error => error
      raise(error)
    end

    begin
      Gitlab.branch(gitlab_project.id, @to_branch)
    rescue Gitlab::Error::NotFound => error
      raise Error.new("Branch '#{@to_branch}' not exist in remote '#{remote}'.")
    rescue Gitlab::Error::Error => error
      raise(error)
    end

    compare_response = Gitlab.compare(gitlab_project.id, @to_branch, @from_branch);
    if compare_response.commits.size >= 1
      if @show_diff
        puts "\ncommits"
        compare_response.commits.each_with_index do |commit, index|
          unless index == 0
            puts ""
          end
          puts "  #{index} id:" + commit["id"]
          puts "  author:" + commit["author_name"]
          puts "  create at: " + commit["created_at"]
          puts "  title: " + commit["title"]
        end
        puts ""
      end
    else
      info "Can't find new commit on #{@from_branch} to #{@to_branch} in project #{project.name}."
      puts
      next
    end

    if compare_response.diffs.size >= 1
      if @show_diff
        puts "Diffs"
        compare_response.diffs.each do |diff|
          if diff["new_file"]
            puts "  created " + diff["new_path"]
          elsif diff["renamed_file"]
            puts "  renamed " + diff["old_path"] + "=>" + diff["new_path"]
          elsif diff["deleted_file"]
            puts "  deleted" + diff["old_path"]
          else
            puts "  edited " + diff["new_path"]
          end

          diff = diff["diff"];
          lines = diff.split("\n")
          lines.each do |line|
            puts "  " + line
          end

        end
      end
    else
      info "Can't find diff between #{@from_branch} and #{@to_branch} in project #{project.name}."
      puts
      next
    end

    if user.nil?
      users = gitlab_get_team_members(gitlab_project.id)
      begin
        info "\nSelect user name or index for review."
        input_user = STDIN.gets.chomp
        if input_user =~ /[[:digit:]]/
          user = users[input_user.to_i - 1]
        else
          user = gitlab_search_user(input_user)
        end
        if user.nil?
          error "Can not found user '#{input_user}'."
        else
          info "Assign to #{user.username}(#{user.name})"
        end
      end until !user.nil?
    end

    if @title.nil? || @title.empty?
      begin
        info "\nInput merge request title for project '#{project.name}'"
        @title = STDIN.gets.chomp
      end until @title.length > 0
    end

    # 总共 0 (差异 0),复用 0 (差异 0)
    # remote:
    #     remote: To create a merge request for dev-v3.9.0-luobin, visit:
    #     remote:   http://git.tianxiao100.com/tianxiao-ios/tianxiao/tianxiao-base-iphone-sdk/merge_requests/new?merge_request%5Bfrom_branch%5D=dev-v3.9.0-luobin
    # remote:
    #     To http://git.tianxiao100.com/tianxiao-ios/tianxiao/tianxiao-base-iphone-sdk.git
    # * [new branch]        dev-v3.9.0-luobin -> dev-v3.9.0-luobin

    begin
      merge_request = Gitlab.create_merge_request(gitlab_project.id, @title,
                                                  { source_branch: @from_branch, target_branch: @to_branch, assignee_id:user ? user.id : "" })
      info "Create merge request for #{project.name} success. see detail url:#{merge_request.web_url}"
      if !Gem.win_platform?
        `open -a "/Applications/Google Chrome.app"    '#{merge_request.web_url}/diffs'`
        exitstatus = $?.exitstatus
        if exitstatus != 0
          raise Error.new("open chrome failed.")
        else
          # info "Please review diff, then input any to continue."
          # STDIN.gets.chomp
        end
      end
    rescue Gitlab::Error::Conflict => error
      # merge exists
      info "Merge request from '#{@from_branch}' to '#{@to_branch}' exist."
    rescue Gitlab::Error::Error => error
      raise(error)
    end
    puts

  end
end

#validate!Object



37
38
39
40
41
42
43
44
45
# File 'lib/commands/merge.rb', line 37

def validate!
  super
  if @from_branch.nil?
    help! 'from_branch is required.'
  end
  if @to_branch.nil?
    help! 'remote_branch is required.'
  end
end