Skip to content

Instantly share code, notes, and snippets.

@PratikBodawala
Last active December 22, 2023 07:06
Show Gist options
  • Save PratikBodawala/7d44d86928313029a9e065b9690c6970 to your computer and use it in GitHub Desktop.
Save PratikBodawala/7d44d86928313029a9e065b9690c6970 to your computer and use it in GitHub Desktop.
Amplify hooks for slack notification: post-push. Purpose of this notification is to answer, who did what, when, where
(async () => {
const fs = require('fs');
const {simpleGit} = require('simple-git');
const parameters = JSON.parse(fs.readFileSync(0, {encoding: 'utf8'}));
console.log(JSON.stringify(parameters, null, 2))
const currentCLIVersion = parameters.data.amplify.version
const env = parameters.data.amplify.environment.envName;
const myHeaders = new Headers();
myHeaders.append("Content-type", "application/json");
const git = simpleGit(process.cwd())
const jsonBody = {
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": parameters?.error ? "Deployment failed :x:" : "Deployment finished :white_check_mark:",
"emoji": true
}
},
{
"type": "header",
"text": {
"type": "plain_text",
"text": `Environment: ${env}`,
"emoji": true
}
},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_list",
"style": "bullet",
"elements": []
}
]
}
]
}
const latestCommit = (await git.log()).latest
delete latestCommit.body
delete latestCommit.author_email
delete latestCommit.author_name
latestCommit.amplifyCLIVersion = currentCLIVersion
if (parameters?.error) {
jsonBody.blocks[2] = {
"type": "header",
"text": {
"type": "plain_text",
"text": parameters?.error?.message || '',
"emoji": true
}
}
jsonBody.blocks.push({
"type": "rich_text",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": parameters?.error?.stack || ''
}
]
}
]
})
} else {
jsonBody.blocks[2].elements[0].elements = Object.entries(latestCommit)
.map(line => line.join(' : '))
.map(line => ({
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": line
}
]
}))
}
const raw = JSON.stringify(jsonBody);
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
await fetch("https://hooks.slack.com/services/ABCD/PQRS/XYZ", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment