octokit 4.21.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +120 -96
  3. data/Rakefile +20 -14
  4. data/lib/ext/sawyer/relation.rb +4 -2
  5. data/lib/octokit/arguments.rb +3 -3
  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 +111 -8
  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 +63 -74
  13. data/lib/octokit/client/checks.rb +18 -9
  14. data/lib/octokit/client/code_scanning.rb +190 -0
  15. data/lib/octokit/client/codespaces_secrets.rb +108 -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 +31 -34
  20. data/lib/octokit/client/community_profile.rb +2 -3
  21. data/lib/octokit/client/contents.rb +19 -22
  22. data/lib/octokit/client/dependabot_secrets.rb +108 -0
  23. data/lib/octokit/client/deployments.rb +8 -8
  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 +58 -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 +1 -7
  42. data/lib/octokit/client/objects.rb +14 -14
  43. data/lib/octokit/client/organizations.rb +106 -62
  44. data/lib/octokit/client/pages.rb +5 -7
  45. data/lib/octokit/client/projects.rb +44 -64
  46. data/lib/octokit/client/pull_requests.rb +66 -45
  47. data/lib/octokit/client/rate_limit.rb +9 -11
  48. data/lib/octokit/client/reactions.rb +78 -16
  49. data/lib/octokit/client/refs.rb +15 -18
  50. data/lib/octokit/client/releases.rb +13 -13
  51. data/lib/octokit/client/repositories.rb +92 -69
  52. data/lib/octokit/client/repository_invitations.rb +3 -3
  53. data/lib/octokit/client/reviews.rb +8 -8
  54. data/lib/octokit/client/say.rb +4 -5
  55. data/lib/octokit/client/search.rb +24 -10
  56. data/lib/octokit/client/service_status.rb +19 -9
  57. data/lib/octokit/client/source_import.rb +7 -12
  58. data/lib/octokit/client/stats.rb +10 -9
  59. data/lib/octokit/client/statuses.rb +5 -5
  60. data/lib/octokit/client/tokens.rb +31 -0
  61. data/lib/octokit/client/traffic.rb +6 -11
  62. data/lib/octokit/client/users.rb +54 -33
  63. data/lib/octokit/client.rb +44 -20
  64. data/lib/octokit/configurable.rb +51 -31
  65. data/lib/octokit/connection.rb +33 -24
  66. data/lib/octokit/default.rb +66 -35
  67. data/lib/octokit/enterprise_admin_client/admin_stats.rb +13 -14
  68. data/lib/octokit/enterprise_admin_client/license.rb +3 -4
  69. data/lib/octokit/enterprise_admin_client/orgs.rb +3 -4
  70. data/lib/octokit/enterprise_admin_client/search_indexing.rb +7 -8
  71. data/lib/octokit/enterprise_admin_client/users.rb +5 -4
  72. data/lib/octokit/enterprise_admin_client.rb +9 -3
  73. data/lib/octokit/enterprise_management_console_client/management_console.rb +51 -33
  74. data/lib/octokit/enterprise_management_console_client.rb +8 -2
  75. data/lib/octokit/error.rb +53 -34
  76. data/lib/octokit/gist.rb +3 -4
  77. data/lib/octokit/manage_ghes_client/manage_ghes.rb +178 -0
  78. data/lib/octokit/manage_ghes_client.rb +64 -0
  79. data/lib/octokit/middleware/follow_redirects.rb +16 -12
  80. data/lib/octokit/organization.rb +3 -1
  81. data/lib/octokit/rate_limit.rb +8 -6
  82. data/lib/octokit/repo_arguments.rb +3 -4
  83. data/lib/octokit/repository.rb +32 -25
  84. data/lib/octokit/response/base_middleware.rb +10 -0
  85. data/lib/octokit/response/feed_parser.rb +5 -7
  86. data/lib/octokit/response/raise_error.rb +4 -4
  87. data/lib/octokit/user.rb +4 -2
  88. data/lib/octokit/version.rb +4 -2
  89. data/lib/octokit/warnable.rb +4 -5
  90. data/lib/octokit.rb +30 -8
  91. data/octokit.gemspec +12 -11
  92. metadata +22 -34
  93. data/lib/octokit/client/authorizations.rb +0 -182
  94. data/lib/octokit/client/pub_sub_hubbub.rb +0 -111
  95. data/lib/octokit/preview.rb +0 -46
@@ -1,34 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Octokit
2
4
  class EnterpriseManagementConsoleClient
3
-
4
5
  # Methods for the Enterprise Management Console API
5
6
  #
6
7
  # @see https://developer.github.com/v3/enterprise-admin/management_console/
7
8
  module ManagementConsole
8
-
9
9
  # Uploads a license for the first time
10
10
  #
11
11
  # @param license [String] The path to your .ghl license file.
12
12
  # @param settings [Hash] A hash configuration of the initial settings.
13
13
  #
14
- # @see http: //git.io/j5NT
14
+ # @see https://docs.github.com/en/enterprise-server@3.4/rest/enterprise-admin/management-console#create-a-github-license
15
15
  # @return nil
16
16
  def upload_license(license, settings = nil)
17
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
17
18
  conn = faraday_configuration
18
19
 
19
- params = { }
20
+ params = {}
20
21
  params[:license] = Faraday::UploadIO.new(license, 'binary')
21
22
  params[:password] = @management_console_password
22
- params[:settings] = "#{settings.to_json}" unless settings.nil?
23
+ params[:settings] = settings.to_json.to_s unless settings.nil?
23
24
 
24
- @last_response = conn.post("/setup/api/start", params)
25
+ @last_response = conn.post('/setup/api/start', params)
25
26
  end
26
27
 
27
28
  # Start a configuration process.
28
29
  #
29
30
  # @return nil
30
31
  def start_configuration
31
- post "/setup/api/configure", password_hash
32
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
33
+ post '/setup/api/configure', password_hash
32
34
  end
33
35
 
34
36
  # Upgrade an Enterprise installation
@@ -37,29 +39,32 @@ module Octokit
37
39
  #
38
40
  # @return nil
39
41
  def upgrade(license)
42
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
40
43
  conn = faraday_configuration
41
44
 
42
- params = { }
45
+ params = {}
43
46
  params[:license] = Faraday::UploadIO.new(license, 'binary')
44
47
  params[:api_key] = @management_console_password
45
- @last_response = conn.post("/setup/api/upgrade", params)
48
+ @last_response = conn.post('/setup/api/upgrade', params)
46
49
  end
47
50
 
48
51
  # Get information about the Enterprise installation
49
52
  #
50
53
  # @return [Sawyer::Resource] The installation information
51
54
  def config_status
52
- get "/setup/api/configcheck", password_hash
55
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
56
+ get '/setup/api/configcheck', password_hash
53
57
  end
54
- alias :config_check :config_status
58
+ alias config_check config_status
55
59
 
56
60
  # Get information about the Enterprise installation
57
61
  #
58
62
  # @return [Sawyer::Resource] The settings
59
63
  def settings
60
- get "/setup/api/settings", password_hash
64
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
65
+ get '/setup/api/settings', password_hash
61
66
  end
62
- alias :get_settings :settings
67
+ alias get_settings settings
63
68
 
64
69
  # Modify the Enterprise settings
65
70
  #
@@ -67,48 +72,53 @@ module Octokit
67
72
  #
68
73
  # @return [nil]
69
74
  def edit_settings(settings)
75
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
70
76
  queries = password_hash
71
- queries[:query][:settings] = "#{settings.to_json}"
72
- put "/setup/api/settings", queries
77
+ queries[:query][:settings] = settings.to_json.to_s
78
+ put '/setup/api/settings', queries
73
79
  end
74
80
 
75
81
  # Get information about the Enterprise maintenance status
76
82
  #
77
83
  # @return [Sawyer::Resource] The maintenance status
78
84
  def maintenance_status
79
- get "/setup/api/maintenance", password_hash
85
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
86
+ get '/setup/api/maintenance', password_hash
80
87
  end
81
- alias :get_maintenance_status :maintenance_status
88
+ alias get_maintenance_status maintenance_status
82
89
 
83
90
  # Start (or turn off) the Enterprise maintenance mode
84
91
  #
85
92
  # @param maintenance [Hash] A hash configuration of the maintenance settings
86
93
  # @return [nil]
87
94
  def set_maintenance_status(maintenance)
95
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
88
96
  queries = password_hash
89
- queries[:query][:maintenance] = "#{maintenance.to_json}"
90
- post "/setup/api/maintenance", queries
97
+ queries[:query][:maintenance] = maintenance.to_json.to_s
98
+ post '/setup/api/maintenance', queries
91
99
  end
92
- alias :edit_maintenance_status :set_maintenance_status
100
+ alias edit_maintenance_status set_maintenance_status
93
101
 
94
102
  # Fetch the authorized SSH keys on the Enterprise install
95
103
  #
96
104
  # @return [Sawyer::Resource] An array of authorized SSH keys
97
105
  def authorized_keys
98
- get "/setup/api/settings/authorized-keys", password_hash
106
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
107
+ get '/setup/api/settings/authorized-keys', password_hash
99
108
  end
100
- alias :get_authorized_keys :authorized_keys
109
+ alias get_authorized_keys authorized_keys
101
110
 
102
111
  # Add an authorized SSH keys on the Enterprise install
103
112
  #
104
113
  # @param key Either the file path to a key, a File handler to the key, or the contents of the key itself
105
114
  # @return [Sawyer::Resource] An array of authorized SSH keys
106
115
  def add_authorized_key(key)
116
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
107
117
  queries = password_hash
108
118
  case key
109
119
  when String
110
120
  if File.exist?(key)
111
- key = File.open(key, "r")
121
+ key = File.open(key, 'r')
112
122
  content = key.read.strip
113
123
  key.close
114
124
  else
@@ -120,7 +130,7 @@ module Octokit
120
130
  end
121
131
 
122
132
  queries[:query][:authorized_key] = content
123
- post "/setup/api/settings/authorized-keys", queries
133
+ post '/setup/api/settings/authorized-keys', queries
124
134
  end
125
135
 
126
136
  # Removes an authorized SSH keys from the Enterprise install
@@ -128,11 +138,12 @@ module Octokit
128
138
  # @param key Either the file path to a key, a File handler to the key, or the contents of the key itself
129
139
  # @return [Sawyer::Resource] An array of authorized SSH keys
130
140
  def remove_authorized_key(key)
141
+ octokit_warn('The Management Console API will be deprecated in GitHub Enterprise Server 3.15.0, please use the ManageGHES client instead.')
131
142
  queries = password_hash
132
143
  case key
133
144
  when String
134
145
  if File.exist?(key)
135
- key = File.open(key, "r")
146
+ key = File.open(key, 'r')
136
147
  content = key.read.strip
137
148
  key.close
138
149
  else
@@ -144,27 +155,34 @@ module Octokit
144
155
  end
145
156
 
146
157
  queries[:query][:authorized_key] = content
147
- delete "/setup/api/settings/authorized-keys", queries
158
+ delete '/setup/api/settings/authorized-keys', queries
148
159
  end
149
- alias :delete_authorized_key :remove_authorized_key
150
-
160
+ alias delete_authorized_key remove_authorized_key
151
161
  end
162
+
152
163
  private
153
164
 
154
165
  def password_hash
155
- { :query => { :api_key => @management_console_password } }
166
+ { query: { api_key: @management_console_password } }
156
167
  end
157
168
 
158
169
  # We fall back to raw Faraday for handling the licenses because I'm suspicious
159
- # that Sawyer isn't handling binary POSTs correctly: http://git.io/jMir
170
+ # that Sawyer isn't handling binary POSTs correctly: https://github.com/lostisland/sawyer/blob/03fca4c020f465ec42856d0486ec3991859b0aed/lib/sawyer/agent.rb#L85
160
171
  def faraday_configuration
161
- @faraday_configuration ||= Faraday.new(:url => @management_console_endpoint) do |http|
172
+ @faraday_configuration ||= Faraday.new(url: @management_console_endpoint) do |http|
162
173
  http.headers[:user_agent] = user_agent
163
- http.request :multipart
174
+ begin
175
+ http.request :multipart
176
+ rescue Faraday::Error
177
+ raise Faraday::Error, <<~ERROR
178
+ The `faraday-multipart` gem is required to upload a license.
179
+ Please add `gem "faraday-multipart"` to your Gemfile.
180
+ ERROR
181
+ end
164
182
  http.request :url_encoded
165
183
 
166
184
  # Disabling SSL is essential for certain self-hosted Enterprise instances
167
- if self.connection_options[:ssl] && !self.connection_options[:ssl][:verify]
185
+ if connection_options[:ssl] && !connection_options[:ssl][:verify]
168
186
  http.ssl[:verify] = false
169
187
  end
170
188
 
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'octokit/configurable'
2
4
  require 'octokit/connection'
3
5
  require 'octokit/warnable'
4
6
  require 'octokit/enterprise_management_console_client/management_console'
5
7
 
6
8
  module Octokit
7
-
8
9
  # EnterpriseManagementConsoleClient is only meant to be used by GitHub Enterprise Admins
9
10
  # and provides access to the management console API endpoints.
10
11
  #
@@ -12,7 +13,6 @@ module Octokit
12
13
  # and GitHub Enterprise.
13
14
  # @see https://developer.github.com/v3/enterprise-admin/management_console/
14
15
  class EnterpriseManagementConsoleClient
15
-
16
16
  include Octokit::Configurable
17
17
  include Octokit::Connection
18
18
  include Octokit::Warnable
@@ -20,7 +20,13 @@ module Octokit
20
20
 
21
21
  def initialize(options = {})
22
22
  # Use options passed in, but fall back to module defaults
23
+ # rubocop:disable Style/HashEachMethods
24
+ #
25
+ # This may look like a `.keys.each` which should be replaced with `#each_key`, but
26
+ # this doesn't actually work, since `#keys` is just a method we've defined ourselves.
27
+ # The class doesn't fulfill the whole `Enumerable` contract.
23
28
  Octokit::Configurable.keys.each do |key|
29
+ # rubocop:enable Style/HashEachMethods
24
30
  instance_variable_set(:"@#{key}", options[key] || Octokit.instance_variable_get(:"@#{key}"))
25
31
  end
26
32
  end
data/lib/octokit/error.rb CHANGED
@@ -1,12 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Octokit
2
4
  # Custom error class for rescuing from all GitHub errors
3
5
  class Error < StandardError
4
6
  attr_reader :context
7
+
5
8
  # Returns the appropriate Octokit::Error subclass based
6
9
  # on status and response message
7
10
  #
8
11
  # @param [Hash] response HTTP response
9
12
  # @return [Octokit::Error]
13
+ # rubocop:disable Metrics/CyclomaticComplexity
10
14
  def self.from_response(response)
11
15
  status = response[:status].to_i
12
16
  body = response[:body].to_s
@@ -20,6 +24,7 @@ module Octokit
20
24
  when 405 then Octokit::MethodNotAllowed
21
25
  when 406 then Octokit::NotAcceptable
22
26
  when 409 then Octokit::Conflict
27
+ when 410 then Octokit::Deprecated
23
28
  when 415 then Octokit::UnsupportedMediaType
24
29
  when 422 then error_for_422(body)
25
30
  when 451 then Octokit::UnavailableForLegalReasons
@@ -33,14 +38,15 @@ module Octokit
33
38
  klass.new(response)
34
39
  end
35
40
  end
41
+ # rubocop:enable Metrics/CyclomaticComplexity
36
42
 
37
- def build_error_context
38
- if RATE_LIMITED_ERRORS.include?(self.class)
39
- @context = Octokit::RateLimit.from_response(@response)
40
- end
41
- end
43
+ def build_error_context
44
+ if RATE_LIMITED_ERRORS.include?(self.class)
45
+ @context = Octokit::RateLimit.from_response(@response)
46
+ end
47
+ end
42
48
 
43
- def initialize(response=nil)
49
+ def initialize(response = nil)
44
50
  @response = response
45
51
  super(build_error_message)
46
52
  build_error_context
@@ -55,7 +61,9 @@ module Octokit
55
61
 
56
62
  # Returns most appropriate error for 401 HTTP status code
57
63
  # @private
64
+ # rubocop:disable Naming/VariableNumber
58
65
  def self.error_for_401(headers)
66
+ # rubocop:enbale Naming/VariableNumber
59
67
  if Octokit::OneTimePasswordRequired.required_header(headers)
60
68
  Octokit::OneTimePasswordRequired
61
69
  else
@@ -66,25 +74,27 @@ module Octokit
66
74
  # Returns most appropriate error for 403 HTTP status code
67
75
  # @private
68
76
  def self.error_for_403(body)
69
- if body =~ /rate limit exceeded/i
77
+ # rubocop:enable Naming/VariableNumber
78
+ case body
79
+ when /rate limit exceeded/i, /exceeded a secondary rate limit/i
70
80
  Octokit::TooManyRequests
71
- elsif body =~ /login attempts exceeded/i
81
+ when /login attempts exceeded/i
72
82
  Octokit::TooManyLoginAttempts
73
- elsif body =~ /returns blobs up to [0-9]+ MB/i
83
+ when /(returns|for) blobs (up to|between) [0-9-]+ MB/i
74
84
  Octokit::TooLargeContent
75
- elsif body =~ /abuse/i
85
+ when /abuse/i
76
86
  Octokit::AbuseDetected
77
- elsif body =~ /repository access blocked/i
87
+ when /repository access blocked/i
78
88
  Octokit::RepositoryUnavailable
79
- elsif body =~ /email address must be verified/i
89
+ when /email address must be verified/i
80
90
  Octokit::UnverifiedEmail
81
- elsif body =~ /account was suspended/i
91
+ when /account was suspended/i
82
92
  Octokit::AccountSuspended
83
- elsif body =~ /billing issue/i
93
+ when /billing issue/i
84
94
  Octokit::BillingIssue
85
- elsif body =~ /Resource protected by organization SAML enforcement/i
95
+ when /Resource protected by organization SAML enforcement/i
86
96
  Octokit::SAMLProtected
87
- elsif body =~ /suspended your access|This installation has been suspended/i
97
+ when /suspended your access|This installation has been suspended/i
88
98
  Octokit::InstallationSuspended
89
99
  else
90
100
  Octokit::Forbidden
@@ -93,7 +103,9 @@ module Octokit
93
103
 
94
104
  # Return most appropriate error for 404 HTTP status code
95
105
  # @private
106
+ # rubocop:disable Naming/VariableNumber
96
107
  def self.error_for_404(body)
108
+ # rubocop:enable Naming/VariableNumber
97
109
  if body =~ /Branch not protected/i
98
110
  Octokit::BranchNotProtected
99
111
  else
@@ -103,7 +115,9 @@ module Octokit
103
115
 
104
116
  # Return most appropriate error for 422 HTTP status code
105
117
  # @private
118
+ # rubocop:disable Naming/VariableNumber
106
119
  def self.error_for_422(body)
120
+ # rubocop:enable Naming/VariableNumber
107
121
  if body =~ /PullRequestReviewComment/i && body =~ /(commit_id|end_commit_oid) is not part of the pull request/i
108
122
  Octokit::CommitIsNotPartOfPullRequest
109
123
  elsif body =~ /Path diff too large/i
@@ -116,7 +130,7 @@ module Octokit
116
130
  # Array of validation errors
117
131
  # @return [Array<Hash>] Error info
118
132
  def errors
119
- if data && data.is_a?(Hash)
133
+ if data.is_a?(Hash)
120
134
  data[:errors] || []
121
135
  else
122
136
  []
@@ -150,15 +164,13 @@ module Octokit
150
164
  @data ||=
151
165
  if (body = @response[:body]) && !body.empty?
152
166
  if body.is_a?(String) &&
153
- @response[:response_headers] &&
154
- @response[:response_headers][:content_type] =~ /json/
167
+ @response[:response_headers] &&
168
+ @response[:response_headers][:content_type] =~ /json/
155
169
 
156
170
  Sawyer::Agent.serializer.decode(body)
157
171
  else
158
172
  body
159
173
  end
160
- else
161
- nil
162
174
  end
163
175
  end
164
176
 
@@ -178,10 +190,12 @@ module Octokit
178
190
  def response_error_summary
179
191
  return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty?
180
192
 
181
- summary = "\nError summary:\n"
193
+ summary = +"\nError summary:\n"
194
+ return summary << data[:errors] if data[:errors].is_a?(String)
195
+
182
196
  summary << data[:errors].map do |error|
183
197
  if error.is_a? Hash
184
- error.map { |k,v| " #{k}: #{v}" }
198
+ error.map { |k, v| " #{k}: #{v}" }
185
199
  else
186
200
  " #{error}"
187
201
  end
@@ -193,19 +207,21 @@ module Octokit
193
207
  def build_error_message
194
208
  return nil if @response.nil?
195
209
 
196
- message = "#{@response[:method].to_s.upcase} "
197
- message << redact_url(@response[:url].to_s) + ": "
210
+ message = +"#{@response[:method].to_s.upcase} "
211
+ message << "#{redact_url(@response[:url].to_s.dup)}: "
198
212
  message << "#{@response[:status]} - "
199
- message << "#{response_message}" unless response_message.nil?
200
- message << "#{response_error}" unless response_error.nil?
201
- message << "#{response_error_summary}" unless response_error_summary.nil?
213
+ message << response_message.to_s unless response_message.nil?
214
+ message << response_error.to_s unless response_error.nil?
215
+ message << response_error_summary.to_s unless response_error_summary.nil?
202
216
  message << " // See: #{documentation_url}" unless documentation_url.nil?
203
217
  message
204
218
  end
205
219
 
206
220
  def redact_url(url_string)
207
- %w[client_secret access_token].each do |token|
208
- url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
221
+ %w[client_secret access_token api_key].each do |token|
222
+ if url_string.include? token
223
+ url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)")
224
+ end
209
225
  end
210
226
  url_string
211
227
  end
@@ -223,10 +239,10 @@ module Octokit
223
239
  # Raised when GitHub returns a 401 HTTP status code
224
240
  # and headers include "X-GitHub-OTP"
225
241
  class OneTimePasswordRequired < ClientError
226
- #@private
227
- OTP_DELIVERY_PATTERN = /required; (\w+)/i
242
+ # @private
243
+ OTP_DELIVERY_PATTERN = /required; (\w+)/i.freeze
228
244
 
229
- #@private
245
+ # @private
230
246
  def self.required_header(headers)
231
247
  OTP_DELIVERY_PATTERN.match headers['X-GitHub-OTP'].to_s
232
248
  end
@@ -306,6 +322,9 @@ module Octokit
306
322
  # Raised when GitHub returns a 409 HTTP status code
307
323
  class Conflict < ClientError; end
308
324
 
325
+ # Raised when GHES Manage return a 410 HTTP status code
326
+ class Deprecated < ClientError; end
327
+
309
328
  # Raised when GitHub returns a 414 HTTP status code
310
329
  class UnsupportedMediaType < ClientError; end
311
330
 
@@ -348,5 +367,5 @@ module Octokit
348
367
  # Raised when a repository is created with an invalid format
349
368
  class InvalidRepository < ArgumentError; end
350
369
 
351
- RATE_LIMITED_ERRORS = [Octokit::TooManyRequests, Octokit::AbuseDetected]
370
+ RATE_LIMITED_ERRORS = [Octokit::TooManyRequests, Octokit::AbuseDetected].freeze
352
371
  end
data/lib/octokit/gist.rb CHANGED
@@ -1,8 +1,8 @@
1
- module Octokit
1
+ # frozen_string_literal: true
2
2
 
3
+ module Octokit
3
4
  # Class to parse and create Gist URLs
4
5
  class Gist
5
-
6
6
  # !@attribute id
7
7
  # @return [String] Gist ID
8
8
  attr_accessor :id
@@ -10,7 +10,7 @@ module Octokit
10
10
  # Instantiate {Gist} object from Gist URL
11
11
  # @ return [Gist]
12
12
  def self.from_url(url)
13
- Gist.new(URI.parse(url).path[1..-1])
13
+ Gist.new(URI.parse(url).path[1..])
14
14
  end
15
15
 
16
16
  def initialize(gist)
@@ -31,6 +31,5 @@ module Octokit
31
31
  def url
32
32
  "https://gist.github.com/#{@id}"
33
33
  end
34
-
35
34
  end
36
35
  end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Octokit
4
+ # Client for the Manage GitHub Enterprise Server API
5
+ class ManageGHESClient
6
+ # Methods for the Manage GitHub Enterprise Server API
7
+ #
8
+ # @see https://developer.github.com/v3/enterprise-admin/manage-ghes
9
+ module ManageAPI
10
+ # Get information about the maintenance status of the GHES instance
11
+ #
12
+ # @return [nil]
13
+ def maintenance_mode
14
+ conn = authenticated_client
15
+
16
+ @last_response = conn.get('/manage/v1/maintenance')
17
+ end
18
+
19
+ # Configure the maintenance mode of the GHES instance
20
+ #
21
+ # @param maintenance [Hash] A hash configuration of the maintenance mode status
22
+ # @return [nil]
23
+ def set_maintenance_mode(enabled, options = {})
24
+ conn = authenticated_client
25
+
26
+ options[:enabled] = enabled
27
+ @last_response = conn.post('/manage/v1/maintenance', options)
28
+ end
29
+ alias configure_maintenance_mode set_maintenance_mode
30
+ end
31
+
32
+ # Uploads a license for the first time
33
+ #
34
+ # @param license [String] The path to your .ghl license file.
35
+ #
36
+ # @return [nil]
37
+ def upload_license(license)
38
+ conn = authenticated_client
39
+ begin
40
+ conn.request :multipart
41
+ rescue Faraday::Error
42
+ raise Faraday::Error, <<~ERROR
43
+ The `faraday-multipart` gem is required to upload a license.
44
+ Please add `gem "faraday-multipart"` to your Gemfile.
45
+ ERROR
46
+ end
47
+ params = {}
48
+ params[:license] = Faraday::FilePart.new(license, 'binary')
49
+ params[:password] = @manage_ghes_password
50
+ @last_response = conn.post('/manage/v1/config/init', params, { 'Content-Type' => 'multipart/form-data' })
51
+ end
52
+
53
+ # Start a configuration process.
54
+ #
55
+ # @return [nil]
56
+ def start_configuration
57
+ conn = authenticated_client
58
+ @last_response = conn.post('/manage/v1/config/apply')
59
+ end
60
+
61
+ # Get information about the Enterprise installation
62
+ #
63
+ # @return [nil]
64
+ def config_status
65
+ conn = authenticated_client
66
+ @last_response = conn.get('/manage/v1/config/apply')
67
+ end
68
+ alias config_check config_status
69
+
70
+ # Get information about the Enterprise installation
71
+ #
72
+ # @return [nil]
73
+ def settings
74
+ conn = authenticated_client
75
+ @last_response = conn.get('/manage/v1/config/settings')
76
+ end
77
+ alias get_settings settings
78
+
79
+ # Modify the Enterprise settings
80
+ #
81
+ # @param settings [Hash] A hash configuration of the new settings
82
+ #
83
+ # @return [nil]
84
+ def edit_settings(settings)
85
+ conn = authenticated_client
86
+ @last_response = conn.put('/manage/v1/config/settings', settings.to_json.to_s)
87
+ end
88
+
89
+ def authorized_keys
90
+ conn = authenticated_client
91
+ @last_response = conn.get('/manage/v1/access/ssh')
92
+ end
93
+ alias get_authorized_keys authorized_keys
94
+
95
+ # Add an authorized SSH keys on the Enterprise install
96
+ #
97
+ # @param key Either the file path to a key, a File handler to the key, or the contents of the key itself
98
+ # @return [nil]
99
+ def add_authorized_key(key)
100
+ conn = authenticated_client
101
+ case key
102
+ when String
103
+ if File.exist?(key)
104
+ key = File.open(key, 'r')
105
+ content = key.read.strip
106
+ key.close
107
+ else
108
+ content = key
109
+ end
110
+ when File
111
+ content = key.read.strip
112
+ key.close
113
+ end
114
+
115
+ queries = {}
116
+ queries[:key] = content
117
+ @last_response = conn.post('/manage/v1/access/ssh', queries)
118
+ end
119
+
120
+ # Removes an authorized SSH keys from the Enterprise install
121
+ #
122
+ # @param key Either the file path to a key, a File handler to the key, or the contents of the key itself
123
+ # @return [nil]
124
+ def remove_authorized_key(key)
125
+ conn = authenticated_client
126
+ case key
127
+ when String
128
+ if File.exist?(key)
129
+ key = File.open(key, 'r')
130
+ content = key.read.strip
131
+ key.close
132
+ else
133
+ content = key
134
+ end
135
+ when File
136
+ content = key.read.strip
137
+ key.close
138
+ end
139
+
140
+ queries = {}
141
+ queries[:key] = content
142
+ @last_response = conn.run_request(:delete, '/manage/v1/access/ssh', queries, nil)
143
+ end
144
+ alias delete_authorized_key remove_authorized_key
145
+
146
+ private
147
+
148
+ def basic_authenticated?
149
+ !!(@manage_ghes_username && @manage_ghes_password)
150
+ end
151
+
152
+ # If no username is provided, we assume root site admin should be used
153
+ def root_site_admin_assumed?
154
+ !@manage_ghes_username
155
+ end
156
+
157
+ def authenticated_client
158
+ @authenticated_client ||= Faraday.new(url: @manage_ghes_endpoint) do |c|
159
+ c.headers[:user_agent] = user_agent
160
+ c.request :json
161
+ c.response :json
162
+ c.adapter Faraday.default_adapter
163
+
164
+ if root_site_admin_assumed?
165
+ username = 'api_key'
166
+ elsif basic_authenticated?
167
+ username = @manage_ghes_username
168
+ end
169
+ c.request(*FARADAY_BASIC_AUTH_KEYS, username, @manage_ghes_password)
170
+
171
+ # Disabling SSL is essential for certain self-hosted Enterprise instances
172
+ c.ssl[:verify] = false if connection_options[:ssl] && !connection_options[:ssl][:verify]
173
+
174
+ c.use Octokit::Response::RaiseError
175
+ end
176
+ end
177
+ end
178
+ end