Last active
December 15, 2023 05:47
-
-
Save PratikBodawala/8f482773583192525cbec57f6e227211 to your computer and use it in GitHub Desktop.
Amplify hooks for slack notification: pre-push. Purpose of this notification is to answer, who did what, when, where
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
(async ()=>{const fs = require('fs'); | |
const {simpleGit} = require('simple-git'); | |
const parameters = JSON.parse(fs.readFileSync(0, { encoding: 'utf8' })); | |
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()) | |
await git.status((err, output)=>{ | |
if( | |
output.not_added.length || | |
output.conflicted.length || | |
output.created.length || | |
output.deleted.length | |
){ | |
console.log('Aborted deployment because there are uncommitted changes') | |
process.exit(1) | |
} | |
}) | |
const name = (await git.raw(['config', 'user.name']))?.trim() | |
const jsonBody = { | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": `Deployment started by ${name} :rocket:`, | |
"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 | |
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