class GitHub::PullRequest

Constants

MAX_RESEND

Attributes

configuration[R]
gitlib[R]
remote_name[R]
repo[R]

Public Class Methods

new(lib, remote_name, repo, opts = {}) click to toggle source
# File lib/git-process/github_pull_request.rb, line 25
def initialize(lib, remote_name, repo, opts = {})
  @gitlib = lib
  @repo = repo
  @remote_name = remote_name
  @configuration = GitHubService::Configuration.new(
      gitlib.config,
      :user => opts[:user],
      :password => opts[:password],
      :remote_name => remote_name
  )
end

Public Instance Methods

client() click to toggle source
# File lib/git-process/github_pull_request.rb, line 38
def client
  @configuration.client
end
close(*args) click to toggle source
# File lib/git-process/github_pull_request.rb, line 126
def close(*args)
  pull_number = if args.size == 2
                  get_pull_request(args[0], args[1])[:number]
                elsif args.size == 1
                  args[0]
                else
                  raise ArgumentError.new('close(..) needs 1 or 2 arguments')
                end

  logger.info { "Closing a pull request \##{pull_number} on #{repo}." }

  send_close_req(pull_number, 1)
end
create(base, head, title, body) click to toggle source

Create a pull request

@see developer.github.com/v3/pulls/#create-a-pull-request @param base [String] The branch (or git ref) you want your changes

pulled into. This should be an existing branch on the current
repository. You cannot submit a pull request to one repo that requests
a merge to a base of another repo.

@param head [String] The branch (or git ref) where your changes are implemented. @param title [String] Title for the pull request @param body [String] The body for the pull request (optional). Supports GFM. @return [Hash] The newly created pull request @example

@client.create_pull_request("master", "feature-branch",
  "Pull Request title", "Pull Request body")
# File lib/git-process/github_pull_request.rb, line 62
def create(base, head, title, body)
  logger.info { "Creating a pull request asking for '#{head}' to be merged into '#{base}' on #{repo}." }
  begin
    return sym_hash_JSON(client.create_pull_request(repo, base, head, title, body))
  rescue Octokit::UnprocessableEntity => exp
    pull = pull_requests.find { |p| p[:head][:ref] == head and p[:base][:ref] == base }
    if pull
      logger.warn { "Pull request already exists. See #{pull[:html_url]}" }
    else
      logger.warn { "UnprocessableEntity: #{exp}" }
    end
    return pull
  end
end
find_pull_request(base, head, error_if_missing = false) click to toggle source

Find the pull request (PR) that matches the ‘head’ and ‘base’.

@param [String] base what the PR is merging into @param [String] head the branch of the PR @param [boolean] error_if_missing should this error-out if the PR is not found?

@return [Hash, nil] @raise [NotFoundError] if the pull request does not exist and ‘error_if_missing’ is true

# File lib/git-process/github_pull_request.rb, line 112
def find_pull_request(base, head, error_if_missing = false)
  logger.info { "Looking for a pull request asking for '#{head}' to be merged into '#{base}' on #{repo}." }

  json = pull_requests
  pr = json.find do |p|
    p[:head][:ref] == head and p[:base][:ref] == base
  end

  raise NotFoundError.new(base, head, repo, json) if error_if_missing && pr.nil?

  pr
end
get_pull_request(base, head) click to toggle source

Find the pull request (PR) that matches the ‘head’ and ‘base’.

@param [String] base what the PR is merging into @param [String] head the branch of the PR

@return [Hash] @raise [NotFoundError] if the pull request does not exist

# File lib/git-process/github_pull_request.rb, line 97
def get_pull_request(base, head)
  find_pull_request(base, head, true)
end
logger() click to toggle source
# File lib/git-process/github_pull_request.rb, line 78
def logger
  @gitlib.logger
end
pull_request(pr_number) click to toggle source
# File lib/git-process/github_pull_request.rb, line 83
def pull_request(pr_number)
  sym_hash_JSON(client.pull_request(repo, pr_number))
end
pull_requests(opts = {}) click to toggle source
# File lib/git-process/github_pull_request.rb, line 43
def pull_requests(opts = {})
  @pull_requests ||= sym_hash_JSON(client.pull_requests(repo, opts))
end

Private Instance Methods

send_close_req(pull_number, count) click to toggle source

@return [Sawyer::Resource]

# File lib/git-process/github_pull_request.rb, line 205
def send_close_req(pull_number, count)
  begin
    sym_hash_JSON(client.patch("repos/#{Octokit::Repository.new(repo)}/pulls/#{pull_number}", {:state => 'closed'}))
  rescue Octokit::UnprocessableEntity => exp
    if count > MAX_RESEND
      raise exp
    end
    logger.warn { "Retrying closing a pull request \##{pull_number} on #{repo} - try \##{count + 1}" }
    send_close_req(pull_number, count + 1)
  end
end
sym_hash_JSON(str) click to toggle source

@param [String, Sawyer::Resource] str the String to parse as JSON, or a simple pass through

@return [Array, Hash, Sawyer::Resource, nil] an Array/Hash where all the hash keys are Symbols

# File lib/git-process/github_pull_request.rb, line 176
def sym_hash_JSON(str)
  return nil if str.nil?
  case str
    when String
      raise ArgumentError.new('Can not parse an empty JSON string') if str.empty?
      to_sym_hash(JSON.parse(str))
    when Array, Hash
      to_sym_hash(str)
    else
      str
  end
end
to_sym_hash(source) click to toggle source
# File lib/git-process/github_pull_request.rb, line 190
def to_sym_hash(source)
  if source.is_a? Hash
    Hash[source.map { |k, v|
           [k.to_sym, to_sym_hash(v)]
         }]
  elsif source.is_a? Array
    source.map { |e| to_sym_hash(e) }
  else
    source
    # raise "Don't know what to do with #{source.class} - #{source}"
  end
end