Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/actions/slatify/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
3 changes: 3 additions & 0 deletions .github/actions/slatify/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Never reintroduce a caret range: every dependency here is pinned exact, and the
# committed package-lock.json is what actually pins the transitive tree.
save-exact=true
1 change: 1 addition & 0 deletions .github/actions/slatify/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24.19.0
10 changes: 10 additions & 0 deletions .github/actions/slatify/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid"
}
21 changes: 21 additions & 0 deletions .github/actions/slatify/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 GitHub, Inc. and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
76 changes: 76 additions & 0 deletions .github/actions/slatify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Slatify

Slack notification action for GitHub Actions.

Forked from [lazy-actions/slatify](https://github.com/lazy-actions/slatify) (MIT — see [LICENSE](./LICENSE)). Upstream has been unmaintained since March 2024 and still declares the removed `node12` runtime; this fork runs on `node24` against current dependencies.

## Usage

```yaml
- name: Slack Notification
uses: defold/github-actions-common/.github/actions/slatify@<ref>
if: always()
with:
type: ${{ job.status }}
job_name: '*Test*'
channel: '#random'
url: ${{ secrets.SLACK_WEBHOOK }}
```

Including the latest commit information:

```yaml
- name: Slack Notification
uses: defold/github-actions-common/.github/actions/slatify@<ref>
if: always()
with:
type: ${{ job.status }}
job_name: '*Lint Check*'
mention: 'here'
mention_if: 'failure'
channel: '#random'
url: ${{ secrets.SLACK_WEBHOOK }}
commit: true
token: ${{ secrets.GITHUB_TOKEN }}
```

## Inputs

| input | required | default | description |
| --- | --- | --- | --- |
| `type` | required | — | Job result: `success`, `failure` or `cancelled`. Use `${{ job.status }}`. |
| `job_name` | required | — | Slack notification title. |
| `url` | required | — | Slack Incoming Webhook URL. May instead be supplied as the `SLACK_WEBHOOK` environment variable. |
| `mention` | optional | — | Slack mention target, e.g. `here`, `channel`. |
| `mention_if` | optional | — | When to mention: `success`, `failure`, `cancelled` or `always`. An invalid value drops the mention and logs a warning. |
| `icon_emoji` | optional | webhook config | Slack icon. |
| `username` | optional | webhook config | Slack username. |
| `channel` | optional | webhook config | Slack channel name. |
| `commit` | optional | `false` | If `true`, include the latest commit message and author. |
| `token` | case by case | — | Required when `commit` is `true`. `${{ secrets.GITHUB_TOKEN }}` is recommended. |

## Differences from upstream

The input surface is unchanged, so this is a drop-in replacement apart from the `uses:` line. Behaviour differs in three places:

- **The `workflow` field links to the run.** Upstream linked to `…/commit/<sha>/checks` or `…/pull/<n>/checks`, which never identified the run that sent the message. It now points at `…/actions/runs/<run_id>`.
- **`GITHUB_SERVER_URL` is honoured**, so URLs are correct on GitHub Enterprise. Upstream hardcoded `https://github.com`.
- **The invalid-`mention_if` warning reports the offending value.** Upstream cleared the variable before formatting the message, so it always printed an empty value.
- **A failed post produces one annotation, not two.** Upstream called `core.error()` with the underlying cause and then threw a bare `Failed to post message to Slack`, so the detail and the failure arrived as two unrelated annotations. The cause is now folded into the thrown error and reported once via `core.setFailed()`. As a side effect, exercising the failure path in tests no longer paints red annotations onto a green run.

## Development

Requires the Node version in [`.nvmrc`](./.nvmrc) — building on a different Node can produce a `dist/` that fails CI.

```bash
cd .github/actions/slatify
npm ci --ignore-scripts
npm test
npm run build # regenerates dist/index.js and dist/package.json
```

`dist/` is committed because GitHub runs the bundle directly; `.github/workflows/slatify-build.yml` fails the build if it is stale.

Every dependency is pinned to an exact version, and `package-lock.json` is committed — it is what actually pins the transitive tree, with an integrity hash per package. `.npmrc` sets `save-exact=true` so `npm install <pkg>` cannot reintroduce a caret range.

**Dependabot PRs for this directory will fail CI on arrival.** Dependabot updates `package.json` and the lockfile but cannot rebuild the bundle, so each npm bump needs `npm run build` and an amended commit before it can merge.
43 changes: 43 additions & 0 deletions .github/actions/slatify/__tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as github from '@actions/github';
import {getWorkflowUrls} from '../src/github.js';
import {pullUrl, repoUrl, runUrl, setupContext} from './helpers.js';

setupContext();

describe('Workflow URL Tests', () => {
test('Pull Request event', () => {
github.context.eventName = 'pull_request';
expect(getWorkflowUrls()).toEqual({
repo: repoUrl,
event: pullUrl,
action: runUrl
});
});

test('Push event', () => {
github.context.eventName = 'push';
expect(getWorkflowUrls()).toEqual({
repo: repoUrl,
action: runUrl
});
});

test('Action URL points at the run, not the checks tab', () => {
github.context.eventName = 'push';
expect(getWorkflowUrls().action).not.toContain('/checks');
});

test('Honors a GitHub Enterprise server URL', () => {
github.context.eventName = 'push';
github.context.serverUrl = 'https://ghe.example.com';
try {
const urls = getWorkflowUrls();
expect(urls.repo).toBe('https://ghe.example.com/lazy-actions/slatify');
expect(urls.action).toBe(
'https://ghe.example.com/lazy-actions/slatify/actions/runs/99'
);
} finally {
setupContext();
}
});
});
68 changes: 68 additions & 0 deletions .github/actions/slatify/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as github from '@actions/github';
import type {FetchFunction} from '@slack/webhook';

// @slack/webhook re-exports FetchFunction but not FetchResponse, so derive it.
type FetchResponse = Awaited<ReturnType<FetchFunction>>;

export const commonContext = {
workflow: 'test',
ref: '1',
sha: '2',
owner: 'lazy-actions',
repo: 'slatify',
number: 3,
runId: 99,
serverUrl: 'https://github.com'
};

export const repoUrl = `${commonContext.serverUrl}/${commonContext.owner}/${commonContext.repo}`;
export const runUrl = `${repoUrl}/actions/runs/${commonContext.runId}`;
export const pullUrl = `${repoUrl}/pull/${commonContext.number}`;

/**
* Pin @actions/github's context singleton to the fixture above.
*
* `context.repo` reads GITHUB_REPOSITORY in preference to the payload, so it
* has to be unset — otherwise these tests resolve the *real* repository when
* they run on a GitHub Actions runner and every URL assertion fails.
*/
export function setupContext(): void {
delete process.env.GITHUB_REPOSITORY;

github.context.workflow = commonContext.workflow;
github.context.ref = commonContext.ref;
github.context.sha = commonContext.sha;
github.context.runId = commonContext.runId;
github.context.serverUrl = commonContext.serverUrl;
github.context.payload = {
issue: {
number: commonContext.number
},
repository: {
owner: {
login: commonContext.owner
},
name: commonContext.repo
}
};
}

/**
* Minimal stand-in for a fetch Response. @slack/webhook only reads `ok`,
* `status`, `statusText` and `text()`, so injecting this via the webhook's
* `fetch` option exercises the real send path with no HTTP interception.
*/
export function stubFetch(response: {
ok: boolean;
status: number;
statusText: string;
body: string;
}): FetchFunction {
return async () =>
({
ok: response.ok,
status: response.status,
statusText: response.statusText,
text: async () => response.body
}) as unknown as FetchResponse;
}
23 changes: 23 additions & 0 deletions .github/actions/slatify/__tests__/payload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"text": "This is test",
"attachments": [
{
"color": "danger",
"blocks": [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "Hello World"
},
{
"type": "mrkdwn",
"text": "YEAH!!!!!"
}
]
}
]
}
]
}
Loading