|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | # Example handler for GitHub webhooks |
4 | | -class GitHubHandler < Hooks::Handlers::Base |
| 4 | +class GithubHandler < Hooks::Handlers::Base |
5 | 5 | # Process GitHub webhook |
6 | 6 | # |
7 | 7 | # @param payload [Hash, String] GitHub webhook payload |
8 | 8 | # @param headers [Hash<String, String>] HTTP headers |
9 | 9 | # @param config [Hash] Endpoint configuration |
10 | 10 | # @return [Hash] Response data |
11 | 11 | def call(payload:, headers:, config:) |
12 | | - # GitHub sends event type in header |
13 | | - event_type = headers["X-GitHub-Event"] || "unknown" |
14 | | - |
15 | | - puts "GitHubHandler: Received #{event_type} event" |
16 | | - |
17 | | - return handle_raw_payload(payload, config) unless payload.is_a?(Hash) |
18 | | - |
19 | | - case event_type |
20 | | - when "push" |
21 | | - handle_push_event(payload, config) |
22 | | - when "pull_request" |
23 | | - handle_pull_request_event(payload, config) |
24 | | - when "issues" |
25 | | - handle_issues_event(payload, config) |
26 | | - when "ping" |
27 | | - handle_ping_event(payload, config) |
28 | | - else |
29 | | - handle_unknown_event(payload, event_type, config) |
30 | | - end |
31 | | - end |
32 | | - |
33 | | - private |
34 | | - |
35 | | - # Handle raw string payload |
36 | | - # |
37 | | - # @param payload [String] Raw payload |
38 | | - # @param config [Hash] Configuration |
39 | | - # @return [Hash] Response |
40 | | - def handle_raw_payload(payload, config) |
41 | | - { |
42 | | - status: "raw_payload_processed", |
43 | | - handler: "GitHubHandler", |
44 | | - payload_size: payload.length, |
45 | | - repository: config.dig(:opts, :repository), |
46 | | - timestamp: Time.now.iso8601 |
47 | | - } |
48 | | - end |
49 | | - |
50 | | - # Handle push events |
51 | | - # |
52 | | - # @param payload [Hash] Push event payload |
53 | | - # @param config [Hash] Configuration |
54 | | - # @return [Hash] Response |
55 | | - def handle_push_event(payload, config) |
56 | | - ref = payload["ref"] |
57 | | - branch = ref&.split("/")&.last |
58 | | - commits_count = payload.dig("commits")&.length || 0 |
59 | | - |
60 | | - # Check if branch is in filter |
61 | | - branch_filter = config.dig(:opts, :branch_filter) |
62 | | - if branch_filter && !branch_filter.include?(branch) |
63 | | - return { |
64 | | - status: "ignored", |
65 | | - handler: "GitHubHandler", |
66 | | - reason: "branch_not_in_filter", |
67 | | - branch: branch, |
68 | | - filter: branch_filter, |
69 | | - timestamp: Time.now.iso8601 |
70 | | - } |
71 | | - end |
72 | | - |
73 | | - { |
74 | | - status: "push_processed", |
75 | | - handler: "GitHubHandler", |
76 | | - repository: payload.dig("repository", "full_name"), |
77 | | - branch: branch, |
78 | | - commits_count: commits_count, |
79 | | - pusher: payload.dig("pusher", "name"), |
80 | | - timestamp: Time.now.iso8601 |
81 | | - } |
82 | | - end |
83 | | - |
84 | | - # Handle pull request events |
85 | | - # |
86 | | - # @param payload [Hash] Pull request event payload |
87 | | - # @param config [Hash] Configuration |
88 | | - # @return [Hash] Response |
89 | | - def handle_pull_request_event(payload, config) |
90 | | - action = payload["action"] |
91 | | - pr_number = payload.dig("pull_request", "number") |
92 | | - pr_title = payload.dig("pull_request", "title") |
93 | | - |
94 | | - { |
95 | | - status: "pull_request_processed", |
96 | | - handler: "GitHubHandler", |
97 | | - action: action, |
98 | | - repository: payload.dig("repository", "full_name"), |
99 | | - pr_number: pr_number, |
100 | | - pr_title: pr_title, |
101 | | - author: payload.dig("pull_request", "user", "login"), |
102 | | - timestamp: Time.now.iso8601 |
103 | | - } |
104 | | - end |
105 | | - |
106 | | - # Handle issues events |
107 | | - # |
108 | | - # @param payload [Hash] Issues event payload |
109 | | - # @param config [Hash] Configuration |
110 | | - # @return [Hash] Response |
111 | | - def handle_issues_event(payload, config) |
112 | | - action = payload["action"] |
113 | | - issue_number = payload.dig("issue", "number") |
114 | | - issue_title = payload.dig("issue", "title") |
115 | | - |
116 | | - { |
117 | | - status: "issue_processed", |
118 | | - handler: "GitHubHandler", |
119 | | - action: action, |
120 | | - repository: payload.dig("repository", "full_name"), |
121 | | - issue_number: issue_number, |
122 | | - issue_title: issue_title, |
123 | | - author: payload.dig("issue", "user", "login"), |
124 | | - timestamp: Time.now.iso8601 |
125 | | - } |
126 | | - end |
127 | | - |
128 | | - # Handle ping events (webhook test) |
129 | | - # |
130 | | - # @param payload [Hash] Ping event payload |
131 | | - # @param config [Hash] Configuration |
132 | | - # @return [Hash] Response |
133 | | - def handle_ping_event(payload, config) |
134 | | - { |
135 | | - status: "ping_acknowledged", |
136 | | - handler: "GitHubHandler", |
137 | | - repository: payload.dig("repository", "full_name"), |
138 | | - hook_id: payload.dig("hook", "id"), |
139 | | - zen: payload["zen"], |
140 | | - timestamp: Time.now.iso8601 |
141 | | - } |
142 | | - end |
143 | | - |
144 | | - # Handle unknown events |
145 | | - # |
146 | | - # @param payload [Hash] Event payload |
147 | | - # @param event_type [String] Event type |
148 | | - # @param config [Hash] Configuration |
149 | | - # @return [Hash] Response |
150 | | - def handle_unknown_event(payload, event_type, config) |
151 | | - { |
152 | | - status: "unknown_event_processed", |
153 | | - handler: "GitHubHandler", |
154 | | - event_type: event_type, |
155 | | - repository: payload.dig("repository", "full_name"), |
156 | | - supported_events: config.dig(:opts, :events), |
157 | | - timestamp: Time.now.iso8601 |
| 12 | + return { |
| 13 | + status: "success" |
158 | 14 | } |
159 | 15 | end |
160 | 16 | end |
0 commit comments