@fiducian/alerts 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +25 -0
- package/dist/index.js +44 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +26 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Violation } from "@fiducian/conductor";
|
|
2
|
+
export interface AlertPayload {
|
|
3
|
+
source: "fiducia-trace";
|
|
4
|
+
passed: boolean;
|
|
5
|
+
violationCount: number;
|
|
6
|
+
violations: Violation[];
|
|
7
|
+
specPath?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AlertOptions {
|
|
10
|
+
webhookUrl?: string;
|
|
11
|
+
slackWebhookUrl?: string;
|
|
12
|
+
specPath?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function notifyTraceResult(result: {
|
|
15
|
+
passed: boolean;
|
|
16
|
+
violations: Violation[];
|
|
17
|
+
}, options: AlertOptions): Promise<{
|
|
18
|
+
webhook?: boolean;
|
|
19
|
+
slack?: boolean;
|
|
20
|
+
}>;
|
|
21
|
+
export declare function formatSlackMessage(payload: AlertPayload): string;
|
|
22
|
+
export declare function notifyViolations(violations: Violation[], options: AlertOptions): Promise<{
|
|
23
|
+
webhook?: boolean;
|
|
24
|
+
slack?: boolean;
|
|
25
|
+
}>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export async function notifyTraceResult(result, options) {
|
|
2
|
+
const payload = {
|
|
3
|
+
source: "fiducia-trace",
|
|
4
|
+
passed: result.passed,
|
|
5
|
+
violationCount: result.violations.length,
|
|
6
|
+
violations: result.violations,
|
|
7
|
+
specPath: options.specPath,
|
|
8
|
+
};
|
|
9
|
+
const sent = {};
|
|
10
|
+
if (options.webhookUrl) {
|
|
11
|
+
const res = await fetch(options.webhookUrl, {
|
|
12
|
+
method: "POST",
|
|
13
|
+
headers: { "content-type": "application/json" },
|
|
14
|
+
body: JSON.stringify(payload),
|
|
15
|
+
});
|
|
16
|
+
sent.webhook = res.ok;
|
|
17
|
+
}
|
|
18
|
+
if (options.slackWebhookUrl) {
|
|
19
|
+
const text = formatSlackMessage(payload);
|
|
20
|
+
const res = await fetch(options.slackWebhookUrl, {
|
|
21
|
+
method: "POST",
|
|
22
|
+
headers: { "content-type": "application/json" },
|
|
23
|
+
body: JSON.stringify({ text }),
|
|
24
|
+
});
|
|
25
|
+
sent.slack = res.ok;
|
|
26
|
+
}
|
|
27
|
+
return sent;
|
|
28
|
+
}
|
|
29
|
+
export function formatSlackMessage(payload) {
|
|
30
|
+
if (payload.passed) {
|
|
31
|
+
return `:white_check_mark: *Fiducia Trace* passed${payload.specPath ? ` (\`${payload.specPath}\`)` : ""}`;
|
|
32
|
+
}
|
|
33
|
+
const lines = payload.violations.slice(0, 5).map((v) => `• \`${v.contextId}\` — ${v.message}`);
|
|
34
|
+
return [
|
|
35
|
+
`:rotating_light: *Fiducia Trace* — ${payload.violationCount} violation(s)`,
|
|
36
|
+
payload.specPath ? `Spec: \`${payload.specPath}\`` : "",
|
|
37
|
+
...lines,
|
|
38
|
+
]
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join("\n");
|
|
41
|
+
}
|
|
42
|
+
export async function notifyViolations(violations, options) {
|
|
43
|
+
return notifyTraceResult({ passed: violations.length === 0, violations }, options);
|
|
44
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { formatSlackMessage } from "./index.js";
|
|
4
|
+
test("formatSlackMessage passes", () => {
|
|
5
|
+
const msg = formatSlackMessage({
|
|
6
|
+
source: "fiducia-trace",
|
|
7
|
+
passed: true,
|
|
8
|
+
violationCount: 0,
|
|
9
|
+
violations: [],
|
|
10
|
+
specPath: "pizza.waltz",
|
|
11
|
+
});
|
|
12
|
+
assert.match(msg, /passed/);
|
|
13
|
+
});
|
|
14
|
+
test("formatSlackMessage violations", () => {
|
|
15
|
+
const violations = [
|
|
16
|
+
{ contextId: "ctx-1", message: "Out-of-order step" },
|
|
17
|
+
];
|
|
18
|
+
const msg = formatSlackMessage({
|
|
19
|
+
source: "fiducia-trace",
|
|
20
|
+
passed: false,
|
|
21
|
+
violationCount: 1,
|
|
22
|
+
violations,
|
|
23
|
+
});
|
|
24
|
+
assert.match(msg, /violation/);
|
|
25
|
+
assert.match(msg, /ctx-1/);
|
|
26
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fiducian/alerts",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Webhook and Slack notifications for Fiducian Trace violations",
|
|
5
|
+
"license": "AGPL-3.0-or-later",
|
|
6
|
+
"keywords": ["fiducia", "fiducia", "slack", "webhook", "alerts"],
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/thesnmc/Fiducian.git",
|
|
10
|
+
"directory": "trace/packages/alerts"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": { "access": "public" },
|
|
13
|
+
"files": ["dist"],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@fiducian/conductor": "0.4.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -p tsconfig.json",
|
|
28
|
+
"test": "node --test dist/**/*.test.js",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
}
|
|
31
|
+
}
|