github_api 0.10.2 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/README.md +78 -59
  2. data/features/cassettes/repos/assets/delete.yml +52 -0
  3. data/features/cassettes/repos/assets/edit.yml +65 -0
  4. data/features/cassettes/repos/assets/get.yml +74 -0
  5. data/features/cassettes/repos/assets/list.yml +73 -0
  6. data/features/cassettes/repos/assets/upload.yml +66 -0
  7. data/features/cassettes/repos/list_org.yml +167 -0
  8. data/features/cassettes/repos/list_org_instance.yml +300 -0
  9. data/features/cassettes/repos/releases/create.yml +64 -0
  10. data/features/cassettes/repos/releases/delete.yml +52 -0
  11. data/features/cassettes/repos/releases/edit.yml +63 -0
  12. data/features/cassettes/repos/releases/get.yml +78 -0
  13. data/features/cassettes/repos/releases/list.yml +81 -0
  14. data/features/cassettes/search/code.yml +252 -0
  15. data/features/cassettes/search/code_query.yml +65 -0
  16. data/features/cassettes/search/issues.yml +616 -170
  17. data/features/cassettes/search/legacy/email.yml +72 -0
  18. data/features/cassettes/search/legacy/issues.yml +119 -0
  19. data/features/cassettes/search/legacy/repos.yml +394 -0
  20. data/features/cassettes/search/legacy/users.yml +72 -0
  21. data/features/cassettes/search/legacy/users_keyword.yml +63 -0
  22. data/features/cassettes/search/repos.yml +375 -364
  23. data/features/cassettes/search/users.yml +40 -22
  24. data/features/cassettes/search/users_keyword.yml +51 -56
  25. data/features/repos.feature +22 -0
  26. data/features/repos/assets.feature +59 -0
  27. data/features/repos/releases.feature +58 -0
  28. data/features/search.feature +24 -13
  29. data/features/search/legacy.feature +59 -0
  30. data/features/settings.yml +2 -5
  31. data/features/step_definitions/github_api_steps.rb +12 -0
  32. data/lib/github_api/configuration.rb +3 -1
  33. data/lib/github_api/error.rb +6 -17
  34. data/lib/github_api/error/unknown_media.rb +18 -0
  35. data/lib/github_api/params_hash.rb +20 -10
  36. data/lib/github_api/repos.rb +26 -15
  37. data/lib/github_api/repos/releases.rb +146 -0
  38. data/lib/github_api/repos/releases/assets.rb +126 -0
  39. data/lib/github_api/request.rb +14 -9
  40. data/lib/github_api/resource.rb +8 -1
  41. data/lib/github_api/search.rb +68 -27
  42. data/lib/github_api/search/legacy.rb +94 -0
  43. data/lib/github_api/ssl_certs/cacert.pem +41 -0
  44. data/lib/github_api/ssl_certs/cacerts.pem +2183 -0
  45. data/lib/github_api/version.rb +2 -2
  46. data/spec/fixtures/repos/asset.json +12 -0
  47. data/spec/fixtures/repos/assets.json +14 -0
  48. data/spec/fixtures/repos/release.json +15 -0
  49. data/spec/fixtures/repos/releases.json +17 -0
  50. data/spec/fixtures/search/code.json +76 -0
  51. data/spec/fixtures/search/issues.json +47 -16
  52. data/spec/fixtures/search/issues_legacy.json +23 -0
  53. data/spec/fixtures/search/repos.json +39 -0
  54. data/spec/fixtures/search/{repositories.json → repos_legacy.json} +0 -0
  55. data/spec/fixtures/search/users.json +15 -19
  56. data/spec/fixtures/search/users_legacy.json +24 -0
  57. data/spec/github/configuration_spec.rb +2 -0
  58. data/spec/github/error/unknown_media_spec.rb +21 -0
  59. data/spec/github/git_data/references/create_spec.rb +1 -1
  60. data/spec/github/git_data/references/get_spec.rb +6 -4
  61. data/spec/github/git_data/references/list_spec.rb +6 -16
  62. data/spec/github/pagination/iterator/number_spec.rb +2 -2
  63. data/spec/github/pagination/iterator/sha_spec.rb +2 -2
  64. data/spec/github/params_hash_spec.rb +6 -0
  65. data/spec/github/repos/assets/delete_spec.rb +40 -0
  66. data/spec/github/repos/assets/edit_spec.rb +40 -0
  67. data/spec/github/repos/assets/get_spec.rb +45 -0
  68. data/spec/github/repos/assets/list_spec.rb +55 -0
  69. data/spec/github/repos/assets/upload_spec.rb +44 -0
  70. data/spec/github/repos/downloads/upload_spec.rb +5 -5
  71. data/spec/github/repos/releases/create_spec.rb +44 -0
  72. data/spec/github/repos/releases/delete_spec.rb +40 -0
  73. data/spec/github/repos/releases/edit_spec.rb +40 -0
  74. data/spec/github/repos/releases/get_spec.rb +45 -0
  75. data/spec/github/repos/releases/list_spec.rb +54 -0
  76. data/spec/github/request/endpoint_spec.rb +1 -3
  77. data/spec/github/request/request_spec.rb +4 -8
  78. data/spec/github/request_spec.rb +3 -3
  79. data/spec/github/response_wrapper_spec.rb +3 -3
  80. data/spec/github/s3_uploader_spec.rb +2 -3
  81. data/spec/github/search/code_spec.rb +38 -0
  82. data/spec/github/search/issues_spec.rb +38 -0
  83. data/spec/github/search/legacy/email_spec.rb +34 -0
  84. data/spec/github/search/legacy/issues_spec.rb +39 -0
  85. data/spec/github/search/legacy/repos_spec.rb +36 -0
  86. data/spec/github/search/legacy/users_spec.rb +36 -0
  87. data/spec/github/search/repos_spec.rb +38 -0
  88. data/spec/github/search/users_spec.rb +38 -0
  89. data/spec/github/users/get_spec.rb +1 -1
  90. data/spec/github/validations/presence_spec.rb +2 -5
  91. data/spec/integration/repos_spec.rb +4 -0
  92. data/spec/integration/search_spec.rb +12 -0
  93. metadata +122 -62
  94. data/features/cassettes/search/email.yml +0 -99
  95. data/spec/github/search_spec.rb +0 -102
@@ -1,7 +1,4 @@
1
1
  ---
2
- oauth_token: 08e1475aa919c41b1b3c9d60e7808d252e559d47
3
- basic_auth: murek:Testing123
2
+ oauth_token: token
3
+ basic_auth: Murek:Testing123
4
4
  user: murek
5
- repo: github_api_test
6
- org:
7
- email: p_murek@wp.pl
@@ -12,6 +12,18 @@ Given /^I have "([^"]*)" instance$/ do |api_classes|
12
12
  )
13
13
  end
14
14
 
15
+ Given /^I do not verify ssl$/ do
16
+ @instance.ssl = {:verify => false}
17
+ end
18
+
19
+ Given /^I set the following (?:attribute|attributes) of instance:$/ do |table|
20
+ table.hashes.each do |element|
21
+ element.each do |attr, val|
22
+ @instance.send(:"#{attr}=", val)
23
+ end
24
+ end
25
+ end
26
+
15
27
  When /^I am not authorized$/ do
16
28
  [:basic_auth, :login, :password, :oauth_token].each do |attr|
17
29
  @instance.send("#{attr}=", nil)
@@ -51,7 +51,9 @@ module Github
51
51
  DEFAULT_SITE = 'https://github.com'.freeze
52
52
 
53
53
  # The default SSL configuration
54
- DEFAULT_SSL = {}
54
+ DEFAULT_SSL = {
55
+ :ca_file => File.expand_path('../ssl_certs/cacerts.pem', __FILE__)
56
+ }
55
57
 
56
58
  # The value sent in the http header for 'User-Agent' if none is set
57
59
  DEFAULT_USER_AGENT = "Github Ruby Gem #{Github::VERSION::STRING}".freeze
@@ -28,21 +28,10 @@ module Github
28
28
  end # Error
29
29
  end # Github
30
30
 
31
- %w[
32
- service_error
33
- bad_request
34
- unauthorized
35
- forbidden
36
- not_found
37
- not_acceptable
38
- unprocessable_entity
39
- internal_server_error
40
- service_unavailable
41
- client_error
42
- invalid_options
43
- required_params
44
- unknown_value
45
- validations
46
- ].each do |error|
47
- require "github_api/error/#{error}"
31
+ require 'github_api/error/service_error'
32
+ require 'github_api/error/client_error'
33
+ Dir[File.dirname(__FILE__) + '/error/*.rb'].sort.each do |path|
34
+ filename = File.basename(path)
35
+ next if ['service_error.rb', 'client_error.rb'].include?(filename)
36
+ require "github_api/error/#{filename}"
48
37
  end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ module Github #:nodoc
4
+ # Raised when invalid options are passed to a request body
5
+ module Error
6
+ class UnknownMedia < ClientError
7
+ def initialize(file)
8
+ super(
9
+ generate_message(
10
+ :problem => "Unknown content type for: '#{file}' provided for this request.",
11
+ :summary => "Github gem infers the content type of the resource by calling the mime-types gem type inference.",
12
+ :resolution => "Please install mime-types gem to infer the resource content type."
13
+ )
14
+ )
15
+ end
16
+ end
17
+ end # Error
18
+ end # Github
@@ -14,19 +14,25 @@ module Github
14
14
  super(normalize!(Hash[hash]))
15
15
  end
16
16
 
17
+ # Create empty hash
18
+ #
19
+ def self.empty
20
+ new({})
21
+ end
22
+
17
23
  # Extract and parse media type param
18
24
  #
19
25
  # [.version].param[+json]
20
26
  #
21
27
  def media
22
- parse(self.delete('media'))
28
+ parse(delete('media'))
23
29
  end
24
30
 
25
31
  # Return accept header if present
26
32
  #
27
33
  def accept
28
34
  if has_key?('accept')
29
- self.delete('accept')
35
+ delete('accept')
30
36
  elsif has_key?('media')
31
37
  media
32
38
  else
@@ -34,26 +40,30 @@ module Github
34
40
  end
35
41
  end
36
42
 
37
- # Extract request data from paramters
43
+ # Extract request data from parameters
38
44
  #
39
45
  def data
40
46
  if has_key?('data') && !self['data'].nil?
41
- return self.delete('data')
47
+ return delete('data')
42
48
  else
43
- return self.to_hash
49
+ return to_hash
44
50
  end
45
51
  end
46
52
 
47
53
  # Any client configuration options
48
54
  #
49
55
  def options
50
- hash = has_key?('options') ? self.delete('options') : {}
56
+ opts = has_key?('options') ? delete('options') : {}
57
+ headers = opts.fetch(:headers) { {} }
51
58
  if value = accept
52
- hash[:headers] = {} unless hash.has_key?(:headers)
53
- hash[:headers]['Accept'] = value
59
+ headers[:accept] = value
60
+ end
61
+ if value = delete('content_type')
62
+ headers[:content_type] = value
54
63
  end
55
- hash[:raw] = has_key?('raw') ? self.delete('raw') : false
56
- hash
64
+ opts[:raw] = has_key?('raw') ? delete('raw') : false
65
+ opts[:headers] = headers unless headers.empty?
66
+ opts
57
67
  end
58
68
 
59
69
  # Update hash with default parameters for non existing keys
@@ -16,6 +16,7 @@ module Github
16
16
  :Keys => 'keys',
17
17
  :Merging => 'merging',
18
18
  :PubSubHubbub => 'pub_sub_hubbub',
19
+ :Releases => 'releases',
19
20
  :Statistics => 'statistics',
20
21
  :Statuses => 'statuses'
21
22
 
@@ -52,7 +53,7 @@ module Github
52
53
 
53
54
  # Access to Repos::Comments API
54
55
  def comments(options={}, &block)
55
- @commits ||= ApiFactory.new('Repos::Comments', current_options.merge(options), &block)
56
+ @comments ||= ApiFactory.new('Repos::Comments', current_options.merge(options), &block)
56
57
  end
57
58
 
58
59
  # Access to Repos::Commits API
@@ -87,7 +88,7 @@ module Github
87
88
 
88
89
  # Access to Repos::Merging API
89
90
  def merging(options={}, &block)
90
- @mergin ||= ApiFactory.new('Repos::Merging', current_options.merge(options), &block)
91
+ @merging ||= ApiFactory.new('Repos::Merging', current_options.merge(options), &block)
91
92
  end
92
93
 
93
94
  # Access to Repos::PubSubHubbub API
@@ -95,6 +96,11 @@ module Github
95
96
  @pubsubhubbub ||= ApiFactory.new('Repos::PubSubHubbub', current_options.merge(options), &block)
96
97
  end
97
98
 
99
+ # Access to Repos::Releases API
100
+ def releases(options={}, &block)
101
+ @releases ||= ApiFactory.new('Repos::Releases', current_options.merge(options), &block)
102
+ end
103
+
98
104
  # Access to Repos::Statistics API
99
105
  def stats(options={}, &block)
100
106
  @stats ||= ApiFactory.new('Repos::Statistics', current_options.merge(options), &block)
@@ -143,15 +149,15 @@ module Github
143
149
  end
144
150
  params = arguments.params
145
151
 
146
- response = if (user_name = (params.delete("user")))
152
+ response = if (user_name = params.delete('user') || user)
147
153
  get_request("/users/#{user_name}/repos", params)
148
- elsif (org_name = (params.delete("org")))
154
+ elsif (org_name = params.delete('org') || org)
149
155
  get_request("/orgs/#{org_name}/repos", params)
150
- elsif args.map(&:to_s).include?("every")
151
- get_request("/repositories", params)
156
+ elsif args.map(&:to_s).include?('every')
157
+ get_request('/repositories', params)
152
158
  else
153
159
  # For authenticated user
154
- get_request("/user/repos", params)
160
+ get_request('/user/repos', params)
155
161
  end
156
162
  return response unless block_given?
157
163
  response.each { |el| yield el }
@@ -180,11 +186,17 @@ module Github
180
186
  # <tt>:name</tt> - Required string
181
187
  # <tt>:description</tt> - Optional string
182
188
  # <tt>:homepage</tt> - Optional string
183
- # <tt>:private</tt> - Optional boolean - <tt>true</tt> to create a private repository, <tt>false</tt> to create a public one.
184
- # <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
185
- # <tt>:has_wiki</tt> - Optional boolean - <tt>true</tt> to enable the wiki for this repository, <tt>false</tt> to disable it. Default is <tt>true</tt>
186
- # <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
187
- # <tt>:org</tt> Optional string - The organisation in which this repository will be created
189
+ # <tt>:private</tt> - Optional boolean - <tt>true</tt> to create a private
190
+ # repository, <tt>false</tt> to create a public one.
191
+ # <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues
192
+ # for this repository, <tt>false</tt> to disable them
193
+ # <tt>:has_wiki</tt> - Optional boolean - <tt>true</tt> to enable the wiki
194
+ # for this repository, <tt>false</tt> to disable it.
195
+ # Default is <tt>true</tt>
196
+ # <tt>:has_downloads</tt> - Optional boolean - <tt>true</tt> to enable
197
+ # downloads for this repository
198
+ # <tt>:org</tt> Optional string - The organisation in which this repository
199
+ # will be created
188
200
  # <tt>:team_id</tt> Optional number - The id of the team that will be granted access to this repository. This is only valid when creating a repo in an organization
189
201
  # <tt>:auto_init</tt> Optional boolean - true to create an initial commit with empty README. Default is false.
190
202
  # <tt>:gitignore_template</tt> Optional string - Desired language or platform .gitignore template to apply. Use the name of the template without the extension. For example, “Haskell” Ignored if auto_init parameter is not provided.
@@ -214,10 +226,10 @@ module Github
214
226
  params = arguments.params
215
227
 
216
228
  # Requires authenticated user
217
- if (org = params.delete("org"))
229
+ if (org = params.delete('org') || org)
218
230
  post_request("/orgs/#{org}/repos", params.merge_default(DEFAULT_REPO_OPTIONS))
219
231
  else
220
- post_request("/user/repos", params.merge_default(DEFAULT_REPO_OPTIONS))
232
+ post_request('/user/repos', params.merge_default(DEFAULT_REPO_OPTIONS))
221
233
  end
222
234
  end
223
235
 
@@ -324,7 +336,6 @@ module Github
324
336
  # def branches(user_name, repo_name, params={})
325
337
  def branches(*args)
326
338
  arguments(args, :required => [:user, :repo])
327
- params = arguments.params
328
339
 
329
340
  response = get_request("/repos/#{user}/#{repo}/branches", arguments.params)
330
341
  return response unless block_given?
@@ -0,0 +1,146 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+
5
+ # The Releases API
6
+ class Repos::Releases < API
7
+ extend AutoloadHelper
8
+
9
+ autoload_all 'github_api/repos/releases',
10
+ :Assets => 'assets'
11
+
12
+ VALID_RELEASE_PARAM_NAMES = %w[
13
+ tag_name
14
+ target_commitish
15
+ name
16
+ body
17
+ draft
18
+ prerelease
19
+ ].freeze # :nodoc:
20
+
21
+ # Access to Repos::Releases::Assets API
22
+ def assets(options = {}, &block)
23
+ @assets ||= ApiFactory.new('Repos::Releases::Assets', current_options.merge(options), &block)
24
+ end
25
+
26
+ # List releases for a repository
27
+ #
28
+ # Users with push access to the repository will receive all releases
29
+ # (i.e., published releases and draft releases). Users with pull access
30
+ # will receive published releases only.
31
+ #
32
+ # = Examples
33
+ # github = Github.new
34
+ # github.repos.releases.list 'owner', 'repo'
35
+ # github.repos.releases.list 'owner', 'repo' { |release| ... }
36
+ #
37
+ def list(*args)
38
+ params = arguments(args, required: [:owner, :repo]).params
39
+ response = get_request("/repos/#{owner}/#{repo}/releases", params)
40
+ return response unless block_given?
41
+ response.each { |el| yield el }
42
+ end
43
+ alias :all :list
44
+
45
+ # Get a single release
46
+ #
47
+ # = Examples
48
+ # github = Github.new
49
+ # github.repos.releases.get 'owner', 'repo', 'id'
50
+ #
51
+ def get(*args)
52
+ params = arguments(args, required: [:owner, :repo, :id]).params
53
+ get_request("/repos/#{owner}/#{repo}/releases/#{id}" , params)
54
+ end
55
+ alias :find :get
56
+
57
+ # Create a release
58
+ #
59
+ # = Inputs
60
+ # * <tt>:tag_name</tt> - Required string
61
+ # * <tt>:target_commitish</tt> - Optional string - Specifies the commitish
62
+ # value that determines where the Git tag is created from. Can be
63
+ # any branch or commit SHA. Defaults to the repository's default
64
+ # branch (usually 'master'). Unused if the Git tag already exists.
65
+ # * <tt>:name</tt> - Optional string
66
+ # * <tt>:body</tt> - Optional string
67
+ # * <tt>:draft</tt> - Optional boolean - <tt>true</tt> to create a draft
68
+ # (unpublished) release, <tt>false</tt> to create
69
+ # a published one. Default is false.
70
+ # * <tt>:prerelease</tt> - Optional boolean - <tt>true</tt> to identify
71
+ # the release as a prerelease. false to identify
72
+ # the release as a full release. Default is false.
73
+ #
74
+ # = Examples
75
+ # github = Github.new
76
+ # github.repos.releases.create 'owner', 'repo', 'tag-name',
77
+ # "tag_name": "v1.0.0",
78
+ # "target_commitish": "master",
79
+ # "name": "v1.0.0",
80
+ # "body": "Description of the release",
81
+ # "draft": false,
82
+ # "prerelease": false
83
+ #
84
+ def create(*args)
85
+ arguments(args, required: [:owner, :repo, :tag_name]) do
86
+ sift VALID_RELEASE_PARAM_NAMES
87
+ end
88
+ params = arguments.params
89
+ params['tag_name'] = tag_name
90
+
91
+ post_request("/repos/#{owner}/#{repo}/releases", params)
92
+ end
93
+
94
+ # Edit a release
95
+ #
96
+ # = Inputs
97
+ # * <tt>:tag_name</tt> - Required string
98
+ # * <tt>:target_commitish</tt> - Optional string - Specifies the commitish
99
+ # value that determines where the Git tag is created from. Can be
100
+ # any branch or commit SHA. Defaults to the repository's default
101
+ # branch (usually 'master'). Unused if the Git tag already exists.
102
+ # * <tt>:name</tt> - Optional string
103
+ # * <tt>:body</tt> - Optional string
104
+ # * <tt>:draft</tt> - Optional boolean - <tt>true</tt> to create a draft
105
+ # (unpublished) release, <tt>false</tt> to create
106
+ # a published one. Default is false.
107
+ # * <tt>:prerelease</tt> - Optional boolean - <tt>true</tt> to identify
108
+ # the release as a prerelease. false to identify
109
+ # the release as a full release. Default is false.
110
+ #
111
+ # = Examples
112
+ # github = Github.new
113
+ # github.repos.releases.edit 'owner', 'repo', 'id',
114
+ # "tag_name": "v1.0.0",
115
+ # "target_commitish": "master",
116
+ # "name": "v1.0.0",
117
+ # "body": "Description of the release",
118
+ # "draft": false,
119
+ # "prerelease": false
120
+ #
121
+ def edit(*args)
122
+ arguments(args, required: [:owner, :repo, :id]) do
123
+ sift VALID_RELEASE_PARAM_NAMES
124
+ end
125
+ params = arguments.params
126
+
127
+ patch_request("/repos/#{owner}/#{repo}/releases/#{id}", params)
128
+ end
129
+ alias :update :edit
130
+
131
+ # Delete a release
132
+ #
133
+ # Users with push access to the repository can delete a release.
134
+ #
135
+ # = Examples
136
+ # github = Github.new
137
+ # github.repos.releases.delete 'owner', 'repo', 'id'
138
+ #
139
+ def delete(*args)
140
+ params = arguments(args, required: [:owner, :repo, :id]).params
141
+
142
+ delete_request("/repos/#{owner}/#{repo}/releases/#{id}", params)
143
+ end
144
+
145
+ end # Repos::Statuses
146
+ end # Github
@@ -0,0 +1,126 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+
5
+ # The Release Assets API
6
+ class Repos::Releases::Assets < API
7
+
8
+ VALID_ASSET_PARAM_NAMES = %w[
9
+ name
10
+ label
11
+ content_type
12
+ ].freeze # :nodoc:
13
+
14
+ # List assets for a release
15
+ #
16
+ # = Examples
17
+ # github = Github.new
18
+ # github.repos.releases.assets.list 'owner', 'repo', 'id'
19
+ # github.repos.releases.assets.list 'owner', 'repo', 'id' { |asset| ... }
20
+ #
21
+ def list(*args)
22
+ params = arguments(args, required: [:owner, :repo, :id]).params
23
+
24
+ response = get_request("/repos/#{owner}/#{repo}/releases/#{id}/assets", params)
25
+ return response unless block_given?
26
+ response.each { |el| yield el }
27
+ end
28
+ alias :all :list
29
+
30
+ # Get a single release asset
31
+ #
32
+ # = Examples
33
+ # github = Github.new
34
+ # github.repos.releases.assets.get 'owner', 'repo', 'id'
35
+ #
36
+ def get(*args)
37
+ params = arguments(args, required: [:owner, :repo, :id]).params
38
+
39
+ get_request("/repos/#{owner}/#{repo}/releases/assets/#{id}" , params)
40
+ end
41
+ alias :find :get
42
+
43
+ # Upload a release asset
44
+ #
45
+ # = Inputs
46
+ # * <tt>:name</tt> - Required string - The file name of the asset
47
+ # * <tt>:content_type</tt> - Required string - The content type
48
+ # of the asset. Example: “application/zip”.
49
+ #
50
+ # = Examples
51
+ # github = Github.new
52
+ # github.repos.releases.assets.upload 'owner', 'repo', 'id', 'file-path'
53
+ # "name": "batman.jpg",
54
+ # "content_type": "application/octet-stream"
55
+ #
56
+ def upload(*args)
57
+ arguments(args, required: [:owner, :repo, :id, :filepath]) do
58
+ sift VALID_ASSET_PARAM_NAMES
59
+ end
60
+ params = arguments.params
61
+
62
+ unless type = params['content_type']
63
+ type = infer_media(filepath)
64
+ end
65
+
66
+ file = Faraday::UploadIO.new(filepath, type)
67
+ options = {
68
+ headers: { content_type: type },
69
+ endpoint: 'https://uploads.github.com',
70
+ query: {'name' => params['name']}
71
+ }
72
+ params['data'] = file.read
73
+ params['options'] = options
74
+
75
+ post_request("/repos/#{owner}/#{repo}/releases/#{id}/assets", params)
76
+ ensure
77
+ file.close if file
78
+ end
79
+
80
+ # Infer media type of the asset
81
+ #
82
+ def infer_media(filepath)
83
+ require 'mime/types'
84
+ types = MIME::Types.type_for(filepath)
85
+ types.empty? ? 'application/octet-stream' : types.first
86
+ rescue LoadError
87
+ raise Github::Error::UnknownMedia.new(filepath)
88
+ end
89
+
90
+ # Edit a release asset
91
+ #
92
+ # = Inputs
93
+ # * <tt>:name</tt> - Required string - the filename of the asset
94
+ # * <tt>:label</tt> - Optional string - An alternate short description of
95
+ # the asset. Used in place of the filename.
96
+ #
97
+ # = Examples
98
+ # github = Github.new
99
+ # github.repos.releases.assets.edit 'owner', 'repo', 'id',
100
+ # "name": "foo-1.0.0-osx.zip",
101
+ # "label": "Mac binary"
102
+ #
103
+ def edit(*args)
104
+ arguments(args, required: [:owner, :repo, :id]) do
105
+ sift VALID_ASSET_PARAM_NAMES
106
+ end
107
+ params = arguments.params
108
+
109
+ patch_request("/repos/#{owner}/#{repo}/releases/assets/#{id}", params)
110
+ end
111
+ alias :update :edit
112
+
113
+ # Delete a release asset
114
+ #
115
+ # = Examples
116
+ # github = Github.new
117
+ # github.repos.releases.assets.delete 'owner', 'repo', 'id'
118
+ #
119
+ def delete(*args)
120
+ params = arguments(args, required: [:owner, :repo, :id]).params
121
+
122
+ delete_request("/repos/#{owner}/#{repo}/releases/assets/#{id}", params)
123
+ end
124
+
125
+ end # Repos::Statuses
126
+ end # Github