Class: ModuleSync::PR::GitLab

Inherits:
Object
  • Object
show all
Defined in:
lib/modulesync/pr/gitlab.rb

Overview

GitLab creates and manages merge requests on gitlab.com or private GitLab installations.

Instance Method Summary collapse

Constructor Details

#initialize(token, endpoint) ⇒ GitLab

Returns a new instance of GitLab.



9
10
11
12
13
14
# File 'lib/modulesync/pr/gitlab.rb', line 9

def initialize(token, endpoint)
  @api = Gitlab::Client.new(
    :endpoint => endpoint,
    :private_token => token
  )
end

Instance Method Details

#manage(namespace, module_name, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/modulesync/pr/gitlab.rb', line 16

def manage(namespace, module_name, options)
  repo_path = File.join(namespace, module_name)
  head = "#{namespace}:#{options[:branch]}"
  target_branch = options[:pr_target_branch] || 'master'

  if options[:noop]
    $stdout.puts \
      "Using no-op. Would submit MR '#{options[:pr_title]}' to #{repo_path} " \
      "- merges #{options[:branch]} into #{target_branch}"
    return
  end

  merge_requests = @api.merge_requests(repo_path,
                                       :state => 'opened',
                                       :source_branch => head,
                                       :target_branch => target_branch)
  unless merge_requests.empty?
    # Skip creating the MR if it exists already.
    $stdout.puts "Skipped! #{merge_requests.length} MRs found for branch #{options[:branch]}"
    return
  end

  mr_labels = ModuleSync::Util.parse_list(options[:pr_labels])
  mr = @api.create_merge_request(repo_path,
                                 options[:pr_title],
                                 :source_branch => options[:branch],
                                 :target_branch => target_branch,
                                 :labels => mr_labels)
  $stdout.puts \
    "Submitted MR '#{options[:pr_title]}' to #{repo_path} " \
    "- merges #{options[:branch]} into #{target_branch}"

  return if mr_labels.empty?
  $stdout.puts "Attached the following labels to MR #{mr.iid}: #{mr_labels.join(', ')}"
end