Skip to content
Browse files

Merge pull request #315 from shiningpanda/branch-filter

Add a branch filter on ShiningPanda hook
  • Loading branch information...
2 parents 680c0f6 + 76f1eb1 commit d078d808ad577ffdef001c00d798ce1c66a337e5 @technoweenie technoweenie committed May 1, 2012
Showing with 81 additions and 28 deletions.
  1. +3 −1 docs/shiningpanda
  2. +20 −14 services/shiningpanda.rb
  3. +58 −13 test/shiningpanda_test.rb
View
4 docs/shiningpanda
@@ -13,7 +13,8 @@ Install Notes
1. Workspace - This your workspace key. For instance if your Jenkins URL is https://jenkins.shiningpanda.com/shiningpanda.org, then your workspace key is "shiningpanda.org".
2. Job - This is the name of the job you want to trigger upon commit. For instance if the URL of your job in Jenkins is https://jenkins.shiningpanda.com/shiningpanda.org/job/selenose/, then your job name is selenose.
3. Token - This is the value of the "Authentication Token" field of the "Trigger builds remotely (e.g., from scripts)" option in the "Build Triggers" section of your job configuration.
- 4. Parameters (optional) - If your job takes parameters, this is the query string built from the parameters and their value (for instance "arg1=foo&arg2=bar"). All parameters have to be properly URL-escaped.
+ 4. Branches (optional) - This is a space-separated list of branches to listen for commits on. Commits to other branches will not trigger your job. If no branches are specified, jobs are triggered for all commits.
+ 5. Parameters (optional) - If your job takes parameters, this is the query string built from the parameters and their value (for instance "arg1=foo&arg2=bar"). All parameters have to be properly URL-escaped.
Developer Notes
---------------
@@ -22,6 +23,7 @@ data
- workspace
- job
- token
+ - branches (optional)
- parameters (optional)
payload
View
34 services/shiningpanda.rb
@@ -1,6 +1,6 @@
class Service::ShiningPanda < Service
- string :workspace, :job, :token, :parameters
- white_list :workspace, :job, :parameters
+ string :workspace, :job, :token, :branches, :parameters
+ white_list :workspace, :job, :branches, :parameters
def receive_push
if workspace.empty?
@@ -18,42 +18,48 @@ def receive_push
if query.has_key?('cause')
raise_config_error "Illegal parameter: cause"
end
- Rack::Utils.parse_query(data['parameters']).each do |key, values|
- if !values.is_a?(String) and values.length > 1
- raise_config_error "Only one parameter value allowed for " + key
+ branch = payload['ref'].to_s.split('/').last
+ if branches.empty? || branches.include?(branch)
+ Rack::Utils.parse_query(data['parameters']).each do |key, values|
+ if !values.is_a?(String) and values.length > 1
+ raise_config_error "Only one parameter value allowed for " + key
+ end
end
+ query[:token] = token
+ query[:cause] = "Triggered by a push of #{payload['pusher']['name']} to #{branch} (commit: #{payload['after']})"
+ http_post url, query
end
- query[:token] = token
- query[:cause] = "Triggered by a push of #{payload['pusher']['name']} (commit: #{payload['after']})"
- http_post url, query
end
def cleanup(key)
( data.has_key?(key) and data[key] != nil ) ? data[key] : ''
end
-
+
def workspace
@workspace ||= cleanup('workspace').strip
end
-
+
def job
@job ||= cleanup('job').strip
end
def token
@token ||= cleanup('token').strip
end
-
+
+ def branches
+ @branches ||= cleanup('branches').strip.split(/\s+/)
+ end
+
def parameters
@parameters ||= Rack::Utils.parse_nested_query(cleanup('parameters'))
end
-
+
def query
@query ||= parameters.clone
end
-
+
def url
@url ||= "https://jenkins.shiningpanda.com/#{workspace}/job/#{job}/#{parameters.empty? ? 'build' : 'buildWithParameters'}"
end
-
end
View
71 test/shiningpanda_test.rb
@@ -9,7 +9,7 @@ def test_receive_push_without_parameters
@stubs.post '/shiningpanda.org/job/pygments/build' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
- assert_equal 'Triggered by a push of omansion (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
+ assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
end
svc = service(data, payload)
svc.receive_push
@@ -19,13 +19,57 @@ def test_receive_push_with_parameters
@stubs.post '/shiningpanda.org/job/pygments/buildWithParameters' do |env|
form = Rack::Utils.parse_query(env[:body])
assert_equal 'PWFm8c2T', form['token']
- assert_equal 'Triggered by a push of omansion (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
+ assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
assert_equal 'bar', form['foo']
end
svc = service(data.merge({'parameters' => 'foo=bar'}), payload)
svc.receive_push
end
-
+
+ def test_receive_push_branch_match
+ @stubs.post '/shiningpanda.org/job/pygments/build' do |env|
+ form = Rack::Utils.parse_query(env[:body])
+ assert_equal 'PWFm8c2T', form['token']
+ assert_equal 'Triggered by a push of omansion to dev (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
+ end
+ svc = service(data.merge({'branches' => 'dev'}), payload.merge({'ref' => 'refs/head/dev'}))
+ svc.receive_push
+ end
+
+ def test_receive_push_branches_match
+ @stubs.post '/shiningpanda.org/job/pygments/build' do |env|
+ form = Rack::Utils.parse_query(env[:body])
+ assert_equal 'PWFm8c2T', form['token']
+ assert_equal 'Triggered by a push of omansion to master (commit: 83d9205e73c25092ce7cb7ce530d2414e6d068cb)', form['cause']
+ end
+ svc = service(data.merge({'branches' => 'master dev'}), payload)
+ svc.receive_push
+ end
+
+ def test_receive_push_branch_mismatch
+ @stubs.post('/shiningpanda.org/job/pygments/build')
+ svc = service(data.merge({'branches' => 'dev'}), payload)
+ svc.receive_push
+ begin
+ @stubs.verify_stubbed_calls
+ rescue RuntimeError
+ else
+ assert_true false
+ end
+ end
+
+ def test_receive_push_branch_mismatch
+ @stubs.post('/shiningpanda.org/job/pygments/build')
+ svc = service(data.merge({'branches' => 'foo bar baz qux'}), payload)
+ svc.receive_push
+ begin
+ @stubs.verify_stubbed_calls
+ rescue RuntimeError
+ else
+ assert_true false
+ end
+ end
+
def test_workspace_missing
svc = service({ 'job' => 'pygments', 'token' => 'PWFm8c2T' }, payload)
assert_raise Service::ConfigurationError do
@@ -46,7 +90,7 @@ def test_workspace_blank
svc.receive_push
end
end
-
+
def test_job_missing
svc = service({ 'workspace' => 'shiningpanda.org', 'token' => 'PWFm8c2T' }, payload)
assert_raise Service::ConfigurationError do
@@ -60,14 +104,14 @@ def test_job_nil
svc.receive_push
end
end
-
+
def test_job_blank
svc = service(data.merge({'job' => ''}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
-
+
def test_token_missing
svc = service({ 'workspace' => 'shiningpanda.org', 'job' => 'pygments' }, payload)
assert_raise Service::ConfigurationError do
@@ -81,14 +125,14 @@ def test_token_nil
svc.receive_push
end
end
-
+
def test_token_blank
svc = service(data.merge({'token' => ''}), payload)
assert_raise Service::ConfigurationError do
svc.receive_push
end
end
-
+
def test_url_without_parameters
svc = service(data, payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
@@ -98,17 +142,17 @@ def test_url_nil_parameters
svc = service(data.merge({'parameters' => nil}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
-
+
def test_url_blank_parameters
svc = service(data.merge({'parameters' => ''}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
end
-
+
def test_url_with_parameters
svc = service(data.merge({'parameters' => 'foo=bar'}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/buildWithParameters", svc.url
end
-
+
def test_url_strip_workspace
svc = service(data.merge({'workspace' => ' shiningpanda.org '}), payload)
assert_equal "https://jenkins.shiningpanda.com/shiningpanda.org/job/pygments/build", svc.url
@@ -133,20 +177,21 @@ def test_multi_valued_parameter
svc.receive_push
end
end
-
+
def service(*args)
super Service::ShiningPanda, *args
end
def payload
{
'after' => '83d9205e73c25092ce7cb7ce530d2414e6d068cb',
+ 'ref' => 'refs/heads/master',
'pusher' => {
'name' => 'omansion',
}
}
end
-
+
def data
{
'workspace' => 'shiningpanda.org',

0 comments on commit d078d80

Please sign in to comment.
Something went wrong with that request. Please try again.