Skip to content

5 process

tei187 edited this page Jan 15, 2025 · 1 revision

Process overview

This wiki page explains the process of how the GitHub-Discord webhook works in our project.

Overview

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.

General events overview

  1. GitHub Event Trigger: An event occurs in the GitHub repository (e.g., push, pull request, issue creation).
  2. Webhook Activation: GitHub sends a POST request to the configured webhook URL.
  3. Message Formatting: GitHub formats the event data into a Discord-compatible message.
  4. Discord Webhook Endpoint: The webhook URL points to a Discord channel's webhook endpoint.
  5. Message Delivery: Discord receives the formatted message and posts it to the specified channel.

Process flow

  1. The necessary the necessary components are initialized, including the WebhookFactory, PayloadFactory, and MessageFactory.
  2. The PayloadFactory creates a payload instance based on the GitHub event type. It processes the incoming JSON payload from GitHub and creates the appropriate PayloadInterface object.
  3. The WebhookFactory is 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 the config/webhooks.php file. Returned webhook profile will incorporate payload object.
  4. The MessageFactory generates a message object based on the event type and webhook data. It uses the configuration from config/messages.php to determine which message class to instantiate. Returned message instance will incorporate webhook (and within that payload) object.
  5. A message is then created using the MessageFactory, passing the webhook profile and event information.
  6. Finally, the message is sent to the Discord channel using the webhook URL specified in the incorporated webhook profile instance.

Communicaton with GitHub API

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:

  1. The ResponseHandler uses the static send method to generate and send HTTP responses.
  2. It sets the Content-Type header to 'application/json', ensuring GitHub receives a properly formatted JSON response.
  3. 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
  4. It uses http_response_code() to set the appropriate HTTP status code for the response.
  5. 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
  6. The JSON response is encoded using json_encode() with the JSON_PRETTY_PRINT option for better readability.
  7. 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.

Clone this wiki locally