github_api 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
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,115 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Commits
6
+
7
+ REQUIRED_COMMENT_PARAMS = %w[ body commit_id line path position ]
8
+
9
+ # Creates a commit comment
10
+ #
11
+ # = Examples
12
+ # @github = Github.new
13
+ # @github.repos.create_comment(...)
14
+ #
15
+ def create_comment(user_name=nil, repo_name=nil, params={})
16
+ raise ArgumentError, "Expected following inputs to the method: #{REQUIRED_COMMENT_PARAMS.join(', ')}" unless _validate_inputs(REQUIRED_COMMENT_PARAMS, inputs)
17
+
18
+ post("/repos/#{user}/#{repo}/commits/#{sha}/comments", inputs)
19
+ end
20
+
21
+ # Deletes a commit comment
22
+ #
23
+ # = Examples
24
+ # @github = Github.new
25
+ # @github.repos.delete_comment(...)
26
+ #
27
+ def delete_comment(user, repo, comment_id)
28
+ delete("/repos/#{user}/#{repo}/comments/#{comment_id}")
29
+ end
30
+
31
+ # List commits on a repository
32
+ #
33
+ # = Parameters
34
+ # :sha Optional string. Sha or branch to start listing commits from.
35
+ # :path Optional string. Only commits containing this file path will be returned
36
+ # = Examples
37
+ # @github = Github.new
38
+ # @github.repos.commits('user-name', 'repo-name', { :sha => ... })
39
+ #
40
+ def commits(user_name=nil, repo_name=nil, params={})
41
+ _update_user_repo_params(user_name, repo_name)
42
+ _validate_user_repo_params(user, repo) unless user? && repo?
43
+ _normalize_params_keys(params)
44
+ _filter_params_keys(%w[ sha path], params)
45
+
46
+ response = get("/repos/#{user}/#{repo}/commits", params)
47
+ return response unless block_given?
48
+ response.each { |el| yield el }
49
+ end
50
+
51
+ # List commit comments for a repository
52
+ #
53
+ # = Examples
54
+ # @github = Github.new
55
+ # @github.repos.list_repo_comments('user-name', 'repo-name')
56
+ #
57
+ def list_repo_comments(user_name=nil, repo_name=nil)
58
+ _update_user_repo_params(user_name, repo_name)
59
+ _validate_user_repo_params(user, repo) unless user? && repo?
60
+
61
+ response = get("/repos/#{user}/#{repo}/comments")
62
+ return response unless block_given?
63
+ response.each { |el| yield el }
64
+ end
65
+
66
+ # List comments for a single commit
67
+ #
68
+ # = Examples
69
+ # @github = Github.new
70
+ # @github.repos.list_commit_comments('user-name', 'repo-name', '6dcb09b5b57875f334f61aebed695e2e4193db5e')
71
+ #
72
+ def list_commit_comments(user, repo, sha)
73
+ get("/repos/#{user}/#{repo}/commits/#{sha}/comments")
74
+ end
75
+
76
+ # Gets a single commit
77
+ #
78
+ # Examples:
79
+ # @github = Github.new
80
+ # @github.repos.get_commit('user-name', 'repo-name', '6dcb09b5b57875f334f61aebed6')
81
+ #
82
+ def get_commit(user, repo, sha)
83
+ get("/repos/#{user}/#{repo}/commits/#{sha}")
84
+ end
85
+
86
+ # Gets a single commit comment
87
+ #
88
+ # = Examples
89
+ # @github = Github.new
90
+ # @github.repos.get_comment 'user-name', 'repo-name', 'comment-id'
91
+ #
92
+ def get_comment(user, repo, comment_id)
93
+ get("/repos/#{user}/#{repo}/comments/#{comment_id}")
94
+ end
95
+
96
+ # Gets a single commit
97
+ #
98
+ def get_commit(user, repo, sha)
99
+ get("/repos/#{user}/#{repo}/commits/#{sha}")
100
+ end
101
+
102
+ # Update a commit comment
103
+ #
104
+ # = Examples
105
+ # @github = Github.new
106
+ # @github.repos.update_comment(...)
107
+ #
108
+ def update_comment(user, repo, comment_id)
109
+ raise ArgumentError, "expected following inputs to the method: 'body'" unless _validate_inputs(["body"], inputs)
110
+ patch("/repos/#{user}/#{repo}/comments/#{comment_id}")
111
+ end
112
+
113
+ end # Commits
114
+ end # Repos
115
+ end # Github
@@ -0,0 +1,77 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Downloads
6
+
7
+ REQUIRED_PARAMS = %w[ name size ]
8
+ VALID_PARAMS = %w[ name size description content_type ]
9
+
10
+ REQUIRED_S3_PARAMS = %w[ key acl success_action_status Filename AWSAccessKeyId Policy Signature Content-Type file ]
11
+
12
+ # List downloads for a repository
13
+ #
14
+ # GET /repos/:user/:repo/downloads
15
+ #
16
+ def downloads(user, repo)
17
+ get("/repos/#{user}/#{repo}/downloads")
18
+ end
19
+
20
+ # Get a single download
21
+ #
22
+ # GET /repos/:user/:repo/downloads/:id
23
+ #
24
+ def get_download(user, repo, download_id)
25
+ get("/repos/#{user}/#{repo}/downloads/#{download_id}")
26
+ end
27
+
28
+ # Delete download from a repository
29
+ #
30
+ # DELETE /repos/:user/:repo/downloads/:id
31
+ #
32
+ def delete_download(user, repo, download_id)
33
+ delete("/repos/#{user}/#{repo}/downloads/#{download_id}")
34
+ end
35
+
36
+ # Creating a new download is a two step process.
37
+ # You must first create a new download resource using this method.
38
+ # Response from this method is to be used in #upload method.
39
+ #
40
+ # POST /repos/:user/:repo/downloads
41
+ #
42
+ def create_download(user, repo, params={})
43
+ _normalize_params_keys(params)
44
+ raise ArgumentError, "expected following inputs to the method: #{REQUIRED_INPUTS.join(', ')}" unless _valid_inputs(REQUIRED_PARAMS, params)
45
+ _filter_params_keys(VALID_PARAMS, params)
46
+
47
+ post("/repos/#{user}/#{repo}/downloads", params)
48
+ end
49
+
50
+ # Upload a file to Amazon, using the reponse instance from
51
+ # Github::Repos::Downloads#create. This can be done by passing
52
+ # the response object as an argument to upload method.
53
+ #
54
+ def upload(result, file)
55
+ REQUIRED_S3_PARAMS.each do |key|
56
+ raise ArgumentError, "expected following keys: #{REQUIRED_S3_PARAMS.join(', ')}" unless result.respond_to?(key)
57
+ end
58
+
59
+ # TODO use ordered hash if Ruby < 1.9
60
+ mapped_params = {
61
+ "key" => result.path,
62
+ "acl" => result.acl,
63
+ "success_action_status" => 201,
64
+ "Filename" => result.name,
65
+ "AWSAccessKeyId" => result.accesskeyid,
66
+ "Policy" => result.policy,
67
+ "Signature" => result.signature,
68
+ "Content-Type" => result.mime_type,
69
+ "file" => file
70
+ }
71
+
72
+ post(result.s3_url, mapped_params)
73
+ end
74
+
75
+ end # Downloads
76
+ end # Repos
77
+ end # Github
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Forks
6
+
7
+ # List forks
8
+ #
9
+ # GET /repos/:user/:repo/forks
10
+ #
11
+ # Examples
12
+ #
13
+ def forks(user, repo)
14
+ get("/repos/#{user}/#{repo}/forks")
15
+ end
16
+
17
+ # Create a fork for the authenticated user
18
+ #
19
+ # POST /repos/:user/:repo/forks
20
+ #
21
+ def create_fork(user, repo, params={})
22
+ _normalize_params_keys(params)
23
+ _filter_params_keys(%w[ org ], params)
24
+
25
+ post("/repos/#{user}/#{repo}/forks", params)
26
+ end
27
+ end
28
+ end
29
+ end # Github
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Hooks
6
+
7
+ REQUIRED_PARAMS = %w[ name config ]
8
+
9
+ # List repository hooks
10
+ #
11
+ # GET /repos/:user/:repo/hooks
12
+ #
13
+ def hooks(user, repo)
14
+ get("/repos/#{user}/#{repo}/hooks")
15
+ end
16
+
17
+ # Get a single hook
18
+ #
19
+ # GET /repos/:user/:repo/hooks/:id
20
+ #
21
+ def get_hook(user, repo, hook_id)
22
+ get("/repos/#{user}/#{repo}/hooks/#{hook_id}")
23
+ end
24
+
25
+ # Create a hook
26
+ #
27
+ # POST /repos/:user/:repo/hooks
28
+ #
29
+ def create_hook(user, repo, params)
30
+ _normalize_params_keys(params)
31
+ _filter_params_keys(%w[ name config active ], params)
32
+ raise ArgumentError, "Required parameters are: #{REQUIRED_PARAMS.join(', ')}" unless _validate_inputs(REQUIRED_PARAMS, params)
33
+
34
+ post("/repos/#{user}/#{repo}/hooks", params)
35
+ end
36
+
37
+ # Edit a hook
38
+ #
39
+ # PATCH /repos/:user/:repo/hooks/:id
40
+ #
41
+ def edit_hook(user, repo, hook_id, params)
42
+ _normalize_params_keys(params)
43
+ _filter_params_keys(%w[ name config active ], params)
44
+ raise ArgumentError, "Required parameters are: #{REQUIRED_PARAMS.join(', ')}" unless _validate_inputs(REQUIRED_PARAMS, params)
45
+
46
+ patch("/repos/#{user}/#{repo}/hooks/#{hook_id}")
47
+ end
48
+
49
+ # Test a hook
50
+ #
51
+ # POST /repos/:user/:repo/hooks/:id/test
52
+ #
53
+ def test_hook(user, repo, hook_id)
54
+ post("/repos/#{user}/#{repo}/hooks/#{hook_id}/test")
55
+ end
56
+
57
+ # Delete a hook
58
+ #
59
+ # DELETE /repos/:user/:repo/hooks/:id
60
+ #
61
+ def delete_hook(user, repo, hook_id)
62
+ delete("/repos/#{user}/#{repo}/hooks/#{hook_id}")
63
+ end
64
+
65
+ end # Hooks
66
+ end # Repos
67
+ end # Github
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Keys
6
+
7
+ # List keys
8
+ #
9
+ # GET /repos/:user/:repo/keys
10
+ #
11
+ def keys(user, repo)
12
+ get("/repos/#{user}/#{repo}/keys")
13
+ end
14
+
15
+ # Get a key
16
+ #
17
+ # GET /repos/:user/:repo/keys/:id
18
+ #
19
+ def get_key(user, repo, key_id)
20
+ get("/repos/#{user}/#{repo}/keys/#{key_id}")
21
+ end
22
+
23
+ # Create a key
24
+ #
25
+ # POST /repos/:user/:repo/keys
26
+ def create_key(user, repo, params={})
27
+ _normalize_params_keys(params)
28
+ _filter_params_keys(%w[ title key ], params)
29
+
30
+ post("/repos/#{user}/#{repo}/keys", params)
31
+ end
32
+
33
+ # Edit key
34
+ #
35
+ # PATCH /repos/:user/:repo/keys/:id
36
+ #
37
+ def edit_key(user, repo, key_id)
38
+ _normalize_params_keys(params)
39
+ _filter_params_keys(%w[ title key ], params)
40
+
41
+ patch("/repos/#{user}/#{repo}/keys/#{key_id}")
42
+ end
43
+
44
+ # Delete key
45
+ #
46
+ # DELETE /repos/:user/:repo/keys/:id
47
+ def delete_key(user, repo, key_id)
48
+ delete("/repos/#{user}/#{repo}/keys/#{key_id}")
49
+ end
50
+
51
+ end # Keys
52
+ end # Repos
53
+ end # Github
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ module Github
4
+ class Repos
5
+ module Watching
6
+
7
+ # List repo watchers
8
+ #
9
+ # GET /repos/:user/:repo/watchers
10
+ #
11
+ def watchers
12
+ get("/repos/#{user}/#{repo}/watchers")
13
+ end
14
+
15
+ # List repos being watched
16
+ def watched(user=nil)
17
+ if user
18
+ get("/users/#{user}/watched")
19
+ else
20
+ get("/user/watched")
21
+ end
22
+ end
23
+
24
+ # Check if you are watching a repo
25
+ #
26
+ # GET /user/watched/:user/:repo
27
+ #
28
+ def watching?(user, repo)
29
+ get("/user/watched/#{user}/#{repo}")
30
+ end
31
+
32
+ # Watch a repo
33
+ #
34
+ # PUT /user/watched/:user/:repo
35
+ #
36
+ def start_watching(user, repo)
37
+ put("/user/watched/#{user}/#{repo}")
38
+ end
39
+
40
+ # Stop watching a repo
41
+ #
42
+ # DELETE /user/watched/:user/:repo
43
+ #
44
+ def stop_watching(user, repo)
45
+ delete("/user/watched/#{user}/#{repo}")
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,75 @@
1
+ # encoding: utf-8
2
+
3
+ require 'base64'
4
+ require 'addressable/uri'
5
+ require 'set'
6
+
7
+ module Github
8
+ # Defines HTTP verbs
9
+ module Request
10
+
11
+ METHODS = [:get, :post, :put, :delete, :patch]
12
+ METHODS_WITH_BODIES = [ :post, :put, :patch ]
13
+
14
+ TOKEN_REQUIRED_REGEXP = [
15
+ /repos\/.*\/.*\/comments/,
16
+ ]
17
+
18
+ def get(path, params={}, options={})
19
+ request(:get, path, params, options)
20
+ end
21
+
22
+ def patch(path, params={}, options={})
23
+ request(:patch, path, params, options)
24
+ end
25
+
26
+ def post(path, params={}, options={})
27
+ request(:post, path, params, options)
28
+ end
29
+
30
+ def put(path, params={}, options={})
31
+ request(:put, path, params, options)
32
+ end
33
+
34
+ def delete(path, params={}, options={})
35
+ request(:delete, path, params, options)
36
+ end
37
+
38
+ def request(method, path, params, options)
39
+ if !METHODS.include?(method)
40
+ raise ArgumentError, "unkown http method: #{method}"
41
+ end
42
+
43
+ puts "EXECUTED: #{method} - #{path} with #{params} and #{options}"
44
+
45
+ response = connection(options).send(method) do |request|
46
+ case method.to_sym
47
+ when *(METHODS - METHODS_WITH_BODIES)
48
+ request.url(path, params)
49
+ when *METHODS_WITH_BODIES
50
+ request.path = path
51
+ request.body = _process_params(params) unless params.empty?
52
+ end
53
+ end
54
+ response.body
55
+ end
56
+
57
+ def _process_params(params)
58
+ return params['data'] if params.has_key?('data')
59
+ return params
60
+ end
61
+ # no need for this smizzle
62
+ def formatted_path(path, options={})
63
+ [ path, options.fetch(:format, format) ].compact.join('.')
64
+ end
65
+
66
+ def basic_auth(login, password)
67
+ auth = Base64.encode("#{login}:#{password}")
68
+ auth.gsub!("\n", "")
69
+ end
70
+
71
+ def token_auth
72
+ end
73
+
74
+ end # Request
75
+ end # Github