Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically calculate some prefixes #236

Merged
merged 3 commits into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 26 additions & 9 deletions templates/webhook.bin.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions templates/webhook.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
value
when Array
value.sort.inspect.to_s
when /^:/
value.to_sym
else
"\"#{value}\""
end
Expand Down