@briefkasten/adapter-nightwatch 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +28 -2
- package/dist/index.d.mts +151 -0
- package/dist/index.mjs +323 -0
- package/package.json +59 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lukas Mateffy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @briefkasten/adapter-nightwatch
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Laravel Nightwatch adapter for [briefkasten](https://www.npmjs.com/package/briefkasten):
|
|
4
|
+
Nightwatch's issue webhooks, verified with `Nightwatch-Signature` (HMAC-SHA256) and a five-minute
|
|
5
|
+
timestamp window, as typed error events.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install briefkasten @briefkasten/adapter-nightwatch
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { briefkasten } from "briefkasten";
|
|
13
|
+
import { nightwatch } from "@briefkasten/adapter-nightwatch";
|
|
14
|
+
|
|
15
|
+
const hooks = briefkasten({
|
|
16
|
+
sources: { nightwatch: nightwatch({ secret: process.env.NIGHTWATCH_WEBHOOK_SECRET ?? "" }) },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
hooks.on("nightwatch:error", async (event) => {
|
|
20
|
+
console.log(event.body.action, event.body.title);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Mount it: a fetch handler, or `hooks.node` for Express and node:http.
|
|
24
|
+
app.post("/webhooks/*", (c) => hooks.fetch(c.req.raw));
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Docs: <https://briefkasten.fabrik.computer/docs/adapters/nightwatch>
|
|
28
|
+
|
|
29
|
+
MIT licensed.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Adapter, AdapterBinding, AdapterInstance, AdapterSample, NewEvent, NoOptions } from "briefkasten";
|
|
3
|
+
//#region src/nightwatch-schemas.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Laravel Nightwatch's webhook delivery, as its documentation describes it
|
|
6
|
+
* (https://nightwatch.laravel.com/docs/webhooks.md):
|
|
7
|
+
*
|
|
8
|
+
* { event: "issue.opened", timestamp: "2026-02-18T12:00:00.000000Z",
|
|
9
|
+
* payload: { webhook_id, application_id, organization_id, issue: { … },
|
|
10
|
+
* environment?: { id, name }, actor?: { id, name, email, type } } }
|
|
11
|
+
*
|
|
12
|
+
* The four events are `issue.opened`, `issue.reopened` (with `environment`), `issue.ignored`
|
|
13
|
+
* and `issue.resolved` (with `actor` when a person acted). The fields this adapter publishes are
|
|
14
|
+
* required; everything else passes through, so a field Nightwatch adds later does not drop a
|
|
15
|
+
* delivery.
|
|
16
|
+
*/
|
|
17
|
+
/** The `issue.<action>` events Nightwatch sends. */
|
|
18
|
+
declare const ACTIONS: readonly ["opened", "reopened", "ignored", "resolved"];
|
|
19
|
+
type NightwatchAction = (typeof ACTIONS)[number];
|
|
20
|
+
/** The `payload` of an `issue.*` delivery. */
|
|
21
|
+
declare const issuePayloadSchema: z.ZodObject<{
|
|
22
|
+
webhook_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
23
|
+
application_id: z.ZodString;
|
|
24
|
+
organization_id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
25
|
+
issue: z.ZodObject<{
|
|
26
|
+
id: z.ZodString;
|
|
27
|
+
ref: z.ZodNumber;
|
|
28
|
+
type: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
29
|
+
title: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
30
|
+
status: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
31
|
+
priority: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
32
|
+
url: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
33
|
+
}, z.core.$loose>;
|
|
34
|
+
environment: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
35
|
+
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
36
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
37
|
+
}, z.core.$strip>>>;
|
|
38
|
+
actor: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
39
|
+
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
40
|
+
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
41
|
+
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
42
|
+
}, z.core.$strip>>>;
|
|
43
|
+
}, z.core.$loose>;
|
|
44
|
+
type NightwatchIssuePayload = z.infer<typeof issuePayloadSchema>;
|
|
45
|
+
/** The delivery's envelope: the event, when it was sent, and its payload. */
|
|
46
|
+
declare const deliverySchema: z.ZodObject<{
|
|
47
|
+
event: z.ZodString;
|
|
48
|
+
timestamp: z.ZodString;
|
|
49
|
+
payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
50
|
+
}, z.core.$strip>;
|
|
51
|
+
type NightwatchDelivery = z.infer<typeof deliverySchema>;
|
|
52
|
+
/**
|
|
53
|
+
* The body of the `error` event this adapter publishes: one Nightwatch issue changed. The target
|
|
54
|
+
* is `<application>.<ref>`.
|
|
55
|
+
*/
|
|
56
|
+
declare const errorEventSchema: z.ZodObject<{
|
|
57
|
+
action: z.ZodEnum<{
|
|
58
|
+
opened: "opened";
|
|
59
|
+
reopened: "reopened";
|
|
60
|
+
ignored: "ignored";
|
|
61
|
+
resolved: "resolved";
|
|
62
|
+
}>;
|
|
63
|
+
application: z.ZodString;
|
|
64
|
+
ref: z.ZodNumber;
|
|
65
|
+
issue: z.ZodString;
|
|
66
|
+
status: z.ZodOptional<z.ZodString>;
|
|
67
|
+
type: z.ZodOptional<z.ZodString>;
|
|
68
|
+
title: z.ZodOptional<z.ZodString>;
|
|
69
|
+
priority: z.ZodOptional<z.ZodString>;
|
|
70
|
+
url: z.ZodOptional<z.ZodString>;
|
|
71
|
+
environment: z.ZodOptional<z.ZodString>;
|
|
72
|
+
actor: z.ZodOptional<z.ZodString>;
|
|
73
|
+
at: z.ZodString;
|
|
74
|
+
}, z.core.$strip>;
|
|
75
|
+
type NightwatchErrorEvent = z.infer<typeof errorEventSchema>;
|
|
76
|
+
/** The events this adapter publishes. */
|
|
77
|
+
declare const events: {
|
|
78
|
+
error: z.ZodObject<{
|
|
79
|
+
action: z.ZodEnum<{
|
|
80
|
+
opened: "opened";
|
|
81
|
+
reopened: "reopened";
|
|
82
|
+
ignored: "ignored";
|
|
83
|
+
resolved: "resolved";
|
|
84
|
+
}>;
|
|
85
|
+
application: z.ZodString;
|
|
86
|
+
ref: z.ZodNumber;
|
|
87
|
+
issue: z.ZodString;
|
|
88
|
+
status: z.ZodOptional<z.ZodString>;
|
|
89
|
+
type: z.ZodOptional<z.ZodString>;
|
|
90
|
+
title: z.ZodOptional<z.ZodString>;
|
|
91
|
+
priority: z.ZodOptional<z.ZodString>;
|
|
92
|
+
url: z.ZodOptional<z.ZodString>;
|
|
93
|
+
environment: z.ZodOptional<z.ZodString>;
|
|
94
|
+
actor: z.ZodOptional<z.ZodString>;
|
|
95
|
+
at: z.ZodString;
|
|
96
|
+
}, z.core.$strip>;
|
|
97
|
+
};
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/nightwatch.d.ts
|
|
100
|
+
/** How far a delivery's `timestamp` may be from now, either way, for it to be acted on. */
|
|
101
|
+
declare const DELIVERY_WINDOW_MS: number;
|
|
102
|
+
/**
|
|
103
|
+
* Nightwatch needs no options: the signing secret from the application's Issues settings →
|
|
104
|
+
* Webhooks → Edit is the source's secret.
|
|
105
|
+
*/
|
|
106
|
+
declare const nightwatchOptionsSchema: z.ZodObject<{}, z.core.$strip>;
|
|
107
|
+
type NightwatchOptions = NoOptions;
|
|
108
|
+
/** Whether `sent` (ISO 8601) is within `windowMs` of `now`; a time that does not parse is not. */
|
|
109
|
+
declare function isFresh(sent: string, now: number, windowMs: number): boolean;
|
|
110
|
+
/** The action of a delivery's event (`issue.resolved` → `resolved`), or none for another event. */
|
|
111
|
+
declare function actionOf(event: string): NightwatchAction | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* The target of an issue: its application and its ref, the pair Nightwatch finds it by. A client
|
|
114
|
+
* may treat it as the issue's key: fabrik's Nightwatch tracker keys an error the same way
|
|
115
|
+
* (`issueKey` in `fabrik-plugin-nightwatch`), and its gateway plugin reads the target as that key.
|
|
116
|
+
*/
|
|
117
|
+
declare function targetOf(application: string, ref: number): string;
|
|
118
|
+
/** The `error` event of an issue delivery the schema accepted. */
|
|
119
|
+
declare function toErrorEvent(action: NightwatchAction, payload: NightwatchIssuePayload, sent: string): NewEvent;
|
|
120
|
+
/** One Nightwatch webhook, verified with its signing secret. */
|
|
121
|
+
declare function nightwatchInstance(secret: string, now?: () => number): AdapterInstance;
|
|
122
|
+
/** The `nightwatch` adapter's definition, which a JSON config names as `"adapter": "nightwatch"`. */
|
|
123
|
+
declare const adapter: Adapter<NightwatchOptions>;
|
|
124
|
+
/**
|
|
125
|
+
* The `nightwatch` adapter builder, for a source in a TypeScript config:
|
|
126
|
+
*
|
|
127
|
+
* sources: { nightwatch: nightwatch({ secret: env("NIGHTWATCH_WEBHOOK_SECRET") }) }
|
|
128
|
+
*/
|
|
129
|
+
declare const nightwatch: ((input: NightwatchOptions & {
|
|
130
|
+
readonly secret: string | undefined;
|
|
131
|
+
}) => AdapterBinding<"nightwatch", typeof events>) & {
|
|
132
|
+
events: {
|
|
133
|
+
readonly issue: {
|
|
134
|
+
readonly opened: import("briefkasten").DeliveryBuilder;
|
|
135
|
+
readonly resolved: import("briefkasten").DeliveryBuilder;
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
/** The Nightwatch adapter's event tables, for a client's typed handlers. */
|
|
140
|
+
type NightwatchEvents = typeof events;
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/samples.d.ts
|
|
143
|
+
declare const samples: readonly AdapterSample[];
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/signature.d.ts
|
|
146
|
+
/** HMAC-SHA256 of the raw body, lowercase hex, as Nightwatch computes it. */
|
|
147
|
+
declare function signatureOf(secret: string, body: Uint8Array): string;
|
|
148
|
+
/** Constant-time comparison, so a near-miss signature cannot be found byte by byte. */
|
|
149
|
+
declare function signaturesEqual(expected: string, provided: string): boolean;
|
|
150
|
+
//#endregion
|
|
151
|
+
export { ACTIONS, DELIVERY_WINDOW_MS, type NightwatchAction, type NightwatchDelivery, type NightwatchErrorEvent, type NightwatchEvents, type NightwatchIssuePayload, type NightwatchOptions, actionOf, adapter, deliverySchema, errorEventSchema, events, isFresh, issuePayloadSchema, nightwatch, nightwatchInstance, nightwatchOptionsSchema, samples, signatureOf, signaturesEqual, targetOf, toErrorEvent };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { builtSample, defineBinding } from "briefkasten";
|
|
3
|
+
import { createHmac, timingSafeEqual } from "node:crypto";
|
|
4
|
+
//#region src/nightwatch-schemas.ts
|
|
5
|
+
/**
|
|
6
|
+
* Laravel Nightwatch's webhook delivery, as its documentation describes it
|
|
7
|
+
* (https://nightwatch.laravel.com/docs/webhooks.md):
|
|
8
|
+
*
|
|
9
|
+
* { event: "issue.opened", timestamp: "2026-02-18T12:00:00.000000Z",
|
|
10
|
+
* payload: { webhook_id, application_id, organization_id, issue: { … },
|
|
11
|
+
* environment?: { id, name }, actor?: { id, name, email, type } } }
|
|
12
|
+
*
|
|
13
|
+
* The four events are `issue.opened`, `issue.reopened` (with `environment`), `issue.ignored`
|
|
14
|
+
* and `issue.resolved` (with `actor` when a person acted). The fields this adapter publishes are
|
|
15
|
+
* required; everything else passes through, so a field Nightwatch adds later does not drop a
|
|
16
|
+
* delivery.
|
|
17
|
+
*/
|
|
18
|
+
/** The `issue.<action>` events Nightwatch sends. */
|
|
19
|
+
const ACTIONS = [
|
|
20
|
+
"opened",
|
|
21
|
+
"reopened",
|
|
22
|
+
"ignored",
|
|
23
|
+
"resolved"
|
|
24
|
+
];
|
|
25
|
+
const issueSchema = z.object({
|
|
26
|
+
/** The issue's UUID. */
|
|
27
|
+
id: z.string().min(1),
|
|
28
|
+
/** The number people say: issue #42. */
|
|
29
|
+
ref: z.number().int().nonnegative(),
|
|
30
|
+
type: z.string().nullish(),
|
|
31
|
+
title: z.string().nullish(),
|
|
32
|
+
status: z.string().nullish(),
|
|
33
|
+
priority: z.string().nullish(),
|
|
34
|
+
url: z.string().nullish()
|
|
35
|
+
}).passthrough();
|
|
36
|
+
const environmentSchema = z.object({
|
|
37
|
+
id: z.string().nullish(),
|
|
38
|
+
name: z.string().nullish()
|
|
39
|
+
});
|
|
40
|
+
const actorSchema = z.object({
|
|
41
|
+
id: z.string().nullish(),
|
|
42
|
+
name: z.string().nullish(),
|
|
43
|
+
email: z.string().nullish()
|
|
44
|
+
});
|
|
45
|
+
/** The `payload` of an `issue.*` delivery. */
|
|
46
|
+
const issuePayloadSchema = z.object({
|
|
47
|
+
webhook_id: z.string().nullish(),
|
|
48
|
+
/** The application's UUID: with the ref, what finds the issue in Nightwatch. */
|
|
49
|
+
application_id: z.string().min(1),
|
|
50
|
+
organization_id: z.string().nullish(),
|
|
51
|
+
issue: issueSchema,
|
|
52
|
+
environment: environmentSchema.nullish(),
|
|
53
|
+
actor: actorSchema.nullish()
|
|
54
|
+
}).passthrough();
|
|
55
|
+
/** The delivery's envelope: the event, when it was sent, and its payload. */
|
|
56
|
+
const deliverySchema = z.object({
|
|
57
|
+
event: z.string().min(1),
|
|
58
|
+
timestamp: z.string().min(1),
|
|
59
|
+
payload: z.record(z.string(), z.unknown())
|
|
60
|
+
});
|
|
61
|
+
/**
|
|
62
|
+
* The body of the `error` event this adapter publishes: one Nightwatch issue changed. The target
|
|
63
|
+
* is `<application>.<ref>`.
|
|
64
|
+
*/
|
|
65
|
+
const errorEventSchema = z.object({
|
|
66
|
+
action: z.enum(ACTIONS),
|
|
67
|
+
/** The application's UUID. */
|
|
68
|
+
application: z.string(),
|
|
69
|
+
/** The issue's number in the application. */
|
|
70
|
+
ref: z.number().int(),
|
|
71
|
+
/** The issue's UUID. */
|
|
72
|
+
issue: z.string(),
|
|
73
|
+
status: z.string().optional(),
|
|
74
|
+
type: z.string().optional(),
|
|
75
|
+
title: z.string().optional(),
|
|
76
|
+
priority: z.string().optional(),
|
|
77
|
+
url: z.string().optional(),
|
|
78
|
+
environment: z.string().optional(),
|
|
79
|
+
actor: z.string().optional(),
|
|
80
|
+
/** When Nightwatch sent it (ISO 8601). */
|
|
81
|
+
at: z.string()
|
|
82
|
+
});
|
|
83
|
+
/** The events this adapter publishes. */
|
|
84
|
+
const events = { error: errorEventSchema };
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/signature.ts
|
|
87
|
+
/** HMAC-SHA256 of the raw body, lowercase hex, as Nightwatch computes it. */
|
|
88
|
+
function signatureOf(secret, body) {
|
|
89
|
+
return createHmac("sha256", secret).update(body).digest("hex");
|
|
90
|
+
}
|
|
91
|
+
/** Constant-time comparison, so a near-miss signature cannot be found byte by byte. */
|
|
92
|
+
function signaturesEqual(expected, provided) {
|
|
93
|
+
const left = Buffer.from(expected, "utf8");
|
|
94
|
+
const right = Buffer.from(provided, "utf8");
|
|
95
|
+
return left.length === right.length && timingSafeEqual(left, right);
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/samples.ts
|
|
99
|
+
/**
|
|
100
|
+
* Nightwatch's issue webhooks, typed: a builder per event makes the payload its documentation
|
|
101
|
+
* shows (invented ids and names) with the fields a caller gives (`title`, `number` the issue's
|
|
102
|
+
* ref), stamped with the time it is signed and signed the way Nightwatch signs
|
|
103
|
+
* (`Nightwatch-Signature`, HMAC-SHA256 of the raw body, lowercase hex), so it passes the
|
|
104
|
+
* adapter's verification and freshness check.
|
|
105
|
+
*/
|
|
106
|
+
const APPLICATION = "0b9a3c1e-1111-4222-8333-944455556666";
|
|
107
|
+
const ORGANIZATION = "5e6f7a8b-0000-4000-8000-000000000002";
|
|
108
|
+
const WEBHOOK = "5e6f7a8b-0000-4000-8000-000000000001";
|
|
109
|
+
const ISSUE = {
|
|
110
|
+
id: "7c1d2e3f-aaaa-4bbb-8ccc-0d1e2f3a4b5c",
|
|
111
|
+
ref: 42,
|
|
112
|
+
type: "exception",
|
|
113
|
+
title: "RuntimeException: The invoice could not be rendered",
|
|
114
|
+
priority: "high"
|
|
115
|
+
};
|
|
116
|
+
/** Signs a payload as Nightwatch does, stamping it with the time it is sent. */
|
|
117
|
+
const sign = (payload, context) => {
|
|
118
|
+
const body = JSON.stringify({
|
|
119
|
+
...payload,
|
|
120
|
+
timestamp: new Date(context.now).toISOString()
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
headers: {
|
|
124
|
+
"content-type": "application/json",
|
|
125
|
+
"user-agent": "Nightwatch",
|
|
126
|
+
"nightwatch-signature": signatureOf(context.secret, new TextEncoder().encode(body))
|
|
127
|
+
},
|
|
128
|
+
body
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
function issue(event, status, extra) {
|
|
132
|
+
return (fields = {}) => {
|
|
133
|
+
const ref = fields.number ?? ISSUE.ref;
|
|
134
|
+
return {
|
|
135
|
+
adapter: "nightwatch",
|
|
136
|
+
event: "error",
|
|
137
|
+
payload: {
|
|
138
|
+
event,
|
|
139
|
+
payload: {
|
|
140
|
+
webhook_id: WEBHOOK,
|
|
141
|
+
application_id: APPLICATION,
|
|
142
|
+
organization_id: ORGANIZATION,
|
|
143
|
+
issue: {
|
|
144
|
+
...ISSUE,
|
|
145
|
+
ref,
|
|
146
|
+
title: fields.title ?? ISSUE.title,
|
|
147
|
+
url: `https://nightwatch.laravel.com/eu/applications/${APPLICATION}/issues/${ref}`,
|
|
148
|
+
status
|
|
149
|
+
},
|
|
150
|
+
...extra(fields)
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
sign
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** Nightwatch's typed event builders: `nightwatch.events.issue.opened({ title })`. */
|
|
158
|
+
const deliveries = { issue: {
|
|
159
|
+
opened: issue("issue.opened", "open", () => ({ environment: {
|
|
160
|
+
id: "env-1",
|
|
161
|
+
name: "production"
|
|
162
|
+
} })),
|
|
163
|
+
resolved: issue("issue.resolved", "resolved", (fields) => ({ actor: {
|
|
164
|
+
id: "u-1",
|
|
165
|
+
name: fields.author ?? "Ada Example",
|
|
166
|
+
email: "ada@example.com",
|
|
167
|
+
type: "user"
|
|
168
|
+
} }))
|
|
169
|
+
} };
|
|
170
|
+
const samples = [builtSample("issue.opened", "A new issue was opened in production", deliveries.issue.opened), builtSample("issue.resolved", "Somebody resolved an issue", deliveries.issue.resolved)];
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/nightwatch.ts
|
|
173
|
+
/**
|
|
174
|
+
* The Laravel Nightwatch adapter: an issue opened, reopened, ignored or resolved becomes an
|
|
175
|
+
* `error` event, addressed `<application>.<ref>`.
|
|
176
|
+
*
|
|
177
|
+
* Verification is Nightwatch's own, as documented: HMAC-SHA256 of the **raw body** with the
|
|
178
|
+
* webhook's signing secret, in `Nightwatch-Signature`. The documentation's Laravel example
|
|
179
|
+
* compares the header with PHP's `hash_hmac('sha256', …)`, which is lowercase hex, so that is the
|
|
180
|
+
* one form accepted; anything else fails closed. The signature covers no timestamp of its own,
|
|
181
|
+
* but it covers the body, and the body carries `timestamp` (when Nightwatch sent it): a delivery
|
|
182
|
+
* older than `DELIVERY_WINDOW_MS` is refused, so a captured one cannot be replayed later.
|
|
183
|
+
*/
|
|
184
|
+
const SIGNATURE_HEADER = "nightwatch-signature";
|
|
185
|
+
const EVENT_PREFIX = "issue.";
|
|
186
|
+
const ACCEPTED = 202;
|
|
187
|
+
const BAD_REQUEST = 400;
|
|
188
|
+
const UNAUTHORIZED = 401;
|
|
189
|
+
/** How far a delivery's `timestamp` may be from now, either way, for it to be acted on. */
|
|
190
|
+
const DELIVERY_WINDOW_MS = 3e5;
|
|
191
|
+
/**
|
|
192
|
+
* Nightwatch needs no options: the signing secret from the application's Issues settings →
|
|
193
|
+
* Webhooks → Edit is the source's secret.
|
|
194
|
+
*/
|
|
195
|
+
const nightwatchOptionsSchema = z.object({});
|
|
196
|
+
/** Whether `sent` (ISO 8601) is within `windowMs` of `now`; a time that does not parse is not. */
|
|
197
|
+
function isFresh(sent, now, windowMs) {
|
|
198
|
+
const at = Date.parse(sent);
|
|
199
|
+
return !Number.isNaN(at) && Math.abs(now - at) <= windowMs;
|
|
200
|
+
}
|
|
201
|
+
/** The action of a delivery's event (`issue.resolved` → `resolved`), or none for another event. */
|
|
202
|
+
function actionOf(event) {
|
|
203
|
+
const action = event.startsWith(EVENT_PREFIX) ? event.slice(6) : "";
|
|
204
|
+
return ACTIONS.find((each) => each === action);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The target of an issue: its application and its ref, the pair Nightwatch finds it by. A client
|
|
208
|
+
* may treat it as the issue's key: fabrik's Nightwatch tracker keys an error the same way
|
|
209
|
+
* (`issueKey` in `fabrik-plugin-nightwatch`), and its gateway plugin reads the target as that key.
|
|
210
|
+
*/
|
|
211
|
+
function targetOf(application, ref) {
|
|
212
|
+
return `${application}.${ref}`;
|
|
213
|
+
}
|
|
214
|
+
/** The fields present, as the event body carries them: absent and null are left out. */
|
|
215
|
+
function present(fields) {
|
|
216
|
+
const out = {};
|
|
217
|
+
for (const [name, value] of Object.entries(fields)) if (value !== null && value !== void 0) out[name] = value;
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
/** The `error` event of an issue delivery the schema accepted. */
|
|
221
|
+
function toErrorEvent(action, payload, sent) {
|
|
222
|
+
const { issue } = payload;
|
|
223
|
+
return {
|
|
224
|
+
event: "error",
|
|
225
|
+
target: targetOf(payload.application_id, issue.ref),
|
|
226
|
+
body: {
|
|
227
|
+
action,
|
|
228
|
+
application: payload.application_id,
|
|
229
|
+
ref: issue.ref,
|
|
230
|
+
issue: issue.id,
|
|
231
|
+
...present({
|
|
232
|
+
status: issue.status,
|
|
233
|
+
type: issue.type,
|
|
234
|
+
title: issue.title,
|
|
235
|
+
priority: issue.priority,
|
|
236
|
+
url: issue.url,
|
|
237
|
+
environment: payload.environment?.name,
|
|
238
|
+
actor: payload.actor?.email ?? payload.actor?.name
|
|
239
|
+
}),
|
|
240
|
+
at: sent
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function json(body, status) {
|
|
245
|
+
return new Response(JSON.stringify(body), {
|
|
246
|
+
status,
|
|
247
|
+
headers: { "content-type": "application/json" }
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
function refused(status, body) {
|
|
251
|
+
return {
|
|
252
|
+
response: json(body, status),
|
|
253
|
+
events: []
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** The fields a rejected payload got wrong, path by path: no values, so no issue text. */
|
|
257
|
+
function issuesOf(error) {
|
|
258
|
+
return error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`);
|
|
259
|
+
}
|
|
260
|
+
/** The delivery's envelope, or why it is not one. */
|
|
261
|
+
function envelopeOf(raw) {
|
|
262
|
+
let decoded;
|
|
263
|
+
try {
|
|
264
|
+
decoded = JSON.parse(new TextDecoder().decode(raw));
|
|
265
|
+
} catch {
|
|
266
|
+
return "payload is not JSON";
|
|
267
|
+
}
|
|
268
|
+
const parsed = deliverySchema.safeParse(decoded);
|
|
269
|
+
return parsed.success ? parsed.data : "payload is not a Nightwatch delivery";
|
|
270
|
+
}
|
|
271
|
+
/** One Nightwatch webhook, verified with its signing secret. */
|
|
272
|
+
function nightwatchInstance(secret, now = () => Date.now()) {
|
|
273
|
+
return { async handleWebhook(request) {
|
|
274
|
+
const raw = new Uint8Array(await request.arrayBuffer());
|
|
275
|
+
const signature = request.headers.get(SIGNATURE_HEADER);
|
|
276
|
+
if (signature === null || !signaturesEqual(signatureOf(secret, raw), signature)) return refused(UNAUTHORIZED, { error: "invalid signature" });
|
|
277
|
+
const delivery = envelopeOf(raw);
|
|
278
|
+
if (typeof delivery === "string") return refused(BAD_REQUEST, { error: delivery });
|
|
279
|
+
if (!isFresh(delivery.timestamp, now(), 3e5)) return refused(UNAUTHORIZED, { error: "stale delivery" });
|
|
280
|
+
const action = actionOf(delivery.event);
|
|
281
|
+
if (action === void 0) return {
|
|
282
|
+
response: json({ ignored: delivery.event }, ACCEPTED),
|
|
283
|
+
events: []
|
|
284
|
+
};
|
|
285
|
+
const payload = issuePayloadSchema.safeParse(delivery.payload);
|
|
286
|
+
if (!payload.success) return refused(BAD_REQUEST, {
|
|
287
|
+
error: "unreadable issue",
|
|
288
|
+
at: issuesOf(payload.error),
|
|
289
|
+
keys: Object.keys(delivery.payload)
|
|
290
|
+
});
|
|
291
|
+
return {
|
|
292
|
+
response: json({ ok: true }, ACCEPTED),
|
|
293
|
+
events: [toErrorEvent(action, payload.data, delivery.timestamp)]
|
|
294
|
+
};
|
|
295
|
+
} };
|
|
296
|
+
}
|
|
297
|
+
/** The `nightwatch` adapter's definition, which a JSON config names as `"adapter": "nightwatch"`. */
|
|
298
|
+
const adapter = {
|
|
299
|
+
id: "nightwatch",
|
|
300
|
+
name: "Nightwatch",
|
|
301
|
+
samples,
|
|
302
|
+
optionsSchema: nightwatchOptionsSchema,
|
|
303
|
+
events,
|
|
304
|
+
targetDescription: "a Nightwatch issue: <application id>.<ref>",
|
|
305
|
+
create: (_options, context) => nightwatchInstance(context.secret)
|
|
306
|
+
};
|
|
307
|
+
/**
|
|
308
|
+
* The `nightwatch` adapter builder, for a source in a TypeScript config:
|
|
309
|
+
*
|
|
310
|
+
* sources: { nightwatch: nightwatch({ secret: env("NIGHTWATCH_WEBHOOK_SECRET") }) }
|
|
311
|
+
*/
|
|
312
|
+
const nightwatch = Object.assign((input) => {
|
|
313
|
+
const { secret, ...options } = input;
|
|
314
|
+
return defineBinding({
|
|
315
|
+
id: "nightwatch",
|
|
316
|
+
events,
|
|
317
|
+
options,
|
|
318
|
+
secret,
|
|
319
|
+
definition: adapter
|
|
320
|
+
});
|
|
321
|
+
}, { events: deliveries });
|
|
322
|
+
//#endregion
|
|
323
|
+
export { ACTIONS, DELIVERY_WINDOW_MS, actionOf, adapter, deliverySchema, errorEventSchema, events, isFresh, issuePayloadSchema, nightwatch, nightwatchInstance, nightwatchOptionsSchema, samples, signatureOf, signaturesEqual, targetOf, toErrorEvent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@briefkasten/adapter-nightwatch",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Laravel Nightwatch adapter for briefkasten: HMAC-verified issue webhooks as typed error events",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"adapter",
|
|
7
|
+
"briefkasten",
|
|
8
|
+
"errors",
|
|
9
|
+
"laravel",
|
|
10
|
+
"nightwatch",
|
|
11
|
+
"typescript",
|
|
12
|
+
"webhooks"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://briefkasten.fabrik.computer/docs",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/mateffy/fabrik/issues"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Lukas Mateffy",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/mateffy/fabrik.git",
|
|
23
|
+
"directory": "packages/briefkasten-adapter-nightwatch"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"type": "module",
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"main": "./dist/index.mjs",
|
|
33
|
+
"types": "./dist/index.d.mts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/index.d.mts",
|
|
37
|
+
"import": "./dist/index.mjs",
|
|
38
|
+
"default": "./dist/index.mjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"zod": "^4.4.3",
|
|
46
|
+
"briefkasten": "0.1.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "^22.0.0",
|
|
50
|
+
"tsdown": "^0.22.3",
|
|
51
|
+
"typescript": "^5.8.0",
|
|
52
|
+
"vitest": "^4.0.0"
|
|
53
|
+
},
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=22"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"typecheck": "tsc --noEmit",
|
|
60
|
+
"test": "vitest run"
|
|
61
|
+
}
|
|
6
62
|
}
|