@notation/factory-github 0.0.0-stage → 0.1.1
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/implementations/channel.ts +69 -0
- package/implementations/code-host.ts +40 -0
- package/implementations/connection-ref.ts +26 -0
- package/implementations/connection.ts +88 -0
- package/implementations/index.ts +16 -0
- package/implementations/operator-auth.ts +33 -0
- package/implementations/outpost.ts +21 -0
- package/implementations/policy.ts +41 -0
- package/implementations/selectors.ts +66 -0
- package/lib/app-name.ts +11 -0
- package/lib/app.ts +222 -0
- package/lib/permissions.ts +36 -0
- package/package.json +36 -3
- package/resources/app.ts +119 -0
- package/router/app-jwt.test.ts +26 -0
- package/router/app-jwt.ts +67 -0
- package/router/index.test.ts +198 -0
- package/router/index.ts +316 -0
- package/router/repos.test.ts +44 -0
- package/router/repos.ts +202 -0
- package/README.md +0 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DeepPartial } from "@notation/resource";
|
|
2
|
+
|
|
3
|
+
export type Permission = "read" | "write";
|
|
4
|
+
export type Permissions = Partial<
|
|
5
|
+
Record<
|
|
6
|
+
"actions_variables" | "checks" | "contents" | "issues" | "metadata" | "pull_requests",
|
|
7
|
+
Permission
|
|
8
|
+
>
|
|
9
|
+
>;
|
|
10
|
+
export type GithubEvent =
|
|
11
|
+
| "issues"
|
|
12
|
+
| "issue_comment"
|
|
13
|
+
| "push"
|
|
14
|
+
| "pull_request"
|
|
15
|
+
| "pull_request_review_comment";
|
|
16
|
+
|
|
17
|
+
/** Combines event lists: each event once. */
|
|
18
|
+
export const union = (items: (GithubEvent[] | undefined)[]): GithubEvent[] => [
|
|
19
|
+
...new Set(items.flatMap((list) => list ?? [])),
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
/** Combines permission sets: for each permission, write wins over read. */
|
|
23
|
+
export const highestPermissions = (
|
|
24
|
+
items: DeepPartial<Permissions | undefined>[],
|
|
25
|
+
): Permissions => {
|
|
26
|
+
const result: Permissions = {};
|
|
27
|
+
for (const item of items) {
|
|
28
|
+
for (const [name, level] of Object.entries(item ?? {}) as [
|
|
29
|
+
keyof Permissions,
|
|
30
|
+
Permission,
|
|
31
|
+
][]) {
|
|
32
|
+
if (result[name] !== "write") result[name] = level;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@notation/factory-github",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"author": "Daniel Grant",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/notationlabs/factory.git",
|
|
9
|
+
"directory": "packages/github"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"factory-outpost": "./implementations/outpost.ts",
|
|
15
|
+
"default": "./implementations/index.ts"
|
|
16
|
+
},
|
|
17
|
+
"./app-name": "./lib/app-name.ts",
|
|
18
|
+
"./router": "./router/index.ts"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"implementations",
|
|
22
|
+
"lib",
|
|
23
|
+
"resources",
|
|
24
|
+
"router"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@notation/resource": "0.28.0",
|
|
31
|
+
"valibot": "^1.1.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@notation/factory": "^0.1.1"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@notation/factory": "0.1.1"
|
|
38
|
+
}
|
|
6
39
|
}
|
package/resources/app.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { defineResource } from "@notation/resource";
|
|
2
|
+
import * as v from "valibot";
|
|
3
|
+
import {
|
|
4
|
+
createApp,
|
|
5
|
+
readApp,
|
|
6
|
+
requestAppChange,
|
|
7
|
+
requestAppDeletion,
|
|
8
|
+
} from "../lib/app.ts";
|
|
9
|
+
import {
|
|
10
|
+
type GithubEvent,
|
|
11
|
+
highestPermissions,
|
|
12
|
+
type Permissions,
|
|
13
|
+
union,
|
|
14
|
+
} from "../lib/permissions.ts";
|
|
15
|
+
|
|
16
|
+
/** One GitHub App, which backs a connection. Its channels' rules contribute the events and permissions they need. */
|
|
17
|
+
type AppApi = {
|
|
18
|
+
Key: { slug: string };
|
|
19
|
+
CreateParams: {
|
|
20
|
+
name: string;
|
|
21
|
+
org?: string;
|
|
22
|
+
hookUrl: string;
|
|
23
|
+
events?: GithubEvent[];
|
|
24
|
+
permissions?: Permissions;
|
|
25
|
+
};
|
|
26
|
+
UpdateParams: {
|
|
27
|
+
hookUrl: string;
|
|
28
|
+
events?: GithubEvent[];
|
|
29
|
+
permissions?: Permissions;
|
|
30
|
+
};
|
|
31
|
+
ReadResult: { slug: string; id: string };
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const githubEvent = v.picklist([
|
|
35
|
+
"issues",
|
|
36
|
+
"issue_comment",
|
|
37
|
+
"push",
|
|
38
|
+
"pull_request",
|
|
39
|
+
"pull_request_review_comment",
|
|
40
|
+
] satisfies GithubEvent[]);
|
|
41
|
+
|
|
42
|
+
const permission = v.picklist(["read", "write"]);
|
|
43
|
+
|
|
44
|
+
const permissions = v.partial(
|
|
45
|
+
v.object({
|
|
46
|
+
actions_variables: permission,
|
|
47
|
+
checks: permission,
|
|
48
|
+
contents: permission,
|
|
49
|
+
issues: permission,
|
|
50
|
+
metadata: permission,
|
|
51
|
+
pull_requests: permission,
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
export const App = defineResource<AppApi>({
|
|
56
|
+
type: "github/apps/App",
|
|
57
|
+
provider: "@notation/factory-github",
|
|
58
|
+
})
|
|
59
|
+
.defineSchema({
|
|
60
|
+
name: {
|
|
61
|
+
propertyType: "param",
|
|
62
|
+
presence: "required",
|
|
63
|
+
immutable: true,
|
|
64
|
+
value: v.string(),
|
|
65
|
+
},
|
|
66
|
+
org: {
|
|
67
|
+
propertyType: "param",
|
|
68
|
+
presence: "optional",
|
|
69
|
+
immutable: true,
|
|
70
|
+
value: v.string(),
|
|
71
|
+
},
|
|
72
|
+
hookUrl: { propertyType: "param", presence: "required", value: v.string() },
|
|
73
|
+
events: {
|
|
74
|
+
propertyType: "param",
|
|
75
|
+
presence: "optional",
|
|
76
|
+
value: v.array(githubEvent),
|
|
77
|
+
collect: union,
|
|
78
|
+
},
|
|
79
|
+
permissions: {
|
|
80
|
+
propertyType: "param",
|
|
81
|
+
presence: "optional",
|
|
82
|
+
value: permissions,
|
|
83
|
+
collect: highestPermissions,
|
|
84
|
+
},
|
|
85
|
+
slug: {
|
|
86
|
+
propertyType: "computed",
|
|
87
|
+
presence: "required",
|
|
88
|
+
primaryKey: true,
|
|
89
|
+
value: v.string(),
|
|
90
|
+
},
|
|
91
|
+
id: { propertyType: "computed", presence: "required", value: v.string() },
|
|
92
|
+
/**
|
|
93
|
+
* The App's private key, which GitHub returns once, when the manifest
|
|
94
|
+
* flow creates the App. GitHub has no API that makes another.
|
|
95
|
+
*/
|
|
96
|
+
privateKey: { propertyType: "credential", value: v.string() },
|
|
97
|
+
} as const)
|
|
98
|
+
.defineOperations({
|
|
99
|
+
create: async (params) => createApp(params),
|
|
100
|
+
rotateManually: {
|
|
101
|
+
privateKey:
|
|
102
|
+
"Generate a private key on the App's settings page, under Private keys.",
|
|
103
|
+
},
|
|
104
|
+
read: async (key) => readApp(key),
|
|
105
|
+
update: async (key, patch, params) => {
|
|
106
|
+
if (patch.events !== undefined || patch.permissions !== undefined) {
|
|
107
|
+
await requestAppChange({ ...key, org: params.org }, {
|
|
108
|
+
events: params.events,
|
|
109
|
+
permissions: params.permissions,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// A new hookUrl reaches GitHub through the router's sync, which signs
|
|
113
|
+
// in as the App with its private key.
|
|
114
|
+
},
|
|
115
|
+
// GitHub has no API to delete an App.
|
|
116
|
+
delete: async (key) => requestAppDeletion(key),
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
export type AppInstance = InstanceType<typeof App>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createVerify, generateKeyPairSync } from "node:crypto";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import { appJwt } from "./app-jwt.ts";
|
|
5
|
+
|
|
6
|
+
const decode = (part: string) =>
|
|
7
|
+
JSON.parse(Buffer.from(part, "base64url").toString());
|
|
8
|
+
|
|
9
|
+
for (const type of ["pkcs1", "pkcs8"] as const) {
|
|
10
|
+
test(`appJwt signs with a ${type} key, and the claims name the App`, async () => {
|
|
11
|
+
const { privateKey, publicKey } = generateKeyPairSync("rsa", {
|
|
12
|
+
modulusLength: 2048,
|
|
13
|
+
privateKeyEncoding: { type, format: "pem" },
|
|
14
|
+
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
15
|
+
});
|
|
16
|
+
const jwt = await appJwt("12345", privateKey);
|
|
17
|
+
const [header, claims, signature] = jwt.split(".");
|
|
18
|
+
assert.deepEqual(decode(header!), { alg: "RS256", typ: "JWT" });
|
|
19
|
+
const { iss, iat, exp } = decode(claims!);
|
|
20
|
+
assert.equal(iss, "12345");
|
|
21
|
+
assert.equal(exp - iat, 600);
|
|
22
|
+
const verify = createVerify("RSA-SHA256");
|
|
23
|
+
verify.update(`${header}.${claims}`);
|
|
24
|
+
assert.equal(verify.verify(publicKey, Buffer.from(signature!, "base64url")), true);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
|
|
3
|
+
const base64url = (bytes: Uint8Array) =>
|
|
4
|
+
btoa(String.fromCharCode(...bytes))
|
|
5
|
+
.replace(/\+/g, "-")
|
|
6
|
+
.replace(/\//g, "_")
|
|
7
|
+
.replace(/=+$/, "");
|
|
8
|
+
|
|
9
|
+
const derLength = (n: number) =>
|
|
10
|
+
n < 0x80
|
|
11
|
+
? [n]
|
|
12
|
+
: n < 0x100
|
|
13
|
+
? [0x81, n]
|
|
14
|
+
: n < 0x10000
|
|
15
|
+
? [0x82, n >> 8, n & 0xff]
|
|
16
|
+
: [0x83, n >> 16, (n >> 8) & 0xff, n & 0xff];
|
|
17
|
+
|
|
18
|
+
const der = (tag: number, body: Uint8Array) =>
|
|
19
|
+
new Uint8Array([tag, ...derLength(body.length), ...body]);
|
|
20
|
+
|
|
21
|
+
// AlgorithmIdentifier for rsaEncryption (1.2.840.113549.1.1.1) with NULL
|
|
22
|
+
// parameters, and the PKCS#8 version 0.
|
|
23
|
+
const RSA_ALGORITHM = [
|
|
24
|
+
0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
|
|
25
|
+
0x05, 0x00,
|
|
26
|
+
];
|
|
27
|
+
const VERSION_0 = [0x02, 0x01, 0x00];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The PKCS#8 DER of a PEM private key. GitHub issues App keys as PKCS#1
|
|
31
|
+
* (`BEGIN RSA PRIVATE KEY`), which Web Crypto can't import, so a PKCS#1 key
|
|
32
|
+
* is wrapped in a PKCS#8 structure.
|
|
33
|
+
*/
|
|
34
|
+
const pkcs8 = (pem: string): Uint8Array<ArrayBuffer> => {
|
|
35
|
+
const body = pem.replace(/-----[^-]+-----/g, "").replace(/\s+/g, "");
|
|
36
|
+
const bytes = Uint8Array.from(atob(body), (c) => c.charCodeAt(0));
|
|
37
|
+
if (!pem.includes("BEGIN RSA PRIVATE KEY")) return bytes;
|
|
38
|
+
const inner = new Uint8Array([
|
|
39
|
+
...VERSION_0,
|
|
40
|
+
...RSA_ALGORITHM,
|
|
41
|
+
...der(0x04, bytes),
|
|
42
|
+
]);
|
|
43
|
+
return der(0x30, inner);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** A JWT that authenticates as the App with id `appId`. It is valid for 9 minutes, from a minute ago to allow for clock drift. */
|
|
47
|
+
export const appJwt = async (appId: string, privateKeyPem: string) => {
|
|
48
|
+
const key = await crypto.subtle.importKey(
|
|
49
|
+
"pkcs8",
|
|
50
|
+
pkcs8(privateKeyPem),
|
|
51
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
52
|
+
false,
|
|
53
|
+
["sign"],
|
|
54
|
+
);
|
|
55
|
+
const now = Math.floor(Date.now() / 1000);
|
|
56
|
+
const header = base64url(encoder.encode(JSON.stringify({ alg: "RS256", typ: "JWT" })));
|
|
57
|
+
const claims = base64url(
|
|
58
|
+
encoder.encode(JSON.stringify({ iat: now - 60, exp: now + 540, iss: appId })),
|
|
59
|
+
);
|
|
60
|
+
const input = `${header}.${claims}`;
|
|
61
|
+
const signature = await crypto.subtle.sign(
|
|
62
|
+
"RSASSA-PKCS1-v1_5",
|
|
63
|
+
key,
|
|
64
|
+
encoder.encode(input),
|
|
65
|
+
);
|
|
66
|
+
return `${input}.${base64url(new Uint8Array(signature))}`;
|
|
67
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createHmac } from "node:crypto";
|
|
3
|
+
import { test } from "node:test";
|
|
4
|
+
import type { Delivery } from "@notation/factory/router";
|
|
5
|
+
import { channel as github, type GithubEvent, mentions, verifySignature, webhookSecret } from "./index.ts";
|
|
6
|
+
|
|
7
|
+
const encode = (s: string) => new TextEncoder().encode(s);
|
|
8
|
+
|
|
9
|
+
// The example in GitHub's "Validating webhook deliveries" docs.
|
|
10
|
+
test("verifySignature accepts GitHub's example and rejects a changed body", async () => {
|
|
11
|
+
const secret = "It's a Secret to Everybody";
|
|
12
|
+
const header =
|
|
13
|
+
"sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17";
|
|
14
|
+
assert.equal(await verifySignature(secret, encode("Hello, World!"), header), true);
|
|
15
|
+
assert.equal(await verifySignature(secret, encode("Hello, World?"), header), false);
|
|
16
|
+
assert.equal(await verifySignature(secret, encode("Hello, World!"), null), false);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const ROOT_KEY = "test-root-key";
|
|
20
|
+
|
|
21
|
+
const channel = github({
|
|
22
|
+
secret: async (name) => (name === "ROOT_KEY" ? ROOT_KEY : ""),
|
|
23
|
+
value: (name) => (name === "APP_ID" ? "123" : ""),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const request = async (event: string, payload: object, { appId = "123", sign = true } = {}) => {
|
|
27
|
+
const body = JSON.stringify(payload);
|
|
28
|
+
const secret = sign ? await webhookSecret(ROOT_KEY, appId) : "wrong";
|
|
29
|
+
return new Request("https://router.test/webhook/github/acme-agents", {
|
|
30
|
+
method: "POST",
|
|
31
|
+
headers: {
|
|
32
|
+
"x-github-event": event,
|
|
33
|
+
"x-github-delivery": "d1",
|
|
34
|
+
"x-github-hook-installation-target-id": appId,
|
|
35
|
+
"x-hub-signature-256": `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`,
|
|
36
|
+
},
|
|
37
|
+
body,
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const commentPayload = {
|
|
42
|
+
action: "created",
|
|
43
|
+
repository: { full_name: "acme/api", default_branch: "main" },
|
|
44
|
+
issue: { number: 7, title: "Broken", body: "It breaks", user: { login: "bob" }, labels: [{ name: "bug" }] },
|
|
45
|
+
comment: { id: 99, body: "@claude fix", user: { login: "alice" }, author_association: "NONE" },
|
|
46
|
+
sender: { login: "alice" },
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
test("receive parses a signed comment to its App", async () => {
|
|
50
|
+
const received = await channel.receive(await request("issue_comment", commentPayload));
|
|
51
|
+
assert.deepEqual(received, {
|
|
52
|
+
status: "accepted",
|
|
53
|
+
delivery: {
|
|
54
|
+
id: "d1",
|
|
55
|
+
scope: "acme/api",
|
|
56
|
+
repo: "acme/api",
|
|
57
|
+
event: "issue_comment",
|
|
58
|
+
data: {
|
|
59
|
+
action: "created",
|
|
60
|
+
author: { login: "alice", association: "NONE", bot: false },
|
|
61
|
+
opener: { login: "bob", association: "", bot: false },
|
|
62
|
+
body: "@claude fix",
|
|
63
|
+
before: "@claude fix",
|
|
64
|
+
labels: ["bug"],
|
|
65
|
+
},
|
|
66
|
+
payload: {
|
|
67
|
+
action: "created",
|
|
68
|
+
issue: { number: 7, title: "Broken", body: "It breaks", labels: [{ name: "bug" }], user: { login: "bob" } },
|
|
69
|
+
comment: { id: 99, body: "@claude fix", user: { login: "alice" } },
|
|
70
|
+
repository: { full_name: "acme/api", default_branch: "main" },
|
|
71
|
+
sender: { login: "alice" },
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("receive ignores a comment that the App posted", async () => {
|
|
78
|
+
const own = { ...commentPayload, comment: { ...commentPayload.comment, performed_via_github_app: { id: 123 } } };
|
|
79
|
+
const received = await channel.receive(await request("issue_comment", own));
|
|
80
|
+
assert.deepEqual(received, { status: "ignored", reason: "posted by this App" });
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("a comment's conversation is its issue, and its prompt quotes the issue and the comment", async () => {
|
|
84
|
+
const received = await channel.receive(await request("issue_comment", commentPayload));
|
|
85
|
+
assert.equal(received.status, "accepted");
|
|
86
|
+
const conversation = channel.conversation!((received as { delivery: Delivery<GithubEvent> }).delivery);
|
|
87
|
+
assert.equal(conversation.id, "github:acme/api#7");
|
|
88
|
+
assert.deepEqual(conversation.thread, { repo: "acme/api", issue: 7 });
|
|
89
|
+
assert.match(conversation.prompt, /acme\/api, on issue #7: Broken/);
|
|
90
|
+
assert.match(conversation.prompt, /@bob opened it with:\nIt breaks/);
|
|
91
|
+
assert.match(conversation.prompt, /@alice commented:\n@claude fix/);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("receive rejects a bad signature and another App's delivery, and ignores a ping", async () => {
|
|
95
|
+
const bad = await channel.receive(await request("issue_comment", commentPayload, { sign: false }));
|
|
96
|
+
assert.equal(bad.status, "rejected");
|
|
97
|
+
const unknown = await channel.receive(await request("issue_comment", commentPayload, { appId: "9" }));
|
|
98
|
+
assert.equal(unknown.status, "rejected");
|
|
99
|
+
const ping = await channel.receive(await request("ping", { zen: "hi" }));
|
|
100
|
+
assert.equal(ping.status, "ignored");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const delivery = (data: Partial<GithubEvent>): Delivery<GithubEvent> => ({
|
|
104
|
+
id: "d1",
|
|
105
|
+
scope: "acme/api",
|
|
106
|
+
repo: "acme/api",
|
|
107
|
+
event: "issue_comment",
|
|
108
|
+
data: {
|
|
109
|
+
action: "created",
|
|
110
|
+
author: { login: "alice", association: "NONE", bot: false },
|
|
111
|
+
body: "@claude fix",
|
|
112
|
+
before: "@claude fix",
|
|
113
|
+
labels: [],
|
|
114
|
+
...data,
|
|
115
|
+
},
|
|
116
|
+
payload: {},
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const selector = (over: object = {}) => ({
|
|
120
|
+
actions: ["created", "edited"],
|
|
121
|
+
mention: "@claude",
|
|
122
|
+
from: { kind: "user", login: "alice" },
|
|
123
|
+
...over,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("from allows a listed user, and a collaborator by author association", () => {
|
|
127
|
+
assert.deepEqual(channel.match(selector(), delivery({})), { result: "allowed" });
|
|
128
|
+
assert.deepEqual(
|
|
129
|
+
channel.match(selector(), delivery({ author: { login: "bob", association: "NONE", bot: false } })),
|
|
130
|
+
{ result: "denied" },
|
|
131
|
+
);
|
|
132
|
+
const collaborators = selector({ from: { kind: "collaborators" } });
|
|
133
|
+
assert.deepEqual(
|
|
134
|
+
channel.match(collaborators, delivery({ author: { login: "bob", association: "COLLABORATOR", bot: false } })),
|
|
135
|
+
{ result: "allowed" },
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("a comment matches only if the issue's author matches from too", () => {
|
|
140
|
+
const collaborators = selector({ from: { kind: "collaborators" } });
|
|
141
|
+
const member = { login: "alice", association: "MEMBER", bot: false };
|
|
142
|
+
assert.deepEqual(
|
|
143
|
+
channel.match(collaborators, delivery({ author: member, opener: { login: "eve", association: "NONE", bot: false } })),
|
|
144
|
+
{ result: "denied" },
|
|
145
|
+
);
|
|
146
|
+
assert.deepEqual(
|
|
147
|
+
channel.match(collaborators, delivery({ author: member, opener: { login: "bob", association: "OWNER", bot: false } })),
|
|
148
|
+
{ result: "allowed" },
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("bots never match, even with write access", () => {
|
|
153
|
+
const collaborators = selector({ from: { kind: "collaborators" } });
|
|
154
|
+
const bot = { login: "renovate[bot]", association: "MEMBER", bot: true };
|
|
155
|
+
assert.deepEqual(channel.match(collaborators, delivery({ author: bot })), { result: "denied" });
|
|
156
|
+
const human = { login: "alice", association: "MEMBER", bot: false };
|
|
157
|
+
assert.deepEqual(channel.match(collaborators, delivery({ author: human, opener: bot })), { result: "denied" });
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("receive marks bot authors", async () => {
|
|
161
|
+
const fromBot = {
|
|
162
|
+
...commentPayload,
|
|
163
|
+
issue: { ...commentPayload.issue, user: { login: "dependabot[bot]", type: "Bot" }, author_association: "NONE" },
|
|
164
|
+
};
|
|
165
|
+
const received = await channel.receive(await request("issue_comment", fromBot));
|
|
166
|
+
assert.equal(received.status, "accepted");
|
|
167
|
+
const { data } = (received as { delivery: Delivery<GithubEvent> }).delivery;
|
|
168
|
+
assert.deepEqual(data.opener, { login: "dependabot[bot]", association: "NONE", bot: true });
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("a team in from leaves a check for the router to make", () => {
|
|
172
|
+
const team = selector({ from: [{ kind: "team", slug: "acme/core" }] });
|
|
173
|
+
assert.deepEqual(
|
|
174
|
+
channel.match(team, delivery({ author: { login: "bob", association: "NONE", bot: false } })),
|
|
175
|
+
{ result: "check", sets: [{ kind: "team", slug: "acme/core" }] },
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("labels must all be on the issue", () => {
|
|
180
|
+
const labelled = selector({ labels: ["bug", "p1"] });
|
|
181
|
+
assert.equal(channel.match(labelled, delivery({ labels: ["bug"] })), undefined);
|
|
182
|
+
assert.ok(channel.match(labelled, delivery({ labels: ["bug", "p1"] })));
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("an edit matches only if it newly adds the mention", () => {
|
|
186
|
+
const edit = (before: string) => delivery({ action: "edited", before });
|
|
187
|
+
assert.ok(channel.match(selector(), edit("fix")));
|
|
188
|
+
assert.equal(channel.match(selector(), edit("@claude fix")), undefined);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("mentions match as GitHub does", () => {
|
|
192
|
+
assert.equal(mentions("@Claude please", "@claude"), true);
|
|
193
|
+
assert.equal(mentions("hi (@claude)", "@claude"), true);
|
|
194
|
+
assert.equal(mentions("@claude.", "@claude"), true);
|
|
195
|
+
assert.equal(mentions("@claudette", "@claude"), false);
|
|
196
|
+
assert.equal(mentions("me@claude.ai", "@claude"), false);
|
|
197
|
+
assert.equal(mentions("@claude-bot", "@claude"), false);
|
|
198
|
+
});
|