Skip to content

Instantly share code, notes, and snippets.

@wildan3105
Created November 9, 2024 00:40
Show Gist options
  • Save wildan3105/86746bee589a544c714dc09a04d87ad0 to your computer and use it in GitHub Desktop.
Save wildan3105/86746bee589a544c714dc09a04d87ad0 to your computer and use it in GitHub Desktop.
a code to transform heroku/github payload to telegram-readable message
const data = startTrigger.data;
const statusEmojis = {
// github
"queued": "βŒ›",
"in_progress": "πŸƒβ€β™‚οΈ",
"completed": "πŸš€",
"failure": "❌",
// heroku
"pending": "βŒ›",
"succeeded": "πŸš€",
"failed": "❌"
};
let deploymentStatus = {};
const timeZone = 'Asia/Bangkok'; // GMT+7 timezone
if (data?.workflow_job?.workflow_name === 'Deploy to VPS') {
// GitHub deployment
// Each job represents a single job for entire deployment
// Hence we use created_at => job created and completed_at => job completed/failed
const platform = "Github";
const repo = data.repository.full_name;
const status = data.action;
const identifier = data.workflow_job.id;
const jobLink = data.workflow_job.html_url;
const conclusion = data.workflow_job.conclusion;
const created = new Date(
data.workflow_job.status === "completed"
? data.workflow_job.completed_at
: data.workflow_job.created_at
).toLocaleString("en-US", { timeZone });
const displayStatus = conclusion === "failure" ? "failure" : status;
const statusEmoji = statusEmojis[displayStatus];
const statusText = displayStatus === "failure" ? "FAILED" : displayStatus.toUpperCase();
const message = `πŸ›  *Deployment Bot*\n\n` +
`βš™οΈ *Platform*: ${platform}\n` +
`πŸ“¦ *Repository*: ${repo}\n` +
`πŸ†” *ID*: ${identifier}\n` +
`πŸ”— *Link*: ${jobLink}\n` +
`πŸ“… *Created*: ${created}\n` +
`πŸ“Œ *Status*: ${statusEmoji} ${statusText}\n`
deploymentStatus = {
text: message,
parse_mode: "Markdown"
};
} else if (data?.resource === 'build') {
// Heroku deployment using build resource
// Each build is used for a different state (pending vs succeeded vs failed)
// Hence we can safely use 'created_at' as its timestamp identifier when each state happens
const platform = 'Heroku';
const appName = data.data.app.name;
const buildBaseURL = `https://dashboard.heroku.com/apps/${appName}/activity/builds`
const identifier = data.data.id;
const status = data.data.status;
const buildURL = `${buildBaseURL}/${identifier}`;
const created = new Date(data.created_at).toLocaleString("en-US", { timeZone });
const statusEmoji = statusEmojis[status];
const message = `πŸ›  *Deployment Bot*\n\n` +
`βš™οΈ *Platform*: ${platform}\n` +
`πŸ“¦ *Repository*: wildan3105/${appName}\n` +
`πŸ†” *ID*: ${identifier}\n` +
`πŸ”— *Link*: ${buildURL}\n` +
`πŸ“… *Created*: ${created}\n` +
`πŸ“Œ *Status*: ${statusEmoji} ${status.toUpperCase()} \n`;
deploymentStatus = {
text: message,
parse_mode: "Markdown"
};
}
return deploymentStatus;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment