Skip to content

Instantly share code, notes, and snippets.

@ujwaldhakal
Last active July 3, 2021 15:09
Show Gist options
  • Save ujwaldhakal/9523e928e77d3c09d1c7c22e14668829 to your computer and use it in GitHub Desktop.
Save ujwaldhakal/9523e928e77d3c09d1c7c22e14668829 to your computer and use it in GitHub Desktop.
robot.hear (`@${process.env.BOT_ID}`,async (bot) => {
// @deploy api featurex to production
const payload = bot.message.text.split(" ")
const service = payload[2];
const branch = payload[3];
const environment = payload[5];
const username = bot.message.user.name;
bot.send(`Roger that! Please wait.`);
if(!validateCommand(bot,username,service,branch,environment)) {
return;
}
await triggerWorkflow(bot,username,service,environment,branch)
bot.send(`Github Action has been triggered successfully`);
})
const validateCommand = (bot,username,service,branch,environment) => {
if(!validServices.includes(service)) {
bot.send(`${service} is not availble, Only ${validServices.join(', ')} are available`);
return false;
}
if(!validEnvironments.includes(environment)) {
bot.send(`${environment} is not availble. Only ${validEnvironments.join(', ')} are available`);
return false;
}
return true;
}
const triggerWorkflow = (bot,username,service,environment,branch) => {
try {
const data = await axios.post(`https://api.github.com/repos/ujwaldhakal/hubot-deployer/dispatches`,{
'event_type': 'deploy-service',
'client_payload': {'environment': environment, 'ref': branch}
},{headers:{
Authorization: `token ${token}`,
}})
}
catch(e) {
bot.send(`Sorry @${username} could not trigger github action. Please check my logs ${e.message}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment