GitHub Bot to Automate PR reviews— in 10 minutes
By: Lakshan Bandara
Tags: Pull Request, Bots, Github, Coding, Nodejs

In here I am going to discuss about how can you use “Probot” framework to create a simple GitBot which detect deprecated tags in the code.

What is ProBot?

If you’ve ever thought, “wouldn’t it be cool if GitHub could…”; imma stop you right there. Most features can actually be added via GitHub Apps, which extend GitHub and can be installed directly on organizations and user accounts and granted access to specific repositories.

Probot framework allow you to create simple but powerful GitHub applications without focusing on the configurations. Mainly you have to only focus on your business logic. Probot framework will handle the struggle of webhook creation, receiving responses and authentication for you. Without further talk lets jump into the implementation.

In this tutorial we are going to create a probot app using Node.js.

Requirements

Node.js installed NPM installed

Create Probot project

open the terminal , navigate to the working directory and run below command to create a probot project. While the installation in progress you will be prompt between typescript project or javascript project. ( I usually prefer typescript over javascript. It’s up to you!)

npx create-probot-app pr-bot

wait few minutes until all the dependencies are getting installed. After installation process completed you can open the project using one of your favourite IDE.( I am a VS code fan by the way 😅)

You will have the below folder structure in your application.

Even though there are tons of file we need to only focus on the index.ts which is inside the src folder. All the logic we are going to implements goes in to this folder.

Before going in to coding lets connect our app to our github account. That will automatically generate you webhook ID and the AppId inside the .env folder.

Connect application to your GitHub account

run below command to start the probot server which can be accessible through the web browser.

npm start

Usually server start at : http://localhost:3000 . you can access this link through you browser.

click on Register GitHub App and It will prompt you to the GitHub login screen. Put your credentials and Hit Login button.

After successfully logged in, you will be prompt to add a name to your app. Then you will be prompt to screen where you can select the repos where you need to install your app. You can simply select all the repos or specific ones.

After connection between your git app and account successful you will be able to find your AppID and webhook ID inside the project’s .env file.

Granting permissions to Access the PR’s

Initially your GitHub application only has minimal permissions. If you need to access the PR’s events and data you need to grant the permissions.

GitHub > Settings > applications > YOUR_APP > configure > App Settings > Permissions and Events

Here you need to enable the following permissions: Read and Write permissions

Repository permissions > Pull Requests under event select > pull requests and save and apply all the changes

Implementation

Now we are ready to start coding. Navigate in to the src/ index.ts and type below lines of code.

import { Probot } from "probot";

import { checkDepreacatedTags } from "./pull-request-check" export = ({ app }: { app: Probot }) => {

app.log.info("GIT-BOT at your service") /** handle depreacated tags in the andi PR's */

app.on("pull_request.opened", checkDepreacatedTags) };

This code will start the app and starts listening on the PR open events. Whenever PR got created “checkDeprecatedTags” function will be triggered by passing a payload which contains all the data related to the PR.

Now let’s look at the check function implementation now. We will implement this function in a separate file.

create file “pull-request-check” and type the following code lines,

import { Context } from "probot"; export const checkDepreacatedTags = async (context: Context) => { const repoName: string = context.payload.repository.name;

const repoOwner: string = context.payload.repository.owner.login;

const pullNumber: number = context.payload.number; const listFiles = await context.octokit.pulls.listFiles({

repo: repoName,

owner: repoOwner,

pull_number: pullNumber

}) listFiles.data.forEach( file => {

const regex = new RegExp("YOUR TAG HERE", "g")

const result = file.patch.match(regex)

if(result){

context.octokit.issues.createComment({

repo: repoName,

owner: repoOwner,

issue_number: pullNumber,

body: " :x: :x: :x: Deprecated :x: :x: :x:",

})

}) }

This function will extract the code changes from the pull request and match the code with your tag. If the function found similar tag inside the code It will add a comment on the pull request.

You can find similar project like this in my git , where I have used google sheets to store a list of deprecated tags.

Follow me for more javascript tutorials. 👀🙃

Send your problems via linked in . I am happy to help anyone.

linkedin.com/in/lakshan-bandara-9917a588