Snap-in triggered by an external source
Introduction
In this tutorial, you’ll learn how to create a snap-in that responds to an external source, particularly GitHub. The objective is to seamlessly integrate commit comments from GitHub into the DevRev platform.
Background context
-
Understanding webhooks in GitHub: Explore the fundamentals of webhooks and learn how to incorporate them into the GitHub repository. Gain insights into the mechanisms of triggering events and handling notifications.
-
Extracting information from external payload: Delve into the external payload generated by GitHub and comprehend the process of extracting relevant information. Understand the structure and content of the payload to facilitate seamless integration with DevRev.
-
Encoding SHA-256 in the header with secret: Learn the technique of encoding SHA-256 in the header using a secret key. Learn the security measures involved in ensuring the integrity and authenticity of data transmitted via webhooks from GitHub to DevRev.
Installation guide
- Install DevRev CLI
- Install jq
- Install DevRev SDK
If you did not follow the getting started tutorial then follow these steps to authenticate and initialize the snap-in TypeScript template:
Trigger
The main trigger for this snap-in is an external webhook, which necessitates registration within GitHub. This ensures that the relevant payloads are dispatched to our designated endpoint.
Action
The primary action involves a straightforward process of examining our commit messages and displaying them on the discussion tab of the specified Product section. The configuration for this action can be customized through the input parameters of the snap-in.
Creating the snap-in
Updating the manifest
To outline the structure of the snap-in, the initial step is to define key attributes in the snap-in’s manifest. Begin by specifying the name, description, and account display name for the snap-in.
Configuration
To enhance the flexibility and adaptability of the snap-in, configurable inputs need to be incorporated. In this instance, organization-level inputs are introduced to specify the product part for which comments should be added. This configuration allows users to customize the behavior of the snap-in by providing a meaningful description of the product part where comments are intended to be added.
Event source
To establish the event source from which the snap-in will receive events, the
flow-custom-webhook
event is introduced. This event source serves as the
conduit for receiving relevant events.
Here, in the configuration, two crucial tasks are primarily addressed:
-
Extracting request body for consumption: The configuration is designed to retrieve the body from the incoming request and forward it to the event, facilitating seamless consumption by the handler function. This ensures that the relevant data from the request is accessible for further processing.
-
Authentication of requests: The configuration plays a pivotal role in authenticating incoming requests by validating the signature present in the header. This validation process involves comparing the signature in the header with the one sent by GitHub along with its request body. Detailed setup instructions are provided within the snap-in screen, allowing users to copy the URL and Secret. Subsequently, users can employ this information when creating the webhook on GitHub. During webhook creation, users can specify the events for which they intend to subscribe, with a focus on the events pertinent to this snap-in:
push
.
Functions and automations
With the foundational configurations in place, the next step involves defining the functions and automations that orchestrate the core logic of the snap-in. These automations serve as the bridge between triggers and functions, ensuring a seamless execution flow when specific events are detected.
Function logic
Having established the manifest that outlines the overall logic of the snap-in, the subsequent step involves defining the function logic responsible for handling the business logic. The initial point of focus is understanding the payload structure of the event, which can be gleaned from the GitHub webhook event.
Once acquainted with the payload structure, the function logic can be crafted to extract pertinent information and utilize the DevRev SDK for creating timeline entries on the relevant object.
This involves a multi-step process:
-
Payload structure exploration: Refer to the GitHub webhook event documentation to gain insights into the structure of the payload.
-
Information extraction: Derive essential information from the payload, discerning details crucial for the subsequent actions.
-
DevRev SDK integration: Utilize the DevRev SDK to create timeline entries on the appropriate object, ensuring seamless integration with the DevRev platform.
By adhering to this process, the function logic is poised to effectively handle the business logic, responding dynamically to incoming events and enriching the object timeline in DevRev.
Deploying the snap-in to your organization
Upon completion and validation of the code changes, the subsequent crucial step involves deploying the snap-in to your organization. Execute the following steps for a seamless deployment:
-
Navigate to the
code
folder in your project directory. -
Run the following commands sequentially to build, package, and generate the necessary artifacts:
-
Following this, proceed to create your snap-in package and version as described in the previous steps.
-
During the installation of the snap-in, crucial inputs such as the product part description need to be provided. Additionally, it’s imperative to register the webhook in GitHub during this process.
Resources
The final snap-in code and manifest can be found here.