Snap-in triggered by a DevRev event
Introduction
In this tutorial, you’ll learn to create a dynamic snap-in that responds to both DevRev Webhook events triggered by the creation of a work and specialized command within DevRev.
The focus is on the process of working with the payload associated with these events. The objective is to adeptly extract pertinent fields from the payload within the defined functions, subsequently executing a well-defined action. This action involves the addition of a comment to the object timeline, a feature that can be conveniently customized.
Background context
-
Webhook event handling: Understand the intricacies of responding to a DevRev webhook event, specifically focused on work creation.
-
Command-based activation: Explore how the snap-in can be triggered by a dedicated command within the DevRev environment.
-
Payload exploration: Delve into the payload structure, gaining insights into how to efficiently extract essential fields from within the functions.
-
Action execution: Become proficient in executing a specified action - adding a customizable comment to the object timeline.
To learn more, refer to the Using a snap-in to perform a DevRev action tutorial.
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
To invoke the snap-in, two distinct triggers are implemented:
-
Creation of work item: This trigger is activated when new objects of type Issue or Ticket are created in DevRev.
-
Text command: This manual trigger is achieved by utilizing a slash command in the Discussions tab of the objects that support this feature.
Action
To implement the desired action of adding a comment to the object timeline, it
is essential to identify the appropriate [API]
for this task. In this scenario, the /timeline-entries.create
API is the
designated choice for executing the action of adding a text comment from the
snap-in. To accomplish this task, the DevRev SDK is employed.
Creating the snap-in
Updating the manifest
The next step involves updating the manifest to define key attributes of the snap-in. This includes deciding on the name and providing a descriptive overview that is visible to users, offering context about the snap-in’s purpose.
Event source
Following the manifest update, the next step is to incorporate the event source.
For this scenario, events from DevRev are essential. Therefore, an event source
of type devrev-webhook
is added. In the configuration, the event type is
specified as work_created
.
Input fields
To enable users to configure specific comments, input fields must be defined. For our use case, the following input fields are specified:
-
Input field: This text field empowers users to input a custom message to be added as a comment. The default value is set to “Message from the input field.”
-
Should extra comment be added?: This boolean field determines whether an additional comment should be included. The default value is set to true.
-
List of extra names: This array field allows users to provide a list of names to be added as comments. The default value is set to [“name1”, “name2”].
By incorporating these input fields, the snap-in becomes more adaptable and user-friendly, enabling users to supply specific comments tailored to their needs.
Functions
With the groundwork laid out, the next crucial step involves defining the functions responsible for handling the logic in TypeScript to add comments using the DevRev SDK. The actual implementation of these functions is added in the subsequent step.
Automations
With the building blocks in place, the final piece of the puzzle involves defining automations. These automations encapsulate a trigger and a corresponding function that executes when an event of a specified type occurs.
Function logic
Having established the architectural framework for the snap-in, identified its
triggers, and outlined the overall workflow, the next crucial phase involves
crafting the functions - function_1
and function_2
. These functions serve as
the engines driving the business logic behind the snap-in’s behavior.
Getting the payload
To kickstart the implementation process, understanding the structure of the payload is paramount. Whether dealing with a work creation event or a command event, having insight into the payload’s composition is crucial. In scenarios where the payload structure is known, implementation can proceed seamlessly. However, if the payload structure is uncertain or real data exploration is desired, logging the entire payload becomes an invaluable step.
To achieve this, the following code snippet can be added to the index.ts
file:
Handling DevRev event
Having gained clarity on the expected payload structure, the next step involves
crafting custom code to handle the incoming events. In this context, the
objective is to print information based on the event type. The provided
TypeScript snippet exemplifies a function, handleEvent
, tailored for our use
case.
This function illustrates the process of handling events, extracting necessary information from the payload, and utilizing the DevRev SDK to create a timeline entry comment. It serves as a foundational template that can be adapted based on specific use cases and requirements.
Handling command event
For scenarios where a command event is triggered, a dedicated function,
handleCommandEvent
, can be implemented to handle the event appropriately. The
provided TypeScript snippet outlines the structure of this function,
demonstrating how to extract relevant information from the event payload and
utilize the DevRev SDK to create a timeline entry comment.
Deploying the snap-in to your organization
Upon completing and validating the code changes, the next crucial step is deploying the snap-in to your organization. Follow these steps for a seamless deployment:
-
Navigate to the
code
folder in your project directory. -
Execute the following commands in sequence to build, package, and create the necessary artifacts:
Resources
The final snap-in code and manifest can be found here