Skip to content

Instantly share code, notes, and snippets.

@bkeepers
Last active November 2, 2020 17:32
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bkeepers/d607a107dc3844cd501c196326c7b33b to your computer and use it in GitHub Desktop.
Probot plugin that ensures every commit message has the word "bananas"
# The ID of your GitHub App
APP_ID=
WEBHOOK_SECRET=development
# Uncomment this to get verbose logging
# LOG_LEVEL=trace # or `info` to show less
# Go to https://smee.io/new set this to the URL that you are redirected to.
# WEBHOOK_PROXY_URL=
node_modules
npm-debug.log
*.pem
.env
package-lock.json

Build a bot!

This guide will walk you through running a simple GitHub App that ensures every commit has the word bananas and get it running on a test repository. All of the source code you need is in this gist.

Here's what it will look like when your commits are not bananas:

…and here's what it will look like when your commits are bananas:

Ready? Let's get started!

  1. Clone this gist

     $ git clone https://gist.github.com/bkeepers/546efd41fa08f1af418d5c58e02ec696 bananas
    
  2. Make sure you have a recent version of Node.js installed.

     $ node --version
     v7.8.0
    
  3. package.json lists the dependencies for our app. Check it out, and then run:

     $ npm install
    
  4. index.js is the meat of the app. Read through the code and see if you can make sense of what it's doing.

  5. Go to smee.io and click Start a new channel. Set WEBHOOK_PROXY_URL in your .env to the URL that you are redirected to.

  6. Create a new GitHub App with:

    • GitHub App name: yourusername-bananas, replacing yourusername with your GitHub username.
    • Homepage URL and User authorization callback URL: It doesn't matter for this tutorial, so set to https://github.com/yourusername, replacing yourusername with your GitHub username.
    • Webhook URL: Use your WEBHOOK_PROXY_URL from the previous step.
    • Webhook Secret: development
    • Permissions:
      • Commit statuses: Read & Write
      • Pull requests: Read-only
    • Subscribe to events
      • Pull request
    • Click Create GitHub App
  7. Most importantly, your bot needs an adorable icon. Feel free to use bananas.jpg.

  8. Generate a private key and move it to the project directory.

  9. Edit .env and set APP_ID to the ID of the app you just created, and SUBDOMAIN to your GitHub username.

  10. Click the Install button on your app page, and install it on a repository.

  11. Go back to your terminal and run $ npm start to start the server, which will output Listening on http://localhost:3000.

  12. Now go to your repository and create a new file, commit it to a branch, and open a pull request.

Want to learn more:

// A plugin is a Node module that exports a function which takes a `robot` argument
module.exports = robot => {
// Listen for a pull request being opened or synchronized
robot.on('pull_request', async context => {
// Just assign a variable to make our life easier
const pr = context.payload.pull_request;
// Get all the commits in the pull request
const compare = await context.github.repos.compareCommits(context.repo({
base: pr.base.sha,
head: pr.head.sha
}));
// Check that every commit has the word "bananas" in it
const isBananas = compare.data.commits.every(data => {
return data.commit.message.match(/bananas/i);
});
// Parameters for the status API
const params = {
sha: pr.head.sha,
context: 'bananas',
state: isBananas ? 'success' : 'failure',
description: `Your commits ${isBananas ? 'are' : 'need more'} bananas`
}
// Create the status
return context.github.repos.createStatus(context.repo(params));
});
};
{
"private": true,
"scripts": {
"start": "probot run ./index.js"
},
"dependencies": {
"probot": "^5.0.0"
},
"devDependencies": {
"smee-client": "^1.0.1"
},
"engines": {
"node": ">= 7.7.0",
"npm": ">= 4.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment