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
31
32
33
34
35
# 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

Instance Attribute Details

#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

#clientObject



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

def client
  @configuration.client
end

#close(*args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Hash

Create a pull request

Examples:

@client.create_pull_request("master", "feature-branch",
  "Pull Request title", "Pull Request body")

Parameters:

  • 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.

  • head (String)

    The branch (or git ref) where your changes are implemented.

  • title (String)

    Title for the pull request

  • body (String)

    The body for the pull request (optional). Supports GFM.

Returns:

  • (Hash)

    The newly created pull request

See Also:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ 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



112
113
114
115
116
117
118
119
120
121
122
123
# 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) ⇒ 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:



97
98
99
# File 'lib/git-process/github_pull_request.rb', line 97

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

#loggerObject



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

def logger
  @gitlib.logger
end

#pull_request(pr_number) ⇒ Object



83
84
85
# 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 = {}) ⇒ Object



43
44
45
# 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