Class: Sinatra::Parsers::WebhookJsonParser
- Inherits:
-
Object
- Object
- Sinatra::Parsers::WebhookJsonParser
- Defined in:
- lib/parsers/webhook_json_parser.rb
Overview
rubocop:disable Style/Documentation
Instance Method Summary collapse
- #bitbucket_webhook? ⇒ Boolean
- #branch ⇒ Object
- #call(body) ⇒ Object
- #deleted? ⇒ Boolean
- #detect_vcs ⇒ Object
- #github_webhook? ⇒ Boolean
- #gitlab_webhook? ⇒ Boolean
- #repo_name ⇒ Object
- #repo_user ⇒ Object
-
#stash_webhook? ⇒ Boolean
stash/bitbucket server.
- #tfs_webhook? ⇒ Boolean
Instance Method Details
#bitbucket_webhook? ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/parsers/webhook_json_parser.rb', line 53 def bitbucket_webhook? # https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html return false unless @data.key? 'actor' return false unless @data.key? 'repository' return false unless @data.key? 'push' true end |
#branch ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/parsers/webhook_json_parser.rb', line 68 def branch case @vcs when 'github' if @data.key? 'ref' @data['ref'].sub('refs/heads/', '') else @data['repository']['default_branch'] end when 'gitlab' @data['ref'].sub('refs/heads/', '') when 'stash' @data['refChanges'][0]['refId'].sub('refs/heads/', '') when 'bitbucket' return @data['push']['changes'][0]['new']['name'] unless deleted? @data['push']['changes'][0]['old']['name'] when 'tfs' @data['resource']['refUpdates'][0]['name'].sub('refs/heads/', '') end end |
#call(body) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/parsers/webhook_json_parser.rb', line 7 def call(body) @data = JSON.parse(body, quirks_mode: true) @vcs = detect_vcs { branch: branch, deleted: deleted?, module_name: repo_name.sub(%r{^.*-}, ''), repo_name: repo_name, repo_user: repo_user }.delete_if { |_k, v| v.nil? } end |
#deleted? ⇒ Boolean
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/parsers/webhook_json_parser.rb', line 88 def deleted? case @vcs when 'github' @data['deleted'] when 'gitlab' @data['after'] == '0000000000000000000000000000000000000000' when 'stash' @data['refChanges'][0]['type'] == 'DELETE' when 'bitbucket' @data['push']['changes'][0]['closed'] when 'tfs' @data['resource']['refUpdates'][0]['newObjectId'] == '0000000000000000000000000000000000000000' else false end end |
#detect_vcs ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/parsers/webhook_json_parser.rb', line 19 def detect_vcs return 'github' if github_webhook? return 'gitlab' if gitlab_webhook? return 'stash' if stash_webhook? return 'bitbucket' if bitbucket_webhook? return 'tfs' if tfs_webhook? raise StandardError, 'payload not recognised' end |
#github_webhook? ⇒ Boolean
28 29 30 31 32 33 34 35 36 |
# File 'lib/parsers/webhook_json_parser.rb', line 28 def github_webhook? # https://developer.github.com/v3/activity/events/types/#pushevent # X-GitHub-Event header is set, but not accessible here. return false unless @data.key? 'repository' return false unless @data['repository'].key? 'id' return false unless @data['repository'].key? 'html_url' return false unless @data['repository']['html_url'] =~ %r{github\.com} true end |
#gitlab_webhook? ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/parsers/webhook_json_parser.rb', line 38 def gitlab_webhook? # https://docs.gitlab.com/ce/user/project/integrations/webhooks.html # X-Gitlab-Event is set, but not accessible here. return false unless @data.key? 'object_kind' return false unless @data.key? 'ref' true end |
#repo_name ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/parsers/webhook_json_parser.rb', line 105 def repo_name if @vcs == 'gitlab' @data['project']['name'] elsif @vcs == 'tfs' @data['resource']['repository']['name'] else @data['repository']['name'] end end |
#repo_user ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/parsers/webhook_json_parser.rb', line 115 def repo_user # TODO: Clarify what repo_user actually is. # github is currently using the repo's 'owner', gitlab is using the user who pushed. case @vcs when 'github' @data['repository']['owner']['login'] when 'gitlab' @data['user_username'] end # TODO: Bitbucket, Stash/Bitbucket Server, TFS end |
#stash_webhook? ⇒ Boolean
stash/bitbucket server
47 48 49 50 51 |
# File 'lib/parsers/webhook_json_parser.rb', line 47 def stash_webhook? # https://confluence.atlassian.com/bitbucketserver/post-service-webhook-for-bitbucket-server-776640367.html return false unless @data.key? 'refChanges' true end |
#tfs_webhook? ⇒ Boolean
61 62 63 64 65 66 |
# File 'lib/parsers/webhook_json_parser.rb', line 61 def tfs_webhook? # https://docs.microsoft.com/en-us/vsts/service-hooks/services/webhooks return false unless @data.key? 'resource' return false unless @data.key? 'eventType' true end |