Skip to content

Commit

Permalink
Add mention-message option (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Nov 20, 2024
1 parent 3864cc6 commit 065ef94
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Otherwise, it inspects the current workflow run.
| `slack-app-token` | (dry-run) | Bot token of Slack App |
| `github-token` | `github.token` | GitHub token |
| `lost-communication-error-message` | See action.yaml | Message to send when "The self-hosted runner lost communication with the server" error |
| `mention-message` | `github.actor` | Message to mention the current user |

### Outputs

Expand Down
5 changes: 5 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ inputs:
description: Message to send when "The self-hosted runner lost communication with the server" error
required: true
default: The job was failed due to an infrastructure problem. Please rerun it if needed.
mention-message:
description: Message to mention the current user. Set to empty to disable mentioning.
required: false
default: |-
@${{ github.actor }}
runs:
using: 'node20'
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const main = async (): Promise<void> => {
slackAppToken: core.getInput('slack-app-token'),
githubToken: core.getInput('github-token', { required: true }),
lostCommunicationErrorMessage: core.getInput('lost-communication-error-message', { required: true }),
mentionMessage: core.getInput('mention-message'),
githubCurrentJobStatus: core.getInput('github-job-status', { required: true }),
})
}
Expand Down
7 changes: 4 additions & 3 deletions src/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Context = {

export type Templates = {
lostCommunicationErrorMessage: string
mentionMessage: string
}

export const getSlackBlocks = (w: WorkflowRunSummary, c: Context, templates: Templates): KnownBlock[] => {
Expand All @@ -29,7 +30,7 @@ export const getSlackBlocks = (w: WorkflowRunSummary, c: Context, templates: Tem
text: `:github: ${c.repository}/*${w.branch}*`,
},
...getPullRequestBlock(w),
...getMentionBlock(w, c),
...getMentionBlock(w, templates),
],
},
]
Expand Down Expand Up @@ -79,15 +80,15 @@ const getPullRequestBlock = (w: WorkflowRunSummary): MrkdwnElement[] => {
]
}

const getMentionBlock = (w: WorkflowRunSummary, c: Context): MrkdwnElement[] => {
const getMentionBlock = (w: WorkflowRunSummary, templates: Templates): MrkdwnElement[] => {
if (w.event === 'schedule') {
// For a scheduled event, github.actor is the last committer. Do not mention it.
return []
}
return [
{
type: 'mrkdwn',
text: `@${c.actor}`,
text: templates.mentionMessage,
},
]
}
11 changes: 7 additions & 4 deletions tests/slack.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFailedJobCause } from '../src/slack.js'
import { getFailedJobCause, Templates } from '../src/slack.js'

describe('getFailedJobCause', () => {
it('should return lostCommunicationErrorMessage when the failureAnnotationMessages contain lost communication message', () => {
Expand All @@ -8,8 +8,9 @@ describe('getFailedJobCause', () => {
'The self-hosted runner: example-vnvrd-runner-98tzt lost communication with the server. Verify the machine is running and has a healthy network connection. Anything in your workflow that terminates the runner process, starves it for CPU/Memory, or blocks its network access can cause this error.',
],
}
const templates = {
const templates: Templates = {
lostCommunicationErrorMessage: 'SORRY',
mentionMessage: '@octocat',
}
const cause = getFailedJobCause(failedJob, templates)
expect(cause).toStrictEqual(['SORRY'])
Expand All @@ -20,8 +21,9 @@ describe('getFailedJobCause', () => {
name: 'job2',
failureAnnotationMessages: [],
}
const templates = {
const templates: Templates = {
lostCommunicationErrorMessage: 'SORRY',
mentionMessage: '@octocat',
}
const cause = getFailedJobCause(failedJob, templates)
expect(cause).toStrictEqual([])
Expand All @@ -32,8 +34,9 @@ describe('getFailedJobCause', () => {
name: 'job3',
failureAnnotationMessages: ['message1', 'message2'],
}
const templates = {
const templates: Templates = {
lostCommunicationErrorMessage: 'SORRY',
mentionMessage: '@octocat',
}
const cause = getFailedJobCause(failedJob, templates)
expect(cause).toStrictEqual(['```', 'message1', 'message2', '```'])
Expand Down

0 comments on commit 065ef94

Please sign in to comment.