Class: ModuleSync::PR::GitHub

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

Overview

GitHub creates and manages pull requests on github.com or GitHub Enterprise installations.

Instance Method Summary collapse

Constructor Details

#initialize(token, endpoint) ⇒ GitHub



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

def initialize(token, endpoint)
  Octokit.configure do |c|
    c.api_endpoint = endpoint
  end
  @api = Octokit::Client.new(:access_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
51
52
53
# File 'lib/modulesync/pr/github.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 PR '#{options[:pr_title]}' to #{repo_path} " \
      "- merges #{options[:branch]} into #{target_branch}"
    return
  end

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

  pr_labels = ModuleSync::Util.parse_list(options[:pr_labels])
  pr = @api.create_pull_request(repo_path,
                                target_branch,
                                options[:branch],
                                options[:pr_title],
                                options[:message])
  $stdout.puts \
    "Submitted PR '#{options[:pr_title]}' to #{repo_path} " \
    "- merges #{options[:branch]} into #{target_branch}"

  # We only assign labels to the PR if we've discovered a list > 1. The labels MUST
  # already exist. We DO NOT create missing labels.
  return if pr_labels.empty?
  $stdout.puts "Attaching the following labels to PR #{pr['number']}: #{pr_labels.join(', ')}"
  @api.add_labels_to_an_issue(repo_path, pr['number'], pr_labels)
end