Class: Octokit::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, Authorizations, CommitComments, Commits, Contents, Deployments, Downloads, Emojis, Events, Feeds, Gists, Gitignore, Hooks, Issues, Labels, LegacySearch, Markdown, Meta, Milestones, Notifications, Objects, Organizations, Pages, PubSubHubbub, PullRequests, RateLimit, Refs, Releases, Repositories, Say, Search, ServiceStatus, Stats, Statuses, Users, Configurable
Defined in:
lib/octokit/client.rb,
lib/octokit/client/say.rb,
lib/octokit/client/meta.rb,
lib/octokit/client/refs.rb,
lib/octokit/client/feeds.rb,
lib/octokit/client/gists.rb,
lib/octokit/client/hooks.rb,
lib/octokit/client/pages.rb,
lib/octokit/client/stats.rb,
lib/octokit/client/users.rb,
lib/octokit/client/emojis.rb,
lib/octokit/client/events.rb,
lib/octokit/client/issues.rb,
lib/octokit/client/labels.rb,
lib/octokit/client/search.rb,
lib/octokit/client/commits.rb,
lib/octokit/client/objects.rb,
lib/octokit/client/contents.rb,
lib/octokit/client/markdown.rb,
lib/octokit/client/releases.rb,
lib/octokit/client/statuses.rb,
lib/octokit/client/downloads.rb,
lib/octokit/client/gitignore.rb,
lib/octokit/client/milestones.rb,
lib/octokit/client/rate_limit.rb,
lib/octokit/client/deployments.rb,
lib/octokit/client/repositories.rb,
lib/octokit/client/legacy_search.rb,
lib/octokit/client/notifications.rb,
lib/octokit/client/organizations.rb,
lib/octokit/client/pull_requests.rb,
lib/octokit/client/authorizations.rb,
lib/octokit/client/pub_sub_hubbub.rb,
lib/octokit/client/service_status.rb,
lib/octokit/client/commit_comments.rb

Overview

Client for the GitHub API

Defined Under Namespace

Modules: Authorizations, CommitComments, Commits, Contents, Deployments, Downloads, Emojis, Events, Feeds, Gists, Gitignore, Hooks, Issues, Labels, LegacySearch, Markdown, Meta, Milestones, Notifications, Objects, Organizations, Pages, PubSubHubbub, PullRequests, RateLimit, Refs, Releases, Repositories, Say, Search, ServiceStatus, Stats, Statuses, Users

Constant Summary collapse

CONVENIENCE_HEADERS =

Header keys that can be passed in options hash to #get,#head

Set.new([:accept, :content_type])

Constants included from Statuses

Statuses::COMBINED_STATUS_MEDIA_TYPE

Constants included from ServiceStatus

ServiceStatus::STATUS_ROOT

Constants included from Organizations

Organizations::ORG_INVITATIONS_PREVIEW_MEDIA_TYPE

Constants included from Deployments

Deployments::DEPLOYMENTS_PREVIEW_MEDIA_TYPE

Instance Attribute Summary

Attributes included from Configurable

#access_token, #api_endpoint, #auto_paginate, #client_id, #client_secret, #connection_options, #default_media_type, #login, #middleware, #netrc, #netrc_file, #password, #per_page, #proxy, #user_agent, #web_endpoint

Instance Method Summary collapse

Methods included from Users

#add_email, #add_key, #all_users, #emails, #exchange_code_for_token, #follow, #followers, #following, #follows?, #key, #keys, #remove_email, #remove_key, #starred, #starred?, #subscriptions, #unfollow, #update_key, #update_user, #user, #user_keys, #validate_credentials

Methods included from Statuses

#combined_status, #create_status, #statuses

Methods included from Stats

#code_frequency_stats, #commit_activity_stats, #contributors_stats, #participation_stats, #punch_card_stats

Methods included from ServiceStatus

#github_status, #github_status_last_message, #github_status_messages

Methods included from Search

#search_code, #search_issues, #search_repositories, #search_users

Methods included from Say

#say

Methods included from Repositories

#add_collaborator, #add_deploy_key, #all_repositories, #branch, #branches, #check_assignee, #collaborator?, #collaborators, #contributors, #create_hook, #create_repository, #delete_repository, #delete_subscription, #deploy_key, #deploy_keys, #edit_deploy_key, #edit_hook, #edit_repository, #fork, #forks, #hook, #hooks, #languages, #remove_collaborator, #remove_deploy_key, #remove_hook, #repositories, #repository, #repository?, #repository_assignees, #repository_teams, #set_private, #set_public, #star, #stargazers, #subscribers, #subscription, #tags, #test_hook, #unstar, #unwatch, #update_subscription, #watch, #watchers

Methods included from Releases

#create_release, #delete_release, #delete_release_asset, #release, #release_asset, #release_assets, #releases, #update_release, #update_release_asset, #upload_asset

Methods included from Refs

#create_ref, #delete_branch, #delete_ref, #ref, #refs, #update_branch, #update_ref

Methods included from RateLimit

#rate_limit, #rate_limit!, #rate_limit_remaining, #rate_limit_remaining!

Methods included from PullRequests

#close_pull_request, #create_pull_request, #create_pull_request_comment, #create_pull_request_comment_reply, #create_pull_request_for_issue, #delete_pull_request_comment, #merge_pull_request, #pull_merged?, #pull_request, #pull_request_comment, #pull_request_comments, #pull_request_commits, #pull_request_files, #pull_requests, #pull_requests_comments, #update_pull_request, #update_pull_request_comment

Methods included from PubSubHubbub

#subscribe, #subscribe_service_hook, #unsubscribe, #unsubscribe_service_hook

Methods included from Pages

#latest_pages_build, #pages, #pages_builds

Methods included from Organizations

#add_team_member, #add_team_membership, #add_team_repository, #create_team, #delete_team, #organization, #organization_member?, #organization_members, #organization_public_member?, #organization_public_members, #organization_repositories, #organization_teams, #organizations, #publicize_membership, #remove_organization_member, #remove_team_member, #remove_team_membership, #remove_team_repository, #team, #team_member?, #team_members, #team_membership, #team_repositories, #team_repository?, #unpublicize_membership, #update_organization, #update_team, #user_teams

Methods included from Objects

#blob, #create_blob, #create_tag, #create_tree, #tag, #tree

Methods included from Notifications

#delete_thread_subscription, #mark_notifications_as_read, #mark_repository_notifications_as_read, #mark_thread_as_read, #notifications, #repository_notifications, #thread_notifications, #thread_subscription, #update_thread_subscription

Methods included from Milestones

#create_milestone, #delete_milestone, #list_milestones, #milestone, #update_milestone

Methods included from Markdown

#markdown

Methods included from Meta

#meta

Methods included from LegacySearch

#legacy_search_issues, #legacy_search_repositories, #legacy_search_users

Methods included from Labels

#add_label, #add_labels_to_an_issue, #delete_label!, #label, #labels, #labels_for_issue, #labels_for_milestone, #remove_all_labels, #remove_label, #replace_all_labels, #update_label

Methods included from Issues

#add_comment, #close_issue, #create_issue, #delete_comment, #issue, #issue_comment, #issue_comments, #issues_comments, #list_issues, #org_issues, #reopen_issue, #update_comment, #update_issue, #user_issues

Methods included from Hooks

#available_hooks

Methods included from Gitignore

#gitignore_template, #gitignore_templates

Methods included from Gists

#create_gist, #create_gist_comment, #delete_gist, #delete_gist_comment, #edit_gist, #fork_gist, #gist, #gist_comment, #gist_comments, #gist_starred?, #gists, #public_gists, #star_gist, #starred_gists, #unstar_gist, #update_gist_comment

Methods included from Feeds

#feed, #feeds

Methods included from Events

#issue_event, #issue_events, #organization_events, #organization_public_events, #public_events, #received_events, #received_public_events, #repository_events, #repository_issue_events, #repository_network_events, #user_events, #user_public_events

Methods included from Emojis

#emojis

Methods included from Downloads

#delete_download, #download, #downloads

Methods included from Deployments

#create_deployment, #create_deployment_status, #deployment_statuses, #deployments

Methods included from Contents

#archive_link, #contents, #create_contents, #delete_contents, #readme, #update_contents

Methods included from CommitComments

#commit_comment, #commit_comments, #create_commit_comment, #delete_commit_comment, #list_commit_comments, #update_commit_comment

Methods included from Commits

#commit, #commits, #commits_before, #commits_between, #commits_on, #commits_since, #compare, #create_commit, #git_commit, #merge

Methods included from Authorizations

#authorization, #authorizations, #authorize_url, #check_application_authorization, #create_authorization, #delete_authorization, #reset_application_authorization, #revoke_all_application_authorizations, #revoke_application_authorization, #scopes, #update_authorization

Methods included from Configurable

#configure, keys, #netrc?, #reset!

Methods included from Authentication

#application_authenticated?, #basic_authenticated?, #token_authenticated?, #user_authenticated?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



93
94
95
96
97
98
99
100
# File 'lib/octokit/client.rb', line 93

def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  Octokit::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || Octokit.instance_variable_get(:"@#{key}"))
  end

   unless user_authenticated? || application_authenticated?
end

Instance Method Details

#access_token=(value) ⇒ Object

Set OAuth access token for authentication

Parameters:

  • value (String)

    40 character GitHub OAuth access token



293
294
295
296
# File 'lib/octokit/client.rb', line 293

def access_token=(value)
  reset_agent
  @access_token = value
end

#agentSawyer::Agent

Hypermedia agent for the GitHub API

Returns:

  • (Sawyer::Agent)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/octokit/client.rb', line 220

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent] = user_agent
    if basic_authenticated?
      http.basic_auth(@login, @password)
    elsif token_authenticated?
      http.authorization 'token', @access_token
    elsif application_authenticated?
      http.params = http.params.merge application_authentication
    end
  end
end

#as_app(key = client_id, secret = client_secret) {|app_client| ... } ⇒ Object

Duplicate client using client_id and client_secret as Basic Authentication credentials.

Examples:

Octokit.client_id = "foo"
Octokit.client_secret = "bar"

# GET https://api.github.com/?client_id=foo&client_secret=bar
Octokit.get "/"

Octokit.client.as_app do |client|
  # GET https://foo:[email protected]/
  client.get "/"
end

Yields:

  • (app_client)


262
263
264
265
266
267
268
269
270
271
272
# File 'lib/octokit/client.rb', line 262

def as_app(key = client_id, secret = client_secret, &block)
  if key.to_s.empty? || secret.to_s.empty?
    raise ApplicationCredentialsRequired, "client_id and client_secret required"
  end
  app_client = self.dup
  app_client.client_id = app_client.client_secret = nil
  app_client.    = key
  app_client.password = secret

  yield app_client if block_given?
end

#client_id=(value) ⇒ Object

Set OAuth app client_id

Parameters:

  • value (String)

    20 character GitHub OAuth app client_id



301
302
303
304
# File 'lib/octokit/client.rb', line 301

def client_id=(value)
  reset_agent
  @client_id = value
end

#client_secret=(value) ⇒ Object

Set OAuth app client_secret

Parameters:

  • value (String)

    40 character GitHub OAuth app client_secret



309
310
311
312
# File 'lib/octokit/client.rb', line 309

def client_secret=(value)
  reset_agent
  @client_secret = value
end

#delete(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

Returns:

  • (Sawyer::Resource)


170
171
172
# File 'lib/octokit/client.rb', line 170

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

Returns:

  • (Sawyer::Resource)


134
135
136
# File 'lib/octokit/client.rb', line 134

def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

Returns:

  • (Sawyer::Resource)


179
180
181
# File 'lib/octokit/client.rb', line 179

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/octokit/client.rb', line 113

def inspect
  inspected = super

  # mask password
  inspected = inspected.gsub! @password, "*******" if @password
  # Only show last 4 of token, secret
  if @access_token
    inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
  end
  if @client_secret
    inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}"
  end

  inspected
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


245
246
247
# File 'lib/octokit/client.rb', line 245

def last_response
  @last_response if defined? @last_response
end

#login=(value) ⇒ Object

Set username for authentication

Parameters:

  • value (String)

    GitHub username



277
278
279
280
# File 'lib/octokit/client.rb', line 277

def login=(value)
  reset_agent
  @login = value
end

#octokit_warn(*message) ⇒ nil

Wrapper around Kernel#warn to print warnings unless OCTOKIT_SILENT is set to true.

Returns:

  • (nil)


318
319
320
321
322
# File 'lib/octokit/client.rb', line 318

def octokit_warn(*message)
  unless ENV['OCTOKIT_SILENT']
    warn message
  end
end

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in Octokit::Configurable#auto_paginate.

Parameters:

  • url (String)

    The path, relative to Octokit::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • block (Block)

    Block to perform the data concatination of the multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.

Returns:

  • (Sawyer::Resource)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/octokit/client.rb', line 194

def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts)

  if @auto_paginate
    while @last_response.rels[:next] && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end

#password=(value) ⇒ Object

Set password for authentication

Parameters:

  • value (String)

    GitHub password



285
286
287
288
# File 'lib/octokit/client.rb', line 285

def password=(value)
  reset_agent
  @password = value
end

#patch(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

Returns:

  • (Sawyer::Resource)


161
162
163
# File 'lib/octokit/client.rb', line 161

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

Returns:

  • (Sawyer::Resource)


143
144
145
# File 'lib/octokit/client.rb', line 143

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

Returns:

  • (Sawyer::Resource)


152
153
154
# File 'lib/octokit/client.rb', line 152

def put(url, options = {})
  request :put, url, options
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


238
239
240
# File 'lib/octokit/client.rb', line 238

def root
  get "/"
end

#same_options?(opts) ⇒ Boolean

Compares client options to a Hash of requested options

Parameters:

  • opts (Hash)

    Options to compare with current client options

Returns:

  • (Boolean)


106
107
108
# File 'lib/octokit/client.rb', line 106

def same_options?(opts)
  opts.hash == options.hash
end