Open In App

How to create a comment in a pull request using octokit?

Last Updated : 12 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction: The Octokit client can be used to send requests to GitHub’s REST API and queries to GitHub’s GraphQL API. The octokit package integrates the three main Octokit libraries:

  1. API client (REST API requests, GraphQL API queries, Authentication)
  2. App client (GitHub App & installations, Webhooks, OAuth)
  3. Action client (Pre-authenticated API client for single repository).

Approach: We are going to create a script  using Github’s API Client octokit/rest.js through which we will comment on a pull request as soon as it opened .

Below is the step-by-step implementation of the above approach.

Step 1: Initialize a node project 

First of all the create a root folder named as GFG:

mkdir GFG

Navigate into the GFG folder:

cd GFG

Initialize this folder as a node repository:

npm init

After this, you will find a package.json file at the root of your project.

Step 2: Create a new Github action

Create a new file named as action.yml with the below content:

name: 'GeeksForGeeks'
description: 'Say "GeeksForGeeks" to new pull requests'
author: '[GFG]'

inputs:
  GITHUB_TOKEN:
    description: 'GitHub token'
    required: true

runs:
  using: 'node12'
  main: 'dist/index.js' 

An action is declared above with the name GeeksForGeeks taking one input GITHUB_TOKEN which should run in node12 version and the main entry point is set to ‘dist/index.js’.

Step3: Create the base file for the app to run

First of all install two dependencies to use octokit and context payloads of pull request.

npm install @actions/core @actions/github

Create a src directory. Then create an action.js file in src directory with the following content:

Javascript




const core = require('@actions/core');
const github = require('@actions/github');
const { context } = require('@actions/github')
const GITHUB_TOKEN = core.getInput('GITHUB_TOKEN');
const octokit = github.getOctokit(GITHUB_TOKEN);
 
const { pull_request } = context.payload;
 
async function run() {
  await octokit.rest.issues.createComment({
    ...context.repo,
    issue_number: pull_request.number,
    body: 'Thank you for submitting a pull request! We will
          try to review this as soon as we can.'
  });
}
 
run();


What we are doing above is:

  • After the two dependencies are installed they are imported into the file.
const core = require('@actions/core');
const github = require('@actions/github');
  • In each function which requires access to the github api, use the following to create octokit.
const GITHUB_TOKEN = core.getInput('GITHUB_TOKEN');
const octokit = github.getOctokit(GITHUB_TOKEN);
  • Now as we have got the octokit instance, we can now create comment using Github API’s client octokit.
octokit.rest.issues.createComment({
  owner,
  repo,
  issue_number,
  body,
});

Step 4: Building src/action.js with Vercel’s ncc

While we’re not yet doing anything inside of our src/action.js that requires anything more than node to run, we’re going to set up our Action to build and compile into a dist folder which is what our Action will use to actually run the code.

If you remember, we set the main attribute inside of action.yml to dist/index.js!

To start, we can first install ncc from Vercel, which will take our scripts and dependencies and package it all up in one file for us.

In your terminal, install the file with:

npm install @vercel/ncc

Next, inside of our package.json file, under the scripts object, let’s add a new script:

"scripts": {
 "build": "ncc build src/action.js -o dist"
 ...,
},

This sets up a new script so any time we run the build command, it will tell ncc to build our Action script and output it into the dist folder.

We can try it out by running:

npm run build

And once it’s finished, you should now see a dist folder at the root of your project with an index.js file inside of it. If you look inside it, you might notice a bunch of weird-looking code. ncc uses webpack to compile our script so that it’s able to be used as a module, allowing different processes to understand it. The reason we’re compiling our script is when GitHub tries to use our Action from another repository, it doesn’t have all of the dependencies available. Packaging it up in a single file allows our script to work with just that one file!

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads