This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Get rack/parser working #5
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
*.gem | ||
webhook.lock | ||
.ruby_version | ||
*.log | ||
logs/ | ||
logs/*.log | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do believe that |
||
|
||
# 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 | ||
|
This file was deleted.
Oops, something went wrong.
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.