From 37fc80e5e239a934fef5f1b2c30df52a15c6f1cf Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Thu, 6 Aug 2015 19:24:54 -0700 Subject: [PATCH 1/3] Automatically calculate some prefixes Allows one to set the webhook's `prefix` to: * `:repo`: the repository name * `:user`: the owner of the repository * `:command`: run a command using the posted data to determine prefix * This is existing behaviour * Uses `prefix command` * Aliased to `true` for backwards compatibility * A string: pass the prefix as a static string * `false`: disable the prefix This is currently tested only on GitHub. --- manifests/params.pp | 2 +- templates/webhook.bin.erb | 35 ++++++++++++++++++++++++++--------- templates/webhook.yaml.erb | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index 14de707a..1853c347 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -39,7 +39,7 @@ $webhook_protected = true $webhook_discovery_timeout = 10 $webhook_client_timeout = 120 - $webhook_prefix = false + $webhook_prefix = false # :repo | :user | :command (or true for backwards compatibility) | 'string' | false $webhook_prefix_command = '/bin/echo example' $webhook_enable_ssl = true $webhook_use_mcollective = true diff --git a/templates/webhook.bin.erb b/templates/webhook.bin.erb index 30eed1f0..e86a6c7d 100755 --- a/templates/webhook.bin.erb +++ b/templates/webhook.bin.erb @@ -126,17 +126,24 @@ class Server < Sinatra::Base # github sends a 'ref', stash sends an array in 'refChanges' branch = ( data['ref'] || data['refChanges'][0]['refId'] rescue nil || data['push']['changes'][0]['new']['name'] ).sub('refs/heads/', '') - # If prefix is enabled in our config file run the command to determine the prefix - if $config['prefix'] - prefix = run_prefix_command(data.to_json) - if prefix.empty? - deploy(branch) - else - deploy("#{prefix}_#{branch}") - end - else + # If prefix is enabled in our config file, determine what the prefix should be + prefix = case $config['prefix'] + when :repo + repo_name(data) + when :user + repo_user(data) + when :command, TrueClass + run_prefix_command(data.to_json) + when String + $config['prefix'] + end + + if prefix.nil? or prefix.empty? deploy(branch) + else + deploy("#{prefix}_#{branch}") end + end not_found do @@ -239,6 +246,16 @@ class Server < Sinatra::Base @auth.credentials == [$config['user'],$config['pass']] end #end authorized? + def repo_name(data) + # Tested with GitHub only + data['repository']['name'] rescue nil + end + + def repo_user(data) + # Tested with GitHub only + data['repository']['owner']['name'] rescue nil + end + def run_prefix_command(payload) IO.popen($config['prefix_command'], mode='r+') do |io| io.write payload.to_s diff --git a/templates/webhook.yaml.erb b/templates/webhook.yaml.erb index 0a4642f2..c3d6943d 100755 --- a/templates/webhook.yaml.erb +++ b/templates/webhook.yaml.erb @@ -3,7 +3,7 @@ <% if value != :undef and ! value.nil? -%> <% value = case value - when TrueClass, FalseClass + when TrueClass, FalseClass, Symbol value when Array value.sort.inspect.to_s From 39f26192af905720bd4f97a8950e1daafabedc68 Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Fri, 7 Aug 2015 13:24:05 -0700 Subject: [PATCH 2/3] yeah... so about that symbol thing --- templates/webhook.yaml.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/templates/webhook.yaml.erb b/templates/webhook.yaml.erb index c3d6943d..36be99cf 100755 --- a/templates/webhook.yaml.erb +++ b/templates/webhook.yaml.erb @@ -3,10 +3,12 @@ <% if value != :undef and ! value.nil? -%> <% value = case value - when TrueClass, FalseClass, Symbol + when TrueClass, FalseClass value when Array value.sort.inspect.to_s + when /^:/ + value.to_sym else "\"#{value}\"" end From 537d3ab42cf6e6ef5a4e6f92a49086b2d3dbffd3 Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Fri, 7 Aug 2015 13:25:49 -0700 Subject: [PATCH 3/3] update params comment --- manifests/params.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/params.pp b/manifests/params.pp index 1853c347..d01546a9 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -39,7 +39,7 @@ $webhook_protected = true $webhook_discovery_timeout = 10 $webhook_client_timeout = 120 - $webhook_prefix = false # :repo | :user | :command (or true for backwards compatibility) | 'string' | false + $webhook_prefix = false # ':repo' | ':user' | ':command' (or true for backwards compatibility) | 'string' | false $webhook_prefix_command = '/bin/echo example' $webhook_enable_ssl = true $webhook_use_mcollective = true