Skip to content

Commit b1b6baa

Browse files
GHA-219 Add Slack notification when rule metadata PR is opened or updated (#122)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f8d134d commit b1b6baa

4 files changed

Lines changed: 69 additions & 37 deletions

File tree

notify-slack/README.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
# Notify Slack on Failure Action
1+
# Notify Slack Action
22

3-
This GitHub Action sends a Slack notification summarizing failed jobs in a workflow run when used with an `if: failure()` condition.
3+
This GitHub Action sends a Slack notification. It supports two modes:
4+
- **Failure mode** (default): summarizes failed jobs in a workflow run, intended for use with `if: failure()`.
5+
- **Custom message mode**: sends a free-text message directly, useful for informational notifications such as PR creation alerts.
46

57
## Description
68

7-
The action posts a concise message to a Slack channel containing:
8-
1. A link to the failed GitHub Actions run
9-
2. A list of failed jobs provided by the workflow
10-
3. A custom project-branded username and icon
11-
12-
It is intended to be used as the last step (or in a dedicated job) guarded by `if: failure()` so that it only triggers on failures.
9+
The action posts a message to a Slack channel containing either:
10+
- A link to the failed GitHub Actions run and a list of failed jobs (failure mode), or
11+
- A custom message provided by the caller (custom message mode).
1312

1413
## Dependencies
1514

@@ -19,22 +18,22 @@ This action depends on:
1918

2019
## Inputs
2120

22-
| Input | Description | Required | Default |
23-
|-----------------|------------------------------------------------------------------------|----------|-----------|
24-
| `project-name` | The display name of the project; used in the Slack username. | Yes | - |
25-
| `icon` | Emoji icon for the Slack message (Slack emoji code). | No | `:alert:` |
26-
| `slack-channel` | Slack channel (without `#`) to post the notification into. | Yes | - |
27-
| `jobs` | Comma-separated list of job names to report as failed (provided by you). | Yes | - |
28-
29-
Note: The list of failed jobs must be provided via the `jobs` input by your workflow logic.
21+
| Input | Description | Required | Default |
22+
|-----------------|--------------------------------------------------------------------------------------------------------|----------|-----------|
23+
| `project-name` | The display name of the project; used in the Slack username. | Yes | - |
24+
| `icon` | Emoji icon for the Slack message (Slack emoji code). | No | `:alert:` |
25+
| `slack-channel` | Slack channel (without `#`) to post the notification into. | Yes | - |
26+
| `jobs` | A GitHub needs-like object string of jobs and their results (e.g., from `toJSON(needs)`). Required when `message` is not set. | No | - |
27+
| `message` | A custom free-text Slack message. When provided, skips job-failure parsing and sends this message directly. | No | - |
28+
| `color` | Slack attachment color (`good`, `danger`, `warning`, or a hex code). | No | `danger` |
3029

3130
## Outputs
3231

3332
No outputs are produced by this action.
3433

3534
## Usage
3635

37-
### Basic usage (in a dedicated failure notification job)
36+
### Failure notification (in a dedicated failure notification job)
3837

3938
```yaml
4039
jobs:
@@ -53,18 +52,21 @@ jobs:
5352
jobs: ${{ toJSON(needs) }}
5453
```
5554
56-
### Minimal usage (only required inputs)
55+
### Custom message (e.g. PR ready for review)
5756
5857
```yaml
5958
- uses: SonarSource/release-github-actions/notify-slack@v1
60-
if: failure()
6159
with:
6260
project-name: 'My Project'
63-
slack-channel: 'engineering-alerts'
64-
jobs: 'build, test'
61+
slack-channel: 'team-channel'
62+
icon: ':memo:'
63+
color: good
64+
message: |
65+
*my-repo* rule metadata PR is ready for review:
66+
https://github.com/org/my-repo/pull/42
6567
```
6668
67-
### Custom icon
69+
### Custom icon (failure mode)
6870
6971
```yaml
7072
- uses: SonarSource/release-github-actions/notify-slack@v1
@@ -73,18 +75,18 @@ jobs:
7375
project-name: 'My Project'
7476
slack-channel: 'engineering-alerts'
7577
icon: ':rocket:'
76-
jobs: 'build, test'
78+
jobs: ${{ toJSON(needs) }}
7779
```
7880
7981
## Implementation Details
8082
8183
The action is a composite action that:
8284
- Fetches `SLACK_TOKEN` from Vault path `development/kv/data/slack` using `vault-action-wrapper`
83-
- Uses a workflow-provided `jobs` input (comma-separated string) to populate the "Failed Jobs" section
84-
- Constructs a Slack message with run URL and the provided failed jobs list
85-
- Uses `rtCamp/action-slack-notify` to send a minimal styled message (no title/footer) with danger color
85+
- When `message` is not set: parses the `jobs` JSON input to extract failed job names and constructs a message with the run URL
86+
- When `message` is set: sends the provided message directly, skipping job-failure parsing
87+
- Uses `rtCamp/action-slack-notify` to send a minimal styled message (no title/footer)
88+
- Sets the attachment color from the `color` input (defaults to `danger`)
8689
- Sets Slack username to: `<project-name> CI Notifier`
87-
- If the `jobs` list is empty or not provided, the "Failed Jobs" line will be blank
8890

8991
## Prerequisites
9092

@@ -94,8 +96,6 @@ The action is a composite action that:
9496

9597
## Notes
9698

97-
- Use `if: failure()` on the step or job; otherwise it will also fire on success.
98-
- `project-name`, `slack-channel`, and `jobs` are required inputs (no defaults).
99+
- For failure notifications, use `if: failure()` on the step or job; otherwise it will also fire on success.
99100
- Do not prefix the channel with `#`.
100101
- Message formatting is intentionally minimal for quick triage in alert-focused channels.
101-
- How you compute the failed jobs list is up to your workflow; you can use prior steps to gather job results and pass them to this action via `jobs`.

notify-slack/action.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: 'Notify Slack on Failure'
2-
description: 'Sends a Slack notification when a job fails.'
2+
description: 'Sends a Slack notification when a job fails, or a custom message.'
33

44
inputs:
55
project-name:
@@ -13,13 +13,21 @@ inputs:
1313
description: 'The Slack channel to send the notification to.'
1414
required: true
1515
jobs:
16-
description: 'A GitHub needs-like object string of jobs and their results (e.g., from toJSON(needs)).'
17-
required: true
16+
description: 'A GitHub needs-like object string of jobs and their results (e.g., from toJSON(needs)). Required when message is not set.'
17+
required: false
18+
message:
19+
description: 'A custom free-text Slack message. When provided, skips job-failure parsing and sends this message directly.'
20+
required: false
21+
color:
22+
description: 'Slack attachment color (e.g. good, danger, warning, or a hex color code).'
23+
required: false
24+
default: 'danger'
1825

1926
runs:
2027
using: "composite"
2128
steps:
2229
- name: Collect Failed Jobs
30+
if: ${{ inputs.message == '' }}
2331
shell: bash
2432
id: collect_failed_jobs
2533
env:
@@ -43,11 +51,9 @@ runs:
4351
SLACK_MSG_AUTHOR: " "
4452
SLACK_TITLE: " "
4553
SLACK_FOOTER: " "
46-
SLACK_COLOR: danger
54+
SLACK_COLOR: ${{ inputs.color }}
4755
MSG_MINIMAL: true
48-
SLACK_MESSAGE: |
49-
*Run:* ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
50-
*Failed Jobs:* ${{ steps.collect_failed_jobs.outputs.failed-jobs }}
56+
SLACK_MESSAGE: ${{ inputs.message != '' && inputs.message || format('*Run:* {0}/{1}/actions/runs/{2}\n*Failed Jobs:* {3}', github.server_url, github.repository, github.run_id, steps.collect_failed_jobs.outputs.failed-jobs) }}
5157
SLACK_TOKEN: "${{ fromJSON(steps.secrets.outputs.vault).SLACK_TOKEN }}"
5258
SLACK_ICON_EMOJI: "${{ inputs.icon }}"
5359
SLACK_USERNAME: "${{ inputs.project-name }} CI Notifier"

update-rule-metadata/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This action depends on:
2727
| `sonarpedia-files` | Comma-separated list of sonarpedia files to be updated. By default, it will update all Sonarpedia files in the repository. | No | Auto-discovered |
2828
| `branch` | Branch to run the check against and create the PR for. By default, it will use master. | No | `master` |
2929
| `rspec-branch` | Branch of the rspec repository to be used. If not specified, the `master` branch will be used by default. | No | `master` |
30+
| `slack-channel` | Slack channel to notify when a PR is created or updated. If empty, no notification is sent. | No | - |
3031

3132
## Outputs
3233

@@ -81,6 +82,15 @@ permissions:
8182
branch: 'develop'
8283
```
8384
85+
### Notify a Slack channel when a PR is created or updated
86+
87+
```yaml
88+
- name: Update Rule Metadata
89+
uses: SonarSource/release-github-actions/update-rule-metadata@v1
90+
with:
91+
slack-channel: 'team-channel'
92+
```
93+
8494
### Complete example with all inputs
8595
8696
```yaml
@@ -99,6 +109,7 @@ jobs:
99109
sonarpedia-files: 'frontend/java/sonarpedia.json,frontend/csharp/sonarpedia.json'
100110
branch: 'develop'
101111
rspec-branch: 'feature/my-rspec-branch'
112+
slack-channel: 'team-channel'
102113
```
103114
104115
## Implementation Details

update-rule-metadata/action.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ inputs:
3030
rspec-token-suffix:
3131
description: 'Suffix for the RSpec token if different from "rspec-read".'
3232
required: false
33+
slack-channel:
34+
description: 'Slack channel to notify when a PR is created or updated. If empty, no notification is sent.'
35+
required: false
3336

3437
outputs:
3538
has-changes:
@@ -279,3 +282,15 @@ runs:
279282
base: ${{ inputs.branch }}
280283
branch: bot/update-rule-metadata
281284
labels: ${{ inputs.labels }}
285+
286+
- name: Notify Slack
287+
if: ${{ inputs.slack-channel != '' && steps.create-pr.outputs.pull-request-operation != 'none' }}
288+
uses: SonarSource/release-github-actions/notify-slack@v1
289+
with:
290+
project-name: ${{ github.repository }}
291+
slack-channel: ${{ inputs.slack-channel }}
292+
icon: ':memo:'
293+
color: good
294+
message: |
295+
*${{ github.event.repository.name }}* rule metadata PR is ready for review:
296+
${{ steps.create-pr.outputs.pull-request-url }}

0 commit comments

Comments
 (0)