octokit 4.22.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +113 -75
  3. data/Rakefile +20 -14
  4. data/lib/ext/sawyer/relation.rb +4 -2
  5. data/lib/octokit/arguments.rb +2 -2
  6. data/lib/octokit/authentication.rb +10 -3
  7. data/lib/octokit/client/actions_artifacts.rb +71 -0
  8. data/lib/octokit/client/actions_secrets.rb +10 -9
  9. data/lib/octokit/client/actions_workflow_jobs.rb +65 -0
  10. data/lib/octokit/client/actions_workflow_runs.rb +23 -3
  11. data/lib/octokit/client/actions_workflows.rb +27 -2
  12. data/lib/octokit/client/apps.rb +26 -26
  13. data/lib/octokit/client/checks.rb +18 -9
  14. data/lib/octokit/client/code_scanning.rb +53 -0
  15. data/lib/octokit/client/codespaces_secrets.rb +59 -0
  16. data/lib/octokit/client/commit_branches.rb +2 -2
  17. data/lib/octokit/client/commit_comments.rb +8 -8
  18. data/lib/octokit/client/commit_pulls.rb +2 -2
  19. data/lib/octokit/client/commits.rb +25 -33
  20. data/lib/octokit/client/community_profile.rb +2 -3
  21. data/lib/octokit/client/contents.rb +20 -21
  22. data/lib/octokit/client/dependabot_secrets.rb +59 -0
  23. data/lib/octokit/client/deployments.rb +6 -6
  24. data/lib/octokit/client/downloads.rb +5 -6
  25. data/lib/octokit/client/emojis.rb +3 -3
  26. data/lib/octokit/client/environments.rb +55 -0
  27. data/lib/octokit/client/events.rb +4 -5
  28. data/lib/octokit/client/feeds.rb +4 -5
  29. data/lib/octokit/client/gists.rb +6 -6
  30. data/lib/octokit/client/gitignore.rb +3 -3
  31. data/lib/octokit/client/hooks.rb +9 -19
  32. data/lib/octokit/client/issues.rb +14 -15
  33. data/lib/octokit/client/labels.rb +10 -10
  34. data/lib/octokit/client/legacy_search.rb +3 -3
  35. data/lib/octokit/client/licenses.rb +3 -6
  36. data/lib/octokit/client/markdown.rb +3 -3
  37. data/lib/octokit/client/marketplace.rb +4 -4
  38. data/lib/octokit/client/meta.rb +4 -5
  39. data/lib/octokit/client/milestones.rb +5 -5
  40. data/lib/octokit/client/notifications.rb +6 -6
  41. data/lib/octokit/client/oauth_applications.rb +0 -6
  42. data/lib/octokit/client/objects.rb +14 -14
  43. data/lib/octokit/client/organizations.rb +100 -56
  44. data/lib/octokit/client/pages.rb +5 -7
  45. data/lib/octokit/client/projects.rb +44 -64
  46. data/lib/octokit/client/pub_sub_hubbub.rb +15 -15
  47. data/lib/octokit/client/pull_requests.rb +54 -42
  48. data/lib/octokit/client/rate_limit.rb +9 -11
  49. data/lib/octokit/client/reactions.rb +13 -16
  50. data/lib/octokit/client/refs.rb +14 -17
  51. data/lib/octokit/client/releases.rb +13 -13
  52. data/lib/octokit/client/repositories.rb +49 -69
  53. data/lib/octokit/client/repository_invitations.rb +3 -3
  54. data/lib/octokit/client/reviews.rb +8 -8
  55. data/lib/octokit/client/say.rb +4 -5
  56. data/lib/octokit/client/search.rb +24 -10
  57. data/lib/octokit/client/service_status.rb +19 -9
  58. data/lib/octokit/client/source_import.rb +7 -12
  59. data/lib/octokit/client/stats.rb +10 -9
  60. data/lib/octokit/client/statuses.rb +5 -5
  61. data/lib/octokit/client/tokens.rb +31 -0
  62. data/lib/octokit/client/traffic.rb +6 -11
  63. data/lib/octokit/client/users.rb +27 -33
  64. data/lib/octokit/client.rb +40 -16
  65. data/lib/octokit/configurable.rb +34 -30
  66. data/lib/octokit/connection.rb +28 -21
  67. data/lib/octokit/default.rb +48 -35
  68. data/lib/octokit/enterprise_admin_client/admin_stats.rb +13 -14
  69. data/lib/octokit/enterprise_admin_client/license.rb +3 -4
  70. data/lib/octokit/enterprise_admin_client/orgs.rb +3 -4
  71. data/lib/octokit/enterprise_admin_client/search_indexing.rb +7 -8
  72. data/lib/octokit/enterprise_admin_client/users.rb +5 -4
  73. data/lib/octokit/enterprise_admin_client.rb +9 -3
  74. data/lib/octokit/enterprise_management_console_client/management_console.rb +32 -32
  75. data/lib/octokit/enterprise_management_console_client.rb +8 -2
  76. data/lib/octokit/error.rb +45 -34
  77. data/lib/octokit/gist.rb +3 -4
  78. data/lib/octokit/middleware/follow_redirects.rb +12 -11
  79. data/lib/octokit/organization.rb +3 -1
  80. data/lib/octokit/rate_limit.rb +8 -6
  81. data/lib/octokit/repo_arguments.rb +2 -3
  82. data/lib/octokit/repository.rb +24 -24
  83. data/lib/octokit/response/base_middleware.rb +10 -0
  84. data/lib/octokit/response/feed_parser.rb +5 -7
  85. data/lib/octokit/response/raise_error.rb +4 -4
  86. data/lib/octokit/user.rb +4 -2
  87. data/lib/octokit/version.rb +4 -2
  88. data/lib/octokit/warnable.rb +4 -5
  89. data/lib/octokit.rb +15 -8
  90. data/octokit.gemspec +12 -11
  91. metadata +20 -33
  92. data/lib/octokit/client/authorizations.rb +0 -182
  93. data/lib/octokit/preview.rb +0 -46
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Octokit
2
4
  class Client
3
-
4
5
  # Methods for the Users API
5
6
  #
6
7
  # @see https://developer.github.com/v3/users/
7
8
  module Users
8
-
9
9
  # List all GitHub users
10
10
  #
11
11
  # This provides a list of every user, in the order that they signed up
@@ -19,7 +19,7 @@ module Octokit
19
19
  #
20
20
  # @return [Array<Sawyer::Resource>] List of GitHub users.
21
21
  def all_users(options = {})
22
- paginate "users", options
22
+ paginate 'users', options
23
23
  end
24
24
 
25
25
  # Get a single user
@@ -30,7 +30,7 @@ module Octokit
30
30
  # @see https://developer.github.com/v3/users/#get-the-authenticated-user
31
31
  # @example
32
32
  # Octokit.user("sferik")
33
- def user(user=nil, options = {})
33
+ def user(user = nil, options = {})
34
34
  get User.path(user), options
35
35
  end
36
36
 
@@ -45,14 +45,14 @@ module Octokit
45
45
  # Octokit.exchange_code_for_token('aaaa', 'xxxx', 'yyyy', {:accept => 'application/json'})
46
46
  def exchange_code_for_token(code, app_id = client_id, app_secret = client_secret, options = {})
47
47
  options = options.merge({
48
- :code => code,
49
- :client_id => app_id,
50
- :client_secret => app_secret,
51
- :headers => {
52
- :content_type => 'application/json',
53
- :accept => 'application/json'
54
- }
55
- })
48
+ code: code,
49
+ client_id: app_id,
50
+ client_secret: app_secret,
51
+ headers: {
52
+ content_type: 'application/json',
53
+ accept: 'application/json'
54
+ }
55
+ })
56
56
 
57
57
  post "#{web_endpoint}login/oauth/access_token", options
58
58
  end
@@ -84,7 +84,7 @@ module Octokit
84
84
  # @example
85
85
  # Octokit.update_user(:name => "Erik Michaels-Ober", :email => "sferik@gmail.com", :company => "Code for America", :location => "San Francisco", :hireable => false)
86
86
  def update_user(options)
87
- patch "user", options
87
+ patch 'user', options
88
88
  end
89
89
 
90
90
  # Get a user's followers.
@@ -96,7 +96,7 @@ module Octokit
96
96
  # @see https://developer.github.com/v3/users/followers/#list-followers-of-a-user
97
97
  # @example
98
98
  # Octokit.followers('pengwynn')
99
- def followers(user=login, options = {})
99
+ def followers(user = login, options = {})
100
100
  paginate "#{User.path user}/followers", options
101
101
  end
102
102
 
@@ -109,7 +109,7 @@ module Octokit
109
109
  # @see https://developer.github.com/v3/users/followers/#list-users-followed-by-another-user
110
110
  # @example
111
111
  # Octokit.following('pengwynn')
112
- def following(user=login, options = {})
112
+ def following(user = login, options = {})
113
113
  paginate "#{User.path user}/following", options
114
114
  end
115
115
 
@@ -174,7 +174,7 @@ module Octokit
174
174
  # @see https://developer.github.com/v3/activity/starring/#list-repositories-being-starred
175
175
  # @example
176
176
  # Octokit.starred('pengwynn')
177
- def starred(user=login, options = {})
177
+ def starred(user = login, options = {})
178
178
  paginate user_path(user, 'starred'), options
179
179
  end
180
180
 
@@ -227,7 +227,7 @@ module Octokit
227
227
  # @example
228
228
  # @client.keys
229
229
  def keys(options = {})
230
- paginate "user/keys", options
230
+ paginate 'user/keys', options
231
231
  end
232
232
 
233
233
  # Get list of public keys for user.
@@ -253,7 +253,7 @@ module Octokit
253
253
  # @example
254
254
  # @client.add_key('Personal projects key', 'ssh-rsa AAA...')
255
255
  def add_key(title, key, options = {})
256
- post "user/keys", options.merge({:title => title, :key => key})
256
+ post 'user/keys', options.merge({ title: title, key: key })
257
257
  end
258
258
 
259
259
  # Update a public key
@@ -297,7 +297,7 @@ module Octokit
297
297
  # @example
298
298
  # @client.emails
299
299
  def emails(options = {})
300
- paginate "user/emails", options
300
+ paginate 'user/emails', options
301
301
  end
302
302
 
303
303
  # Add email address to user.
@@ -309,9 +309,9 @@ module Octokit
309
309
  # @see https://developer.github.com/v3/users/emails/#add-email-addresses
310
310
  # @example
311
311
  # @client.add_email('new_email@user.com')
312
- def add_email(email, options = {})
312
+ def add_email(email, _options = {})
313
313
  email = Array(email)
314
- post "user/emails", email
314
+ post 'user/emails', email
315
315
  end
316
316
 
317
317
  # Remove email from user.
@@ -325,7 +325,7 @@ module Octokit
325
325
  # @client.remove_email('old_email@user.com')
326
326
  def remove_email(email)
327
327
  email = Array(email)
328
- boolean_from_response :delete, "user/emails", email
328
+ boolean_from_response :delete, 'user/emails', email
329
329
  end
330
330
 
331
331
  # List repositories being watched by a user.
@@ -335,10 +335,10 @@ module Octokit
335
335
  # @see https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
336
336
  # @example
337
337
  # @client.subscriptions("pengwynn")
338
- def subscriptions(user=login, options = {})
338
+ def subscriptions(user = login, options = {})
339
339
  paginate user_path(user, 'subscriptions'), options
340
340
  end
341
- alias :watched :subscriptions
341
+ alias watched subscriptions
342
342
 
343
343
  # Initiates the generation of a migration archive.
344
344
  #
@@ -352,9 +352,8 @@ module Octokit
352
352
  # @client.start_migration(['octocat/hello-world'])
353
353
  # @see https://docs.github.com/en/rest/reference/migrations#start-a-user-migration
354
354
  def start_user_migration(repositories, options = {})
355
- options = ensure_api_media_type(:migrations, options)
356
355
  options[:repositories] = repositories
357
- post "user/migrations", options
356
+ post 'user/migrations', options
358
357
  end
359
358
 
360
359
  # Lists the most recent migrations.
@@ -364,8 +363,7 @@ module Octokit
364
363
  # @return [Array<Sawyer::Resource>] Array of migration resources.
365
364
  # @see https://docs.github.com/en/rest/reference/migrations#list-user-migrations
366
365
  def user_migrations(options = {})
367
- options = ensure_api_media_type(:migrations, options)
368
- paginate "user/migrations", options
366
+ paginate 'user/migrations', options
369
367
  end
370
368
 
371
369
  # Fetches the status of a migration.
@@ -375,7 +373,6 @@ module Octokit
375
373
  # @param id [Integer] ID number of the migration.
376
374
  # @see https://docs.github.com/en/rest/reference/migrations#get-a-user-migration-status
377
375
  def user_migration_status(id, options = {})
378
- options = ensure_api_media_type(:migrations, options)
379
376
  get "user/migrations/#{id}", options
380
377
  end
381
378
 
@@ -386,7 +383,6 @@ module Octokit
386
383
  # @param id [Integer] ID number of the migration.
387
384
  # @see https://docs.github.com/en/rest/reference/migrations#download-a-user-migration-archive
388
385
  def user_migration_archive_url(id, options = {})
389
- options = ensure_api_media_type(:migrations, options)
390
386
  url = "user/migrations/#{id}/archive"
391
387
 
392
388
  response = client_without_redirects(options).get(url)
@@ -400,7 +396,6 @@ module Octokit
400
396
  # @param id [Integer] ID number of the migration.
401
397
  # @see https://docs.github.com/en/rest/reference/migrations#delete-a-user-migration-archive
402
398
  def delete_user_migration_archive(id, options = {})
403
- options = ensure_api_media_type(:migrations, options)
404
399
  delete "user/migrations/#{id}/archive", options
405
400
  end
406
401
 
@@ -411,7 +406,6 @@ module Octokit
411
406
  # @param id [Integer] ID number of the migration.
412
407
  # @see https://docs.github.com/en/rest/reference/migrations#list-repositories-for-a-user-migration
413
408
  def user_migration_repositories(id, options = {})
414
- options = ensure_api_media_type(:migrations, options)
415
409
  get "user/migrations/#{id}/repositories", options
416
410
  end
417
411
 
@@ -423,12 +417,12 @@ module Octokit
423
417
  # @param repo [String] Name of the repository.
424
418
  # @see https://docs.github.com/en/rest/reference/migrations#unlock-a-user-repository
425
419
  def unlock_user_repository(id, repo, options = {})
426
- options = ensure_api_media_type(:migrations, options)
427
420
  delete "user/migrations/#{id}/repos/#{repo}/lock", options
428
421
  end
429
422
  end
430
423
 
431
424
  private
425
+
432
426
  # convenience method for constructing a user specific path, if the user is logged in
433
427
  def user_path(user, path)
434
428
  if user == login && user_authenticated?
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'octokit/connection'
2
4
  require 'octokit/warnable'
3
5
  require 'octokit/arguments'
@@ -9,13 +11,15 @@ require 'octokit/rate_limit'
9
11
  require 'octokit/repository'
10
12
  require 'octokit/user'
11
13
  require 'octokit/organization'
12
- require 'octokit/preview'
14
+ require 'octokit/client/actions_artifacts'
13
15
  require 'octokit/client/actions_secrets'
14
16
  require 'octokit/client/actions_workflows'
17
+ require 'octokit/client/actions_workflow_jobs'
15
18
  require 'octokit/client/actions_workflow_runs'
16
19
  require 'octokit/client/apps'
17
- require 'octokit/client/authorizations'
18
20
  require 'octokit/client/checks'
21
+ require 'octokit/client/code_scanning'
22
+ require 'octokit/client/codespaces_secrets'
19
23
  require 'octokit/client/commits'
20
24
  require 'octokit/client/commit_comments'
21
25
  require 'octokit/client/commit_pulls'
@@ -23,7 +27,9 @@ require 'octokit/client/commit_branches'
23
27
  require 'octokit/client/community_profile'
24
28
  require 'octokit/client/contents'
25
29
  require 'octokit/client/downloads'
30
+ require 'octokit/client/dependabot_secrets'
26
31
  require 'octokit/client/deployments'
32
+ require 'octokit/client/environments'
27
33
  require 'octokit/client/emojis'
28
34
  require 'octokit/client/events'
29
35
  require 'octokit/client/feeds'
@@ -59,33 +65,35 @@ require 'octokit/client/service_status'
59
65
  require 'octokit/client/source_import'
60
66
  require 'octokit/client/stats'
61
67
  require 'octokit/client/statuses'
68
+ require 'octokit/client/tokens'
62
69
  require 'octokit/client/traffic'
63
70
  require 'octokit/client/users'
64
71
  require 'ext/sawyer/relation'
65
72
 
66
73
  module Octokit
67
-
68
74
  # Client for the GitHub API
69
75
  #
70
76
  # @see https://developer.github.com
71
77
  class Client
72
-
73
78
  include Octokit::Authentication
74
79
  include Octokit::Configurable
75
80
  include Octokit::Connection
76
- include Octokit::Preview
77
81
  include Octokit::Warnable
82
+ include Octokit::Client::ActionsArtifacts
78
83
  include Octokit::Client::ActionsSecrets
79
- include Octokit::Client::Authorizations
80
84
  include Octokit::Client::Checks
85
+ include Octokit::Client::CodeScanning
86
+ include Octokit::Client::CodespacesSecrets
81
87
  include Octokit::Client::Commits
82
88
  include Octokit::Client::CommitComments
83
89
  include Octokit::Client::CommitPulls
84
90
  include Octokit::Client::CommitBranches
85
91
  include Octokit::Client::CommunityProfile
86
92
  include Octokit::Client::Contents
93
+ include Octokit::Client::DependabotSecrets
87
94
  include Octokit::Client::Deployments
88
95
  include Octokit::Client::Downloads
96
+ include Octokit::Client::Environments
89
97
  include Octokit::Client::Emojis
90
98
  include Octokit::Client::Events
91
99
  include Octokit::Client::Feeds
@@ -93,6 +101,7 @@ module Octokit
93
101
  include Octokit::Client::Gitignore
94
102
  include Octokit::Client::Hooks
95
103
  include Octokit::Client::ActionsWorkflows
104
+ include Octokit::Client::ActionsWorkflowJobs
96
105
  include Octokit::Client::ActionsWorkflowRuns
97
106
  include Octokit::Client::Apps
98
107
  include Octokit::Client::Issues
@@ -124,16 +133,24 @@ module Octokit
124
133
  include Octokit::Client::SourceImport
125
134
  include Octokit::Client::Stats
126
135
  include Octokit::Client::Statuses
136
+ include Octokit::Client::Tokens
127
137
  include Octokit::Client::Traffic
128
138
  include Octokit::Client::Users
129
139
 
130
140
  # Header keys that can be passed in options hash to {#get},{#head}
131
- CONVENIENCE_HEADERS = Set.new([:accept, :content_type])
141
+ CONVENIENCE_HEADERS = Set.new(%i[accept content_type])
132
142
 
133
143
  def initialize(options = {})
134
144
  # Use options passed in, but fall back to module defaults
145
+ #
146
+ # rubocop:disable Style/HashEachMethods
147
+ #
148
+ # This may look like a `.keys.each` which should be replaced with `#each_key`, but
149
+ # this doesn't actually work, since `#keys` is just a method we've defined ourselves.
150
+ # The class doesn't fulfill the whole `Enumerable` contract.
135
151
  Octokit::Configurable.keys.each do |key|
136
- value = options.key?(key) ? options[key] : Octokit.instance_variable_get(:"@#{key}")
152
+ # rubocop:enable Style/HashEachMethods
153
+ value = options[key].nil? ? Octokit.instance_variable_get(:"@#{key}") : options[key]
137
154
  instance_variable_set(:"@#{key}", value)
138
155
  end
139
156
 
@@ -148,11 +165,17 @@ module Octokit
148
165
 
149
166
  # mask password
150
167
  inspected.gsub! @password, '*******' if @password
151
- inspected.gsub! @management_console_password, '*******' if @management_console_password
168
+ if @management_console_password
169
+ inspected.gsub! @management_console_password, '*******'
170
+ end
152
171
  inspected.gsub! @bearer_token, '********' if @bearer_token
153
172
  # Only show last 4 of token, secret
154
- inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}" if @access_token
155
- inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}" if @client_secret
173
+ if @access_token
174
+ inspected.gsub! @access_token, "#{'*' * 36}#{@access_token[36..]}"
175
+ end
176
+ if @client_secret
177
+ inspected.gsub! @client_secret, "#{'*' * 36}#{@client_secret[36..]}"
178
+ end
156
179
 
157
180
  inspected
158
181
  end
@@ -170,11 +193,12 @@ module Octokit
170
193
  # # GET https://foo:bar@api.github.com/
171
194
  # client.get "/"
172
195
  # end
173
- def as_app(key = client_id, secret = client_secret, &block)
196
+ def as_app(key = client_id, secret = client_secret)
174
197
  if key.to_s.empty? || secret.to_s.empty?
175
- raise ApplicationCredentialsRequired, "client_id and client_secret required"
198
+ raise ApplicationCredentialsRequired, 'client_id and client_secret required'
176
199
  end
177
- app_client = self.dup
200
+
201
+ app_client = dup
178
202
  app_client.client_id = app_client.client_secret = nil
179
203
  app_client.login = key
180
204
  app_client.password = secret
@@ -235,10 +259,10 @@ module Octokit
235
259
  conn_opts[:url] = @api_endpoint
236
260
  conn_opts[:builder] = @middleware.dup if @middleware
237
261
  conn_opts[:proxy] = @proxy if @proxy
238
- conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
262
+ conn_opts[:ssl] = { verify_mode: @ssl_verify_mode } if @ssl_verify_mode
239
263
  conn = Faraday.new(conn_opts) do |http|
240
264
  if basic_authenticated?
241
- http.request :basic_auth, @login, @password
265
+ http.request(*FARADAY_BASIC_AUTH_KEYS, @login, @password)
242
266
  elsif token_authenticated?
243
267
  http.request :authorization, 'token', @access_token
244
268
  elsif bearer_authenticated?
@@ -1,5 +1,6 @@
1
- module Octokit
1
+ # frozen_string_literal: true
2
2
 
3
+ module Octokit
3
4
  # Configuration options for {Client}, defaulting to values
4
5
  # in {Default}
5
6
  module Configurable
@@ -61,31 +62,30 @@ module Octokit
61
62
  :management_console_endpoint, :management_console_password
62
63
 
63
64
  class << self
64
-
65
65
  # List of configurable keys for {Octokit::Client}
66
66
  # @return [Array] of option keys
67
67
  def keys
68
- @keys ||= [
69
- :access_token,
70
- :api_endpoint,
71
- :auto_paginate,
72
- :bearer_token,
73
- :client_id,
74
- :client_secret,
75
- :connection_options,
76
- :default_media_type,
77
- :login,
78
- :management_console_endpoint,
79
- :management_console_password,
80
- :middleware,
81
- :netrc,
82
- :netrc_file,
83
- :per_page,
84
- :password,
85
- :proxy,
86
- :ssl_verify_mode,
87
- :user_agent,
88
- :web_endpoint
68
+ @keys ||= %i[
69
+ access_token
70
+ api_endpoint
71
+ auto_paginate
72
+ bearer_token
73
+ client_id
74
+ client_secret
75
+ connection_options
76
+ default_media_type
77
+ login
78
+ management_console_endpoint
79
+ management_console_password
80
+ middleware
81
+ netrc
82
+ netrc_file
83
+ per_page
84
+ password
85
+ proxy
86
+ ssl_verify_mode
87
+ user_agent
88
+ web_endpoint
89
89
  ]
90
90
  end
91
91
  end
@@ -97,7 +97,13 @@ module Octokit
97
97
 
98
98
  # Reset configuration options to default values
99
99
  def reset!
100
+ # rubocop:disable Style/HashEachMethods
101
+ #
102
+ # This may look like a `.keys.each` which should be replaced with `#each_key`, but
103
+ # this doesn't actually work, since `#keys` is just a method we've defined ourselves.
104
+ # The class doesn't fulfill the whole `Enumerable` contract.
100
105
  Octokit::Configurable.keys.each do |key|
106
+ # rubocop:enable Style/HashEachMethods
101
107
  instance_variable_set(:"@#{key}", Octokit::Default.options[key])
102
108
  end
103
109
  self
@@ -113,24 +119,22 @@ module Octokit
113
119
  end
114
120
 
115
121
  def api_endpoint
116
- File.join(@api_endpoint, "")
122
+ File.join(@api_endpoint, '')
117
123
  end
118
124
 
119
125
  def management_console_endpoint
120
- File.join(@management_console_endpoint, "")
126
+ File.join(@management_console_endpoint, '')
121
127
  end
122
128
 
123
129
  # Base URL for generated web URLs
124
130
  #
125
131
  # @return [String] Default: https://github.com/
126
132
  def web_endpoint
127
- File.join(@web_endpoint, "")
133
+ File.join(@web_endpoint, '')
128
134
  end
129
135
 
130
136
  def login
131
- @login ||= begin
132
- user.login if token_authenticated?
133
- end
137
+ @login ||= (user.login if token_authenticated?)
134
138
  end
135
139
 
136
140
  def netrc?
@@ -140,7 +144,7 @@ module Octokit
140
144
  private
141
145
 
142
146
  def options
143
- Hash[Octokit::Configurable.keys.map{|key| [key, instance_variable_get(:"@#{key}")]}]
147
+ Octokit::Configurable.keys.to_h { |key| [key, instance_variable_get(:"@#{key}")] }
144
148
  end
145
149
 
146
150
  def fetch_client_id_and_secret(overrides = {})
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sawyer'
2
4
  require 'octokit/authentication'
3
5
  module Octokit
4
-
5
6
  # Network layer for API clients.
6
7
  module Connection
7
-
8
8
  include Octokit::Authentication
9
9
 
10
10
  # Header keys that can be passed in options hash to {#get},{#head}
11
- CONVENIENCE_HEADERS = Set.new([:accept, :content_type])
11
+ CONVENIENCE_HEADERS = Set.new(%i[accept content_type])
12
12
 
13
13
  # Make a HTTP GET request
14
14
  #
@@ -75,17 +75,17 @@ module Octokit
75
75
  # contains the contents of the requests so far and the second parameter
76
76
  # contains the latest response.
77
77
  # @return [Sawyer::Resource]
78
- def paginate(url, options = {}, &block)
78
+ def paginate(url, options = {})
79
79
  opts = parse_query_and_convenience_headers(options)
80
80
  if @auto_paginate || @per_page
81
- opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil)
81
+ opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil)
82
82
  end
83
83
 
84
84
  data = request(:get, url, opts.dup)
85
85
 
86
86
  if @auto_paginate
87
87
  while @last_response.rels[:next] && rate_limit.remaining > 0
88
- @last_response = @last_response.rels[:next].get(:headers => opts[:headers])
88
+ @last_response = @last_response.rels[:next].get(headers: opts[:headers])
89
89
  if block_given?
90
90
  yield(data, @last_response)
91
91
  else
@@ -104,16 +104,16 @@ module Octokit
104
104
  def agent
105
105
  @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http|
106
106
  http.headers[:accept] = default_media_type
107
- http.headers[:content_type] = "application/json"
107
+ http.headers[:content_type] = 'application/json'
108
108
  http.headers[:user_agent] = user_agent
109
109
  if basic_authenticated?
110
- http.request :basic_auth, @login, @password
110
+ http.request(*FARADAY_BASIC_AUTH_KEYS, @login, @password)
111
111
  elsif token_authenticated?
112
112
  http.request :authorization, 'token', @access_token
113
113
  elsif bearer_authenticated?
114
114
  http.request :authorization, 'Bearer', @bearer_token
115
115
  elsif application_authenticated?
116
- http.request :basic_auth, @client_id, @client_secret
116
+ http.request(*FARADAY_BASIC_AUTH_KEYS, @client_id, @client_secret)
117
117
  end
118
118
  end
119
119
  end
@@ -122,7 +122,7 @@ module Octokit
122
122
  #
123
123
  # @return [Sawyer::Resource]
124
124
  def root
125
- get "/"
125
+ get '/'
126
126
  end
127
127
 
128
128
  # Response for last HTTP request
@@ -154,10 +154,10 @@ module Octokit
154
154
  end
155
155
 
156
156
  @last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, options)
157
- response.data
158
- rescue Octokit::Error => error
157
+ response_data_correctly_encoded(response)
158
+ rescue Octokit::Error => e
159
159
  @last_response = nil
160
- raise error
160
+ raise e
161
161
  end
162
162
 
163
163
  # Executes the request, checking if it was successful
@@ -170,21 +170,20 @@ module Octokit
170
170
  false
171
171
  end
172
172
 
173
-
174
173
  def sawyer_options
175
174
  opts = {
176
- :links_parser => Sawyer::LinkParsers::Simple.new
175
+ links_parser: Sawyer::LinkParsers::Simple.new
177
176
  }
178
177
  conn_opts = @connection_options
179
178
  conn_opts[:builder] = @middleware.dup if @middleware
180
179
  conn_opts[:proxy] = @proxy if @proxy
181
180
  if conn_opts[:ssl].nil?
182
- conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
181
+ conn_opts[:ssl] = { verify_mode: @ssl_verify_mode } if @ssl_verify_mode
183
182
  else
184
183
  verify = @connection_options[:ssl][:verify]
185
184
  conn_opts[:ssl] = {
186
- :verify => verify,
187
- :verify_mode => verify == false ? 0 : @ssl_verify_mode
185
+ verify: verify,
186
+ verify_mode: verify == false ? 0 : @ssl_verify_mode
188
187
  }
189
188
  end
190
189
  opts[:faraday] = Faraday.new(conn_opts)
@@ -194,18 +193,26 @@ module Octokit
194
193
 
195
194
  def parse_query_and_convenience_headers(options)
196
195
  options = options.dup
197
- headers = options.delete(:headers) { Hash.new }
196
+ headers = options.delete(:headers) { {} }
198
197
  CONVENIENCE_HEADERS.each do |h|
199
198
  if header = options.delete(h)
200
199
  headers[h] = header
201
200
  end
202
201
  end
203
202
  query = options.delete(:query)
204
- opts = {:query => options}
205
- opts[:query].merge!(query) if query && query.is_a?(Hash)
203
+ opts = { query: options }
204
+ opts[:query].merge!(query) if query.is_a?(Hash)
206
205
  opts[:headers] = headers unless headers.empty?
207
206
 
208
207
  opts
209
208
  end
209
+
210
+ def response_data_correctly_encoded(response)
211
+ content_type = response.headers.fetch('content-type', '')
212
+ return response.data unless content_type.include?('charset') && response.data.is_a?(String)
213
+
214
+ reported_encoding = content_type.match(/charset=([^ ]+)/)[1]
215
+ response.data.force_encoding(reported_encoding)
216
+ end
210
217
  end
211
218
  end