Created
November 9, 2024 00:40
-
-
Save wildan3105/86746bee589a544c714dc09a04d87ad0 to your computer and use it in GitHub Desktop.
a code to transform heroku/github payload to telegram-readable message
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
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