Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Get rack/parser working #5

Merged
merged 1 commit into from
Nov 1, 2017
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.gem
webhook.lock
.ruby_version
*.log
logs/
logs/*.log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should log to /var/log/puppet-webhook or the like, rather than in the gem directory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I just haven't gotten around to it yet.

.idea/
64 changes: 59 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Empty file modified bin/puppet_webhook
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion lib/helpers/data_parsers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/helpers/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions lib/parsers/webhook_json_parser.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 4 additions & 21 deletions lib/puppet_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -87,13 +87,7 @@ class PuppetWebhook < Sinatra::Base
data = JSON.parse(decoded, quirks_mode: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be for a future improvement but I'd expect that if there's a WebhookJsonParser that it'd do all of it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do believe that Rack::Parser is already doing the work. We just need to update our code to add what we need to the params Rack::Parser builds


# 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
Expand All @@ -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
Expand Down
18 changes: 0 additions & 18 deletions lib/webhook_json_parser.rb

This file was deleted.

Empty file added logs/.keep
Empty file.