Skip to content

Commit

Permalink
feat(sqs): add gh event to q
Browse files Browse the repository at this point in the history
  • Loading branch information
wyvern8 committed Nov 28, 2017
1 parent bd76d66 commit 1f6724a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 96 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"semantic-release": "^8.2.0",
"serverless": "^1.24.1",
"serverless-dotenv-plugin": "^1.0.1",
"serverless-webpack": "^4.1.0"
"serverless-webpack": "^4.1.0",
"sqs-producer": "^1.6.2"
},
"engines": {
"node": ">=8.4.0"
Expand Down
98 changes: 7 additions & 91 deletions serverless.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,25 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: gtmGithubHook
plugins:
- serverless-dotenv-plugin
- serverless-webpack

# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"

provider:
name: aws
runtime: nodejs6.10

# you can overwrite defaults here
stage: dev
region: ${env:GTM_AWS_REGION}

environment:
GTM_GITHUB_WEBHOOK_SECRET: ${env:GTM_GITHUB_WEBHOOK_SECRET}

# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" : "ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"

# you can define service wide environment variables here
# environment:
# variable1: value1

# you can add packaging information here
#package:
# include:
# - include-me.js
# - include-me-dir/**
# exclude:
# - exclude-me.js
# - exclude-me-dir/**
iamRoleStatements:
- Effect: Allow
Action:
- sqs:SendMessage
Resource:
Fn::GetAtt: [ PendingQueue, Arn ]

package:
individually: true
Expand All @@ -69,7 +30,7 @@ functions:
gtmGithubHook:
handler: dist/src/serverless/gtmGithubHook/gtmGithubHook.listener
environment:
SLS_PENDING_QUEUE_URL:
SQS_PENDING_QUEUE_URL:
Ref: PendingQueue

package:
Expand All @@ -82,51 +43,6 @@ functions:
method: post
cors: true


# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
# events:
# - http:
# path: users/create
# method: get
# - s3: ${env:BUCKET}
# - schedule: rate(10 minutes)
# - sns: greeter-topic
# - stream: arn:aws:dynamodb:region:XXXXXX:table/foo/stream/1970-01-01T00:00:00.000
# - alexaSkill
# - alexaSmartHome: amzn1.ask.skill.xx-xx-xx-xx
# - iot:
# sql: "SELECT * FROM 'some_topic'"
# - cloudwatchEvent:
# event:
# source:
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp

# Define function environment variables here
# environment:
# variable2: value2

# you can add CloudFormation resource templates here
#resources:
# Resources:
# NewResource:
# Type: AWS::S3::Bucket
# Properties:
# BucketName: my-new-bucket
# Outputs:
# NewOutput:
# Description: "Description for the output"
# Value: "Some output value"
resources:
Resources:
PendingQueue:
Expand Down
28 changes: 24 additions & 4 deletions src/serverless/gtmGithubHook/gtmGithubHook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var json = require('format-json');
let json = require('format-json');
let crypto = require('crypto');
let Producer = require('sqs-producer');

function listener(event, context, callback) {

Expand Down Expand Up @@ -88,11 +89,30 @@ function listener(event, context, callback) {

function handleEvent(type, body) {

if (type === 'pull_request' && body.action && ['opened', 'synchronize'].includes(body.action)) {
console.log(`new pull request: "${body.pull_request.title}" "${body.action}" by ${body.pull_request.user.login}`);
if (type === 'pull_request' && body.action && ['push', 'opened', 'synchronize'].includes(body.action)) {
console.log(`pull request: "${body.pull_request.title}" "${body.action}" by ${body.pull_request.user.login}`);

// add event body to SQS
console.log(process.env.SLS_PENDING_QUEUE_URL);
console.log('adding event to SQS: ' + process.env.SQS_PENDING_QUEUE_URL);

// create simple producer
let producer = Producer.create({
queueUrl: process.env.SQS_PENDING_QUEUE_URL,
region: process.env.GTM_AWS_REGION
});

let bodyString = JSON.stringify(body);
producer.send([
{
id: crypto.createHash('md5').update(bodyString).digest('hex'),
body: bodyString,
messageAttributes: {
ghEventType: { DataType: 'String', StringValue: type }
}
}
], function(err) {
if (err) console.log(err);
});

} else {
console.log(`unsupported event: type: '${type}' action: '${body.action}'`);
Expand Down

0 comments on commit 1f6724a

Please sign in to comment.