github_api 0.1.0.pre

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 (85) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +159 -0
  3. data/Rakefile +52 -0
  4. data/features/github.feature +9 -0
  5. data/features/step_definitions/github_steps.rb +0 -0
  6. data/features/support/env.rb +13 -0
  7. data/lib/github_api.rb +55 -0
  8. data/lib/github_api/api.rb +133 -0
  9. data/lib/github_api/api/extract_options.rb +17 -0
  10. data/lib/github_api/api/mime.rb +5 -0
  11. data/lib/github_api/api/utils.rb +9 -0
  12. data/lib/github_api/client.rb +35 -0
  13. data/lib/github_api/configuration.rb +84 -0
  14. data/lib/github_api/connection.rb +91 -0
  15. data/lib/github_api/error.rb +35 -0
  16. data/lib/github_api/gists.rb +199 -0
  17. data/lib/github_api/gists/comments.rb +74 -0
  18. data/lib/github_api/git_data.rb +26 -0
  19. data/lib/github_api/git_data/blobs.rb +9 -0
  20. data/lib/github_api/git_data/commits.rb +9 -0
  21. data/lib/github_api/git_data/references.rb +9 -0
  22. data/lib/github_api/git_data/tags.rb +9 -0
  23. data/lib/github_api/git_data/trees.rb +9 -0
  24. data/lib/github_api/issues.rb +201 -0
  25. data/lib/github_api/issues/comments.rb +98 -0
  26. data/lib/github_api/issues/events.rb +50 -0
  27. data/lib/github_api/issues/labels.rb +191 -0
  28. data/lib/github_api/issues/milestones.rb +119 -0
  29. data/lib/github_api/orgs.rb +90 -0
  30. data/lib/github_api/orgs/members.rb +109 -0
  31. data/lib/github_api/orgs/teams.rb +236 -0
  32. data/lib/github_api/pull_requests.rb +210 -0
  33. data/lib/github_api/pull_requests/comments.rb +134 -0
  34. data/lib/github_api/repos.rb +256 -0
  35. data/lib/github_api/repos/collaborators.rb +59 -0
  36. data/lib/github_api/repos/commits.rb +115 -0
  37. data/lib/github_api/repos/downloads.rb +77 -0
  38. data/lib/github_api/repos/forks.rb +29 -0
  39. data/lib/github_api/repos/hooks.rb +67 -0
  40. data/lib/github_api/repos/keys.rb +53 -0
  41. data/lib/github_api/repos/watching.rb +50 -0
  42. data/lib/github_api/request.rb +75 -0
  43. data/lib/github_api/request/oauth2.rb +33 -0
  44. data/lib/github_api/response.rb +10 -0
  45. data/lib/github_api/response/jsonize.rb +22 -0
  46. data/lib/github_api/response/mashify.rb +26 -0
  47. data/lib/github_api/response/raise_error.rb +33 -0
  48. data/lib/github_api/users.rb +82 -0
  49. data/lib/github_api/users/emails.rb +49 -0
  50. data/lib/github_api/users/followers.rb +98 -0
  51. data/lib/github_api/users/keys.rb +84 -0
  52. data/lib/github_api/version.rb +12 -0
  53. data/spec/fixtures/collaborators_list.json +6 -0
  54. data/spec/fixtures/commits_list.json +25 -0
  55. data/spec/fixtures/repos_branches_list.json +7 -0
  56. data/spec/fixtures/repos_list.json +27 -0
  57. data/spec/github/api_spec.rb +6 -0
  58. data/spec/github/client_spec.rb +6 -0
  59. data/spec/github/gists/comments_spec.rb +5 -0
  60. data/spec/github/gists_spec.rb +5 -0
  61. data/spec/github/git_data/blobs_spec.rb +5 -0
  62. data/spec/github/git_data/commits_spec.rb +5 -0
  63. data/spec/github/git_data/references_spec.rb +5 -0
  64. data/spec/github/git_data/tags_spec.rb +5 -0
  65. data/spec/github/git_data/trees_spec.rb +5 -0
  66. data/spec/github/git_data_spec.rb +5 -0
  67. data/spec/github/issues/comments_spec.rb +5 -0
  68. data/spec/github/issues/events_spec.rb +5 -0
  69. data/spec/github/issues/labels_spec.rb +5 -0
  70. data/spec/github/issues/milestones_spec.rb +5 -0
  71. data/spec/github/issues_spec.rb +5 -0
  72. data/spec/github/orgs/members_spec.rb +5 -0
  73. data/spec/github/orgs/teams_spec.rb +5 -0
  74. data/spec/github/orgs_spec.rb +5 -0
  75. data/spec/github/repos/collaborators_spec.rb +6 -0
  76. data/spec/github/repos/commits_spec.rb +5 -0
  77. data/spec/github/repos/downloads_spec.rb +5 -0
  78. data/spec/github/repos/forks_spec.rb +5 -0
  79. data/spec/github/repos/hooks_spec.rb +5 -0
  80. data/spec/github/repos/keys_spec.rb +5 -0
  81. data/spec/github/repos/watching_spec.rb +5 -0
  82. data/spec/github/repos_spec.rb +35 -0
  83. data/spec/github_spec.rb +5 -0
  84. data/spec/spec_helper.rb +15 -0
  85. metadata +284 -0
@@ -0,0 +1,134 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class PullRequests
5
+ module Comments
6
+
7
+ VALID_REQUEST_COM_PARAM_NAMES = %w[
8
+ body
9
+ commit_id
10
+ path
11
+ position
12
+ in_reply_to
13
+ ].freeze
14
+
15
+ # List comments on a pull request
16
+ #
17
+ # = Examples
18
+ # @github = Github.new
19
+ # @github.pull_requests.request_comments 'user-name', 'repo-name', 'request-id'
20
+ #
21
+ def request_comments(user_name, repo_name, request_id, params={})
22
+ _update_user_repo_params(user_name, repo_name)
23
+ _validate_user_repo_params(user, repo) unless user? && repo?
24
+ _validate_presence_of request_id
25
+ _normalize_params_keys(params)
26
+
27
+ response = get("/repos/#{user}/#{repo}/pulls/#{request_id}/comments", params)
28
+ return response unless block_given?
29
+ response.each { |el| yield el }
30
+ end
31
+
32
+ # Get a single comment for pull requests
33
+ # = Examples
34
+ # @github = Github.new
35
+ # @github.pull_requests.request_comment 'user-name', 'repo-name', 'comment-id'
36
+ #
37
+ def request_comment(user_name, repo_name, comment_id, params={})
38
+ _update_user_repo_params(user_name, repo_name)
39
+ _validate_user_repo_params(user, repo) unless user? && repo?
40
+ _validate_presence_of comment_id
41
+ _normalize_params_keys(params)
42
+
43
+ get("/repos/#{user}/#{repo}/pulls/comments/#{comment_id}", params)
44
+ end
45
+
46
+ # Create a pull request comment
47
+ #
48
+ # = Inputs
49
+ # * <tt>:body</tt> - Required string
50
+ # * <tt>:commit_id</tt> - Required string - sha of the commit to comment on.
51
+ # * <tt>:path</tt> - Required string - Relative path of the file to comment on.
52
+ # * <tt>:position</tt> - Required number - Line index in the diff to comment on
53
+ #
54
+ # = Examples
55
+ # @github = Github.new
56
+ # @github.pull_requests.create_request_comment 'user-name', 'repo-name', 'request-id', "body" => "Nice change",
57
+ # "commit_id" => "6dcb09b5b57875f334f61aebed695e2e4193db5e",
58
+ # "path" => "file1.txt",
59
+ # "position" => 4
60
+ #
61
+ # = Alternative Inputs
62
+ # Instead of passing commit_id, path, and position you can reply to
63
+ # an existing Pull Request Comment like this
64
+ # * <tt>:body</tt> - Required string
65
+ # * <tt>:in_reply_to</tt> - Required number - comment id to reply to.
66
+ #
67
+ # = Examples
68
+ # @github = Github.new
69
+ # @github.pull_requests.create_request_comment 'user-name', 'repo-name', 'request-id', "body" => "Nice change",
70
+ # "in_reply_to" => 4
71
+ #
72
+ def create_request_comment(user_name, repo_name, request_id, params={})
73
+ _update_user_repo_params(user_name, repo_name)
74
+ _validate_user_repo_params(user, repo) unless user? && repo?
75
+ _validate_presence_of request_id
76
+
77
+ _normalize_params_keys(params)
78
+ _filter_params_keys(VALID_REQUEST_COM_PARAM_NAMES, params)
79
+ _validate_reply_to(params)
80
+
81
+ post("/repos/#{user}/#{repo}/pulls/#{request_id}/comments", params)
82
+ end
83
+
84
+ # Edit a pull request comment
85
+ #
86
+ # = Inputs
87
+ # * <tt>:body</tt> - Required string
88
+ #
89
+ # = Examples
90
+ # @github = Github.new
91
+ # @github.pull_requests.edit_request_comment 'user-name', 'repo-name', 'comment-id', "body" => "Nice change",
92
+ #
93
+ def edit_request_comment(user_name, repo_name, comment_id, params={})
94
+ _update_user_repo_params(user_name, repo_name)
95
+ _validate_user_repo_params(user, repo) unless user? && repo?
96
+ _validate_presence_of comment_id
97
+
98
+ _normalize_params_keys(params)
99
+ _filter_params_keys(VALID_REQUEST_COM_PARAM_NAMES, params)
100
+
101
+ patch("/repos/#{user}/#{repo}/pulls/comments/#{comment_id}", params)
102
+ end
103
+
104
+ # Delete a pull request comment
105
+ #
106
+ # = Examples
107
+ # @github = Github.new
108
+ # @github.pull_requests.delete_request_comment 'user-name', 'repo-name',
109
+ # 'comment-id'
110
+ #
111
+ def delete_request_comment(user_name, repo_name, comment_id, params={})
112
+ _update_user_repo_params(user_name, repo_name)
113
+ _validate_user_repo_params(user, repo) unless user? && repo?
114
+ _validate_presence_of comment_id
115
+ _normalize_params_keys(params)
116
+
117
+ delete("/repos/#{user}/#{repo}/pulls/comments/#{comment_id}", params)
118
+ end
119
+
120
+ private
121
+
122
+ # To let user know that the params supplied are wrong before request is made
123
+ def _validate_reply_to(params)
124
+ if params['in_reply_to'] && !_validate_inputs(%w[ body in_reply_to ], params)
125
+ raise ArgumentError, "Required params are: #{%w[ body in_reply_to].join(',')}"
126
+
127
+ elsif !_validate_inputs(VALID_REQUEST_COM_PARAM_NAMES - %w[ in_reply_to ], params)
128
+ raise ArgumentError, "Required params are: #{VALID_REQUEST_COM_PARAM_NAMES.join(', ')}"
129
+ end
130
+ end
131
+
132
+ end # Comments
133
+ end # PullRequests
134
+ end # Github
@@ -0,0 +1,256 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos < API
5
+ extend AutoloadHelper
6
+
7
+ # Load all the modules after initializing Repos to avoid superclass mismatch
8
+ autoload_all 'github_api/repos',
9
+ :Collaborators => 'collaborators',
10
+ :Commits => 'commits',
11
+ :Downloads => 'downloads',
12
+ :Forks => 'forks',
13
+ :Hooks => 'hooks',
14
+ :Keys => 'keys',
15
+ :Watching => 'watching'
16
+
17
+ include Github::Repos::Collaborators
18
+ include Github::Repos::Commits
19
+ include Github::Repos::Downloads
20
+ include Github::Repos::Forks
21
+ include Github::Repos::Hooks
22
+ include Github::Repos::Keys
23
+ include Github::Repos::Watching
24
+
25
+ DEFAULT_REPO_OPTIONS = {
26
+ "homepage" => "https://github.com",
27
+ "public" => true,
28
+ "has_issues" => true,
29
+ "has_wiki" => true,
30
+ "has_downloads" => true
31
+ }
32
+
33
+ VALID_REPO_OPTIONS = %w[
34
+ name
35
+ description
36
+ homepage
37
+ public
38
+ has_issues
39
+ has_wiki
40
+ has_downloads
41
+ ].freeze
42
+
43
+ VALID_REPO_TYPES = %w[ all public private member ]
44
+
45
+ # Creates new Repos API
46
+ def initialize(options = {})
47
+ super(options)
48
+ end
49
+
50
+ # List branches
51
+ #
52
+ # = Examples
53
+ #
54
+ # @github = Github.new
55
+ # @github.repos.branches 'user-name', 'repo-name'
56
+ #
57
+ # @repos = Github::Repos.new
58
+ # @repos.branches 'user-name', 'repo-name'
59
+ #
60
+ def branches(user_name=nil, repo_name=nil, params={})
61
+ _update_user_repo_params(user_name, repo_name)
62
+ _validate_user_repo_params(user, repo) unless (user? && repo?)
63
+
64
+ response = get("/repos/#{user}/#{repo}/branches", params)
65
+ return response unless block_given?
66
+ response.each { |el| yield el }
67
+ end
68
+
69
+ # Create a new repository for the autheticated user.
70
+ #
71
+ # = Parameters
72
+ # <tt>:name</tt> - Required string
73
+ # <tt>:description</tt> - Optional string
74
+ # <tt>:homepage</tt> - Optional string
75
+ # <tt>:public</tt> - Optional boolean - true to create public repo, false to create a private one
76
+ # <tt>:has_issues</tt> - Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
77
+ # <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>
78
+ # <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
79
+ #
80
+ # = Examples
81
+ # @github = Github.new
82
+ # @github.repos.create_repo :name => 'my_repo_name'
83
+ #
84
+ # Create a new repository in this organisation. The authenticated user
85
+ # must be a member of this organisation
86
+ #
87
+ # Examples:
88
+ # @github = Github.new :oauth_token => '...'
89
+ # @github.repos.create_repo(:name => 'my-repo-name', :org => 'my-organisation')
90
+ #
91
+ def create_repo(*args)
92
+ params = args.last.is_a?(Hash) ? args.pop : {}
93
+ _normalize_params_keys(params)
94
+ _filter_params_keys(VALID_REPO_OPTIONS + %w[ org ], params)
95
+
96
+ raise ArgumentError, "Required params are: :name" unless _validate_inputs(%w[ name ], params)
97
+
98
+ # Requires authenticated user
99
+ if (org = params.delete("org"))
100
+ post("/orgs/#{org}/repos", DEFAULT_REPO_OPTIONS.merge(params))
101
+ else
102
+ post("/user/repos", DEFAULT_REPO_OPTIONS.merge(params))
103
+ end
104
+ end
105
+
106
+ # List contributors
107
+ #
108
+ # = Parameters
109
+ # <tt>:anon</tt> - Optional flag. Set to 1 or true to include anonymous contributors.
110
+ #
111
+ # = Examples
112
+ #
113
+ # @github = Github.new
114
+ # @github.repos.contributors('user-name','repo-name')
115
+ # @github.repos.contributors('user-name','repo-name') { |cont| ... }
116
+ #
117
+ def contributors(user_name=nil, repo_name=nil, params={})
118
+ _update_user_repo_params(user_name, repo_name)
119
+ _validate_user_repo_params(user, repo) unless user? && repo?
120
+ _normalize_params_keys(params)
121
+ _filter_params_keys(['anon'], params)
122
+
123
+ response = get("/repos/#{user}/#{repo}/contributors", params)
124
+ return response unless block_given?
125
+ response.each { |el| yield el }
126
+ end
127
+
128
+ # Edit a repository
129
+ #
130
+ # = Parameters
131
+ # * <tt>:name</tt> Required string
132
+ # * <tt>:description</tt> Optional string
133
+ # * <tt>:homepage</tt> Optional string
134
+ # * <tt>:public</tt> Optional boolean - true to create public repo, false to create a private one
135
+ # * <tt>:has_issues</tt> Optional boolean - <tt>true</tt> to enable issues for this repository, <tt>false</tt> to disable them
136
+ # * <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>
137
+ # * <tt>:has_downloads</tt> Optional boolean - <tt>true</tt> to enable downloads for this repository
138
+ #
139
+ # = Examples
140
+ #
141
+ # @github = Github.new
142
+ # @github.repos.edit_repo('user-name', 'repo-name', { :name => 'hello-world', :description => 'This is your first repo', :homepage => "https://github.com", :public => true, :has_issues => true })
143
+ #
144
+ def edit_repo(user=nil, repo=nil, params={})
145
+ _update_user_repo_params(user_name, repo_name)
146
+ _validate_user_repo_params(user, repo) unless user? && repo?
147
+
148
+ _normalize_params_keys(params)
149
+ _filter_params_keys(VALID_REPO_OPTIONS, params)
150
+
151
+ raise ArgumentError, "Required params are: #{%w[ :name ] }" unless _validate_inputs(%w[ name ], params)
152
+
153
+ patch("/repos/#{user}/#{repo}", DEFAULT_REPO_OPTIONS.merge(params))
154
+ end
155
+
156
+ # Get a repository
157
+ #
158
+ # = Examples
159
+ # @github = Github.new
160
+ # @github.repos.get_repo('user-name', 'repo-name')
161
+ #
162
+ def get_repo(user_name=nil, repo_name=nil, params={})
163
+ _update_user_repo_params(user_name, repo_name)
164
+ _validate_user_repo_params(user, repo) unless user? && repo?
165
+ _normalize_params_keys(params)
166
+
167
+ get("/repos/#{user}/#{repo}", params)
168
+ end
169
+
170
+ # List languages
171
+ #
172
+ # = Examples
173
+ # @github = Github.new
174
+ # @github.repos.languages('user-name', 'repo-name')
175
+ # @github.repos.languages('user-name', 'repo-name') { |lang| ... }
176
+ #
177
+ def languages(user_name=nil, repo_name=nil, params={})
178
+ _update_user_repo_params(user_name, repo_name)
179
+ _validate_user_repo_params(user, repo) unless user? && repo?
180
+ _normalize_params_keys(params)
181
+
182
+ response = get("/repos/#{user}/#{repo}/languages", params)
183
+ return response unless block_given?
184
+ response.each { |el| yield el }
185
+ end
186
+
187
+ # List repositories for the authenticated user
188
+ #
189
+ # = Examples
190
+ # @github = Github.new { :consumer_key => ... }
191
+ # @github.repos.list_repos
192
+ #
193
+ # List public repositories for the specified user.
194
+ #
195
+ # = Examples
196
+ # github = Github.new
197
+ # github.repos.list_repos(:user => 'user-name')
198
+ #
199
+ # List repositories for the specified organisation.
200
+ #
201
+ # = Examples
202
+ # @github = Github.new
203
+ # @github.repos.list_repos(:org => 'org-name')
204
+ #
205
+ def list_repos(*args)
206
+ params = args.last.is_a?(Hash) ? args.pop : {}
207
+ _normalize_params_keys(params)
208
+ _merge_user_into_params!(params) unless params.has_key?('user')
209
+ _filter_params_keys(%w[ org user type ], params)
210
+
211
+ if (user_name = params.delete("user"))
212
+ get("/users/#{user_name}/repos")
213
+ elsif (org_name = params.delete("org"))
214
+ get("/users/#{org_name}/repos", params)
215
+ else
216
+ # For authenticated user
217
+ get("/user/repos", params)
218
+ end
219
+ end
220
+
221
+ # List tags
222
+ #
223
+ # = Examples
224
+ # @github = Github.new
225
+ # @github.repos.tags('user-name', 'repo-name')
226
+ # @github.repos.tags('user-name', 'repo-name') { |tag| ... }
227
+ #
228
+ def tags(user_name=nil, repo_name=nil, params={})
229
+ _update_user_repo_params(user_name, repo_name)
230
+ _validate_user_repo_params(user, repo) unless user? && repo?
231
+ _normalize_params_keys(params)
232
+
233
+ response = get("/repos/#{user}/#{repo}/tags", params)
234
+ return response unless block_given?
235
+ response.each { |el| yield el }
236
+ end
237
+
238
+ # List teams
239
+ #
240
+ # == Examples
241
+ # @github = Github.new
242
+ # @github.repos.teams('user-name', 'repo-name')
243
+ # @github.repos.teams('user-name', 'repo-name') { |team| ... }
244
+ #
245
+ def teams(user_name=nil, repo_name=nil, params={})
246
+ _update_user_repo_params(user_name, repo_name)
247
+ _validate_user_repo_params(user, repo) unless user? && repo?
248
+ _normalize_params_keys(params)
249
+
250
+ response = get("/repos/#{user}/#{repo}/teams", params)
251
+ return response unless block_given?
252
+ response.each { |el| yield el }
253
+ end
254
+
255
+ end # Repos
256
+ end # Github
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Collaborators
6
+
7
+ # Add collaborator
8
+ #
9
+ # Examples:
10
+ # @github = Github.new
11
+ # @github.collaborators.add_collaborator('user', 'repo', 'collaborator')
12
+ #
13
+ # @repos = Github::Repos.new
14
+ # @repos.add_collaborator('user', 'repo', 'collaborator')
15
+ #
16
+ def add_collaborator(user, repo, collaborator)
17
+ put("/repos/#{user}/#{repo}/collaborators/#{collaborator}")
18
+ end
19
+
20
+
21
+ # Checks if user is a collaborator for a given repository
22
+ #
23
+ # Examples:
24
+ # @github = Github.new
25
+ # @github.collaborators.collaborator?('user', 'repo', 'collaborator')
26
+ #
27
+ def collaborator?(user_name, repo_name, collaborator)
28
+ get("/repos/#{user}/#{repo}/collaborators/#{collaborator}")
29
+ end
30
+
31
+ # List collaborators
32
+ #
33
+ # Examples:
34
+ # @github = Github.new
35
+ # @github.repos.collaborators('user-name', 'repo-name')
36
+ # @github.repos.collaborators('user-name', 'repo-name') { |cbr| .. }
37
+ #
38
+ def collaborators(user_name=nil, repo_name=nil)
39
+ _update_user_repo_params(user_name, repo_name)
40
+ _validate_user_repo_params(user, repo) unless (user? && repo?)
41
+
42
+ response = get("/repos/#{user}/#{repo}/collaborators")
43
+ return response unless block_given?
44
+ response.each { |el| yield el }
45
+ end
46
+
47
+ # Removes collaborator
48
+ #
49
+ # Examples:
50
+ # @github = Github.new
51
+ # @github.collaborators.remove('user', 'repo', 'collaborator')
52
+ #
53
+ def remove_collabolator(user, repo, collaborator)
54
+ delete("/repos/#{user}/#{repo}/collaborators/#{user}")
55
+ end
56
+
57
+ end # Collaborators
58
+ end # Repos
59
+ end # Github