Skip to content

Commit

Permalink
Refactor getSlackBlocks() (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Sep 12, 2024
1 parent 0402825 commit 17abba4
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/slack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContextBlock, KnownBlock, MrkdwnElement } from '@slack/web-api'
import { KnownBlock, MrkdwnElement, SectionBlock } from '@slack/web-api'
import { FailedJob, WorkflowRunSummary } from './workflow-run.js'

type Context = {
Expand All @@ -12,41 +12,38 @@ export type Templates = {

export const getSlackBlocks = (w: WorkflowRunSummary, c: Context, templates: Templates): KnownBlock[] => {
const conclusion = w.conclusion?.toLocaleLowerCase() ?? ''
const blocks: KnownBlock[] = [
return [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `Workflow *<${w.workflowRunUrl}|${w.workflowName}>* ${conclusion}`,
},
},
...getFailedJobBlocks(w, templates),
{
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `:github: ${c.repository}/*${w.branch}*`,
},
...getPullRequestBlock(w),
...getMentionBlock(w, c),
],
},
]

for (const failedJob of w.failedJobs) {
blocks.push({
type: 'section',
text: {
type: 'mrkdwn',
text: [`Job *${failedJob.name}*`, ...getFailedJobCause(failedJob, templates)].join('\n'),
},
})
}

const contextBlock: ContextBlock = {
type: 'context',
elements: [
{
type: 'mrkdwn',
text: `:github: ${c.repository}/*${w.branch}*`,
},
...getPullRequestBlock(w),
...getMentionBlock(w, c),
],
}
blocks.push(contextBlock)
return blocks
}

const getFailedJobBlocks = (w: WorkflowRunSummary, templates: Templates): SectionBlock[] =>
w.failedJobs.map((failedJob) => ({
type: 'section',
text: {
type: 'mrkdwn',
text: [`Job *${failedJob.name}*`, ...getFailedJobCause(failedJob, templates)].join('\n'),
},
}))

export const getFailedJobCause = (failedJob: FailedJob, templates: Templates): string[] => {
for (const m of failedJob.failureAnnotationMessages) {
if (m.match(/The self-hosted runner: .+? lost communication with the server/)) {
Expand Down

0 comments on commit 17abba4

Please sign in to comment.