Skip to content

Instantly share code, notes, and snippets.

@ngs
Last active December 15, 2021 16:58
Show Gist options
  • Save ngs/eea5528a91df344a2bf97a460a3e53e6 to your computer and use it in GitHub Desktop.
Save ngs/eea5528a91df344a2bf97a460a3e53e6 to your computer and use it in GitHub Desktop.
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
const fetch = require('node-fetch');
exports.handle = async (req, res) => {
if (req.method !== 'POST') {
res.status(405).send('405');
return;
}
console.info(req.body.payload);
const json = JSON.parse(req.body.payload);
const { response_url: responseURL, message } = json;
const { blocks } = message;
const buttonBlock = blocks
.map(block =>
(block.elements || [])
.filter(element => element.type === 'button')
).find(element => element.length > 0)[0];
const { value } = buttonBlock;
const [ action, repo, ref, build_number ] = value.split(':');
if (action != 'deploy') {
res.status(400).send('400');
return;
}
const url = `https://api.github.com/repos/${repo}/deployments`;
const deployParams = {
ref,
auto_merge: false,
payload: { build_number }
}
await fetch(url, {
method: 'POST',
body: JSON.stringify(deployParams),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
},
});
const slackParams = {
text: `Started deployment ${ref} ${build_number}\nhttps://github.com/${repo}/deployments`
};
await fetch(responseURL, {
method: 'POST',
body: JSON.stringify(slackParams),
headers: {
'Content-Type': 'application/json'
},
})
res.status(200).send(message);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment