DEV Community

Cover image for Pull Request Notification on Slack using AWS Lambda
Harish Aravindan
Harish Aravindan

Posted on

Pull Request Notification on Slack using AWS Lambda

Get notified on new pull request in your teams slack channel. This can be very useful if there are multiple repositories across a wide team.

This post details on how to get github pull request notifications on slack channel

Requirements:
AWS account, Slack Channel, Github

Step 1: Create a Slack Channel webhook

We need a slack webhook to post our events. Follow the below document and note down the webhook url
https://api.slack.com/tutorials/slack-apps-hello-world

Test the webhook using the command in that document to validate.

Step 2: AWS Lambda backend

We will be using lambda to process the pull request event and send notification to slack.

have posted the starter code in the below github repository
clone / download the repo github.com/uptownaravi/pullRequestSlack

Then install the requirements.txt

pip install --target="give-the-current-directory-path" -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

after that, this should be the folder structure. Then Zip these files.

zip

Then navigate to lambda console in aws.

create function --> author from scratch --> give it a name and runtime as Python 3.7

create function

once created select actions --> upload a .zip file --> select the zip package we created earlier

zip upload

lambda should be updated as below

updated function

scroll down to the environment variables section
add key as slackNotification and value as the slack webhook URL we created in step 1.

env

make sure that key is slackNotification as this will be used by lambda code.

Then navigate to API gateway and create API
select the HTTP method and click on build

apig

choose add integration and select the lambda we created earlier

integration

then move on to next, select method as POST and resource path as /pull.

route

next give stage name as dev and let auto deploy be enabled.

stage

once created, note down the api url

apig created

select CORS in the left section and configure it to have POST and GET in the Access-Control-Allow-Methods section

cors

Step 3: Adding the API Gateway url to Github

We need github to send the pull request events to aws lambda through api gateway.

Navigate to your repository in github for which you need to get pull request notifications.

Go to settings in that repository

add the url in the payload url which we got from api gateway as below example
https://sample.execute-api.ap-south-1.amazonaws.com/dev/pull

note: use the api gateway url from step 2 and make sure to add /pull to the end of that url. The above one is an example.

github webhook

We need to keep the events to pull request to trigger the webhook.
choose let me select individual events and scroll down

and select pull request and un-check the Pushes --> Add webhook

pullRequests

That's it once you create a pull request in that repository, there will be a notification on the slack channel

botChat

if required modify the lambda code to handle different type of pull request events.

This is my first post in dev.to, looking forward to your comments.

Top comments (1)

Collapse
 
fasani profile image
Michael Fasani

I am considering building my own comments system for my Gatsby blog. AWS Lambda to open a GitHub PR for moderation. Have you seen anything like that?