Create GitHub Issue

Let's make an API request to GitHub with the configurations and authorisation token we have set up similar to the integration with Asana.

API schema definition

We will start by defining a request template for creating an issue on GitHub. Add the following request schema in the config/requests.json with this additional schema.

1{
2  ...,
3  "create_github_issue": {
4    "schema": {
5      "method": "POST",
6      "host": "api.github.com",
7      "path": "/repos/<%= iparam.github_username %>/<%= iparam.github_repository %>/issues",
8      "headers": {
9        "Authorization": "token <%= access_token %>",
10        "Content-Type": "application/json",
11        "Accept": "application/vnd.github+json",
12        "X-GitHub-Api-Version": "2022-11-28",
13        "User-Agent": "GitHub-Freshservice app"
14      },
15      "query": {}
16    },
17    "options": {
18      "oauth": "github"
19    }
20  }
21}

In the request schema,

  1. We have added the API host, method, path, and headers to make the API request to GitHub.
  2. The options have the OAuth mentioned as github. It will accordingly let the platform use the respective authorisation credential for GitHub when multiple authorisations are used.
  3. In the path and Authorisation header, there are some templates used. The <%= iparam.github_username %> lets the platform automatically use the respective installation parameters (iparams) in the same place. The Authorisation header uses <%= access_token %> template that uses the OAuth access token in the same place.

Define the request on app manifest

Next, let's define the request template on the manifest.json file so that the app uses the request method for the respective module of the app. Update the manifest.json file to include the request attribute as follows:

1{
2  ...
3  "modules": {
4    "common": {
5      "requests": {
6        ...,
7        "create_github_issue": {}
8      }
9    }
10  ...
11  }
12}

Use the Request Method to create an issue

Now, let's use this template to create an issue on GitHub. Add the following code on your app/app.js file to replace the content of the createGitHubIssue function:

1/**
2 * 
3 * @param {String} title - A title for the GitHub issue
4 * @returns {Promise<String>}
5 */
6async function createGitHubIssue(title) {
7  return client.request.invokeTemplate('create_github_issue', {
8    context: {},
9    body: JSON.stringify({
10      "title": title,
11      "body": "I'm having a problem with this."
12    })
13  });
14}

In this function, it takes a title as an argument and creates a GitHub issue using the predefined request template.

Error handling

After creating the issue, we have to handle the errors and send the notifications to the user based on the result of the action. Let's replace the content of the createAsanaTaskAndGitHubIssue function with the following code:

1/**
2 * 
3 * @param {String} asanaTitle - A title for the Asana task
4 * @param {String} githubTitle - A title for the GitHub issue
5 * @returns {Promise<String>}
6 */
7async function createAsanaTaskAndGitHubIssue(asanaTitle, githubTitle) {
8  try {
9    await createAsanaTask(asanaTitle);
10    try {
11      await createGitHubIssue(githubTitle);
12
13      client.interface.trigger('showNotify', {
14        type: 'success',
15        message: 'Asana task and GitHub issue created successfully!'
16      });  
17    } catch (error) {
18      console.error("Error: Failed to create github issue");
19      console.error(error);
20    }
21  } catch (error) {
22    console.error("Error: Failed to create asana ticket");
23    console.error(error);
24  } 
25}

In this function, we call the createGitHubIssue function after the createAsanaTask is called and handle the response. Depending on whether the issue creation was successful or not, we trigger a notification. It only runs when the previous function was successful as the execution will continue if succeed or exit to the catch statement if there was an error with creating task in Asana.

Testing

To ensure everything is working correctly, follow these steps:

  1. Run the App: Use the fdk run command to run your app.
  2. Install the App: Install the app from the Custom App section in the Marketplace. The app will process the authorization.
  3. Authorize the App: During the installation, authorize the app in the GitHub authorization page.
  4. Create an issue: Create an issue in the GitHub repository by clicking on the button in the app UI.
  5. Verify: Verify that the issue is created in the GitHub repository and the appropriate notifications are displayed based on the success or failure of the issue creation.

Awesome! We have successfully integrated the app with Asana and GitHub via API.