Last active
December 1, 2021 21:29
-
-
Save andrewodri/f00dc7bc5949dbe65b6e26a2c1d89a06 to your computer and use it in GitHub Desktop.
Docker Hub to Slack Incoming Webhook
This file contains 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
const fs = require('fs'); | |
const https = require('https'); | |
const querystring = require('querystring'); | |
const url = require('url'); | |
const util = require('util'); | |
let app = function(req, res) { | |
if(req.method.toLowerCase() == 'post'){ | |
let dockerBody = ''; | |
let dockerUrl; | |
let dockerData; | |
let slackBody = ''; | |
let slackData; | |
req.on('data', chunk => dockerBody += chunk).on('end', () => { | |
dockerUrl = url.parse(req.url); | |
dockerData = Object.assign( | |
JSON.parse(dockerBody), | |
querystring.parse(dockerUrl.query) | |
); | |
if(dockerData.include && dockerData.include !== dockerData.repository.repo_name) return; | |
if(dockerData.exclude && dockerData.exclude == dockerData.repository.repo_name) return; | |
//console.log(util.inspect(dockerData, { color: true, depth: null })); | |
let statusText = dockerData.repository.status.toLowerCase(); | |
let statusColor; | |
if(statusText == 'success') statusColor = 'good'; | |
else if(statusText == 'error') statusColor = 'danger'; | |
slackData = { | |
"icon_emoji": ":docker:", | |
"attachments": [ | |
{ | |
"color": statusColor, | |
"title": util.format("Docker Hub Build %s", dockerData.repository.status), | |
"title_link": dockerData.repository.repo_url + "builds/", | |
"text": util.format("The automated build status for the repo *%s* is *%s*.", dockerData.repository.repo_name, statusText), | |
"mrkdwn_in": [ "text" ] | |
} | |
] | |
}; | |
//console.log(util.inspect(slackData, { color: true, depth: null })); | |
let slackReq = https.request({ | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: dockerUrl.pathname, | |
method: 'post', | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
}, slackRes => {}) | |
.end(JSON.stringify(slackData)); | |
}); | |
res.writeHead(200, {'Content-Type': 'application/json'}) | |
res.end(JSON.stringify(slackData)); | |
} | |
} | |
let readFile = fileName => new Promise((resolve, reject) => { | |
fs.readFile(fileName, (err, data) => { | |
if(err){ reject(err); return; } | |
resolve(data.toString()); | |
}); | |
}); | |
Promise.all([ | |
readFile(process.argv[2]), | |
readFile(process.argv[3]), | |
]).then(values => { | |
let server = https.createServer({ | |
key: values[0], | |
cert: values[1], | |
}, app).listen(443); | |
}).catch(reason => { | |
console.log(reason); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can configure this webhook by following these steps:
include
to the URL with the value being the repo you want to filter (e.g. http://s3-website-us-east-1.amazonaws.com/TXXXXX/BXXXXX/XXXXXXXXXX?include=mogo/ui_core)You can execute this application by following these steps:
node app.js /path/to/private/key.key /path/to/ssl/certificate.crt