Module: Sinatra::PuppetWebhookRoutes::Payload
- Defined in:
- lib/routes/payload.rb
Overview
Registers a POST route for the Payload endpoint on PuppetWebhook
Class Method Summary collapse
Class Method Details
.registered(puppet_webhook) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/routes/payload.rb', line 7 def self.registered(puppet_webhook) puppet_webhook.post '/payload' do # rubocop:disable Metrics/BlockLength protected! if settings.protected request.body.rewind # in case someone already read it # Short circuit if we're ignoring this event return 200 if ignore_event? # Check if content type is x-www-form-urlencoded decoded = if request.content_type.to_s.casecmp('application/x-www-form-urlencoded').zero? CGI.unescape(request.body.read).gsub(%r{^payload\=}, '') else request.body.read end verify_signature(settings.github_secret, decoded) if verify_signature? data = JSON.parse(decoded, quirks_mode: true) # Iterate the data structure to determine what's should be deployed branch = env['parsed_body'][:branch] # If prefix is enabled in our config file, determine what the prefix should be prefix = case settings.prefix when :repo env['parsed_body'][:repo_name] when :user env['parsed_body'][:repo_user] when :command, TrueClass run_prefix_command(data.to_json) when String settings.prefix end # When a branch is being deleted, a deploy against it will result in a failure, as it no longer exists. # Instead, deploy the default branch, which will purge deleted branches per the user's configuration deleted = env['parsed_body'][:deleted] branch = if deleted settings.default_branch else sanitize_input(branch) end # r10k doesn't yet know how to deploy all branches from a single source. # The best we can do is just deploy all environments by passing nil to # deploy() if we don't know the correct branch. env = if prefix.nil? || prefix.empty? || branch.nil? || branch.empty? normalize(settings.allow_uppercase, branch) else normalize(settings.allow_uppercase, "#{prefix}_#{branch}") end if ignore_env?(env) LOGGER.info("Skipping deployment of environment #{env} according to ignore_environments configuration parameter") return 200 else LOGGER.info("Deploying environment #{env}") Process.detach(fork { deploy(env, deleted) }) end end end |