-
Notifications
You must be signed in to change notification settings - Fork 0
5 process
This wiki page explains the process of how the GitHub-Discord webhook works in our project.
The GitHub-Discord webhook is a mechanism that automatically sends notifications to a Discord channel whenever specific events occur in our GitHub repository. This integration helps keep the team informed about important repository activities in real-time.
- GitHub Event Trigger: An event occurs in the GitHub repository (e.g., push, pull request, issue creation).
- Webhook Activation: GitHub sends a POST request to the configured webhook URL.
- Message Formatting: GitHub formats the event data into a Discord-compatible message.
- Discord Webhook Endpoint: The webhook URL points to a Discord channel's webhook endpoint.
- Message Delivery: Discord receives the formatted message and posts it to the specified channel.
- The necessary the necessary components are initialized, including the
WebhookFactory,PayloadFactory, andMessageFactory. - The
PayloadFactorycreates a payload instance based on the GitHub event type. It processes the incoming JSON payload from GitHub and creates the appropriatePayloadInterfaceobject. - The
WebhookFactoryis responsible for creating a webhook profile based on the configured settings. It extracts the webhook name from the request URI and loads the appropriate configuration from theconfig/webhooks.phpfile. Returned webhook profile will incorporate payload object. - The
MessageFactorygenerates a message object based on the event type and webhook data. It uses the configuration fromconfig/messages.phpto determine which message class to instantiate. Returned message instance will incorporate webhook (and within that payload) object. - A message is then created using the
MessageFactory, passing the webhook profile and event information. - Finally, the message is sent to the Discord channel using the webhook URL specified in the incorporated webhook profile instance.
The ResponseHandler class in the github-discord-webhook package plays a crucial role in communicating back with the GitHub API. Here's how it works:
- The
ResponseHandleruses the staticsendmethod to generate and send HTTP responses. - It sets the Content-Type header to 'application/json', ensuring GitHub receives a properly formatted JSON response.
- The method takes three parameters:
-
$message: A string containing the response message -
$type: A string indicating the response type (e.g., "success" or "error") -
$statusCode: An integer representing the HTTP status code
-
- It uses
http_response_code()to set the appropriate HTTP status code for the response. - The response body is created as a JSON object containing:
-
responseCode: The HTTP status code -
type: The response type (success/error) -
message: The detailed message
-
- The JSON response is encoded using
json_encode()with theJSON_PRETTY_PRINToption for better readability. - After sending the response, the script execution is terminated using
exit.
This approach allows the package to communicate effectively with GitHub, providing clear feedback on the webhook processing status. It handles both successful operations and controlled errors, such as validation failures or missing classes, by using appropriate HTTP status codes (e.g., 200 for success, 422 for unprocessable entity, etc.). This ensures that GitHub receives meaningful responses, allowing it to log and handle the webhook delivery appropriately.