Created
January 3, 2020 09:36
-
-
Save walterwhites/b638a1f15a593685b723db5a7d90d214 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const AWS = require('aws-sdk'); | |
| const url = require('url'); | |
| const https = require('https'); | |
| const hookUrlParameter = process.env.hookUrlParameter; | |
| let hookUrl; | |
| let ssm = new AWS.SSM(); | |
| function postMessage(message, callback) { | |
| const body = JSON.stringify(message); | |
| const options = url.parse(hookUrl); | |
| options.method = 'POST'; | |
| options.headers = { | |
| 'Content-Type': 'application/json', | |
| 'Content-Length': Buffer.byteLength(body), | |
| }; | |
| const postReq = https.request(options, (res) => { | |
| const chunks = []; | |
| res.setEncoding('utf8'); | |
| res.on('data', (chunk) => chunks.push(chunk)); | |
| res.on('end', () => { | |
| if (callback) { | |
| callback({ | |
| body: chunks.join(''), | |
| statusCode: res.statusCode, | |
| statusMessage: res.statusMessage, | |
| }); | |
| } | |
| }); | |
| return res; | |
| }); | |
| postReq.write(body); | |
| postReq.end(); | |
| } | |
| function processEvent(event, callback) { | |
| const buildArn = event.detail['build-id']; | |
| const buildId = buildArn.split('/').pop(); | |
| const buildUuid = buildId.split(':').pop(); | |
| const projectName = event.detail['project-name']; | |
| const buildStatus = event.detail['build-status']; | |
| const buildVariables = event.detail['additional-information'].environment['environment-variables']; | |
| const region = event.region; | |
| const slackMessage = { | |
| text: `Build has reached ${buildStatus} status. ` + | |
| `Visit the <https:\/\/${region}.console.aws.amazon.com\/codebuild\/home?region=${region}#\/builds\/${encodeURI(buildId)}\/view\/new|AWS console> to view details` | |
| }; | |
| postMessage(slackMessage, (response) => { | |
| if (response.statusCode < 400) { | |
| console.info('Message posted successfully'); | |
| callback(null); | |
| } else if (response.statusCode < 500) { | |
| console.error(`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`); | |
| callback(null); | |
| } else { | |
| callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`); | |
| } | |
| }); | |
| } | |
| exports.handler = (event, context, callback) => { | |
| if (hookUrl) { | |
| processEvent(event, callback); | |
| } else if (hookUrlParameter) { | |
| const params = { | |
| Name: hookUrlParameter, | |
| WithDecryption: true | |
| }; | |
| ssm.getParameter(params, (err, data) => { | |
| if (err) { | |
| console.log('Hook URL parameter error:', err); | |
| return callback(err); | |
| } | |
| hookUrl = data.Parameter.Value; | |
| processEvent(event, callback); | |
| }); | |
| } else { | |
| callback('Hook URL parameter has not been set.'); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment