Using a snap-in to perform an external action
Introduction
In this tutorial, you’ll lean how to develop a snap-in that mirrors an issue from DevRev to GitHub. This requires addition of a command that can be run from the Discussions tab of an issue in DevRev, which creates an issue in GitHub.
Background context
-
GitHub APIs mastery:
- Delve into understanding GitHub APIs.
- Learn how to fetch details and perform actions using GitHub APIs.
-
Secrets and keyrings in DevRev snap-ins:
- Explore the usage of secrets and refer to keyrings in DevRev snap-ins.
-
Command creation and utilization in DevRev:
- Gain proficiency in creating and using commands in 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
To initiate the process of creating a GitHub issue directly from a DevRev issue, a trigger mechanism is essential. In this context, the implementation involves the introduction of a specialized command. This command is designed to be executed exclusively from the discussion section of a DevRev issue, serving as the catalyst for replicating the issue on GitHub.
Action
The primary action involves leveraging the issue creation API provided by GitHub. This API is utilized to create the GitHub issue seamlessly from the corresponding issue in DevRev.
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.
Keyrings
To facilitate authentication for our API calls, the initial step involves creating a Personal Access Token (PAT) in GitHub. This PAT can be stored as a connection within DevRev. Subsequently, this connection is employed within our snap-in in the form of keyrings.
Functions and commands
Having established the foundational configurations, the subsequent step is to define the functions and commands responsible for orchestrating the core logic of the snap-in.
The command clearly states where you can use it. For example, in the Discussions tab of issues.
It also explains the different situations and ways in which you can make use of this command.
To utilize this command, execute /gh_issue OrgName RepoName
in the Discussions
tab of the issue. Within the function logic, validations are implemented to
ensure the correctness of both the organization name (OrgName
) and repository
name (RepoName
) before proceeding with the issue creation.
Function logic
After creating the manifest and establishing the snap-in’s logic, the next step is to define the function logic that handles business logic. Once you understand the payload structure of a command, you can proceed with its implementation.
To proceed, define the handler function for command events.
Within this handler, the initial step involves extracting the GitHub token provided as input in the keyring. Subsequently, the Octokit, responsible for managing GitHub API requests, is initialized:
To facilitate authentication for DevRev API calls, a DevRev token is required. The initialization process for the DevRev SDK involves using the received endpoint.
With the SDKs successfully initialized, the next step is to invoke the necessary APIs.
As a preliminary step, the required fields for creating a GitHub Issue, namely title and body, need to be extracted. These details are sourced from the issue. To facilitate this, introduce a function defined for this specific purpose:
Now, the function can be called within the handler function to obtain the necessary issue details::
Following this, the next step involves parsing the parameters Organization
and
Repository Name
that were passed in the command:
The GitHub REST API (https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#get-an-organization) is used to confirm the specified organisation name.
Similarly, the GET Repository is used to validate whether the entered repository name is correct.
After completing all the necessary verifications, the POST create issue can be invoked to create a new issue.
The completion of functional logic is a key milestone in the process. After laying the framework, now you’ll learn how to install this snap-in for your organization.
Deploying the snap-in to your organization
Once the code has been validated, the next steps involve creating the snap-in version and subsequently creating the snap-in itself. Before installing the snap-in, it’s essential to set up a GitHub Personal Access Token (PAT) and add it to the connections in DevRev as a snap-in secret. Ensure that the secret is shared within the organization so that the snap-in can utilize it.
Follow these steps to install the snap-in in your organization:
Step 1: Create a GitHub personal access token
Generate a GitHub Personal Access Token (PAT) by following the steps outlined in the GitHub documentation.
Step 2: Add PAT to DevRev connections
Add the generated PAT as a snap-in secret in DevRev. This secret will be used during the installation of the snap-in. Ensure that the secret is shared within the organization to allow the snap-in to access it.
Step 3: Install the snap-in
During the installation of the snap-in, utilize the shared secret to authenticate and connect with GitHub. This ensures that the snap-in has the necessary permissions to interact with GitHub APIs.
Resources
The final snap-in code and manifest can be found here.