class ModuleSync::GitService::GitHub

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

Public Class Methods

new(token, endpoint) click to toggle source
Calls superclass method
# File lib/modulesync/git_service/github.rb, line 10
def initialize(token, endpoint)
  super()

  Octokit.configure do |c|
    c.api_endpoint = endpoint
  end
  @api = Octokit::Client.new(access_token: token)
end

Private Instance Methods

_open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:) click to toggle source
# File lib/modulesync/git_service/github.rb, line 21
def _open_pull_request(repo_path:, namespace:, title:, message:, source_branch:, target_branch:, labels:, noop:)
  head = "#{namespace}:#{source_branch}"

  if noop
    $stdout.puts "Using no-op. Would submit PR '#{title}' to '#{repo_path}' " \
                 "- merges '#{source_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 '#{source_branch}'"
    return
  end

  pr = @api.create_pull_request(repo_path,
                                target_branch,
                                source_branch,
                                title,
                                message)
  $stdout.puts \
    "Submitted PR '#{title}' to '#{repo_path}' " \
    "- merges #{source_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 labels.empty?

  $stdout.puts "Attaching the following labels to PR #{pr['number']}: #{labels.join(', ')}"
  @api.add_labels_to_an_issue(repo_path, pr['number'], labels)
end