How to create a comment in a pull request using octokit?
Last Updated :
29 Jul, 2024
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:
- API client (REST API requests, GraphQL API queries, Authentication)
- App client (GitHub App & installations, Webhooks, OAuth)
- 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:
Similar Reads
How to Create Pull Request on GitHub Without Using any IDE?
Creating a pull request (PR) on GitHub is an important part of collaborative software development. It allows you to propose changes to a project, which can then be reviewed and merged by other contributors. You don't need an Integrated Development Environment (IDE) to create a pull request. In this
2 min read
How to Create Branch From a Previous Commit Using Git?
Creating a branch from a previous commit in Git is a common task that allows developers to revisit and build on a specific point in their project's history. This can be useful for bug fixes, new feature development, or exploring alternate project paths without affecting the current state of the proj
2 min read
How to Create a Pull Request on GitHub using Android Studio?
Creating a pull request is an important part of collaborating on projects hosted on GitHub. It allows you to propose changes to a repository, enabling others to review, discuss, and merge your changes. Hereâs a step-by-step guide on how to create a pull request on GitHub using Android Studio. Steps
2 min read
How To Create a Pull Request in GitHub?
Pull requests are an important part of collaborative software development on GitHub. They allow developers to propose changes, review code, and discuss improvements before integrating new code into a project. This guide will walk you through the process of creating a pull request in GitHub, ensuring
3 min read
How to create and send POST requests in Postman?
Postman is an API(application programming interface) development tool which helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), saving environments for later use, and convert save the API to code for various languages(like JavaScript, and Python).
2 min read
How to create a new request in Postman?
Postman is a development tool that is used for testing web APIs i.e. Application Programming Interfaces. It allows you to test the functionality of any application's APIs. Almost every developer uses Postman for testing purposes. We can create any type of HTTP request in it such as GET, POST, PUT, D
2 min read
Creating Pull Requests from VS Code on GitHub
Creating pull requests is an important part of collaborative software development. A pull request (PR) allows you to propose changes to a codebase, which can then be reviewed and merged by others. While GitHub's web interface is a common way to handle pull requests, Visual Studio Code (VS Code) offe
5 min read
How to Convert a Postman Request to cURL?
If you're a web developer, quality assurance engineer, or system administrator, chances are you're familiar with Postman, a crucial tool for API testing. However, there are instances where you may need to convert a Postman request to cURL, a command-line tool for data transfer. This article provides
3 min read
How to Create a New Branch on Github using Pycharm?
Git is an open-source version control system. It means that whenever a developer develops some project (like an app or website) or something, he/she can constantly update, Git is a version control system that lets you manage and keep track of your source code history. Letâs say you have a project, a
2 min read
How to Revert a Pushed Merge Commit in Git?
In Git, merge commits are created when integrating changes from one branch into another. Sometimes, you may find yourself in a situation where you need to revert a merge commit that has already been pushed to a remote repository. Reverting a merge commit requires careful consideration to maintain th
3 min read