Class: OctocatHerder::PullRequest

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/octocat_herder/pull_request.rb,
lib/octocat_herder/pull_request/repo.rb

Overview

Interface to the GitHub v3 API for interacting with pull requests.

Currently, this only supports retrieval of information about the pull request itself, not the comments, or any updating/creation.

Defined Under Namespace

Classes: Repo

Instance Attribute Summary

Attributes included from Base

#connection, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#available_attributes

Constructor Details

#initialize(raw_hash, raw_detail_hash = nil, conn = OctocatHerder::Connection.new) ⇒ PullRequest

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of PullRequest.

Parameters:

  • raw_hash (Hash)

    The ‘overview’ information retrieved from the pull request listing API.

  • raw_detail_hash (Hash) (defaults to: nil)

    The full information available by querying information about a specific pull request.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Since:

  • 0.0.1



85
86
87
88
# File 'lib/octocat_herder/pull_request.rb', line 85

def initialize(raw_hash, raw_detail_hash = nil, conn = OctocatHerder::Connection.new)
  super raw_hash, conn
  @raw_detail_hash = raw_detail_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args) ⇒ String

Check the “normal” place first for the information returned by the GitHub API, then check @raw_detail_hash (populating it if needed).

Returns:

  • (String)

Since:

  • 0.0.1



116
117
118
119
120
121
122
123
# File 'lib/octocat_herder/pull_request.rb', line 116

def method_missing(id, *args)
  super
rescue NoMethodError => e
  get_detail
  return @raw_detail_hash[id.id2name] if @raw_detail_hash and @raw_detail_hash.keys.include?(id.id2name)

  raise e
end

Class Method Details

.fetch(owner_login, repository_name, pull_request_number, conn = OctocatHerder::Connection.new) ⇒ OctocatHerder::PullRequest

Query information about a specific pull request.

Parameters:

  • owner_login (String)

    The login name of the repository owner.

  • repository_name (String)

    The name of the repository itself.

  • pull_request_number (String, Integer)

    The pull request to retrieve.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Returns:

Since:

  • 0.0.1



71
72
73
74
75
76
77
78
# File 'lib/octocat_herder/pull_request.rb', line 71

def self.fetch(, repository_name, pull_request_number, conn = OctocatHerder::Connection.new)
  request = conn.get(
    "/repos/#{CGI.escape(.to_s)}/#{CGI.escape(repository_name.to_s)}/pulls/#{CGI.escape(pull_request_number.to_s)}",
    :headers   => { 'Accept' => 'application/vnd.github-pull.full+json' }
  )

  new(nil, request, conn)
end

.find_closed_for_repository(owner_login, repository_name, conn = OctocatHerder::Connection.new) ⇒ Array<OctocatHerder::PullRequest>

Query the closed pull requests for a given repository.

Parameters:

  • owner_login (String)

    The login name of the repository owner.

  • repository_name (String)

    The name of the repository itself.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Returns:

Since:

  • 0.0.1



59
60
61
# File 'lib/octocat_herder/pull_request.rb', line 59

def self.find_closed_for_repository(, repository_name, conn = OctocatHerder::Connection.new)
  OctocatHerder::PullRequest.find_for_repository(, repository_name, 'closed', conn)
end

.find_for_repository(owner_login, repository_name, status = 'open', conn = OctocatHerder::Connection.new) ⇒ Array<OctocatHerder::PullRequest>

Query either the open or closed pull requests for a given repository.

Parameters:

  • owner_login (String)

    The login name of the repository owner.

  • repository_name (String)

    The name of the repository itself.

  • status ('open', 'closed') (defaults to: 'open')

    Defaults to querying open pull requests.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Returns:

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/octocat_herder/pull_request.rb', line 25

def self.find_for_repository(, repository_name, status = 'open', conn = OctocatHerder::Connection.new)
  raise ArgumentError.new("Unknown PullRequest status '#{status}'.  Must be one of ['open', 'closed'].") unless
    ['open', 'closed'].include? status

  pull_requests = conn.get(
    "/repos/#{CGI.escape(.to_s)}/#{CGI.escape(repository_name.to_s)}/pulls",
    :paginated => true,
    :params    => { :state => status },
    :headers   => { 'Accept' => 'application/vnd.github-pull.full+json' }
  )

  pull_requests.map do |pull|
    new(pull, nil, conn)
  end
end

.find_open_for_repository(owner_login, repository_name, conn = OctocatHerder::Connection.new) ⇒ Array<OctocatHerder::PullRequest>

Query the open pull requests for a given repository.

Parameters:

  • owner_login (String)

    The login name of the repository owner.

  • repository_name (String)

    The name of the repository itself.

  • conn (OctocatHerder::Connection) (defaults to: OctocatHerder::Connection.new)

    Defaults to unauthenticated requests.

Returns:

Since:

  • 0.0.1



48
49
50
# File 'lib/octocat_herder/pull_request.rb', line 48

def self.find_open_for_repository(, repository_name, conn = OctocatHerder::Connection.new)
  OctocatHerder::PullRequest.find_for_repository(, repository_name, 'open', conn)
end

Instance Method Details

#baseOctocatHerder::PullRequest::Repo

Information about what the pull request was based on in the pull request.

Returns:

Since:

  • 0.0.1



288
289
290
291
292
# File 'lib/octocat_herder/pull_request.rb', line 288

def base
  get_detail

  @base_repo ||= OctocatHerder::PullRequest::Repo.new(@raw_detail_hash['base'], connection)
end

#closed_atTime?

When the pull request was closed, or nil if it is still open.

Returns:

  • (Time, nil)

Since:

  • 0.0.1



260
261
262
# File 'lib/octocat_herder/pull_request.rb', line 260

def closed_at
  parse_date_time(@raw_detail_hash['closed_at'])
end

#created_atTime

When the pull request was first created.

Returns:

  • (Time)

Since:

  • 0.0.1



244
245
246
# File 'lib/octocat_herder/pull_request.rb', line 244

def created_at
  parse_date_time(@raw_detail_hash['created_at'])
end

#diff_textString

The diff introduced by all of the commits in the pull request.

Returns:

  • (String)

Since:

  • 0.1.0



323
324
325
# File 'lib/octocat_herder/pull_request.rb', line 323

def diff_text
  @diff_text ||= connection.raw_get(diff_url).body
end

#get_detailself

Get the full pull request details. When retrieved from .find_for_repository, .find_open_for_repository, or .find_closed_for_repository not all of the details of the pull request are available since GitHub doesn’t return them in the listing. This will query information about the specific pull request, which will get us all of the available details.

Returns:

  • (self)

Since:

  • 0.0.1



99
100
101
102
103
104
105
106
107
108
# File 'lib/octocat_herder/pull_request.rb', line 99

def get_detail
  return if @raw_detail_hash

  @raw_detail_hash = connection.get(
    url,
    :headers => { 'Accept' => 'application/vnd.github-pull.full+json' }
  )

  self
end

#headOctocatHerder::PullRequest::Repo

Information about what is being asked to be merged in the pull request.

Returns:

Since:

  • 0.0.1



277
278
279
280
281
# File 'lib/octocat_herder/pull_request.rb', line 277

def head
  get_detail

  @head_repo ||= OctocatHerder::PullRequest::Repo.new(@raw_detail_hash['head'], connection)
end

#merged_atTime?

When the pull request was merged, or nil if it hasn’t been merged.

Returns:

  • (Time, nil)

Since:

  • 0.0.1



268
269
270
# File 'lib/octocat_herder/pull_request.rb', line 268

def merged_at
  parse_date_time(@raw_detail_hash['merged_at'])
end

#merged_byOctocatHerder::User?

Note:

This is cached locally to the individual pull request, but will make an additional API request to populate it initially.

The user that merged the pull request, or nil if it has not been merged yet.

Returns:

Since:

  • 0.0.1



234
235
236
237
238
# File 'lib/octocat_herder/pull_request.rb', line 234

def merged_by
  return nil unless merged

  @merged_by ||= OctocatHerder::User.fetch(, connection)
end

#merged_by_avatar_urlString?

The URL to the avatar image of the person that merged the pull request, or nil if it has not been merged yet.

Returns:

  • (String, nil)

Since:

  • 0.0.1



208
209
210
211
212
213
# File 'lib/octocat_herder/pull_request.rb', line 208

def merged_by_avatar_url
  return nil unless merged

  get_detail
  @raw_detail_hash['merged_by']['avatar_url']
end

#merged_by_idString?

The ID number of the person that merged the pull request, or nil if it has not been merged yet.

Returns:

  • (String, nil)

Since:

  • 0.0.1



196
197
198
199
200
201
# File 'lib/octocat_herder/pull_request.rb', line 196

def merged_by_id
  return nil unless merged

  get_detail
  @raw_detail_hash['merged_by']['id']
end

#merged_by_loginString?

The login name of the person that merged the pull request, or nil if it has not been merged yet.

Returns:

  • (String, nil)

Since:

  • 0.0.1



184
185
186
187
188
189
# File 'lib/octocat_herder/pull_request.rb', line 184

def 
  return nil unless merged

  get_detail
  @raw_detail_hash['merged_by']['login']
end

#merged_by_urlString?

The URL of the person that merged the pull request, or nil if it has not been merged yet.

Returns:

  • (String, nil)

Since:

  • 0.0.1



220
221
222
223
224
225
# File 'lib/octocat_herder/pull_request.rb', line 220

def merged_by_url
  return nil unless merged

  get_detail
  @raw_detail_hash['merged_by']['url']
end

#patch_textString

The pull request as a single patch, with the pull request title & body as the commit message, and the total diff as the patch.

Returns:

  • (String)

Since:

  • 0.1.0



315
316
317
# File 'lib/octocat_herder/pull_request.rb', line 315

def patch_text
  @patch_text ||= connection.raw_get(patch_url).body
end

#to_hashHash

A Hash representation of the pull request. Combines @raw, and @raw_detail_hash into a single hash.

Returns:

  • (Hash)

Since:

  • 0.0.2



299
300
301
302
303
304
305
306
307
308
# File 'lib/octocat_herder/pull_request.rb', line 299

def to_hash
  raw = @raw || {}
  detail = @raw_detail_hash || {}

  ret = raw.merge(detail)
  ret['patch_text'] = patch_text
  ret['diff_text']  = diff_text

  ret
end

#updated_atTime

When the pull request was last updated.

Returns:

  • (Time)

Since:

  • 0.0.1



252
253
254
# File 'lib/octocat_herder/pull_request.rb', line 252

def updated_at
  parse_date_time(@raw_detail_hash['updated_at'])
end

#userOctocatHerder::User

Note:

This is cached locally to the individual pull request, but will make an additional API request to populate it initially.

The user that opened the pull request.

Returns:

Since:

  • 0.0.1



175
176
177
# File 'lib/octocat_herder/pull_request.rb', line 175

def user
  @user ||= OctocatHerder::User.fetch(, connection)
end

#user_avatar_urlString

Note:

Since this is returned by the pull request API itself, this can be used without making an additional API request.

The URL to the avatar image of the person that opened the pull request.

Returns:

  • (String)

    Avatar URL

Since:

  • 0.0.1



131
132
133
134
# File 'lib/octocat_herder/pull_request.rb', line 131

def user_avatar_url
  return @raw['user']['avatar_url'] if @raw
  @raw_detail_hash['user']['avatar_url']
end

#user_idInteger

Note:

Since this is returned by the pull request API itself, this can be used without making an additional API request.

The ID number of the person that opened the pull request.

Returns:

  • (Integer)

    User ID

Since:

  • 0.0.1



153
154
155
156
# File 'lib/octocat_herder/pull_request.rb', line 153

def user_id
  return @raw['user']['id'] if @raw
  @raw_detail_hash['user']['id']
end

#user_loginString

Note:

Since this is returned by the pull request API itself, this can be used without making an additional API request.

The login name of the person that opened the pull request.

Returns:

  • (String)

    User login name

Since:

  • 0.0.1



164
165
166
167
# File 'lib/octocat_herder/pull_request.rb', line 164

def 
  return @raw['user']['login'] if @raw
  @raw_detail_hash['user']['login']
end

#user_urlString

Note:

Since this is returned by the pull request API itself, this can be used without making an additional API request.

The URL of the person that opened the pull request.

Returns:

  • (String)

    User URL

Since:

  • 0.0.1



142
143
144
145
# File 'lib/octocat_herder/pull_request.rb', line 142

def user_url
  return @raw['user']['url'] if @raw
  @raw_detail_hash['user']['url']
end