Module: ActiveProject::Adapters::Trello::Webhooks

Included in:
ActiveProject::Adapters::TrelloAdapter
Defined in:
lib/active_project/adapters/trello/webhooks.rb

Instance Method Summary collapse

Instance Method Details

#parse_webhook(request_body, _headers = {}) ⇒ ActiveProject::WebhookEvent?

Parses an incoming Trello webhook payload.

Parameters:

  • request_body (String)

    The raw JSON request body.

  • headers (Hash)

    Request headers (unused).

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_project/adapters/trello/webhooks.rb', line 11

def parse_webhook(request_body, _headers = {})
  payload = begin
    JSON.parse(request_body)
  rescue StandardError
    nil
  end
  return nil unless payload.is_a?(Hash) && payload["action"].is_a?(Hash)

  action = payload["action"]
  action_type = action["type"]
  actor_data = action["memberCreator"]
  timestamp = begin
    Time.parse(action["date"])
  rescue StandardError
    nil
  end
  board_id = action.dig("data", "board", "id")
  card_data = action.dig("data", "card")
  action.dig("data", "text")
  old_data = action.dig("data", "old")

  event_type = nil
  object_kind = nil
  event_object_id = nil
  object_key = nil
  changes = nil
  object_data = nil

  case action_type
  when "createCard"
    event_type = :issue_created
    object_kind = :issue
    event_object_id = card_data["id"]
    object_key = card_data["idShort"]
  when "updateCard"
    event_type = :issue_updated
    object_kind = :issue
    event_object_id = card_data["id"]
    object_key = card_data["idShort"]
    if old_data.is_a?(Hash)
      changes = {}
      old_data.each do |field, old_value|
        new_value = card_data[field]
        changes[field.to_sym] = [ old_value, new_value ]
      end
    end
  when "commentCard"
    event_type = :comment_added
    object_kind = :comment
    event_object_id = action["id"]
    object_key = nil
  when "addMemberToCard", "removeMemberFromCard"
    event_type = :issue_updated
    object_kind = :issue
    event_object_id = card_data["id"]
    object_key = card_data["idShort"]
    changes = { assignees: true }
  else
    return nil
  end

  WebhookEvent.new(
    type: event_type,
    resource_type: object_kind,
    resource_id: event_object_id,
    project_id: board_id,
    actor: map_user_data(actor_data),
    timestamp: timestamp,
    source: webhook_type,
    data: (object_data || {}).merge(changes: changes, object_key: object_key),
    raw_data: payload
  )
rescue JSON::ParserError
  nil
end