Skip to content

Instantly share code, notes, and snippets.

@nishanthvijayan
Last active April 7, 2019 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishanthvijayan/ae71dda4be9b8c750e2637e8aa60b336 to your computer and use it in GitHub Desktop.
Save nishanthvijayan/ae71dda4be9b8c750e2637e8aa60b336 to your computer and use it in GitHub Desktop.
Recieve an Email notification when someone stars your GitHub repository

GitHub Star Notification

AWS Lambda script to recieve email notifcation when someone stars your GitHub repository.

Instructions

AWS Lambda

Set up an AWS Lambda function with an API gateway endpoint (POST method) that triggers this lambda function. The code for the Lambda function is in index.js.

The following environment variable needs to be configured for the function to work properly.

  • RECEIVER_EMAIL: EmailID to which the notification email should be sent to.
  • SOURCE_EMAIL: EmailID to which the notification email should be sent from.

The Lambda function should be given the appropriate permissions to send email.

GitHub Webhook

A new webhook needs to be created in all the repositories that should be alerted on. The webhook should be setup to recieve only watch events. See watch event documentation.

Instruction for creating a webhook: GitHub webhooks documentaiton

const AWS = require("aws-sdk");
const RECEIVER_EMAIL = process.env.RECEIVER_EMAIL;
const SOURCE_EMAIL = process.env.SOURCE_EMAIL;
exports.handler = function(event, context) {
AWS.config.update({ region: "us-east-1" });
const repoName = event.repository.name;
const userName = event.sender.login;
const htmlBody = `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>${userName} starred ${repoName}</p>
</body>
</html>
`;
const params = {
Destination: {
ToAddresses: [RECEIVER_EMAIL]
},
Message: {
Body: {
Html: {
Charset: "UTF-8",
Data: htmlBody
},
Text: {
Charset: "UTF-8",
Data: `${userName} starred ${repoName}`
}
},
Subject: {
Charset: "UTF-8",
Data: "Starry starry night.."
}
},
Source: SOURCE_EMAIL
};
// Create the promise and SES service object
const sendPromise = new AWS.SES({ apiVersion: "2010-12-01" })
.sendEmail(params)
.promise();
// Handle promise's fulfilled/rejected states
sendPromise
.then(data => {
console.log(data.MessageId);
context.done(null, "Success");
})
.catch(err => {
console.error(err, err.stack);
context.done(null, "Failed");
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment