@leitwerk-dev/woodpecker 0.0.0-bootstrap.0 → 0.2.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/src/testing.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { LocalProviderStore } from "@leitwerk-dev/test-support/local-git";
2
+ import type { WoodpeckerClientLike } from "./capability.js";
3
+ import type { WoodpeckerPipeline, WoodpeckerRepository } from "./client.js";
4
+ import { boundedLogTail } from "./logs.js";
5
+
6
+ export interface LocalWoodpeckerRepository {
7
+ repository: WoodpeckerRepository;
8
+ pipelines: Array<WoodpeckerPipeline & { logs: string; controlKey?: string }>;
9
+ }
10
+ export interface LocalWoodpeckerState {
11
+ version: 1;
12
+ sequence: number;
13
+ repositories: LocalWoodpeckerRepository[];
14
+ }
15
+ export interface LocalWoodpeckerOptions {
16
+ root: string;
17
+ baseUrl: string;
18
+ now?: () => number;
19
+ nextId?: () => number;
20
+ }
21
+
22
+ /** Local CI state is independent of the repository's Git host. */
23
+ export class LocalWoodpeckerAdapter extends LocalProviderStore<
24
+ LocalWoodpeckerState,
25
+ LocalWoodpeckerOptions
26
+ > {
27
+ constructor(options: LocalWoodpeckerOptions) {
28
+ super(options, "woodpecker.json", {
29
+ version: 1,
30
+ sequence: 0,
31
+ repositories: [],
32
+ });
33
+ }
34
+ seed(fullName: string, id?: number) {
35
+ const existing = this.state.repositories.find((r) => r.repository.full_name === fullName);
36
+ if (existing) return existing;
37
+ const repo = { repository: { id: id ?? this.id(), full_name: fullName }, pipelines: [] };
38
+ this.state.sequence = Math.max(this.state.sequence, repo.repository.id);
39
+ this.state.repositories.push(repo);
40
+ this.save();
41
+ return repo;
42
+ }
43
+ publish(
44
+ repo: LocalWoodpeckerRepository,
45
+ input: {
46
+ branch: string;
47
+ commit: string;
48
+ status: string;
49
+ logs: string;
50
+ event?: string;
51
+ controlKey?: string;
52
+ },
53
+ ) {
54
+ if (
55
+ ![
56
+ "pending",
57
+ "running",
58
+ "success",
59
+ "failure",
60
+ "error",
61
+ "killed",
62
+ "canceled",
63
+ "cancelled",
64
+ "declined",
65
+ "blocked",
66
+ ].includes(input.status)
67
+ )
68
+ throw new Error("Invalid pipeline status");
69
+ const number = this.id();
70
+ const pipeline = {
71
+ ...input,
72
+ id: number,
73
+ number,
74
+ event: input.event ?? "push",
75
+ created_at: Math.floor((this.options.now?.() ?? Date.now()) / 1000),
76
+ workflows: [
77
+ { id: 1, name: "local", steps: [{ id: 1, name: "validation", state: input.status }] },
78
+ ],
79
+ };
80
+ repo.pipelines.push(pipeline);
81
+ this.save();
82
+ return pipeline;
83
+ }
84
+ client(): WoodpeckerClientLike {
85
+ const repo = (id: number) => {
86
+ const value = this.state.repositories.find((r) => r.repository.id === id);
87
+ if (!value) throw new Error("Unknown local CI repository");
88
+ return value;
89
+ };
90
+ const pipeline = (id: number, number: number) => {
91
+ const value = repo(id).pipelines.find((p) => p.number === number);
92
+ if (!value) throw new Error("Unknown local pipeline");
93
+ return value;
94
+ };
95
+ return {
96
+ profile: { baseUrl: this.options.baseUrl, token: "" },
97
+ lookupRepository: async (name) => {
98
+ const value = this.state.repositories.find((r) => r.repository.full_name === name);
99
+ if (!value) throw new Error("Unknown local CI repository");
100
+ return structuredClone(value.repository);
101
+ },
102
+ listPipelines: async (id, _signal, { page = 1, perPage = 100 } = { page: 1 }) =>
103
+ structuredClone(
104
+ [...repo(id).pipelines]
105
+ .sort((a, b) => b.number - a.number)
106
+ .slice((page - 1) * perPage, page * perPage),
107
+ ),
108
+ getPipeline: async (id, number) => structuredClone(pipeline(id, number)),
109
+ getStepLogs: async (id, number, _step, tail = 400, bytes = 262144) => {
110
+ const original = pipeline(id, number).logs;
111
+ const logs = boundedLogTail(original.split("\n"), tail, bytes);
112
+ return { logs, truncated: logs !== original };
113
+ },
114
+ restartPipeline: async (id, number) => {
115
+ const value = pipeline(id, number);
116
+ value.status = "pending";
117
+ this.save();
118
+ return structuredClone(value);
119
+ },
120
+ };
121
+ }
122
+ }
package/src/tools.ts ADDED
@@ -0,0 +1,117 @@
1
+ import {
2
+ createWriteIdentity,
3
+ type ExternalWriteLogRepoLike,
4
+ ensureWrite,
5
+ } from "@leitwerk-dev/external-writes";
6
+ import {
7
+ type IntegrationToolExecutionContext,
8
+ numberArg,
9
+ projectParameters,
10
+ type ServerExtensionAPI,
11
+ stringArg,
12
+ } from "@leitwerk-dev/process-sdk";
13
+ import { resolveWoodpeckerProjectBinding } from "./binding.js";
14
+ import type { WoodpeckerIntegration } from "./capability.js";
15
+
16
+ function target(ctx: IntegrationToolExecutionContext) {
17
+ const binding = resolveWoodpeckerProjectBinding(ctx);
18
+ return { fullName: `${binding.owner}/${binding.repo}`, profile: binding.profile };
19
+ }
20
+
21
+ export function registerWoodpeckerTools(
22
+ api: ServerExtensionAPI,
23
+ integration: WoodpeckerIntegration,
24
+ writes: ExternalWriteLogRepoLike,
25
+ ) {
26
+ const schema = projectParameters();
27
+ api.tool<Record<string, unknown>>({
28
+ name: "woodpecker_lookup_repository",
29
+ description: "Resolve the current process repository in Woodpecker",
30
+ parameters: schema,
31
+ async execute(ctx) {
32
+ const t = target(ctx);
33
+ return integration.client(t.profile).lookupRepository(t.fullName, ctx.signal);
34
+ },
35
+ });
36
+ api.tool<Record<string, unknown>>({
37
+ name: "woodpecker_list_pipelines",
38
+ description: "List recent Woodpecker pipelines for the current process repository",
39
+ parameters: schema,
40
+ async execute(ctx) {
41
+ const t = target(ctx);
42
+ const client = integration.client(t.profile);
43
+ const repo = await client.lookupRepository(t.fullName, ctx.signal);
44
+ return client.listPipelines(repo.id, ctx.signal);
45
+ },
46
+ });
47
+ for (const name of ["woodpecker_get_pipeline", "woodpecker_restart_pipeline"] as const) {
48
+ api.tool<Record<string, unknown>>({
49
+ name,
50
+ description: name.endsWith("restart_pipeline")
51
+ ? "Restart a diagnosed Woodpecker pipeline"
52
+ : "Read a Woodpecker pipeline",
53
+ parameters: projectParameters({
54
+ pipelineNumber: { type: "integer" },
55
+ ...(name === "woodpecker_restart_pipeline"
56
+ ? {
57
+ diagnosis: {
58
+ type: "string",
59
+ description:
60
+ "Why this is an infrastructure or flaky failure rather than a repository defect",
61
+ },
62
+ logEvidence: {
63
+ type: "string",
64
+ description: "Concrete bounded-log evidence supporting a restart",
65
+ },
66
+ }
67
+ : {}),
68
+ }),
69
+ async execute(ctx, args) {
70
+ const t = target(ctx);
71
+ const client = integration.client(t.profile);
72
+ const repo = await client.lookupRepository(t.fullName, ctx.signal);
73
+ const number = numberArg(args, "pipelineNumber");
74
+ if (name === "woodpecker_get_pipeline")
75
+ return client.getPipeline(repo.id, number, ctx.signal);
76
+ const diagnosis = stringArg(args, "diagnosis");
77
+ const logEvidence = stringArg(args, "logEvidence");
78
+ await ensureWrite(
79
+ writes,
80
+ ctx.process.id,
81
+ createWriteIdentity("woodpecker.restart", ctx.idempotencyKey),
82
+ async () => {
83
+ await client.restartPipeline(repo.id, number, ctx.signal);
84
+ return { repoId: repo.id, number, diagnosis, logEvidence };
85
+ },
86
+ );
87
+ return { ok: true };
88
+ },
89
+ });
90
+ }
91
+ api.tool<Record<string, unknown>>({
92
+ name: "woodpecker_get_step_logs",
93
+ description: "Read bounded logs for a Woodpecker pipeline step",
94
+ parameters: projectParameters(
95
+ {
96
+ pipelineNumber: { type: "integer" },
97
+ stepId: { type: "integer" },
98
+ tailLines: { type: "integer" },
99
+ maxBytes: { type: "integer" },
100
+ },
101
+ ["pipelineNumber", "stepId"],
102
+ ),
103
+ async execute(ctx, args) {
104
+ const t = target(ctx);
105
+ const client = integration.client(t.profile);
106
+ const repo = await client.lookupRepository(t.fullName, ctx.signal);
107
+ return client.getStepLogs(
108
+ repo.id,
109
+ numberArg(args, "pipelineNumber"),
110
+ numberArg(args, "stepId"),
111
+ typeof args.tailLines === "number" ? args.tailLines : 400,
112
+ typeof args.maxBytes === "number" ? args.maxBytes : 262_144,
113
+ ctx.signal,
114
+ );
115
+ },
116
+ });
117
+ }