@leitwerk-dev/github 0.2.0 → 0.2.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.
@@ -0,0 +1,126 @@
1
+ import type { IntegrationToolExecutionContext } from "@leitwerk-dev/process-sdk";
2
+ import { createInMemoryExternalWriteLog, createToolCollector } from "@leitwerk-dev/test-support";
3
+ import type { GitHubClientLike } from "../capability.js";
4
+ import type { GitHubPullRequest } from "../client.js";
5
+ import { registerGitHubTools } from "../tools.js";
6
+
7
+ export function toolContext(id = "p"): IntegrationToolExecutionContext {
8
+ return {
9
+ process: { id, paramsJson: "{}" },
10
+ project: {
11
+ instanceId: id,
12
+ workBranch: "feature",
13
+ baseBranch: "main",
14
+ metadata: { github: { owner: "team", repo: "one", profile: "first" } },
15
+ },
16
+ idempotencyKey: "retained-pr-key",
17
+ signal: new AbortController().signal,
18
+ } as unknown as IntegrationToolExecutionContext;
19
+ }
20
+
21
+ export const pullRequestArgs = {
22
+ projectKey: "one",
23
+ owner: "attacker",
24
+ repo: "two",
25
+ profile: "other",
26
+ title: "A change",
27
+ body: "Review",
28
+ head: "feature",
29
+ base: "main",
30
+ };
31
+
32
+ /** Stateful provider boundary, with no Git, filesystem, or network. Unexpected calls fail closed. */
33
+ export function toolFixture() {
34
+ const pulls: GitHubPullRequest[] = [];
35
+ const comments: Array<{ body: string }> = [];
36
+ const requests: Array<{ method: string; owner: string; repo: string }> = [];
37
+ const profiles: string[] = [];
38
+ const failure = { create: "none" as "none" | "before" | "after", comment: false };
39
+ const record = (method: string, owner: string, repo: string) =>
40
+ requests.push({ method, owner, repo });
41
+ const methods = {
42
+ profile: { apiBaseUrl: "https://github.invalid", token: "fake", botLogin: "bot" },
43
+ async listPullRequests(owner, repo) {
44
+ record("list", owner, repo);
45
+ return structuredClone(pulls);
46
+ },
47
+ async createPullRequest(owner, repo, input) {
48
+ record("create", owner, repo);
49
+ if (failure.create === "before") throw new Error("create rejected");
50
+ const pr: GitHubPullRequest = {
51
+ number: pulls.length + 1,
52
+ title: input.title,
53
+ body: input.body,
54
+ state: "open",
55
+ merged: false,
56
+ merge_commit_sha: null,
57
+ html_url: "https://github.invalid/team/one/pull/1",
58
+ head: { ref: input.head, sha: "feature-sha" },
59
+ base: { ref: input.base, sha: "base-sha" },
60
+ };
61
+ pulls.push(pr);
62
+ if (failure.create === "after") throw new Error("response lost");
63
+ return structuredClone(pr);
64
+ },
65
+ async listIssueComments(owner, repo) {
66
+ record("comments", owner, repo);
67
+ return structuredClone(comments);
68
+ },
69
+ async addIssueComment(owner, repo, _number, body) {
70
+ record("comment", owner, repo);
71
+ comments.push({ body });
72
+ if (failure.comment) throw new Error("response lost");
73
+ return {};
74
+ },
75
+ async getPullRequest(owner, repo, number) {
76
+ record("get", owner, repo);
77
+ const pr = pulls.find((p) => p.number === number);
78
+ if (!pr) throw new Error("Unknown PR");
79
+ return structuredClone(pr);
80
+ },
81
+ async updatePullRequest(owner, repo, number, patch) {
82
+ record("update", owner, repo);
83
+ const pr = pulls.find((p) => p.number === number);
84
+ if (!pr) throw new Error("Unknown PR");
85
+ Object.assign(pr, patch);
86
+ return structuredClone(pr);
87
+ },
88
+ } satisfies Pick<
89
+ GitHubClientLike,
90
+ | "profile"
91
+ | "listPullRequests"
92
+ | "createPullRequest"
93
+ | "listIssueComments"
94
+ | "addIssueComment"
95
+ | "updatePullRequest"
96
+ | "getPullRequest"
97
+ >;
98
+ const client = new Proxy(methods, {
99
+ get(target, key) {
100
+ if (!(key in target)) throw new Error(`Unexpected provider access: ${String(key)}`);
101
+ return Reflect.get(target, key);
102
+ },
103
+ }) as unknown as GitHubClientLike;
104
+ const writes = createInMemoryExternalWriteLog();
105
+ const { api, tools } = createToolCollector(writes);
106
+ registerGitHubTools(api, {
107
+ client(profile) {
108
+ profiles.push(profile);
109
+ return client;
110
+ },
111
+ });
112
+ return {
113
+ pulls,
114
+ comments,
115
+ requests,
116
+ profiles,
117
+ failure,
118
+ writes,
119
+ tools,
120
+ execute(name: string, args: Record<string, unknown>, ctx = toolContext()) {
121
+ const tool = tools.get(name);
122
+ if (!tool) throw new Error(`Missing tool ${name}`);
123
+ return tool.execute(ctx, args);
124
+ },
125
+ };
126
+ }
package/src/testing.ts CHANGED
@@ -12,39 +12,85 @@ import type {
12
12
  } from "./client.js";
13
13
  import { assertGitHubRepository } from "./client.js";
14
14
 
15
- export interface LocalGitHubRepository {
15
+ /** @public */
16
+ interface LocalGitHubRepository {
17
+ /** @public */
16
18
  repository: ReturnType<LocalGitHubAdapter["newRepository"]>;
19
+ /** @public */
17
20
  issues: GitHubIssue[];
21
+ /** @public */
18
22
  pulls: GitHubPullRequest[];
23
+ /** @public */
19
24
  comments: Record<string, Array<ReturnType<LocalGitHubAdapter["newComment"]>>>;
25
+ /** @public */
20
26
  feedback: Record<string, GitHubFeedbackItem[]>;
27
+ /** @public */
21
28
  checks: Record<string, GitHubCheckSummary>;
29
+ /** @public */
22
30
  releases: GitHubRelease[];
31
+ /** @public */
23
32
  assets: Record<string, string>;
24
- labels: Array<{ id: number; name: string }>;
33
+ /** @public */
34
+ labels: Array<{
35
+ /** @internal */
36
+ id: number;
37
+ /** @internal */
38
+ name: string;
39
+ }>;
40
+ /** @public */
25
41
  labelEvents: Record<string, GitHubLabelEvent[]>;
26
- reactions: Record<string, Array<{ id: number; content: string; user: { login: string } }>>;
42
+ /** @public */
43
+ reactions: Record<
44
+ string,
45
+ Array<{
46
+ /** @internal */
47
+ id: number;
48
+ /** @internal */
49
+ content: string;
50
+ /** @internal */
51
+ user: {
52
+ /** @internal */
53
+ login: string;
54
+ };
55
+ }>
56
+ >;
57
+ /** @public */
27
58
  feedbackEditors: Record<string, string | null>;
28
59
  }
60
+ /** @public */
29
61
  export interface LocalGitHubState {
62
+ /** @public */
30
63
  version: 1;
64
+ /** @public */
31
65
  sequence: number;
66
+ /** @public */
32
67
  repositories: LocalGitHubRepository[];
68
+ /** @public */
33
69
  failAfterPullRequestWrite: boolean;
70
+ /** @public */
34
71
  members: string[];
72
+ /** @public */
35
73
  failAfterWrite: string | null;
36
74
  }
37
- export interface LocalGitHubOptions {
75
+ /** @public */
76
+ interface LocalGitHubOptions {
77
+ /** @public */
38
78
  root: string;
79
+ /** @public */
39
80
  baseUrl: string;
81
+ /** @public */
40
82
  now?: () => number;
83
+ /** @public */
41
84
  nextId?: () => number;
85
+ /** @public */
42
86
  seeds?: LocalRepositorySeed[];
87
+ /** @public */
43
88
  allowedOrganization?: string;
44
89
  }
45
90
 
46
- /** Persistent local GitHub with actual commit ancestry and configurable release assets. */
91
+ /** Persistent local GitHub with actual commit ancestry and configurable release assets. @public */
47
92
  export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalGitHubOptions> {
93
+ /** @public */
48
94
  constructor(options: LocalGitHubOptions) {
49
95
  super(options, "github.json", {
50
96
  version: 1,
@@ -64,11 +110,13 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
64
110
  }
65
111
  for (const seed of options.seeds ?? []) this.seed(seed);
66
112
  }
113
+ /** @public */
67
114
  repo(owner: string, name: string) {
68
115
  const repo = this.state.repositories.find((r) => r.repository.full_name === `${owner}/${name}`);
69
116
  if (!repo) throw new Error("Unknown local GitHub repository");
70
117
  return repo;
71
118
  }
119
+ /** @public */
72
120
  seed(seed: LocalRepositorySeed) {
73
121
  const existing = this.state.repositories.find(
74
122
  (r) => r.repository.full_name === `${seed.owner}/${seed.name}`,
@@ -92,18 +140,25 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
92
140
  this.save();
93
141
  return repo;
94
142
  }
143
+ /** @public */
95
144
  setChecks(repo: LocalGitHubRepository, summary: GitHubCheckSummary) {
96
145
  this.git.head(repo.repository.ssh_url, summary.headSha);
97
146
  repo.checks[summary.headSha] = structuredClone(summary);
98
147
  this.save();
99
148
  }
149
+ /** @public */
100
150
  publishRelease(
101
151
  repo: LocalGitHubRepository,
102
152
  input: {
153
+ /** @public */
103
154
  tag: string;
155
+ /** @public */
104
156
  ref: string;
157
+ /** @public */
105
158
  assets: Record<string, string>;
159
+ /** @internal */
106
160
  draft?: boolean;
161
+ /** @internal */
107
162
  prerelease?: boolean;
108
163
  },
109
164
  ) {
@@ -128,9 +183,17 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
128
183
  this.save();
129
184
  return release;
130
185
  }
186
+ /** @public */
131
187
  override newRepository(seed: LocalRepositorySeed) {
132
- return { ...super.newRepository(seed), archived: false, has_issues: true };
188
+ return {
189
+ ...super.newRepository(seed),
190
+ /** @public */
191
+ archived: false,
192
+ /** @public */
193
+ has_issues: true,
194
+ };
133
195
  }
196
+ /** @public */
134
197
  setMembership(login: string, member: boolean) {
135
198
  this.state.members = this.state.members.filter(
136
199
  (value) => value.toLowerCase() !== login.toLowerCase(),
@@ -138,6 +201,7 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
138
201
  if (member) this.state.members.push(login);
139
202
  this.save();
140
203
  }
204
+ /** @public */
141
205
  failNextResponse(operation: "comment" | "reply" | "reaction" | "issue" | "label") {
142
206
  this.state.failAfterWrite = operation;
143
207
  this.save();
@@ -148,9 +212,17 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
148
212
  this.save();
149
213
  throw new Error(`Local GitHub: response lost after ${operation} write`);
150
214
  }
215
+ /** @public */
151
216
  createIssue(
152
217
  repo: LocalGitHubRepository,
153
- input: { title: string; body?: string; author?: string },
218
+ input: {
219
+ /** @public */
220
+ title: string;
221
+ /** @public */
222
+ body?: string;
223
+ /** @public */
224
+ author?: string;
225
+ },
154
226
  ) {
155
227
  const number = this.id();
156
228
  const issue: GitHubIssue = {
@@ -167,6 +239,7 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
167
239
  this.save();
168
240
  return issue;
169
241
  }
242
+ /** @public */
170
243
  setIssueLabel(
171
244
  repo: LocalGitHubRepository,
172
245
  number: number,
@@ -190,6 +263,7 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
190
263
  this.save();
191
264
  return event;
192
265
  }
266
+ /** @public */
193
267
  editFeedback(
194
268
  repo: LocalGitHubRepository,
195
269
  number: number,
@@ -206,6 +280,7 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
206
280
  this.save();
207
281
  }
208
282
 
283
+ /** @public */
209
284
  client(): GitHubClientLike {
210
285
  const profile: GitHubProfile = {
211
286
  apiBaseUrl: this.options.baseUrl,
@@ -245,7 +320,8 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
245
320
  name: "Sandbox Developer",
246
321
  email: "developer@sandbox.invalid",
247
322
  }),
248
- ensureLabel: async (owner, name, labelName) => {
323
+ listLabels: async (owner, name) => structuredClone(repo(owner, name).labels),
324
+ createLabel: async (owner, name, labelName) => {
249
325
  const r = repo(owner, name);
250
326
  let label = r.labels.find((value) => value.name === labelName);
251
327
  if (!label) {
@@ -256,6 +332,10 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
256
332
  }
257
333
  return structuredClone(label);
258
334
  },
335
+ ensureLabel: async (owner, name, labelName) => {
336
+ const label = repo(owner, name).labels.find((value) => value.name === labelName);
337
+ return label ? structuredClone(label) : client.createLabel(owner, name, labelName);
338
+ },
259
339
  updateIssue: async (owner, name, number, patch) => {
260
340
  const issue = repo(owner, name).issues.find((i) => i.number === number);
261
341
  if (!issue) throw new Error("Unknown local GitHub issue");
@@ -295,13 +375,15 @@ export class LocalGitHubAdapter extends LocalForgeStore<LocalGitHubState, LocalG
295
375
  .map((f) => ({ ...f })),
296
376
  )
297
377
  : client.listIssueComments(owner, name, number),
298
- replyFeedback: async (owner, name, number, kind, _id, body) => {
378
+ replyFeedback: async (owner, name, number, kind, id, body) => {
299
379
  if (kind !== "inline") return client.addIssueComment(owner, name, number, body);
300
380
  const value = this.addFeedback(repo(owner, name), number, {
301
381
  kind: "inline",
302
382
  body,
303
383
  author: profile.botLogin,
304
384
  });
385
+ Object.assign(value, { in_reply_to_id: id });
386
+ this.save();
305
387
  this.lostResponse("reply");
306
388
  return structuredClone(value);
307
389
  },