(or a Github bot) is a first-class actor on Github which is used to take redundant work off of repository maintainer’s hands. A first-class actor on Github means that it has the ability to do what the maintainer wants and has programmed the bot to do, given the proper permissions beforehand. The bot works by receiving webhooks of the events fired on Github(a comment asking the bot to remove assignees, add labels, move the issue to another repo), and then triggering appropriate code(the code is event styled, which should be apparent later in the article) in response to it. The triggered code makes the bot do things. One of the works that can be outsourced to the bot is managing labels to issues. In this article, we will use
to build a Github app to add or remove labels to issues. Framework used:
We’ll also use the
npm module to input the command.
Initial setup of GitHub App
To setup the github app, one can go through the probot’s documentation. As setup is not the focus of this article, we’ll
remix a Probot app on Glitch
. The link for this setup can be found
in the Glitch remix has a fantastic guide on setting up the app. To check for the proper app set up, run the default code that got supplied with the app install by making an issue on the repo(presuming the app is installed on the repo).
The bot(GitHub app) should write a comment like below.
If this is not the output, it means that the app is not properly installed and one should consult the
. After the app setup is complete and initial tests are done(to ensure the app is configured properly and working), its time to install the npm modules that we’ll need.
We’ll need a probot-commands npm module for this app to work. To install them, go to ‘package.json’ file from the navigation pane on the left side. On the top-left corner of the editor, find a button labeled ‘
‘. Using this button, add ‘
‘ to the app. After these two modules are added, go to ‘
‘ file(using the navigation pane in the left side) and add
const commands = require("probot-commands");
on top of index.js file, like this:
At this point, we should be good to go to develop the core logic for adding or removing the labels to issues.
The index.js code is
Code(from index.js file of the project)
: Here, we’ll use the probot-commands module to identify commands from the issue comments. More details about how the commands module works can be found on their GitHub repo. As a short introduction, the structure of a ‘slash command’ is like this:
/keywordToIdentifyCommand arg1 arg2 arg3
//In our app, we'll use the following command structure
/lm [add/remove], label1, label2, label3
- Listen to the event: a new comment on the issue is created
- If the content of the comment contains a ‘slash command’ starting with /lm, execute the following code.
- Split the commands on the ‘, ‘ and assign to a new var(labelvals, here). For example, if the slash command is ‘lm add, bug, easy’, we’ll get an array [‘add’, ‘bug’, ‘easy’]
- Slice the array obtained to get the values of labels in an array ‘labels’, for example, [‘bug’, ‘easy’]
- Check for the ‘add’ or ‘remove’ argument. If ‘add’, then do x, else if ‘remove’, then do y.
- If ‘add’, supply the entire array to method ‘addLabels’. Else if ‘remove’, iteratively pass ‘labels’ elements to the method ‘removeLabel’. Note: In the remove section, as no method exists to bulk remove the list of labels, we iteratively remove the labels one at a time.
- If neither ‘add’ nor ‘remove’ is supplied, create a comment to let people know of an unsuccessful attempt.
The final work is something like this, in action:
if ((context.payload.issue.author_association === "OWNER") ||
(context.payload.issue.author_association === "COLLABORATOR"))
// commands... code block here
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!