A bot to support the software project of group D in 2020/21 at
CvO Uni Oldenburg.
Completely written in Typescript to manage Java code. π
I am currently, summer 2021, working on a group project at the CvO Uni Oldenburg. We have to use Atlassian's Bitbucket, and we wanted to automate some things. Luckily Bitbucket has some API to work with, which brought this bot about.
This bot has two main features for now:
- Checking Code with Checkstyle
- Formatting Code with IntelliJ IDEA
- Executing Maven Goals with Maven
This bot takes diffs in a pull request and applies the Checkstyle on it. It executes the .jar and parses the results which will be formatted in a good-looking markdown:
β You did some error. (en.stuff.you.are.Stupid:69:3)
β οΈ Warned you. (en.stuff.you.are.Ignorant:420)
These markdown outputs will be commented under the pull request.
If the bot found no conflict it will post this under the pull request:
βοΈ No checkstyle conflicts found.
To format our code easily in one place, the bot runs the IntelliJ CLI formatter over the modified files. This formatting will take place once the pull request has reached it's required approvals (can be defined in the config).
Upon approval the formatter will run over the files and if changes were made, the bot will try to push them with a commit like this:
Auto-Reformat PR#69
This action was performed automatically by a bot.
If nothing has to be committed the bot will comment this under the pull request:
π Nothing to format.
If the bot wanted to push but could not, it will comment this:
β οΈ Could not push changes.
The bot will execute a list of given maven goals. Like the formatter this will
only happen after the pull request reached it's required approvals. All goals
will be executed under the clean
option.
If a maven goal that is set as required fails it will comment under the pull request:
β A required maven goal failed. Will stop now.
After a fail the execution for this PR will be stopped. The bot still runs then.
I don't know if it would be possible to use a gateway or websocket, the bot just polls the Bitbucket server frequently (once a minute). Usually this should have no impact on the rate limits of it.
In order to work correctly the bot needs some software preinstalled. This is just easiest way to implement the features I wanted.
- java
- git
- npm@latest
- node@lts
- mvn
The bot requires some form of authentication for the Bitbucket access. You could
use the password for the account, but I prefer to use a generated token.
This
explains how you can get one. The token you get must be written into a .token
file in the root of the bot. The file should only contain the token, nothing
more.
The bot is able to log also into a discord webhook. For that it needs the
webhook url, and some config on who to ping when an error occurs. For the url a
file called .webhook
should be made in the root, just like the token.
The config is set up like this in the webhookconfig.json
.
{
"pingsOnError": [
"137280289708834816", // a user id
"&605766552427560978" // a role id
]
}
In the repo you find a file called bitbucketconfig.json
. This file contains
the configuration for your Bitbucket repository. Change it how you need it.
{
"host": "https://git.swl.informatik.uni-oldenburg.de", // The url of your Bitbucket instance
"user": "SWP2020D_Bot", // The username of your bot account
"name": "Bot der Gruppe D", // The display name of your bot
"email": "bot@cptpiepmatz.de", // The email your bot commits with
"project": "SP", // The project slug, can also be a user
"repo": "swp2020d", // The repository slug
"isUserRepo": false, // If this repo is a user repo
"approvalsUntilFormat": 3 // Approvals until the bot formats
}
The bot uses the locally installed IntelliJ instance, therefore it has to be configured, so that the bot is able to use it properly.
{
"ideaPath": "C:/Users/derPi/AppData/Local/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7717.56/bin/idea64.exe"
}
Just enter the full path of your idea64.exe
or your idea.sh
.
The bot needs to know how it can execute maven, this will be defined in the config. Also, an array of goals can need to be defined here. The order of goals will also be the order of execution.
{
"cmd": "mvn",
"goals": [
{
"goal": "impsort:sort",
"skipTests": true,
"required": false
}
]
}
Setting a goal as required will cause the bot to stop working on the PR after
the goal failed. Setting the skipTests
to true
will add the maven option
-DskipTests
to the parameters.
To install the bot, simply run the npm install script inside the root directory.
npm install
On default the bot will output into the console with the log level: "INFO".
To change this to another log-level just add an environmental variable with
LOGLEVEL
. You may also create in the root of the bot a .env
file with the
content:
LOGLEVEL=verbose
You can also set one of these log levels which all increase in output:
error
warn
info
http
verbose
debug
silly
Now the bot should be fully configured, and you can run it. Maybe consider running it via some process manager like pm2. To run the bot, execute this:
npm run run
Some directories and files show up once the bot is running correctly.
This one just holds the logging files. The Bot logs a bit different in files than in the console. You find different files including different log levels.
This is a bot made in node, so upon installation this will show up. It holds all the dependencies the bot uses.
This directory contains the local working tree of your repository. Don't touch this. This directory is only for the bot and should not be used by you.
This dot file holds the current cache of the bot, if it needs to restart it will draw all the information from this file to "remember" the known pull requests. It's in the json formatted. You may take a look inside the bot's brain, but do not edit it, otherwise the bot may not work anymore. Deleting it will always fix the issues, but also remove the "memories" of the bot.