Class: GitHub::PullRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/git-process/github_pull_request.rb

Defined Under Namespace

Classes: NotFoundError

Constant Summary collapse

MAX_RESEND =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lib, remote_name, repo, opts = {}) ⇒ PullRequest

Returns a new instance of PullRequest.



25
26
27
28
29
30
# 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])
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



21
22
23
# File 'lib/git-process/github_pull_request.rb', line 21

def client
  @client
end

#configurationObject (readonly)

Returns the value of attribute configuration.



21
22
23
# File 'lib/git-process/github_pull_request.rb', line 21

def configuration
  @configuration
end

#gitlibObject (readonly)

Returns the value of attribute gitlib.



21
22
23
# File 'lib/git-process/github_pull_request.rb', line 21

def gitlib
  @gitlib
end

#remote_nameObject (readonly)

Returns the value of attribute remote_name.



21
22
23
# File 'lib/git-process/github_pull_request.rb', line 21

def remote_name
  @remote_name
end

#repoObject (readonly)

Returns the value of attribute repo.



21
22
23
# File 'lib/git-process/github_pull_request.rb', line 21

def repo
  @repo
end

Instance Method Details

#close(*args) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/git-process/github_pull_request.rb', line 105

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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git-process/github_pull_request.rb', line 43

def create(base, head, title, body)
  logger.info { "Creating a pull request asking for '#{head}' to be merged into '#{base}' on #{repo}." }
  begin
    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
    pull
  end
end

#find_pull_request(base, head, error_if_missing = false) ⇒ Hash?

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

Parameters:

  • base (String)

    what the PR is merging into

  • head (String)

    the branch of the PR

  • error_if_missing (boolean) (defaults to: false)

    should this error-out if the PR is not found?

Returns:

  • (Hash, nil)

Raises:

  • (NotFoundError)

    if the pull request does not exist and ‘error_if_missing’ is true



93
94
95
96
97
98
99
100
101
102
# File 'lib/git-process/github_pull_request.rb', line 93

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 { |p| p[:head][:ref] == head and p[:base][:ref] == base }

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

  pr
end

#get_pull_request(base, head) ⇒ Hash

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

Parameters:

  • base (String)

    what the PR is merging into

  • head (String)

    the branch of the PR

Returns:

  • (Hash)

Raises:



78
79
80
# File 'lib/git-process/github_pull_request.rb', line 78

def get_pull_request(base, head)
  find_pull_request(base, head, true)
end

#loggerObject



59
60
61
# File 'lib/git-process/github_pull_request.rb', line 59

def logger
  @gitlib.logger
end

#pull_request(pr_number) ⇒ Object



64
65
66
# File 'lib/git-process/github_pull_request.rb', line 64

def pull_request(pr_number)
  client.pull_request(repo, pr_number)
end

#pull_requests(state = 'open', opts = {}) ⇒ Object



38
39
40
# File 'lib/git-process/github_pull_request.rb', line 38

def pull_requests(state = 'open', opts = {})
  @pull_requests ||= client.pull_requests(repo, state, opts)
end