github alerts slack bot
?

What is it?
?

This is a simple bot that serves to send some notifications about GitHub events to Slack channels.

These are the features currently developed:

  • notification to inform Pull Requests with conflicts

Others features may be developed in the future.

How it works
?‍?

  • GitHub webhook send a request to bot to selected event (Pull Request has been opened, per example)
  • It handle the request
    • it check if the request comes from the correct GitHub webhook to avoid fraud
    • it don’t send repeated notification with same data on the same day
  • Whether yes, the Slack webhook is called to create a message in on channel with the information (a Pull Request is open)

Configuration
⚙️

Create a access token of GitHub

Navigate to Settings > Developer Settings > Personal access tokens > Generate new token

The permission repo is only need to access necessary datas of yours repositories: image

Save the access token after creating it.

Create a workflow in Slack

The workflow is the point used to send the notifications and here there are a tutorial to create one.

The only var required in the text is prs_list.

You can customize the message of your workflow as you wish:

example of workflow

After create, save the workflow webhook.

Deploy the bot

First, create your fork of this project to can perform the deploy.

The our bot is configured to deploy in Heroku. Heroku is a cloud platform to deploy your simple projects, they have a free plan that will be enough for us. You can also choose another cloud platform to do this.

Create a new app > Select the deploy on GitHub > Choose your fork of this project

Greate! If everything is ok, you can see this message:

success deploy

Install Redis

Redis is used to check if one message is send in the past day with the same pull requests.

In the dashboard of your project in Heroku, go to Resources > Find more add-ons

Select Redis Enterprise Cloud. It will probably ask you to set up the credit card on your account, but don’t worry, the free plan is enough to our bot. After set up your credit card, select the free plan and the project of bot to install the Redis.

It is listed in your add-ons if everything is ok:

redis add-on

Access the add-on and complete the Redis url with the datas: http://rediscloud:[email protected]:port

Like this: http://rediscloud:cofe6kWpNnsdlfkçlç[email protected]:1234

This will be used to connect our bot with the Redis.

Create GitHub webhook

Your repository to track > Settings > Webhooks > add webhook

In the Payload URL, past the URL of your Heroku project and the endpoint check_conflicts. Like this: https://my-heroku-app.herokuapp.com/check_conflicts

The Content type is application/json.

I recommend using a UUID in the Secret. This will ensure the security of the requests. Save this secret to use in virtual environments of the project

In the events, select Let me select individual events. and select Pull requests in the list of events.

Now just save.

Setting envs

Finally, the last step is set the virtual environments in the Heroku project.

Heroku project > Settings > Reveal Config vars.

Now set this envs:

  • SECRET_ACCESS: secret used in GitHub webhook
  • ACCESS_TOKEN: your access token of GitHub
  • PROJECT_TO_TRACK: project of the GitHub webhook. Example: jackson541/github-alerts-slack-bot
  • BRANCH_TO_TRACK: the branch of the project that you want to track. Example: master
  • SLACK_WEBHOOK_LINK: the URL of the workflow created in Slack
  • REDIS_URL: the URL created with the datas of Redis. example: http://rediscloud:cofe6kWpNnsdlfkçlç[email protected]:1234

You can stop and have your coffee, everything is set up!

Contribute

Contribute is always well received! Feel free to open Pull Requests or Issues.
?

GitHub

View Github