@notation/factory-github 0.1.0 → 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notation/factory-github",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "author": "Daniel Grant",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,9 +31,9 @@
31
31
  "valibot": "^1.1.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@notation/factory": "^0.1.0"
34
+ "@notation/factory": "^0.1.1"
35
35
  },
36
36
  "devDependencies": {
37
- "@notation/factory": "0.1.0"
37
+ "@notation/factory": "0.1.1"
38
38
  }
39
39
  }
@@ -57,7 +57,8 @@ test("receive parses a signed comment to its App", async () => {
57
57
  event: "issue_comment",
58
58
  data: {
59
59
  action: "created",
60
- author: { login: "alice", association: "NONE" },
60
+ author: { login: "alice", association: "NONE", bot: false },
61
+ opener: { login: "bob", association: "", bot: false },
61
62
  body: "@claude fix",
62
63
  before: "@claude fix",
63
64
  labels: ["bug"],
@@ -106,7 +107,7 @@ const delivery = (data: Partial<GithubEvent>): Delivery<GithubEvent> => ({
106
107
  event: "issue_comment",
107
108
  data: {
108
109
  action: "created",
109
- author: { login: "alice", association: "NONE" },
110
+ author: { login: "alice", association: "NONE", bot: false },
110
111
  body: "@claude fix",
111
112
  before: "@claude fix",
112
113
  labels: [],
@@ -125,20 +126,52 @@ const selector = (over: object = {}) => ({
125
126
  test("from allows a listed user, and a collaborator by author association", () => {
126
127
  assert.deepEqual(channel.match(selector(), delivery({})), { result: "allowed" });
127
128
  assert.deepEqual(
128
- channel.match(selector(), delivery({ author: { login: "bob", association: "NONE" } })),
129
+ channel.match(selector(), delivery({ author: { login: "bob", association: "NONE", bot: false } })),
129
130
  { result: "denied" },
130
131
  );
131
132
  const collaborators = selector({ from: { kind: "collaborators" } });
132
133
  assert.deepEqual(
133
- channel.match(collaborators, delivery({ author: { login: "bob", association: "COLLABORATOR" } })),
134
+ channel.match(collaborators, delivery({ author: { login: "bob", association: "COLLABORATOR", bot: false } })),
134
135
  { result: "allowed" },
135
136
  );
136
137
  });
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
+
138
171
  test("a team in from leaves a check for the router to make", () => {
139
172
  const team = selector({ from: [{ kind: "team", slug: "acme/core" }] });
140
173
  assert.deepEqual(
141
- channel.match(team, delivery({ author: { login: "bob", association: "NONE" } })),
174
+ channel.match(team, delivery({ author: { login: "bob", association: "NONE", bot: false } })),
142
175
  { result: "check", sets: [{ kind: "team", slug: "acme/core" }] },
143
176
  );
144
177
  });
package/router/index.ts CHANGED
@@ -28,11 +28,16 @@ export const verifySignature = async (
28
28
  export const CHANNEL_SECRETS = { rootKey: "ROOT_KEY", privateKey: "PRIVATE_KEY" } as const;
29
29
  export const CHANNEL_VALUES = { appId: "APP_ID" } as const;
30
30
 
31
+ /** Someone who posted to a thread. `bot` is set for an App or another bot account. */
32
+ export type Author = { login: string; association: string; bot: boolean };
33
+
31
34
  /** The parts of an issue or comment event that selectors read. */
32
35
  export type GithubEvent = {
33
36
  action: string;
34
37
  /** The author of the comment, or of the issue. */
35
- author: { login: string; association: string };
38
+ author: Author;
39
+ /** For a comment, the issue's author, whose opening post the prompt also quotes. */
40
+ opener?: Author;
36
41
  body: string;
37
42
  /** The body before an edit. The same as `body` when an edit didn't change it. */
38
43
  before: string;
@@ -41,10 +46,16 @@ export type GithubEvent = {
41
46
 
42
47
  type Post = {
43
48
  body?: string | null;
44
- user?: { login: string };
49
+ user?: { login: string; type?: string };
45
50
  author_association?: string;
46
51
  };
47
52
 
53
+ const authorOf = (post: Post): Author => ({
54
+ login: post.user?.login ?? "",
55
+ association: post.author_association ?? "",
56
+ bot: post.user?.type === "Bot",
57
+ });
58
+
48
59
  const parse = (event: string, payload: any): GithubEvent | undefined => {
49
60
  const post: Post | undefined =
50
61
  event === "issue_comment"
@@ -56,10 +67,8 @@ const parse = (event: string, payload: any): GithubEvent | undefined => {
56
67
  const body = post.body ?? "";
57
68
  return {
58
69
  action: payload.action,
59
- author: {
60
- login: post.user?.login ?? "",
61
- association: post.author_association ?? "",
62
- },
70
+ author: authorOf(post),
71
+ ...(event === "issue_comment" && payload.issue && { opener: authorOf(payload.issue) }),
63
72
  body,
64
73
  before: payload.changes?.body?.from ?? body,
65
74
  labels: (payload.issue?.labels ?? []).map((l: { name: string }) => l.name),
@@ -148,11 +157,8 @@ const selects = (selector: Selector, event: GithubEvent) => {
148
157
  return true;
149
158
  };
150
159
 
151
- const checkAuthor = (
152
- from: Selector["from"],
153
- { author }: GithubEvent,
154
- ): AuthorCheck => {
155
- const sets = Array.isArray(from) ? from : [from];
160
+ const checkOne = (sets: AuthorSet[], author: Author): AuthorCheck => {
161
+ if (author.bot) return { result: "denied" };
156
162
  const allowed = sets.some((s) =>
157
163
  s.kind === "user"
158
164
  ? s.login.toLowerCase() === author.login.toLowerCase()
@@ -163,6 +169,19 @@ const checkAuthor = (
163
169
  return teams.length ? { result: "check", sets: teams } : { result: "denied" };
164
170
  };
165
171
 
172
+ /**
173
+ * Whether the event's author may start a run. A comment's prompt also
174
+ * quotes the issue's opening post, so the issue's author must match `from`
175
+ * too. Bots never match.
176
+ */
177
+ const checkAuthor = (from: Selector["from"], { author, opener }: GithubEvent): AuthorCheck => {
178
+ const sets = Array.isArray(from) ? from : [from];
179
+ const checks = [author, ...(opener ? [opener] : [])].map((a) => checkOne(sets, a));
180
+ if (checks.some((c) => c.result === "denied")) return { result: "denied" };
181
+ const pending = checks.find((c) => c.result === "check");
182
+ return pending ?? { result: "allowed" };
183
+ };
184
+
166
185
  /** One GitHub App's webhook. A delivery's scope is its repo. */
167
186
  export const channel = (ctx: ChannelContext): Channel<GithubEvent> => {
168
187
  const appId = ctx.value(CHANNEL_VALUES.appId);