Class: CollavreGithub::WebhooksController

Inherits:
ActionController::API
  • Object
show all
Defined in:
app/controllers/collavre_github/webhooks_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/collavre_github/webhooks_controller.rb', line 3

def create
  event = github_event_header
  if event.blank?
    Rails.logger.warn("GitHub event header missing; rejecting request")
    return head :bad_request
  end

  raw_body = request.raw_post.presence || request.body.read
  payload = parse_payload(raw_body)
  @repository_link = find_repository_link(payload)
  return head :unauthorized unless valid_signature?(raw_body)

  payload = payload.presence || {}

  # Process all links for this repo (same repo can be linked to multiple creatives)
  all_links = all_repository_links_for(payload)
  all_links.each do |link|
    create_system_comment_for(link, event, payload) if link.creative
    trigger_markdown_sync_for(link, event, payload) if link.markdown_sync_enabled?
  end

  head :ok
rescue JSON::ParserError
  head :bad_request
end