gwooks 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Gwooks
2
2
 
3
- A DSL for quickly creating endpoints for
4
- [GitHub post-receive webhooks](https://help.github.com/articles/post-receive-hooks).
5
- It makes it easy to perform some actions whenever one of your GitHub repos receives a push matching someone
6
- custom conditions.
3
+ A DSL for quickly creating endpoints for [GitHub post-receive
4
+ webhooks](https://help.github.com/articles/post-receive-hooks). It makes it
5
+ easy to perform some actions whenever one of your GitHub repos receives a push
6
+ matching someone custom conditions.
7
7
 
8
8
  ## Installation
9
9
 
@@ -21,7 +21,8 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- First extend the `Gwooks::Base` class and use the DSL to create actions to be performed in response to a push:
24
+ First extend the `Gwooks::Base` class and use the DSL to create actions to be
25
+ performed in response to a push:
25
26
 
26
27
  ```ruby
27
28
  class MyActions < Gwooks::Base
@@ -31,7 +32,7 @@ class MyActions < Gwooks::Base
31
32
  # a push to a repo named "my_cool_project"
32
33
 
33
34
  # Here you can also access the payload sent by GitHub, parsed to a hash:
34
- contributors = payload[:commits].map {|c| c[:author][:name] }
35
+ contributors = payload[:commits].map {|c| c[:author][:name] }.uniq
35
36
  send_email "someone@email.com", "my_cool_project received changes by: #{ contributors.join(', ') }"
36
37
  end
37
38
 
@@ -52,8 +53,8 @@ class MyActions < Gwooks::Base
52
53
  end
53
54
  ```
54
55
 
55
- Then set up an application providing an endpoint for the GitHub post-receive webhook and make use of the
56
- class you created:
56
+ Then set up an application providing an endpoint for the GitHub post-receive
57
+ webhook and make use of the class you created:
57
58
 
58
59
  ```ruby
59
60
  require "sinatra"
@@ -63,7 +64,8 @@ post "/webhook" do
63
64
  end
64
65
  ```
65
66
 
66
- Alternatively, you can use the sinatra application provided by the class `Gwooks::App`:
67
+ Alternatively, you can use the sinatra application provided by the class
68
+ `Gwooks::App`:
67
69
 
68
70
  ```ruby
69
71
  # In your config.ru
@@ -76,32 +78,41 @@ Gwooks::App.use_webhook MyActions
76
78
  run Gwooks::App
77
79
  ```
78
80
 
79
- Finally [set up your GitHub repo](https://help.github.com/articles/post-receive-hooks) to trigger a
80
- post-receive hook pointing to your endpoint. Whenever GitHub receives a push, your endpoint will be
81
- notified, and _all_ the matching actions will be performed.
81
+ Finally [set up your GitHub
82
+ repo](https://help.github.com/articles/post-receive-hooks) to trigger a
83
+ post-receive hook pointing to your endpoint. Whenever GitHub receives a push,
84
+ your endpoint will be notified, and _all_ the matching actions will be
85
+ performed.
82
86
 
83
- ### DSL methods
87
+ ### Class methods (DSL)
84
88
 
85
- Each DSL method matches a corresponding property in the payload sent by the GitHub post-receive hooks
86
- (e.g. `repository_owner_email` matches `payload["repository"]["owner"]["email"]`).
89
+ Each of the DSL methods matches a corresponding property in the payload object
90
+ (e.g. `repository_owner_email` matches
91
+ `payload["repository"]["owner"]["email"]`). The payload is the one sent by the
92
+ GitHub post-receive hooks, parsed into a hash and with an additional `branch`
93
+ property.
87
94
 
88
- Note that all the methods starting with `commits` are also aliased with the singular `commit`, and
89
- those starting with `repository` are aliased with `repo` to improve code readability.
95
+ Note that all the methods starting with `commits` are also aliased with the
96
+ singular `commit`, and those starting with `repository` are aliased with
97
+ `repo` to improve code readability.
90
98
 
91
99
  The signature is identical for all methods:
92
100
 
93
101
  ```ruby
94
- dsl_method_name(pattern, &block)
102
+ repository_owner_email(pattern, &block)
95
103
  ```
96
104
 
97
- **pattern** can be any object. If it is a Regexp, it is matched against
98
- the target property in the payload, otherwise it is checked for equality.
105
+ **pattern** can be any object. If it is a Regexp, it is matched against the
106
+ target property in the payload, otherwise it is checked for equality.
99
107
 
100
- **block** is called passing the match, or array of matches if the target
101
- property is an array (which is, in all commit* methods). The match is a
102
- MatchData object when regexp matching is used, or the matched pattern otherwise.
108
+ **block** is called once if there is at least one match, and it gets passed
109
+ the match, or an array of matches if the target property is an array (all
110
+ `commits` methods match against the array of commits and their properties). The
111
+ match is a MatchData object when regexp matching is used, or the matched
112
+ pattern otherwise. The block is evaluated in the instance scope, and thus can
113
+ access the `payload` object.
103
114
 
104
- Here is the full list of the DSL methods:
115
+ Here is the full list of DSL methods:
105
116
  ```
106
117
  after
107
118
  before
@@ -128,13 +139,43 @@ repository_watchers (alias: repo_watchers)
128
139
  ```
129
140
 
130
141
 
131
- ## Beta
142
+ ## Payload
132
143
 
133
- Please take into consideration that this is a beta release, and as such the API may change
144
+ The payload sent by GitHub is parsed into a hash-like object and is available
145
+ through the `payload` instance method. For convenience, a `branch` property is
146
+ added to it, indicating the branch that received the push (extrapolated from
147
+ the `ref`). The payload property can be indifferently accessed using symbol or
148
+ string keys.
149
+
150
+ The `payload` object also has a `resolve` method that takes a string key and
151
+ returns the matched property (or array of matched properties) or `nil` if the
152
+ property or one of its parents is not set:
153
+
154
+ ```ruby
155
+ payload.resolve "repository.owner.name"
156
+ # returns the repo owner name, or nil if either payload["repository"],
157
+ # payload["repository"]["owner"] or payload["repository"]["owner"]["name"] are
158
+ # not set.
159
+
160
+ payload.resolve "commits.author.name"
161
+ # returns the array of each commit's author name
162
+ # Equivalent to `payload["commits"].map {|c| c["author"]["name"]}`, but it
163
+ # does not raise errors if some property is not set.
164
+ ```
165
+
166
+
167
+ ## Alpha release
168
+
169
+ Please take into consideration that this is an alpha release, and as such the API
170
+ may change frequently.
134
171
 
135
172
 
136
173
  ## Changelog
137
174
 
175
+ v0.0.5 - Add `branch` payload property and corresponding DSL method
176
+
177
+ v0.0.4 - Payload with indifferent access
178
+
138
179
  v0.0.3 - Alias `repository_` methods to `repo_`
139
180
 
140
181
  v0.0.2 - Alias `commits_` methods to `commit_`
data/lib/gwooks/base.rb CHANGED
@@ -24,6 +24,7 @@ module Gwooks
24
24
  method_names = %w(
25
25
  after
26
26
  before
27
+ branch
27
28
  commits_added
28
29
  commits_author_email
29
30
  commits_author_name
@@ -11,8 +11,17 @@ module Gwooks
11
11
  hash[key.to_s] if key.is_a? Symbol
12
12
  end
13
13
  payload = JSON.parse(payload) if payload.is_a? String
14
+ set_branch payload
14
15
  new_hash.update(payload)
15
16
  end
17
+
18
+ private
19
+
20
+ def set_branch(payload)
21
+ if payload["ref"] and payload["ref"].start_with? "refs/heads/"
22
+ payload["branch"] = payload["ref"].gsub(/^refs\/heads\//, "")
23
+ end
24
+ end
16
25
  end
17
26
 
18
27
  def resolve(key)
@@ -51,7 +60,7 @@ module Gwooks
51
60
  else
52
61
  obj
53
62
  end
54
- end
63
+ end
55
64
 
56
65
  end
57
66
  end
@@ -1,3 +1,3 @@
1
1
  module Gwooks
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -35,6 +35,7 @@ describe "subclass of Gwooks::Base" do
35
35
  method_names = %w(
36
36
  after
37
37
  before
38
+ branch
38
39
  commits_added
39
40
  commits_author_email
40
41
  commits_author_name
@@ -13,6 +13,11 @@ describe Gwooks::Payload do
13
13
  payload = Gwooks::Payload.new({"foo" => "bar"})
14
14
  payload.should == {"foo" => "bar"}
15
15
  end
16
+
17
+ it "creates the additional 'branch' property from ref" do
18
+ payload = Gwooks::Payload.new({"ref" => "refs/heads/foo"})
19
+ payload.should == {"ref" => "refs/heads/foo", "branch" => "foo"}
20
+ end
16
21
  end
17
22
  end
18
23
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gwooks
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease: false
4
+ hash: 21
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Luca Ongaro
@@ -15,12 +15,9 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-01-15 00:00:00 +01:00
19
- default_executable:
18
+ date: 2013-01-19 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: json
23
- prerelease: false
24
21
  requirement: &id001 !ruby/object:Gem::Requirement
25
22
  none: false
26
23
  requirements:
@@ -30,11 +27,11 @@ dependencies:
30
27
  segments:
31
28
  - 0
32
29
  version: "0"
30
+ prerelease: false
33
31
  type: :runtime
32
+ name: json
34
33
  version_requirements: *id001
35
34
  - !ruby/object:Gem::Dependency
36
- name: sinatra
37
- prerelease: false
38
35
  requirement: &id002 !ruby/object:Gem::Requirement
39
36
  none: false
40
37
  requirements:
@@ -44,11 +41,11 @@ dependencies:
44
41
  segments:
45
42
  - 0
46
43
  version: "0"
44
+ prerelease: false
47
45
  type: :runtime
46
+ name: sinatra
48
47
  version_requirements: *id002
49
48
  - !ruby/object:Gem::Dependency
50
- name: rspec
51
- prerelease: false
52
49
  requirement: &id003 !ruby/object:Gem::Requirement
53
50
  none: false
54
51
  requirements:
@@ -58,11 +55,11 @@ dependencies:
58
55
  segments:
59
56
  - 0
60
57
  version: "0"
58
+ prerelease: false
61
59
  type: :development
60
+ name: rspec
62
61
  version_requirements: *id003
63
62
  - !ruby/object:Gem::Dependency
64
- name: rack-test
65
- prerelease: false
66
63
  requirement: &id004 !ruby/object:Gem::Requirement
67
64
  none: false
68
65
  requirements:
@@ -72,7 +69,9 @@ dependencies:
72
69
  segments:
73
70
  - 0
74
71
  version: "0"
72
+ prerelease: false
75
73
  type: :development
74
+ name: rack-test
76
75
  version_requirements: *id004
77
76
  description: A DSL for quickly creating endpoints for GitHub post-receive webhooks.
78
77
  email:
@@ -100,7 +99,6 @@ files:
100
99
  - spec/gwooks/payload_spec.rb
101
100
  - spec/integration/integration_spec.rb
102
101
  - spec/spec_helper.rb
103
- has_rdoc: true
104
102
  homepage: https://github.com/lucaong/gwooks
105
103
  licenses: []
106
104
 
@@ -130,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
128
  requirements: []
131
129
 
132
130
  rubyforge_project:
133
- rubygems_version: 1.3.7
131
+ rubygems_version: 1.8.24
134
132
  signing_key:
135
133
  specification_version: 3
136
134
  summary: A DSL for quickly creating endpoints for GitHub post-receive webhooks. It provides methods for executing blocks of code when GitHub posts a payload matching some conditions in response to a code push.