diff --git a/.gitignore b/.gitignore index f5d0056..71d4e15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ *.gem webhook.lock .ruby_version -*.log -logs/ +logs/*.log .idea/ \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 153c3cd..d1e4f76 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,13 +1,65 @@ +PATH + remote: . + specs: + puppet_webhook (0.0.1) + json + mcollective-client + rack-parser + sinatra + sinatra-contrib + slack-notifier + webrick + GEM remote: https://rubygems.org/ specs: + activesupport (5.1.4) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (~> 0.7) + minitest (~> 5.1) + tzinfo (~> 1.1) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) backports (3.10.3) + concurrent-ruby (1.0.5) + faraday (0.13.1) + multipart-post (>= 1.2, < 3) + faraday-http-cache (2.0.0) + faraday (~> 0.8) + github_changelog_generator (1.14.3) + activesupport + faraday-http-cache + multi_json + octokit (~> 4.6) + rainbow (>= 2.1) + rake (>= 10.0) + retriable (~> 2.1) + i18n (0.9.0) + concurrent-ruby (~> 1.0) json (2.1.0) + mcollective-client (2.11.3) + json + stomp + systemu + minitest (5.10.3) multi_json (1.12.2) + multipart-post (2.0.0) mustermann (1.0.1) + octokit (4.7.0) + sawyer (~> 0.8.0, >= 0.5.3) + public_suffix (3.0.0) rack (2.0.3) + rack-parser (0.7.0) + rack rack-protection (2.0.0) rack + rainbow (2.2.2) + rake + rake (12.2.1) + retriable (2.1.0) + sawyer (0.8.1) + addressable (>= 2.3.5, < 2.6) + faraday (~> 0.8, < 1.0) sinatra (2.0.0) mustermann (~> 1.0) rack (~> 2.0) @@ -21,18 +73,20 @@ GEM sinatra (= 2.0.0) tilt (>= 1.3, < 3) slack-notifier (2.3.1) + stomp (1.4.4) + systemu (2.6.5) + thread_safe (0.3.6) tilt (2.0.8) + tzinfo (1.2.4) + thread_safe (~> 0.1) webrick (1.3.1) PLATFORMS ruby DEPENDENCIES - json - sinatra - sinatra-contrib - slack-notifier - webrick + github_changelog_generator + puppet_webhook! BUNDLED WITH 1.15.4 diff --git a/bin/puppet_webhook b/bin/puppet_webhook old mode 100644 new mode 100755 diff --git a/lib/helpers/data_parsers.rb b/lib/helpers/data_parsers.rb index a4410fe..6f8b3f8 100644 --- a/lib/helpers/data_parsers.rb +++ b/lib/helpers/data_parsers.rb @@ -2,7 +2,6 @@ module DataParsers def sanitize_input(input_string) - puts input_string sanitized = Shellwords.shellescape(input_string) LOGGER.info("Module or Branch name #{sanitized} had to be escaped") unless input_string == sanitized sanitized diff --git a/lib/helpers/tasks.rb b/lib/helpers/tasks.rb index 326f882..e371f40 100644 --- a/lib/helpers/tasks.rb +++ b/lib/helpers/tasks.rb @@ -27,7 +27,6 @@ def ignore_event? return true if request.env['HTTP_X_GITHUB_EVENT'] == 'ping' list = nil unless settings.repository_events - puts list event = request.env['HTTP_X_GITHUB_EVENT'] # Negate this, because we should respond if any of these conditions are true diff --git a/lib/parsers/webhook_json_parser.rb b/lib/parsers/webhook_json_parser.rb new file mode 100644 index 0000000..cc5d8b6 --- /dev/null +++ b/lib/parsers/webhook_json_parser.rb @@ -0,0 +1,40 @@ +require 'rack/parser' +require 'json' + +module Sinatra + module Parsers + class WebhookJsonParser + def call(body) + data = JSON.parse(body, quirks_mode: true) + { + branch: parse_branch(data), + deleted: parse_deleted?(data) + } + end + + def parse_branch(data) + ( + data['ref'] rescue nil || # github & gitlab + data['refChanges'][0]['refId'] rescue nil || # stash + data['push']['changes'][0]['new']['name'] rescue nil || # bitbucket + data['resource']['refUpdates'][0]['name'] rescue nil || # TFS/VisualStudio-Git + data['repository']['default_branch'] rescue nil # github tagged release; no ref. + ).sub('refs/heads/', '') rescue nil + end + + def parse_deleted?(data) + branch_deleted = ( + data['deleted'] rescue nil || # Github + data['push']['changes'][0]['closed'] rescue nil || # Bitbucket + data['refChanges'][0]['type'] rescue nil || # Stash/Bitbucket Server + data['after'] rescue nil # Gitlab + ) + [ + true, + 'DELETED', + '0000000000000000000000000000000000000000' + ].include?(branch_deleted) + end + end + end +end diff --git a/lib/puppet_webhook.rb b/lib/puppet_webhook.rb index 7d3af56..de8f5b0 100644 --- a/lib/puppet_webhook.rb +++ b/lib/puppet_webhook.rb @@ -2,11 +2,11 @@ require 'sinatra/config_file' require 'json' require 'cgi' -require 'webhook_json_parser' +require 'parsers/webhook_json_parser' class PuppetWebhook < Sinatra::Base use Rack::Parser, :parsers => { - 'application/json' => Sinatra::WebhookJsonParser.new, + 'application/json' => Sinatra::Parsers::WebhookJsonParser.new } register Sinatra::ConfigFile config_file '../config.yml' @@ -87,13 +87,7 @@ class PuppetWebhook < Sinatra::Base data = JSON.parse(decoded, quirks_mode: true) # Iterate the data structure to determine what's should be deployed - branch = ( - data['ref'] || # github & gitlab - data['refChanges'][0]['refId'] rescue nil || # stash - data['push']['changes'][0]['new']['name'] rescue nil || # bitbucket - data['resource']['refUpdates'][0]['name'] rescue nil || # TFS/VisualStudio-Git - data['repository']['default_branch'] # github tagged release; no ref. - ).sub('refs/heads/', '') rescue nil + branch = params['branch'] # If prefix is enabled in our config file, determine what the prefix should be prefix = case settings.prefix @@ -109,18 +103,7 @@ class PuppetWebhook < Sinatra::Base # 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 - branch_deleted = ( - data['deleted'] rescue false || # Github - data['push']['changes'][0]['closed'] rescue false || # Bitbucket - data['refChanges'][0]['type'] rescue false || # Stash/Bitbucket Server - data['after'] rescue false # Gitlab - ) - - if [true, 'DELETED', '0000000000000000000000000000000000000000'].include?(branch_deleted) - deleted = true - else - deleted = false - end + deleted = params['deleted'] if deleted branch = settings.default_branch diff --git a/lib/webhook_json_parser.rb b/lib/webhook_json_parser.rb deleted file mode 100644 index e3eeb9c..0000000 --- a/lib/webhook_json_parser.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'rack/parser' -require 'json' - -module Sinatra - class WebhookJsonParser - def call(body) - data = JSON.parse(body, quirks_mode: true) - branch = ( - data['ref'] || # github & gitlab - data['refChanges'][0]['refId'] rescue nil || # stash - data['push']['changes'][0]['new']['name'] rescue nil || # bitbucket - data['resource']['refUpdates'][0]['name'] rescue nil || # TFS/VisualStudio-Git - data['repository']['default_branch'] # github tagged release; no ref. - ).sub('refs/heads/', '') rescue nil - { branch: branch } - end - end -end diff --git a/logs/.keep b/logs/.keep new file mode 100644 index 0000000..e69de29