Skip to content

Commit

Permalink
Merge pull request #16 from capralifecycle/jlj/fix-slack-notify-action
Browse files Browse the repository at this point in the history
fix: Slack notify action that tries response.json() on application/te…
  • Loading branch information
joakimen authored Aug 28, 2024
2 parents 3aa9e6c + df4c745 commit 292930f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions slack-notify/action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,35 @@ const body = JSON.stringify({
if (dryRun) {
appendFileSync(process.env.GITHUB_OUTPUT, `payload=${body}\n`)
} else {
const endpointUrl = incomingWebhookUrl || "https://slack.com/api/chat.postMessage"
const useIncomingWebhook = incomingWebhookUrl !== undefined
const endpointUrl = useIncomingWebhook ? incomingWebhookUrl : "https://slack.com/api/chat.postMessage"
const response = await fetch(endpointUrl, {
method: "POST",
body,
headers: {
/*
* Only set the Authorization header if we're not using webhook and bot token is defined
* Since there's a check above, there will under no circumstances be a situation where
* useIncomingWebhook is true and the botToken-variable is defined
*/
...(botToken && { "Authorization": `Bearer ${botToken}` }),
"Content-Type": "application/json; charset=utf-8"
}
})
if (!response.ok) {
throw new Error(`Request failed with status ${response.status} ${response.statusText}`)
}
const { ok, error } = await response.json()
if (!ok) {
throw new Error(`Request failed with error ${error}`)
/*
* Slack is using a different response scheme for incoming webhooks. Thereby,
* we only do fetch.then(response => resonse.json()) if messages was posted to the
* chat.postMessage-api.
*
* Error handling for incoming webhooks: https://api.slack.com/messaging/webhooks#handling_errors
*/
if (!useIncomingWebhook) {
const { ok, error } = await response.json()
if (!ok) {
throw new Error(`Request failed with error ${error}`)
}
}
}

0 comments on commit 292930f

Please sign in to comment.