Using a snap-in to perform a DevRev action
Introduction
The objective is to build a snap-in that creates a new ticket every 10 minutes.
Background context
In the Getting Started tutorial, you learned how to create a hello-world snap-in which prints a log message when a work item is created. In this tutorial, you will make the following changes to the snap-in:
- Update the trigger condition to run every 10 minutes instead of work creation.
- Create a ticket whenever the snap-in is triggered.
Ensure that you have at least one part in your organization since every ticket must be linked to a part and have an owner.
Since a DevRev ticket is created whenever the function is triggered, DevRev APIs need to be called that allow this functionality. Although one can directly make HTTP requests to the endpoint, it’s strongly suggested to use the DevRev TypeScript SDK since it provides additional types and helper methods which ease development.
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 trigger condition for the snap-in is dictated by the
Event Sources
section in the manifest. The timer-events
event source is suitable for the use-case, since it allows trigger of snap-ins
using CRON expression.
Action
The hello-world snap-in prints a log message whenever the snap-in is triggered. Here, the snap-in should create a work-item of type ticket when triggered. To do that, use the DevRev TypeScript SDK to make API calls for creating the ticket.
Creating the snap-in
Updating the manifest
Update the manifest file to reflect the objective. Update the name, the description, and the service account’s display name to better reflect the snap-in’s behavior.
Next, update the event_sources
section to use the timer-events
event source.
The timer-events
source type takes a config
of type cron
or
interval_seconds
as mentioned in the
documentation.
The cron
config is used here.
Finally, update the function
name to better reflect the behavior and
automation
name to use the event type corresponding to the timer-events
event
source.
After these changes, the final version of the manifest can be found here.
Renaming the function
Next, the function name in the src/functions
folder needs to be renamed to
‘ticket_creator’ which is the one put in the manifest.
These changes need to be reflected in the following files as well:
src/function-factory.ts
src/test-runner/example.test.ts
src/functions/ticket_creator/index.test.ts
Event received by the snap-in function
In the hello-world snap-in, the ID of the created work-item gets extracted from the input event as such.
The schema for a event received by the snap-in is:
The payload
differs by the event_source
and the event_type
received. There
are two fields to be concerned about in the input payload:
devrev_endpoint
the endpoint of the API call.service_account_token
which is a short-lived token provisioned by each snap-in. This is required when making API calls to DevRev.
Updating the code
Update the code in src/functions/ticket_creator/index.ts
to reflect the
behavior.
Firstly, import the DevRev TypeScript SDK in the index.ts
file
Next, update the run
function in the hello-world example. Since the ticket
gets created frequently, set some creation time in the title and the body. The
example uses the part as PROD-1
and keeps the owner as DEVU-1
. Feel free to
edit those values to actual IDs for parts and users in your dev org.
The complete index.ts
file looks like this
Deploying the snap-in to your organization
Once satisfied with the code changes, move to the code
folder, and run
This builds and packages your snap-in and creates a build.tar.gz
. The tar.gz
and manifest.yaml
file is used when creating the snap-in version.
Always remember to build and package the snap-in whenever there are code changes and it needs to be re-deployed.
Steps for deploying this snap-in have been discussed in the Getting Started section.
Resources
The final snap-in code and manifest can be found here