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

Stash webhook helper #78

Merged
merged 2 commits into from
Sep 15, 2014
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ spec/fixtures/manifests/site.pp
.DS_Store
*.swp
.bundle
.ruby-version
Gemfile.lock
65 changes: 65 additions & 0 deletions files/stash_mco.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/opt/puppet/bin/ruby
#
# This script is meant to be used with the external hooks script for Stash:
# https://marketplace.atlassian.com/plugins/com.ngs.stash.externalhooks.external-hooks
#
# The above plugin is used instead of the "Atlassian Post-Receive
# Webhooks plugin" because the Atlassian plugin does not support ignoring
# ssl self signed certificates.
#
# The external hook is useful beyond just this script, thus I prefer it.
#
require 'getoptlong'

opts = GetoptLong.new(
[ '--target', '-t', GetoptLong::REQUIRED_ARGUMENT ],
[ '--insecure', '-k', GetoptLong::NO_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
)

target = nil
insecure = nil

opts.each do |opt,arg|
case opt
when '--help'
puts <<-EOF
stash_mco.rb [OPTIONS]

-h, --help
show help

-t, --target [url]
the target url to post to
example: https://puppet:puppet@localhost:8088/payload
user/pass should be specified as part of the target

-k, --insecure
pass the 'insecure' flag to curl to ignore ssl cert verification
EOF

when '--target'
target = arg
when '--insecure'
insecure = '-k'
end
end

# the external-hooks script passes the following on stdin
#
# old_hash new_hash ref/ref/ref
#
# for example:
# 0000000000000000000000000000000000000000 ad91e3697d0711985e06d5bbbf6a7c5dc3b657f7 refs/heads/production
#
# All we care about is refs/heads/<branch_name>
#
# Test this script with:
# echo "000000 123456 refs/heads/production" | stash_mco.rb -k -t https://puppet:puppet@localhost:8088/payload

while post = gets
post_data = "{ \"ref\": \"#{post.split[2]}\" }"
end

system( "curl -q #{insecure} -d '#{post_data}' -H 'Accept: application/json' #{target}")

4 changes: 2 additions & 2 deletions files/webhook
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class Server < Sinatra::Base
# Simulate a github post:
# curl -d '{ "ref": "refs/heads/production" }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
#
# Simulate a stash post:
# curl -d '{ "refChanges":[ { "refId":"refs/heads/production" } ] }' -H "Accept: application/json" 'https://puppet:puppet@localhost:8088/payload' -k -q
# If using stash look at the stash_mco.rb script included here.
# It will filter the stash post and make it look like a github post.
#
# Simulate a Gitorious post:
# curl -X POST -d '%7b%22ref%22%3a%22master%22%7d' 'http://puppet:puppet@localhost:8088/payload' -q
Expand Down