Skip to content

Commit

Permalink
Add on-failure option to include_jobs
Browse files Browse the repository at this point in the history
And while we're here — don't generate jobs attachment if it's not needed &
mart `include_jobs` argument in README as required as it is
  • Loading branch information
ojab authored and AnthonyKinson committed Nov 24, 2021
1 parent 78c8448 commit ea2df14
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ This action will post workflow status notifications into your Slack channel. The
| ---- | -------- | ----------- |
| **slack_webhook_url** | _required_ | Create a Slack Webhook URL using the [Incoming Webhooks App](https://slack.com/apps/A0F7XDUAZ-incoming-webhooks?next_id=0). It is recommended that you create a new secret on your repo `SLACK_WEBHOOK_URL` for holding this value, and passing it to the action with `${{secrets.SLACK_WEBHOOK_URL}}`.
| **repo_token** | _required_ | A token is automatically available in your workflow secrets var. `${{secrets.GITHUB_TOKEN}}`. You can optionally send an alternative self-generated token.
| **include_jobs** | _required_ | When set to `true`, individual job status and durations in the slack notification. When `false` only the event status and workflow status lines are included. When set to `on-failure` — individual job status is reported only if workflow failed.
| **channel** | _optional_ | Accepts a Slack channel name where you would like the notifications to appear. Overrides the default channel created with your webhook.
| **name** | _optional_ | Allows you to provide a name for the slack bot user posting the notifications. Overrides the default name created with your webhook.
| **icon_emoji** | _optional_ | Allows you to provide an emoji as the slack bot user image when posting notifications. Overrides the default image created with your webhook. _[Emoji Code Cheat Sheet](https://www.webfx.com/tools/emoji-cheat-sheet/)_
| **icon_url** | _optional_ | Allows you to provide a URL for an image to use as the slack bot user image when posting notifications. Overrides the default image created with your webhook.
| **include_jobs** | _optional_ | When set to `true`, individual job status and durations in the slack notification. When `false` only the event status and workflow status lines are included.


## Usage
To use this action properly, you should create a new `job` at the end of your workflow that `needs` all other jobs in the workflow. This ensures that this action is only run once all jobs in your workflow are complete.
Expand Down
20 changes: 16 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9334,7 +9334,9 @@ function main() {
required: true
});
const github_token = core.getInput('repo_token', { required: true });
const include_jobs = core.getInput('include_jobs', { required: true });
const include_jobs = core.getInput('include_jobs', {
required: true
});
const slack_channel = core.getInput('channel');
const slack_name = core.getInput('name');
const slack_icon = core.getInput('icon_url');
Expand All @@ -9360,21 +9362,31 @@ function main() {
// Configure slack attachment styling
let workflow_color; // can be good, danger, warning or a HEX colour (#00FF00)
let workflow_msg;
let job_fields;
if (completed_jobs.every(job => ['success', 'skipped'].includes(job.conclusion))) {
workflow_color = 'good';
workflow_msg = 'Success:';
if (include_jobs === 'on-failure') {
job_fields = [];
}
}
else if (completed_jobs.some(job => job.conclusion === 'cancelled')) {
workflow_color = 'warning';
workflow_msg = 'Cancelled:';
if (include_jobs === 'on-failure') {
job_fields = [];
}
}
else {
// (jobs_response.jobs.some(job => job.conclusion === 'failed')
workflow_color = 'danger';
workflow_msg = 'Failed:';
}
if (include_jobs === 'false') {
job_fields = [];
}
// Build Job Data Fields
const job_fields = completed_jobs.map(job => {
job_fields !== null && job_fields !== void 0 ? job_fields : (job_fields = completed_jobs.map(job => {
let job_status_icon;
switch (job.conclusion) {
case 'success':
Expand All @@ -9397,7 +9409,7 @@ function main() {
short: true,
value: `${job_status_icon} <${job.html_url}|${job.name}> (${job_duration})`
};
});
}));
// Payload Formatting Shortcuts
const workflow_duration = compute_duration({
start: new Date(workflow_run.created_at),
Expand Down Expand Up @@ -9427,7 +9439,7 @@ function main() {
text: status_string + details_string,
footer: repo_url,
footer_icon: 'https://github.githubassets.com/favicon.ico',
fields: include_jobs === 'true' ? job_fields : []
fields: job_fields
};
// Build our notification payload
const slack_payload_body = Object.assign(Object.assign(Object.assign(Object.assign({ attachments: [slack_attachment] }, (slack_name && { username: slack_name })), (slack_channel && { channel: slack_channel })), (slack_emoji && { icon_emoji: slack_emoji })), (slack_icon && { icon_url: slack_icon }));
Expand Down
24 changes: 21 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as core from '@actions/core'
import {context, getOctokit} from '@actions/github'
import {ActionsListJobsForWorkflowRunResponseData} from '@octokit/types'
import {IncomingWebhook} from '@slack/webhook'
import {MessageAttachment} from '@slack/types'

// HACK: https://github.com/octokit/types.ts/issues/205
interface PullRequest {
Expand All @@ -41,6 +42,9 @@ interface PullRequest {
}
}

type IncludeJobs = 'true' | 'false' | 'on-failure'
type SlackMessageAttachementFields = MessageAttachment['fields']

process.on('unhandledRejection', handleError)
main().catch(handleError) // eslint-disable-line github/no-then

Expand All @@ -51,7 +55,9 @@ async function main(): Promise<void> {
required: true
})
const github_token = core.getInput('repo_token', {required: true})
const include_jobs = core.getInput('include_jobs', {required: true})
const include_jobs = core.getInput('include_jobs', {
required: true
}) as IncludeJobs
const slack_channel = core.getInput('channel')
const slack_name = core.getInput('name')
const slack_icon = core.getInput('icon_url')
Expand Down Expand Up @@ -83,22 +89,34 @@ async function main(): Promise<void> {
let workflow_color // can be good, danger, warning or a HEX colour (#00FF00)
let workflow_msg

let job_fields: SlackMessageAttachementFields

if (
completed_jobs.every(job => ['success', 'skipped'].includes(job.conclusion))
) {
workflow_color = 'good'
workflow_msg = 'Success:'
if (include_jobs === 'on-failure') {
job_fields = []
}
} else if (completed_jobs.some(job => job.conclusion === 'cancelled')) {
workflow_color = 'warning'
workflow_msg = 'Cancelled:'
if (include_jobs === 'on-failure') {
job_fields = []
}
} else {
// (jobs_response.jobs.some(job => job.conclusion === 'failed')
workflow_color = 'danger'
workflow_msg = 'Failed:'
}

if (include_jobs === 'false') {
job_fields = []
}

// Build Job Data Fields
const job_fields = completed_jobs.map(job => {
job_fields ??= completed_jobs.map(job => {
let job_status_icon

switch (job.conclusion) {
Expand Down Expand Up @@ -162,7 +180,7 @@ async function main(): Promise<void> {
text: status_string + details_string,
footer: repo_url,
footer_icon: 'https://github.githubassets.com/favicon.ico',
fields: include_jobs === 'true' ? job_fields : []
fields: job_fields
}
// Build our notification payload
const slack_payload_body = {
Expand Down

0 comments on commit ea2df14

Please sign in to comment.