Pull Request notification on Slack using AWS Lambda
Harish Aravindan
Posted on February 4, 2023
Pull request management can become hectic while working across multiple repositories. Asking for approvals individually is also a long process. How about getting notified through a common channel to save time for reviewers and developers.
This post details on how to create a notification system on slack for github pull requests.
Requirements
aws account, github repository, slack channel ( with permission to create a slack app and webhook )
Step 1 : Creating a slack app and webhook in a channel
- Slack has a good document on this, please follow that. https://api.slack.com/tutorials/slack-apps-hello-world once that is done we would get a webhook from slack which would be similar to https://hooks.slack.com/services/aldnfaksndksakd/aljdfkajndkjasn/adfasdfakjdfnaksdfkajldakdnkasndlakjd
you can test this with the curl command given in the same document. Note that down webhook url which will be used later.
Step 2 : Creating Lambda and API gateway for processing the pull request event
we need to package the code required for notifications
clone or download the repo github.com/uptownaravi/pullRequestSlack
it will have lambda_function.py ( the logic ) and the requirements.txt ( dependency package )
we need to install that dependency and zip the files.
navigate to the cloned repository
pip install --target 'give the path of your current directory' -r requirements.txt
once that is done, we can see the below folder structure
then select all the files there except the readme.md and requirements.txt and zip them ( in windows select send to compressed (Zipped) folder )
give the zip a name and
- log into the aws console and navigate to lambda
- click on create function
- select author from scratch ( it should be the default )
- give the function a name
- select python 3.8
Then click on actions --> upload .zip file
after upload it should load the code as seen below
We need to add slack token which we got earlier as a
environment variables in the lambda function
scroll down in the lambda screen and click on manage environment variable --> add environment variable
fill in the details as below screen shot, use the webhook url which you got from slack. Make sure the key is slackNotification
( this is used in the lambda code to get that value )
Note: We can encrypt the value in transit using KMS keys.
but wanted to keep this blog simple
use this link if required https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-encryption
API gateway creating and attaching this lambda
navigate to the api gateway in aws console and click on create api
then select http api --> click on build
then fill in the details as shown in the below image.
we need to use lambda as integration and select the lambda name we created in the last step ( make sure you are using the correct region )
click next and we will add the routes as shown below
using the post method here, so that github can post to this api
moving to next, add a stage name
next review and click on create
already auto deployment is enabled for this api.
so go to stages in the deploy section and copy the invoke url
this will be used in the github repository as post webhook
Step 3 : Adding the api gateway url to github
navigated to your github repistory --> click on settings --> then webhooks
add your api gateway in the below format URL/pull in the payload URL section
example: https://sample.execute-api.ap-south-1.amazonaws.com/dev/pull
( /pull is the route in api gateway, make sure the url will end with dev/pull so that that is used while calling the POST method )
select the Let me select individual events
and scroll down to select the Pull requests option and uncheck the Pushes
then click on add webhook
that's it, create a pull request in that repository and it should create a notification on slack channel
you can modify the lambda code to handle various events in the pull requests.
Posted on February 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.