github-hooker 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 1.9.3-p0-gc
@@ -0,0 +1,16 @@
1
+ # CHANGELOG
2
+
3
+ ## not released yet
4
+
5
+ * `github-hooker campfire` now accepts an optional `--token` that overwrites the `campfire_token` that's usually defined in your `~/.github-hooker.yml`.
6
+
7
+ ## 0.1.1
8
+
9
+ * `github-hooker delete` now doesn't raise an JSON parse exception.
10
+
11
+ ## 0.1.0
12
+
13
+ * creates campfire and web hooks
14
+ * lists hooks
15
+ * deletes hooks
16
+ * first public release!
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- github-hooker (0.1.1)
4
+ github-hooker (0.1.2)
5
5
  activesupport (~> 3.0)
6
6
  rest-client (~> 1.6.7)
7
7
  thor (~> 0.14.6)
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # github-hooker
2
2
 
3
- This is an internal gem at PlataformaTec (<http://plataformatec.com.br>).
3
+ This is an internal gem at Plataformatec (<http://plataformatec.com.br>).
4
4
 
5
- We use it to create and delete hooks in our github repositories (like campfire notifications for push and pull_requests and CI integration with web push hooks).
5
+ We use it to create and delete hooks in our github repositories (like campfire notifications for push and pull_request and CI integration with web push hooks).
6
6
 
7
7
  ## Configuration
8
8
 
@@ -12,14 +12,16 @@ First, create `~/.github-hooker.yml` with
12
12
  user: github_username
13
13
  password: github_password
14
14
  campfire_token: campfire_api_token
15
+ #optional api_url, default https://api.github.com
16
+ #api_url: https://mygithubenterprise/api/v3
15
17
  ```
16
18
 
19
+ Then you can use the command `github-hooker` to list, create email, web and campfire hooks or delete them. Other hooks are not yet implemented (pull requests are appreciated!).
20
+
17
21
  ### Why I need to write my password?
18
22
 
19
23
  The hook API is only accessible by the v3 Github API. There's a Oauth2 authentication method, but in order to use that you would need to set up a new application with Github, and then collect the token by an http callback (that must be accessible on the web). For the sake of simplicity, we use the other way to authenticate (http auth basic).
20
24
 
21
- Then you can use the command `github-hooker` to list, create web and campfire hooks or delete them. Other hooks are not yet implemented (pull requests are appreciated!).
22
-
23
25
  ## Usage
24
26
 
25
27
  The user specified MUST have administration rights for the repositories you want to set/delete hooks.
@@ -31,6 +33,7 @@ $ github-hooker
31
33
  Tasks:
32
34
  github-hooker help [TASK] # Describe available tasks or one specific task
33
35
  github-hooker list user/repo # List hooks in the given repository
36
+ github-hooker email user/repo events --address=ADDRESS # Add an email hook in the given repository. Events must be separated by commas.
34
37
  github-hooker web user/repo events --url=URL # Add a web hook in the given repository. Events must be separated by commas.
35
38
  github-hooker campfire user/repo events --room=ROOM --subdomain=SUBDOMAIN # Add a campfire hook in the given repository. Events must be separated by commas.
36
39
  github-hooker delete user/repo hook_number # Delete the hook from the given repository
@@ -42,10 +45,16 @@ Tasks:
42
45
  github-hooker list plataformatec/devise
43
46
  ```
44
47
 
48
+ ### Create a new email hook
49
+
50
+ ```
51
+ github-hooker email plataformatec/devise "push" --address=email@mydomain.com
52
+ ```
53
+
45
54
  ### Create a new web hook
46
55
 
47
56
  ```
48
- github-hooker web plataformatec/devise "pull_requests, push" --url=http://mycallback.com/callback
57
+ github-hooker web plataformatec/devise "pull_request, push" --url=http://mycallback.com/callback
49
58
  ```
50
59
 
51
60
  This creates a new web hook that calls the url specified by `--url`. The events that this hooks listens must be the third argument and they must be separeted by commas.
@@ -53,7 +62,7 @@ This creates a new web hook that calls the url specified by `--url`. The events
53
62
  ### Create a new campfire hook
54
63
 
55
64
  ```
56
- github-hooker campfire plataformatec/devise pull_requests,push,issue_comment --room="My Room" --subdomain="My Subdomain"
65
+ github-hooker campfire plataformatec/devise pull_request,push,issue_comment --room="My Room" --subdomain="My Subdomain"
57
66
  ```
58
67
 
59
68
  Creates a new campfire hook. The token used for authentication with Campfire must be provided in your `~/.github-hooker.yml` (see Configuration above).
@@ -70,21 +79,30 @@ Deletes the hook 1010 from the given repository. You can get the hook id from th
70
79
 
71
80
  The available events that github gives us are:
72
81
 
82
+ Email events:
83
+ - push
84
+
73
85
  Campfire events:
74
86
 
75
87
  - push
76
- - pull_requests
88
+ - pull_request
77
89
  - issues
78
90
 
79
91
  Web events:
80
92
 
81
93
  - push
82
- - pull_requests
94
+ - pull_request
83
95
  - issues
84
96
  - issue_comment
85
97
 
86
98
  Github's documentation about hooks (http://developer.github.com/v3/repos/hooks/ and https://api.github.com/hooks) does not have all these hooks listed, but they are working with us.
87
99
 
100
+ ## Naming
101
+
102
+ This gem is named after the famous [Hooker Telescope](http://en.wikipedia.org/wiki/Hooker_Telescope), in operation in the Mount Wilson Observatory. Edwin Hubble used this telescope to discover the expansion of the universe.
103
+
104
+ ![Hooker Telescope](http://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/100inchHooker.jpg/250px-100inchHooker.jpg)
105
+
88
106
  ## License
89
107
 
90
- MIT License. Copyright 2012 Plataforma Tecnologia. http://blog.plataformatec.com.br
108
+ MIT License. Copyright 2012 Plataformatec. http://plataformatec.com.br
@@ -1,38 +1,37 @@
1
1
  require "github-hooker/version"
2
+ require "github-hooker/config"
2
3
  require 'net/http'
3
4
  require 'json'
4
- require 'yaml'
5
5
  require 'restclient'
6
6
  require 'active_support/core_ext/hash/reverse_merge'
7
7
 
8
8
  module Github
9
9
  module Hooker
10
10
  def self.hooks(repo)
11
- url = "https://api.github.com/repos/#{repo}/hooks"
12
- github_api(:get, url)
11
+ path = "/repos/#{repo}/hooks"
12
+ github_api(:get, path)
13
13
  end
14
14
 
15
15
  def self.add_hook(repo, payload={})
16
- url = "https://api.github.com/repos/#{repo}/hooks"
16
+ path = "/repos/#{repo}/hooks"
17
17
  payload = payload.reverse_merge(:active => true)
18
- github_api(:post, url, :payload => payload.to_json)
18
+ github_api(:post, path, :payload => payload.to_json)
19
19
  end
20
20
 
21
21
  def self.delete_hook(repo, hook)
22
- url = "https://api.github.com/repos/#{repo}/hooks/#{hook}"
23
- github_api(:delete, url)
22
+ path = "/repos/#{repo}/hooks/#{hook}"
23
+ github_api(:delete, path)
24
24
  end
25
25
 
26
- def self.config
27
- @config ||= YAML.load_file(File.expand_path(config_filename))
28
- end
26
+ def self.github_api(method, path, options={})
27
+ github_url = Github::Hooker::Config.config.fetch("api_url", "https://api.github.com")
28
+ url = github_url + path
29
29
 
30
- def self.github_api(method, url, options={})
31
30
  options.reverse_merge!(
32
- :method => method,
33
- :url => url,
34
- :user => config["user"],
35
- :password => config["password"]
31
+ :method => method,
32
+ :url => url,
33
+ :user => Github::Hooker::Config.config["user"],
34
+ :password => Github::Hooker::Config.config["password"]
36
35
  )
37
36
  response = RestClient::Request.execute(options)
38
37
  case response.code
@@ -42,9 +41,5 @@ module Github
42
41
  response
43
42
  end
44
43
  end
45
-
46
- def self.config_filename
47
- "~/.github-hooker.yml"
48
- end
49
44
  end
50
45
  end
@@ -8,9 +8,12 @@ module Github
8
8
  hooks = Github::Hooker.hooks(repo)
9
9
  hooks.each do |hook|
10
10
  puts "#{hook['url']}"
11
- puts "> name: #{hook['name']}"
12
- puts "> events: #{hook['events'].join(", ")}"
13
- puts "> config: #{hook['config']}"
11
+ puts "> name: #{hook['name']}"
12
+ puts "> updated_at: #{hook['updated_at']}"
13
+ puts "> created_at: #{hook['created_at']}"
14
+ puts "> events: #{hook['events'].join(", ")}"
15
+ puts "> active: #{hook['active']}"
16
+ puts "> config: #{hook['config']}"
14
17
  puts
15
18
  end unless hooks.nil?
16
19
  end
@@ -19,16 +22,30 @@ module Github
19
22
  desc "campfire user/repo events", "Add a campfire hook in the given repository. Events must be separated by commas."
20
23
  method_option :room, :required => true
21
24
  method_option :subdomain, :required => true
25
+ method_option :token, :required => false
22
26
  def campfire(repo, events)
23
27
  handle_404 do
24
28
  check_config!
25
29
  events = split_events(events)
26
- Github::Hooker.add_hook(repo, :name => "campfire", :events => events, :config => options)
30
+ Github::Hooker.add_hook(repo, :name => "campfire", :events => events, :config => options.reverse_merge("token" => Github::Hooker::Config.config['campfire_token']))
31
+ end
32
+ end
33
+
34
+ desc "email user/repo events", "Add an email hook in the given repository. Events must be separated by commas."
35
+ method_option :address, :required => true
36
+ method_option :secret
37
+ method_option :send_from_author, :default => "1"
38
+ def email(repo, events)
39
+ handle_404 do
40
+ check_config!
41
+ events = split_events(events)
42
+ Github::Hooker.add_hook(repo, :name => "email", :events => events, :config => options)
27
43
  end
28
44
  end
29
45
 
30
46
  desc "web user/repo events", "Add a web hook in the given repository. Events must be separated by commas."
31
47
  method_option :url, :required => true
48
+ method_option :secret
32
49
  def web(repo, events)
33
50
  handle_404 do
34
51
  check_config!
@@ -57,7 +74,7 @@ module Github
57
74
  end
58
75
 
59
76
  def check_config!
60
- error("~/.github-hooker.yml is not present. Please set 'user', 'password' and 'campfire_token'.") unless File.exist?(File.expand_path(Github::Hooker.config_filename))
77
+ error("~/.github-hooker.yml is not present. Please set 'user', 'password' and 'campfire_token'.") unless File.exist?(File.expand_path(Github::Hooker::Config.filename))
61
78
  end
62
79
 
63
80
  def resource_not_found_message!
@@ -0,0 +1,15 @@
1
+ require 'yaml'
2
+
3
+ module Github
4
+ module Hooker
5
+ module Config
6
+ def self.filename
7
+ "~/.github-hooker.yml"
8
+ end
9
+
10
+ def self.config
11
+ @config ||= YAML.load_file(File.expand_path(filename))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  module Github
2
2
  module Hooker
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -43,5 +43,26 @@
43
43
  "id": 157271,
44
44
  "url": "https://api.github.com/repos/user/repo/hooks/157271",
45
45
  "active": true
46
+ },
47
+ {
48
+ "updated_at": "2012-11-22T07:36:52Z",
49
+ "last_response": {
50
+ "status": "unused",
51
+ "message": null,
52
+ "code": null
53
+ },
54
+ "created_at": "2012-11-22T07:36:52Z",
55
+ "events": [
56
+ "push"
57
+ ],
58
+ "name": "email",
59
+ "config": {
60
+ "address": "user@example.com",
61
+ "send_from_author": "1",
62
+ "secret": "X-Test-github-hooker"
63
+ },
64
+ "id": 561345,
65
+ "url": "https://api.github.com/repos/user/repo/hooks/561345",
66
+ "active": true
46
67
  }
47
68
  ]
@@ -5,11 +5,9 @@ require 'github-hooker'
5
5
  require 'github-hooker/cli'
6
6
 
7
7
  describe "github-hooker" do
8
- subject { Github::Hooker::CLI.new }
9
-
10
8
  before do
11
- Github::Hooker.stub(:config_filename).and_return("/tmp/.github-hooker.yml")
12
- FileUtils.touch(Github::Hooker.config_filename)
9
+ Github::Hooker::Config.stub(:config_filename).and_return("/tmp/.github-hooker.yml")
10
+ FileUtils.touch(Github::Hooker::Config.config_filename)
13
11
  stub_config!
14
12
  end
15
13
 
@@ -26,7 +24,7 @@ describe "github-hooker" do
26
24
  }
27
25
  ]
28
26
  Github::Hooker.stub(:hooks).with("user/repo").and_return(hooks)
29
- subject.list("user/repo")
27
+ cli("list user/repo")
30
28
  end
31
29
 
32
30
  it "handles 404 errors in github API" do
@@ -34,28 +32,44 @@ describe "github-hooker" do
34
32
  to_return(:status => 404, :body => "NotFound", :headers => {})
35
33
 
36
34
  $stdout.should_receive(:puts).with("Resource Not Found (404): This repository may not exist or you may not have access to it.")
37
- expect { subject.list("user/non-existent-repo") }.to raise_error(SystemExit)
35
+ expect { cli("list user/non-existent-repo") }.to raise_error(SystemExit)
38
36
  end
39
37
  end
40
38
 
41
39
  describe "campfire" do
42
40
  it "calls Github::Hooker with the correct arguments" do
43
- Github::Hooker.stub(:add_hook).with("user/repo", {:name => "campfire", :events => ["pull_requests", "issue"], :config => {}})
44
- subject.campfire("user/repo", "pull_requests, issue")
41
+ Github::Hooker.stub(:add_hook).with("user/repo", {:name => "campfire", :events => ["pull_request", "issue"], :config=> {"token"=>"token", "room"=>"RROM", "subdomain"=>"SUBDOMAIN"}})
42
+ cli("campfire user/repo pull_request,issue --room=RROM --subdomain=SUBDOMAIN")
43
+ end
44
+
45
+ it "ignores campfire_token if --token is passed" do
46
+ Github::Hooker.stub(:add_hook).with("user/repo", {:name => "campfire", :events => ["pull_request", "issue"], :config => {"token"=>"cli_token", "room"=>"ROOM", "subdomain"=>"SUBDOMAIN"}})
47
+ cli("campfire user/repo pull_request,issue --room=ROOM --subdomain=SUBDOMAIN --token=cli_token")
48
+ end
49
+ end
50
+
51
+ describe "email" do
52
+ it "calls Github::Hooker with the correct arguments" do
53
+ Github::Hooker.stub(:add_hook).with("user/repo", {:name => "email", :events => ["push"], :config=> {"address"=>"user@example.com", "secret"=>"X-Test-github-hooker", "send_from_author" => "1"}})
54
+ cli("email user/repo push --address=user@example.com --secret=X-Test-github-hooker --send-from-author=1")
45
55
  end
46
56
  end
47
57
 
48
58
  describe "web" do
49
59
  it "calls Github::Hooker with the correct arguments" do
50
- Github::Hooker.stub(:add_hook).with("user/repo", {:name => "web", :events => ["pull_requests", "issue"], :config => {}})
51
- subject.web("user/repo", "pull_requests, issue")
60
+ Github::Hooker.stub(:add_hook).with("user/repo", {:name => "web", :events => ["pull_request", "issue"], :config=> {"url"=>"http://example.com/callback"}})
61
+ cli("web user/repo pull_request,issue --url=http://example.com/callback")
52
62
  end
53
63
  end
54
64
 
55
65
  describe "delete" do
56
66
  it "calls Github::Hooker with the correct arguments" do
57
- Github::Hooker.stub(:delete_hook).with("user/repo", 1010)
58
- subject.delete("user/repo", 1010)
67
+ Github::Hooker.stub(:delete_hook).with("user/repo", "1010")
68
+ cli("delete user/repo 1010")
59
69
  end
60
70
  end
71
+
72
+ def cli(string)
73
+ Github::Hooker::CLI.start(string.split(" "))
74
+ end
61
75
  end
@@ -36,6 +36,23 @@ describe Github::Hooker do
36
36
  subject.add_hook("user/repo", hook_options)
37
37
  end
38
38
 
39
+ it "adds a email hook in the given repo" do
40
+ WebMock.stub_request(:post, "https://user:password@api.github.com/repos/user/repo/hooks").
41
+ with(:body => "{\"active\":\"true\",\"name\":\"email\",\"events\":[\"push\"],\"config\":{\"address\":\"user@example.com\",\"send_from_author\":\"1\"}}").
42
+ to_return(:status => 201, :body => "{}", :headers => {})
43
+
44
+ hook_options = {
45
+ :name => "email",
46
+ :events => ["push"],
47
+ :active => "true",
48
+ :config => {
49
+ :address => "user@example.com",
50
+ :send_from_author => "1"
51
+ }
52
+ }
53
+ subject.add_hook("user/repo", hook_options)
54
+ end
55
+
39
56
  it "adds a new web hook in the given repo" do
40
57
  WebMock.stub_request(:post, "https://user:password@api.github.com/repos/user/repo/hooks").
41
58
  with(:body => "{\"active\":\"true\",\"name\":\"web\",\"events\":[\"pull_request\",\"issue\"],\"config\":{\"url\":\"http://example.com/callback\"}}").
@@ -1,3 +1,3 @@
1
1
  def stub_config!
2
- Github::Hooker.stub(:config).and_return({ "user" => "user", "password" => "password", "campfire_token" => "token" })
2
+ Github::Hooker::Config.stub(:config).and_return({ "user" => "user", "password" => "password", "campfire_token" => "token" })
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-hooker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-08 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &70291867146740 !ruby/object:Gem::Requirement
16
+ requirement: &70196237187800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.7
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70291867146740
24
+ version_requirements: *70196237187800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: thor
27
- requirement: &70291867146020 !ruby/object:Gem::Requirement
27
+ requirement: &70196237186940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.14.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70291867146020
35
+ version_requirements: *70196237186940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &70291867145520 !ruby/object:Gem::Requirement
38
+ requirement: &70196237186460 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70291867145520
46
+ version_requirements: *70196237186460
47
47
  description: ! ' github-hooker creates and deletes hooks in your github repository '
48
48
  email:
49
49
  - george@plataformatec.com.br
@@ -53,7 +53,9 @@ extensions: []
53
53
  extra_rdoc_files: []
54
54
  files:
55
55
  - .gitignore
56
+ - .rbenv-version
56
57
  - .rspec
58
+ - CHANGELOG.md
57
59
  - Gemfile
58
60
  - Gemfile.lock
59
61
  - Guardfile
@@ -63,6 +65,7 @@ files:
63
65
  - github-hooker.gemspec
64
66
  - lib/github-hooker.rb
65
67
  - lib/github-hooker/cli.rb
68
+ - lib/github-hooker/config.rb
66
69
  - lib/github-hooker/version.rb
67
70
  - spec/fixtures/list_hooks.json
68
71
  - spec/github-hooker/cli_spec.rb
@@ -83,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
86
  version: '0'
84
87
  segments:
85
88
  - 0
86
- hash: 2168196875312501794
89
+ hash: -2266126513958876058
87
90
  required_rubygems_version: !ruby/object:Gem::Requirement
88
91
  none: false
89
92
  requirements:
@@ -92,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
95
  version: '0'
93
96
  segments:
94
97
  - 0
95
- hash: 2168196875312501794
98
+ hash: -2266126513958876058
96
99
  requirements: []
97
100
  rubyforge_project: github-hooker
98
101
  rubygems_version: 1.8.11