| 62 | |
| 63 | |
| 64 | class GithubRequestParser(BaseRequestParser): |
| 65 | provider = IntegrationProviderSlug.GITHUB.value |
| 66 | webhook_identifier = WebhookProviderIdentifier.GITHUB |
| 67 | webhook_endpoint: Any = GitHubIntegrationsWebhookEndpoint |
| 68 | """Overridden in GithubEnterpriseRequestParser""" |
| 69 | |
| 70 | def _get_external_id(self, event: Mapping[str, Any]) -> str | None: |
| 71 | """Overridden in GithubEnterpriseRequestParser""" |
| 72 | return get_github_external_id(event) |
| 73 | |
| 74 | def mailbox_bucket_id(self, data: Mapping[str, Any]) -> int | None: |
| 75 | """Every event type that reaches a cell carries `repository.id`; installation |
| 76 | events are handled on control and never get here.""" |
| 77 | return self.bucket_key_at(data, "repository", "id") |
| 78 | |
| 79 | def mailbox_event_type(self, data: Mapping[str, Any]) -> str | None: |
| 80 | return self.request.META.get(GITHUB_WEBHOOK_TYPE_HEADER) |
| 81 | |
| 82 | def should_route_to_control_silo( |
| 83 | self, parsed_event: Mapping[str, Any], request: HttpRequest |
| 84 | ) -> bool: |
| 85 | return request.META.get(GITHUB_WEBHOOK_TYPE_HEADER) in _CONTROL_ONLY_EVENTS |
| 86 | |
| 87 | @control_silo_function |
| 88 | def get_integration_from_request(self) -> Integration | None: |
| 89 | if not self.is_json_request(): |
| 90 | return None |
| 91 | try: |
| 92 | event = orjson.loads(self.request.body) |
| 93 | except orjson.JSONDecodeError: |
| 94 | return None |
| 95 | external_id = self._get_external_id(event=event) |
| 96 | if not external_id: |
| 97 | return None |
| 98 | return Integration.objects.filter(external_id=external_id, provider=self.provider).first() |
| 99 | |
| 100 | def _get_drop_response( |
| 101 | self, |
| 102 | github_event: str | None, |
| 103 | event: Mapping[str, Any], |
| 104 | action: Any, |
| 105 | action_filter: ActionFilter | None, |
| 106 | ) -> HttpResponse | None: |
| 107 | """A 202 for a webhook no cell-side consumer reads, or None to keep routing it. |
| 108 | |
| 109 | Reads the event header and the request body and nothing else, which is what |
| 110 | lets it run before the integration and cell lookups. |
| 111 | """ |
| 112 | # Only drop when we have a known unprocessed event type. Missing or empty |
| 113 | # X-GitHub-Event is malformed; let the request be forwarded so the cell |
| 114 | # returns 400 and GitHub is notified of the delivery failure. |
| 115 | if github_event and github_event not in CELL_PROCESSED_GITHUB_EVENTS: |
| 116 | metrics.incr( |
| 117 | "github.webhook.drop_unprocessed_event", |
| 118 | tags={ |
| 119 | "event_type": github_event or "unknown", |
| 120 | "reason": "unprocessed_event_type", |
| 121 | }, |
no outgoing calls