module Sinatra::Parsers::WebhookParser

Attributes

data[RW]
env[RW]

Public Instance Methods

bitbucket_server_webhook?() click to toggle source

bitbucket server

# File lib/parsers/webhook_parser.rb, line 43
def bitbucket_server_webhook?
  # https://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html
  env.key?('HTTP_X_EVENT_KEY') && env.key?('HTTP_X_REQUEST_ID')
end
bitbucket_webhook?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 48
def bitbucket_webhook?
  # https://confluence.atlassian.com/bitbucket/event-payloads-740262817.html
  env.key?('HTTP_X_EVENT_KEY') && env.key?('HTTP_X_HOOK_UUID')
end
branch() click to toggle source
# File lib/parsers/webhook_parser.rb, line 66
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 'bitbucket-server'
    @data['changes'][0]['refId'].sub('refs/heads/', '')
  when 'bitbucket'
    return @data['push']['changes'][0]['new']['name'] unless deleted?

    @data['push']['changes'][0]['old']['name']
  when 'stash'
    @data['refChanges'][0]['refId'].sub('refs/heads/', '')
  when 'tfs'
    @data['resource']['refUpdates'][0]['name'].sub('refs/heads/', '')
  end
end
call(body) click to toggle source
# File lib/parsers/webhook_parser.rb, line 9
def call(body)
  @data = parse_data(body)
  @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?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 89
def deleted?
  case @vcs
  when 'github'
    @data['deleted']
  when 'gitlab'
    @data['after'] == '0000000000000000000000000000000000000000'
  when 'bitbucket-server'
    @data['changes'][0]['type'] == 'DELETE'
  when 'bitbucket'
    @data['push']['changes'][0]['closed']
  when 'stash'
    @data['refChanges'][0]['type'] == 'DELETE'
  when 'tfs'
    @data['resource']['refUpdates'][0]['newObjectId'] == '0000000000000000000000000000000000000000'
  end
end
detect_vcs() click to toggle source
# File lib/parsers/webhook_parser.rb, line 21
def detect_vcs
  return 'github'           if github_webhook?
  return 'gitlab'           if gitlab_webhook?
  return 'bitbucket-server' if bitbucket_server_webhook?
  return 'bitbucket'        if bitbucket_webhook?
  return 'stash'            if stash_webhook?
  return 'tfs'              if tfs_webhook?

  raise StandardError, 'payload not recognised'
end
github_webhook?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 32
def github_webhook?
  # https://developer.github.com/v3/activity/events/types/#pushevent
  env.key?('HTTP_X_GITHUB_EVENT')
end
gitlab_webhook?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 37
def gitlab_webhook?
  # https://docs.gitlab.com/ce/user/project/integrations/webhooks.html
  env.key?('HTTP_X_GITLAB_EVENT')
end
repo_name() click to toggle source
# File lib/parsers/webhook_parser.rb, line 106
def repo_name
  if @vcs == 'gitlab'
    @data['project']['name']
  elsif @vcs == 'tfs'
    @data['resource']['repository']['name']
  else
    @data['repository']['name']
  end
end
repo_user() click to toggle source
# File lib/parsers/webhook_parser.rb, line 116
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?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 53
def stash_webhook?
  # https://confluence.atlassian.com/bitbucketserver/post-service-webhook-for-bitbucket-server-776640367.html
  env.key?('HTTP_X_ATLASSIAN_TOKEN')
end
tfs_webhook?() click to toggle source
# File lib/parsers/webhook_parser.rb, line 58
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