@leitwerk-dev/gitlab 0.3.0 → 0.4.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/README.md +154 -1
- package/dist/capability.d.ts +3 -0
- package/dist/capability.d.ts.map +1 -1
- package/dist/chunk-YPCOVBQS.js +2343 -0
- package/dist/chunk-YPCOVBQS.js.map +1 -0
- package/dist/client.d.ts +99 -7
- package/dist/client.d.ts.map +1 -1
- package/dist/external.d.ts +24 -4
- package/dist/external.d.ts.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +512 -10
- package/dist/index.js.map +1 -1
- package/dist/issue-watcher.d.ts +2 -2
- package/dist/issue-watcher.d.ts.map +1 -1
- package/dist/launch.d.ts +40 -0
- package/dist/launch.d.ts.map +1 -0
- package/dist/maintenance-labels.d.ts +30 -0
- package/dist/maintenance-labels.d.ts.map +1 -0
- package/dist/maintenance.d.ts +132 -0
- package/dist/maintenance.d.ts.map +1 -0
- package/dist/publication-maintenance.d.ts +7 -0
- package/dist/publication-maintenance.d.ts.map +1 -0
- package/dist/publication.d.ts +37 -0
- package/dist/publication.d.ts.map +1 -0
- package/dist/repository-catalog.d.ts +16 -0
- package/dist/repository-catalog.d.ts.map +1 -0
- package/dist/setup.d.ts +8 -2
- package/dist/setup.d.ts.map +1 -1
- package/dist/testing.d.ts +10 -1
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +125 -5
- package/dist/testing.js.map +1 -1
- package/dist/ticket-creation.d.ts +5 -0
- package/dist/ticket-creation.d.ts.map +1 -0
- package/dist/tools.d.ts +100 -3
- package/dist/tools.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/capability.ts +3 -0
- package/src/client.ts +275 -12
- package/src/delivery-tools.ts +4 -4
- package/src/external.ts +91 -24
- package/src/index.ts +58 -10
- package/src/issue-watcher.ts +16 -14
- package/src/launch.ts +63 -0
- package/src/maintenance-labels.ts +129 -0
- package/src/maintenance.ts +759 -0
- package/src/publication-maintenance.ts +288 -0
- package/src/publication.ts +234 -0
- package/src/repository-catalog.ts +94 -0
- package/src/setup.ts +115 -6
- package/src/testing.ts +143 -4
- package/src/ticket-creation.ts +145 -0
- package/src/tools.ts +382 -26
- package/dist/chunk-H4JCQNDY.js +0 -1162
- package/dist/chunk-H4JCQNDY.js.map +0 -1
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyPublicationEvidence,
|
|
3
|
+
createPublicationSelection,
|
|
4
|
+
type PublicationContext,
|
|
5
|
+
type PublicationEvidence,
|
|
6
|
+
patchPublicationState,
|
|
7
|
+
publicationFinished,
|
|
8
|
+
type RepositoryChangePublicationAdapter,
|
|
9
|
+
readPublicationState,
|
|
10
|
+
requirePublicationParams,
|
|
11
|
+
} from "@leitwerk-dev/coding/repository-change-publication";
|
|
12
|
+
import type { RepositoryChangeState } from "@leitwerk-dev/coding/repository-change-state";
|
|
13
|
+
import { conflictKey } from "@leitwerk-dev/coding/repository-rebase";
|
|
14
|
+
import type { ExtensionProcessDefinition, ServerExtensionAPI } from "@leitwerk-dev/process-sdk";
|
|
15
|
+
import { type GitLabMaintenanceContext, registerGitLabMaintainedProcess } from "./maintenance.js";
|
|
16
|
+
import {
|
|
17
|
+
type GitLabPublicationParams,
|
|
18
|
+
gitlabPublicationEvidenceForRequest,
|
|
19
|
+
gitlabPublicationRequest,
|
|
20
|
+
} from "./publication.js";
|
|
21
|
+
|
|
22
|
+
/** The publication adapter retains repair policy and budgets; maintenance owns its delivery edges. @internal */
|
|
23
|
+
export function registerGitLabPublicationMaintenance<P extends Partial<GitLabPublicationParams>>(
|
|
24
|
+
api: ServerExtensionAPI,
|
|
25
|
+
process: ExtensionProcessDefinition<P, RepositoryChangeState>,
|
|
26
|
+
adapter: RepositoryChangePublicationAdapter<P>,
|
|
27
|
+
): void {
|
|
28
|
+
const namespace = (key: string) =>
|
|
29
|
+
adapter.repositories ? `${adapter.namespace}:${key}` : adapter.namespace;
|
|
30
|
+
const current = (ctx: GitLabMaintenanceContext<P, RepositoryChangeState>, key: string) =>
|
|
31
|
+
readPublicationState(ctx.state, namespace(key));
|
|
32
|
+
const params = (ctx: GitLabMaintenanceContext<P, RepositoryChangeState>, key: string) =>
|
|
33
|
+
requirePublicationParams(
|
|
34
|
+
adapter.repositories?.(ctx.params).find((repository) => repository.key === key)?.params ??
|
|
35
|
+
ctx.params,
|
|
36
|
+
);
|
|
37
|
+
const patch = (
|
|
38
|
+
ctx: GitLabMaintenanceContext<P, RepositoryChangeState>,
|
|
39
|
+
key: string,
|
|
40
|
+
value: Parameters<typeof patchPublicationState>[2],
|
|
41
|
+
) => patchPublicationState(ctx.state, namespace(key), value);
|
|
42
|
+
const selection = createPublicationSelection(() => adapter.namespace);
|
|
43
|
+
const select = (state: RepositoryChangeState, key: string) =>
|
|
44
|
+
adapter.repositories ? selection.select(state, key) : state;
|
|
45
|
+
const repositories = (ctx: GitLabMaintenanceContext<P, RepositoryChangeState>) =>
|
|
46
|
+
adapter.repositories?.(ctx.params) ?? [{ key: "repo", params: ctx.params }];
|
|
47
|
+
const results = (ctx: GitLabMaintenanceContext<P, RepositoryChangeState>) =>
|
|
48
|
+
repositories(ctx).map(({ key }) => ({ key, current: current(ctx, key) }));
|
|
49
|
+
const workerContext = (
|
|
50
|
+
ctx: GitLabMaintenanceContext<P, RepositoryChangeState>,
|
|
51
|
+
key: string,
|
|
52
|
+
): PublicationContext<P> =>
|
|
53
|
+
({
|
|
54
|
+
...ctx,
|
|
55
|
+
params: params(ctx, key),
|
|
56
|
+
state: select(ctx.state, key),
|
|
57
|
+
repo: {
|
|
58
|
+
get: (name: string) => {
|
|
59
|
+
const project = ctx.projects.find(
|
|
60
|
+
(project) => project.key === (name === "repo" ? key : name),
|
|
61
|
+
);
|
|
62
|
+
if (!project) throw new Error("Maintained process project is unavailable");
|
|
63
|
+
return { ...project, fsPath: "", workBranch: project.workBranch ?? project.baseBranch };
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
callIntegrationTool: (name: string, args: Record<string, unknown>) =>
|
|
67
|
+
ctx.callIntegrationTool(name, { ...args, projectKey: key }),
|
|
68
|
+
}) as unknown as PublicationContext<P>;
|
|
69
|
+
registerGitLabMaintainedProcess(api, {
|
|
70
|
+
processId: process.id,
|
|
71
|
+
paramsCodec: process.paramsCodec,
|
|
72
|
+
stateCodec: process.stateCodec,
|
|
73
|
+
destinations: {
|
|
74
|
+
feedback: "maintenance_feedback",
|
|
75
|
+
conflict: "maintenance_conflict",
|
|
76
|
+
ci: "maintenance_ci",
|
|
77
|
+
},
|
|
78
|
+
idleTurnId: adapter.ids.deliver,
|
|
79
|
+
currentBinding: (ctx) => (adapter.repositories ? selection.activeKey(ctx.state) : "repo"),
|
|
80
|
+
settings: (ctx, binding) => ({
|
|
81
|
+
cursor: current(ctx, binding.projectKey).conversationCursor,
|
|
82
|
+
quietPeriodMs: 120_000,
|
|
83
|
+
pollInterval: "30s",
|
|
84
|
+
}),
|
|
85
|
+
observe(ctx, binding, observed) {
|
|
86
|
+
const key = binding.projectKey;
|
|
87
|
+
let value = current(ctx, key);
|
|
88
|
+
const p = params(ctx, key);
|
|
89
|
+
if (publicationFinished(value)) return { state: ctx.state };
|
|
90
|
+
if (
|
|
91
|
+
ctx.process.selectedTurnId === adapter.ids.deliver &&
|
|
92
|
+
!ctx.process.currentExecution &&
|
|
93
|
+
!value.delivery.adjustment &&
|
|
94
|
+
!value.delivery.feedbackResult
|
|
95
|
+
) {
|
|
96
|
+
if (value.feedbackIds.length) return { state: select(ctx.state, key), action: "feedback" };
|
|
97
|
+
if (value.conflict && value.repairReason === "rebase")
|
|
98
|
+
return { state: select(ctx.state, key), action: "conflict" };
|
|
99
|
+
if (value.repairPending && value.repairReason === "ci" && value.pipeline)
|
|
100
|
+
return { state: select(ctx.state, key), action: "ci" };
|
|
101
|
+
}
|
|
102
|
+
// Preserve staged legacy evidence without replaying its old Deliver execution.
|
|
103
|
+
let evidence: PublicationEvidence;
|
|
104
|
+
if (value.pendingEvidence) {
|
|
105
|
+
evidence = value.pendingEvidence;
|
|
106
|
+
value = { ...value, pendingEvidence: null };
|
|
107
|
+
} else {
|
|
108
|
+
if (
|
|
109
|
+
value.delivery.adjustment ||
|
|
110
|
+
value.delivery.feedbackResult ||
|
|
111
|
+
value.feedbackIds.length ||
|
|
112
|
+
!value.headSha ||
|
|
113
|
+
!value.prNumber
|
|
114
|
+
)
|
|
115
|
+
return { state: ctx.state };
|
|
116
|
+
let event = observed;
|
|
117
|
+
if (
|
|
118
|
+
!observed.targetIntegrated &&
|
|
119
|
+
observed.mr.sha === value.headSha &&
|
|
120
|
+
observed.targetHead &&
|
|
121
|
+
(observed.mr.has_conflicts ||
|
|
122
|
+
["conflict", "need_rebase"].includes(observed.mr.detailed_merge_status ?? ""))
|
|
123
|
+
) {
|
|
124
|
+
if (
|
|
125
|
+
observed.mr.source_project_id !== binding.projectId ||
|
|
126
|
+
observed.mr.target_project_id !== binding.projectId ||
|
|
127
|
+
observed.mr.source_branch !== p.workBranch ||
|
|
128
|
+
observed.mr.target_branch !== p.baseBranch
|
|
129
|
+
)
|
|
130
|
+
throw new Error("GitLab maintenance branch binding changed");
|
|
131
|
+
const conflict = {
|
|
132
|
+
owner: p.owner,
|
|
133
|
+
repo: p.repo,
|
|
134
|
+
prNumber: value.prNumber,
|
|
135
|
+
headBranch: p.workBranch,
|
|
136
|
+
baseBranch: p.baseBranch,
|
|
137
|
+
headSha: value.headSha,
|
|
138
|
+
baseSha: observed.targetHead,
|
|
139
|
+
url: observed.mr.web_url,
|
|
140
|
+
};
|
|
141
|
+
if (conflictKey(conflict) !== value.lastConflictKey) event = { ...observed, conflict };
|
|
142
|
+
}
|
|
143
|
+
evidence = gitlabPublicationEvidenceForRequest(value, event);
|
|
144
|
+
}
|
|
145
|
+
if (evidence.kind === "terminal")
|
|
146
|
+
return {
|
|
147
|
+
state: patch(ctx, key, {
|
|
148
|
+
delivery: { terminalPullRequest: evidence.request },
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
const dispatch = (action: string, operator = false) => ({
|
|
152
|
+
state: select(
|
|
153
|
+
patch(ctx, key, {
|
|
154
|
+
...applyPublicationEvidence(p, value, evidence, !operator),
|
|
155
|
+
repairPending: !operator,
|
|
156
|
+
// Dispatch does not consume feedback; publication/no-change acknowledgement does.
|
|
157
|
+
...(evidence.kind === "feedback"
|
|
158
|
+
? { conversationCursor: value.conversationCursor }
|
|
159
|
+
: {}),
|
|
160
|
+
}),
|
|
161
|
+
key,
|
|
162
|
+
),
|
|
163
|
+
action,
|
|
164
|
+
});
|
|
165
|
+
if (evidence.kind === "feedback" || evidence.kind === "conflict")
|
|
166
|
+
return dispatch(evidence.kind);
|
|
167
|
+
// Labels, mergeability and target-head changes do not create a new CI failure.
|
|
168
|
+
// Keeping the last pipeline status also admits a retry that runs and fails again.
|
|
169
|
+
if (
|
|
170
|
+
evidence.kind === "failure" &&
|
|
171
|
+
(value.pipeline?.status !== "failed" ||
|
|
172
|
+
value.pipeline.number !== evidence.pipeline.number ||
|
|
173
|
+
(value.pipeline.project_id ?? binding.projectId) !==
|
|
174
|
+
(evidence.pipeline.project_id ?? binding.projectId))
|
|
175
|
+
) {
|
|
176
|
+
const operator = value.ciRecoveryCycles >= 3;
|
|
177
|
+
return dispatch(operator ? "maintenance_operator" : "ci", operator);
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
state: patch(ctx, key, {
|
|
181
|
+
...value,
|
|
182
|
+
observationKey: observed.observationKey,
|
|
183
|
+
pipeline: observed.pipeline
|
|
184
|
+
? { ...observed.pipeline, number: observed.pipeline.id }
|
|
185
|
+
: null,
|
|
186
|
+
...(observed.pipeline?.status === "success" ? { ciRecoveryCycles: 0 } : {}),
|
|
187
|
+
}),
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
stopped: (ctx, binding) => {
|
|
191
|
+
const value = current(ctx, binding.projectKey);
|
|
192
|
+
let state = patch(ctx, binding.projectKey, {
|
|
193
|
+
stopped: true,
|
|
194
|
+
prNumber: value.prNumber ?? binding.iid,
|
|
195
|
+
prUrl:
|
|
196
|
+
value.prUrl ??
|
|
197
|
+
ctx.projects.find((project) => project.key === binding.projectKey)?.externalUrl ??
|
|
198
|
+
null,
|
|
199
|
+
repairPending: false,
|
|
200
|
+
feedbackIds: [],
|
|
201
|
+
pendingEvidence: null,
|
|
202
|
+
delivery: { adjustment: null, feedbackResult: null },
|
|
203
|
+
});
|
|
204
|
+
// Adopt an interrupted legacy repair without replaying its consumed CI evidence or budget.
|
|
205
|
+
const activeKey = selection.activeKey(ctx.state);
|
|
206
|
+
if (
|
|
207
|
+
ctx.process.currentExecution &&
|
|
208
|
+
ctx.process.selectedTurnId === adapter.ids.ciRepair &&
|
|
209
|
+
activeKey &&
|
|
210
|
+
activeKey !== binding.projectKey
|
|
211
|
+
)
|
|
212
|
+
state = patch({ ...ctx, state }, activeKey, { repairPending: true });
|
|
213
|
+
return state;
|
|
214
|
+
},
|
|
215
|
+
terminal: (ctx, binding, observed) =>
|
|
216
|
+
patch(ctx, binding.projectKey, {
|
|
217
|
+
repairPending: false,
|
|
218
|
+
pendingEvidence: null,
|
|
219
|
+
delivery: { terminalPullRequest: gitlabPublicationRequest(observed.mr) },
|
|
220
|
+
}),
|
|
221
|
+
messages(ctx, binding) {
|
|
222
|
+
const value = current(ctx, binding.projectKey);
|
|
223
|
+
const outcome = value.delivery.feedbackResult;
|
|
224
|
+
if (!outcome) return [];
|
|
225
|
+
return [
|
|
226
|
+
...new Map(
|
|
227
|
+
value.feedbackIds.map((note) => {
|
|
228
|
+
if (!note.discussionId) throw new Error("GitLab feedback discussion is missing");
|
|
229
|
+
return [
|
|
230
|
+
note.discussionId,
|
|
231
|
+
{
|
|
232
|
+
discussionId: note.discussionId,
|
|
233
|
+
writeKey: `gitlab:${ctx.process.id}:feedback-reply:${note.id}:${value.headSha}:${outcome.published ? "published" : "no-change"}`,
|
|
234
|
+
body: outcome.published
|
|
235
|
+
? `Addressed in ${value.headSha?.slice(0, 8) ?? "the current revision"}.`
|
|
236
|
+
: `No repository change was needed. ${outcome.summary}`,
|
|
237
|
+
},
|
|
238
|
+
];
|
|
239
|
+
}),
|
|
240
|
+
).values(),
|
|
241
|
+
];
|
|
242
|
+
},
|
|
243
|
+
settled(ctx, binding) {
|
|
244
|
+
const value = current(ctx, binding.projectKey);
|
|
245
|
+
if (!value.delivery.feedbackResult) return ctx.state;
|
|
246
|
+
return patch(ctx, binding.projectKey, {
|
|
247
|
+
conversationCursor: Math.max(
|
|
248
|
+
value.conversationCursor,
|
|
249
|
+
...value.feedbackIds.map((note) => note.id),
|
|
250
|
+
),
|
|
251
|
+
feedbackIds: [],
|
|
252
|
+
delivery: { stage: "awaiting", adjustment: null, feedbackResult: null },
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
cancelled: (ctx) =>
|
|
256
|
+
Promise.resolve(
|
|
257
|
+
adapter.sourceCancelled?.(
|
|
258
|
+
workerContext(ctx, adapter.repositories?.(ctx.params)[0]?.key ?? "repo"),
|
|
259
|
+
) ?? false,
|
|
260
|
+
),
|
|
261
|
+
completion(ctx) {
|
|
262
|
+
const observed = results(ctx);
|
|
263
|
+
if (!observed.every(({ current }) => publicationFinished(current))) return undefined;
|
|
264
|
+
const changed = observed.filter(({ current }) => !current.noChanges);
|
|
265
|
+
return changed.length > 0 &&
|
|
266
|
+
changed.every(({ current }) => !current.delivery.terminalPullRequest?.merged)
|
|
267
|
+
? "aborted"
|
|
268
|
+
: "completed";
|
|
269
|
+
},
|
|
270
|
+
async complete(ctx) {
|
|
271
|
+
const observed = results(ctx);
|
|
272
|
+
if (adapter.reconcileAll)
|
|
273
|
+
await adapter.reconcileAll(
|
|
274
|
+
workerContext(ctx, observed[0].key),
|
|
275
|
+
observed,
|
|
276
|
+
!observed.every(({ current }) => publicationFinished(current)),
|
|
277
|
+
);
|
|
278
|
+
else
|
|
279
|
+
for (const { key, current } of observed)
|
|
280
|
+
if (current.delivery.terminalPullRequest)
|
|
281
|
+
await adapter.reconcileTerminal(
|
|
282
|
+
workerContext(ctx, key),
|
|
283
|
+
current,
|
|
284
|
+
current.delivery.terminalPullRequest,
|
|
285
|
+
);
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type PublicationContext,
|
|
3
|
+
type PublicationEvidence,
|
|
4
|
+
type PublicationParams,
|
|
5
|
+
type PublicationRequest,
|
|
6
|
+
type PublicationSource,
|
|
7
|
+
type PublicationState,
|
|
8
|
+
type RepositoryChangePublicationAdapter,
|
|
9
|
+
resolvePullRequestGitIdentity,
|
|
10
|
+
} from "@leitwerk-dev/coding/repository-change-publication";
|
|
11
|
+
import type { GitLabMergeRequest } from "./client.js";
|
|
12
|
+
import type { GitLabDeliveryObservation, GitLabSourceConfig } from "./external.js";
|
|
13
|
+
import { gitlabMaintenanceSource } from "./maintenance.js";
|
|
14
|
+
import { registerGitLabPublicationMaintenance } from "./publication-maintenance.js";
|
|
15
|
+
|
|
16
|
+
/** @public */
|
|
17
|
+
export interface GitLabPublicationParams extends PublicationParams {
|
|
18
|
+
/** @internal */
|
|
19
|
+
gitlabProfile: string;
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
projectId: number;
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
origin: string;
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
issueNumber?: number | null;
|
|
29
|
+
|
|
30
|
+
/** @internal */
|
|
31
|
+
issueUrl?: string | null;
|
|
32
|
+
|
|
33
|
+
/** @internal */
|
|
34
|
+
triggerLabel?: string | null;
|
|
35
|
+
|
|
36
|
+
/** @internal */
|
|
37
|
+
doneLabel?: string | null;
|
|
38
|
+
}
|
|
39
|
+
/** @internal */
|
|
40
|
+
export const gitlabPublicationRequest = (mr: GitLabMergeRequest): PublicationRequest => ({
|
|
41
|
+
number: mr.iid,
|
|
42
|
+
html_url: mr.web_url,
|
|
43
|
+
merged: mr.state === "merged",
|
|
44
|
+
merge_commit_sha: mr.merge_commit_sha,
|
|
45
|
+
});
|
|
46
|
+
const callFor =
|
|
47
|
+
(ctx: Pick<PublicationContext<unknown>, "callIntegrationTool">) =>
|
|
48
|
+
<T>(name: string, args: Record<string, unknown> = {}) =>
|
|
49
|
+
ctx.callIntegrationTool(name, { projectKey: "repo", ...args }) as Promise<T>;
|
|
50
|
+
|
|
51
|
+
/** Shared observation policy for direct and coordinated GitLab delivery. @internal */
|
|
52
|
+
export function gitlabPublicationSource(
|
|
53
|
+
params: GitLabPublicationParams & {
|
|
54
|
+
/** @internal */
|
|
55
|
+
gitlabOrigin: string;
|
|
56
|
+
},
|
|
57
|
+
current: PublicationState,
|
|
58
|
+
): GitLabSourceConfig {
|
|
59
|
+
if (!current.prNumber || !current.headSha)
|
|
60
|
+
throw new Error("Merge request delivery state is incomplete");
|
|
61
|
+
return {
|
|
62
|
+
profile: params.gitlabProfile,
|
|
63
|
+
origin: params.gitlabOrigin,
|
|
64
|
+
projectId: params.projectId,
|
|
65
|
+
iid: current.prNumber,
|
|
66
|
+
pollInterval: "30s",
|
|
67
|
+
afterKey: current.observationKey,
|
|
68
|
+
feedback: { afterId: current.conversationCursor, quietPeriodMs: 120000 },
|
|
69
|
+
delivery: {
|
|
70
|
+
headSha: current.headSha,
|
|
71
|
+
owner: params.owner,
|
|
72
|
+
repo: params.repo,
|
|
73
|
+
headBranch: params.workBranch,
|
|
74
|
+
baseBranch: params.baseBranch,
|
|
75
|
+
lastConflictKey: current.lastConflictKey,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @public */
|
|
81
|
+
export function gitlabPublicationEvidenceForRequest(
|
|
82
|
+
state: PublicationState,
|
|
83
|
+
event: GitLabDeliveryObservation,
|
|
84
|
+
): PublicationEvidence {
|
|
85
|
+
const current = state;
|
|
86
|
+
if (!current.prNumber || !current.headSha)
|
|
87
|
+
throw new Error("Merge request delivery state is incomplete");
|
|
88
|
+
const key = { observationKey: event.observationKey };
|
|
89
|
+
if (event.mr.iid !== current.prNumber) throw new Error("Stale GitLab merge request evidence");
|
|
90
|
+
if (event.mr.state === "merged" || event.mr.state === "closed")
|
|
91
|
+
return { kind: "terminal", request: gitlabPublicationRequest(event.mr), ...key };
|
|
92
|
+
if (event.mr.sha !== current.headSha) return { kind: "observed", ...key };
|
|
93
|
+
if (event.conflict) return { kind: "conflict", conflict: event.conflict, ...key };
|
|
94
|
+
if (event.feedback?.length)
|
|
95
|
+
return {
|
|
96
|
+
kind: "feedback",
|
|
97
|
+
// Leave concurrent CI evidence available after feedback that needs no code change.
|
|
98
|
+
feedbackIds: event.feedback.map((n) => ({
|
|
99
|
+
kind: "inline",
|
|
100
|
+
id: n.id,
|
|
101
|
+
discussionId: n.discussionId,
|
|
102
|
+
})),
|
|
103
|
+
conversationCursor: Math.max(current.conversationCursor, ...event.feedback.map((n) => n.id)),
|
|
104
|
+
reviewCursor: 0,
|
|
105
|
+
inlineCursor: 0,
|
|
106
|
+
};
|
|
107
|
+
if (event.pipeline?.status === "failed")
|
|
108
|
+
return { kind: "failure", pipeline: { ...event.pipeline, number: event.pipeline.id }, ...key };
|
|
109
|
+
return { kind: "observed", ...key };
|
|
110
|
+
}
|
|
111
|
+
/** Shared GitLab publication and repair operations. @public */
|
|
112
|
+
export function createGitLabPublicationAdapter<P extends Partial<GitLabPublicationParams>>(
|
|
113
|
+
sources: PublicationSource<P>[],
|
|
114
|
+
namespace = "gitlabRepoChange",
|
|
115
|
+
describeRequest = (ctx: PublicationContext<P>) => ({
|
|
116
|
+
/** @internal */
|
|
117
|
+
title: ctx.process.title ?? "Leitwerk change",
|
|
118
|
+
/** @internal */
|
|
119
|
+
body: `${ctx.params.origin === "issue" ? `Implements ${ctx.params.issueUrl}\n\n` : ""}Leitwerk process: ${ctx.process.id}`,
|
|
120
|
+
}),
|
|
121
|
+
): RepositoryChangePublicationAdapter<P> {
|
|
122
|
+
async function finalizeIssue(ctx: PublicationContext<P>, pr: PublicationRequest | null) {
|
|
123
|
+
if (ctx.params.origin !== "issue") return;
|
|
124
|
+
const call = callFor(ctx);
|
|
125
|
+
await call("gitlab_finalize_source_issue", {
|
|
126
|
+
issueNumber: ctx.params.issueNumber,
|
|
127
|
+
merged: pr?.merged ?? false,
|
|
128
|
+
triggerLabel: ctx.params.triggerLabel,
|
|
129
|
+
doneLabel: ctx.params.doneLabel,
|
|
130
|
+
writeKey: `gitlab:${ctx.process.id}:${pr?.merged ? "complete-source-issue" : pr ? "remove-source-trigger" : "no-changes-trigger"}`,
|
|
131
|
+
});
|
|
132
|
+
await call("gitlab_add_issue_comment", {
|
|
133
|
+
issueNumber: ctx.params.issueNumber,
|
|
134
|
+
body: !pr
|
|
135
|
+
? "No repository changes were needed. No merge request was opened."
|
|
136
|
+
: pr.merged
|
|
137
|
+
? `Merged ${pr.html_url}${pr.merge_commit_sha ? ` at ${pr.merge_commit_sha}` : ""}.`
|
|
138
|
+
: `Leitwerk stopped because ${pr.html_url} was closed without merge.`,
|
|
139
|
+
writeKey: `gitlab:${ctx.process.id}:${pr ? `${pr.merged ? "merged" : "closed"}-mr-comment` : "no-changes-comment"}`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const adapter: RepositoryChangePublicationAdapter<P> = {
|
|
143
|
+
maintenance: {
|
|
144
|
+
source: gitlabMaintenanceSource(),
|
|
145
|
+
ownershipTool: "gitlab_assert_maintenance",
|
|
146
|
+
register: (api, process) => registerGitLabPublicationMaintenance(api, process, adapter),
|
|
147
|
+
},
|
|
148
|
+
namespace,
|
|
149
|
+
label: "GitLab MR",
|
|
150
|
+
ids: {
|
|
151
|
+
deliver: "deliver_change",
|
|
152
|
+
feedback: "revise_from_merge_request_feedback",
|
|
153
|
+
ciRepair: "repair_gitlab_pipeline",
|
|
154
|
+
operator: "ci_operator_action",
|
|
155
|
+
},
|
|
156
|
+
sources,
|
|
157
|
+
tools: {
|
|
158
|
+
delivery: [
|
|
159
|
+
"gitlab_assert_maintenance",
|
|
160
|
+
"gitlab_ensure_merge_request",
|
|
161
|
+
"gitlab_observe_merge_request",
|
|
162
|
+
"gitlab_comment",
|
|
163
|
+
"gitlab_reply",
|
|
164
|
+
"gitlab_get_identity",
|
|
165
|
+
"gitlab_acknowledge_feedback",
|
|
166
|
+
"gitlab_add_issue_comment",
|
|
167
|
+
"gitlab_finalize_source_issue",
|
|
168
|
+
],
|
|
169
|
+
feedback: [
|
|
170
|
+
"gitlab_observe_merge_request",
|
|
171
|
+
"gitlab_get_changes",
|
|
172
|
+
"gitlab_list_merge_request_feedback",
|
|
173
|
+
],
|
|
174
|
+
ci: ["gitlab_observe_merge_request", "gitlab_list_failed_jobs", "gitlab_get_job_trace"],
|
|
175
|
+
},
|
|
176
|
+
identity: (ctx) => {
|
|
177
|
+
if (!ctx.params.gitlabProfile) throw new Error("GitLab repository binding is not resolved");
|
|
178
|
+
return resolvePullRequestGitIdentity(ctx, "gitlab", ctx.params.gitlabProfile, false);
|
|
179
|
+
},
|
|
180
|
+
async ensureRequest(ctx) {
|
|
181
|
+
return gitlabPublicationRequest(
|
|
182
|
+
await callFor(ctx)<GitLabMergeRequest>("gitlab_ensure_merge_request", describeRequest(ctx)),
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
async observeTerminal(ctx, current) {
|
|
186
|
+
const project = ctx.projects.find((project) => project.key === ctx.repo.get("repo").key);
|
|
187
|
+
if (!current.prNumber && !(project?.metadata?.gitlab as { iid?: number } | undefined)?.iid)
|
|
188
|
+
return null;
|
|
189
|
+
const { mr } = await callFor(ctx)<GitLabDeliveryObservation>("gitlab_observe_merge_request");
|
|
190
|
+
return mr.state === "opened" ? null : gitlabPublicationRequest(mr);
|
|
191
|
+
},
|
|
192
|
+
reconcileTerminal: (ctx, _current, pr) => finalizeIssue(ctx, pr),
|
|
193
|
+
unchanged: (ctx) => finalizeIssue(ctx, null),
|
|
194
|
+
async linkIssue(ctx, current) {
|
|
195
|
+
if (ctx.params.origin === "issue")
|
|
196
|
+
await callFor(ctx)("gitlab_add_issue_comment", {
|
|
197
|
+
issueNumber: ctx.params.issueNumber,
|
|
198
|
+
body: `Leitwerk opened merge request ${current.prUrl}.`,
|
|
199
|
+
writeKey: `gitlab:${ctx.process.id}:source-mr-link:${current.prNumber}`,
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
async acknowledge(ctx, current) {
|
|
203
|
+
for (const feedback of current.feedbackIds)
|
|
204
|
+
await callFor(ctx)("gitlab_acknowledge_feedback", { noteId: feedback.id });
|
|
205
|
+
},
|
|
206
|
+
async reply(ctx, current) {
|
|
207
|
+
for (const feedback of current.feedbackIds) {
|
|
208
|
+
if (!feedback.discussionId) throw new Error("GitLab feedback discussion is missing");
|
|
209
|
+
await callFor(ctx)("gitlab_reply", {
|
|
210
|
+
discussionId: feedback.discussionId,
|
|
211
|
+
body: `Addressed in ${current.headSha?.slice(0, 8) ?? "the current revision"}.`,
|
|
212
|
+
writeKey: `gitlab:${ctx.process.id}:feedback-reply:${feedback.id}:${current.headSha}`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
async head(ctx) {
|
|
217
|
+
return (await callFor(ctx)<GitLabDeliveryObservation>("gitlab_observe_merge_request")).mr.sha;
|
|
218
|
+
},
|
|
219
|
+
async afterRebase(ctx, current) {
|
|
220
|
+
await callFor(ctx)("gitlab_comment", {
|
|
221
|
+
body: `Merge conflict repair completed at ${current.headSha}; base ${current.conflict?.baseSha}.`,
|
|
222
|
+
writeKey: `rebase:${ctx.process.id}:${current.lastConflictKey}:${current.headSha}`,
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
commitMessage: (kind) =>
|
|
226
|
+
kind === "feedback" ? "fix: address GitLab review feedback" : "fix: repair GitLab pipeline",
|
|
227
|
+
prompt(kind, current) {
|
|
228
|
+
return kind === "feedback"
|
|
229
|
+
? `Address the unseen GitLab feedback for MR !${current.prNumber}: ${JSON.stringify(current.feedbackIds)}. Read the full discussions with gitlab_list_merge_request_feedback, inspect the checkout, and make only justified changes. Call changes_ready when ready to publish, no_changes with an explicit diagnosis when no change is needed, or cannot_repair for operator action.`
|
|
230
|
+
: `Diagnose GitLab pipeline ${current.pipeline?.number} for head ${current.headSha}. Read gitlab_list_failed_jobs with pipelineId and bounded gitlab_get_job_trace before making repository repairs. Call changes_ready to publish, no_changes with a justified diagnosis, or cannot_repair when operator action is needed.`;
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
return adapter;
|
|
234
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { GitLabIntegration } from "./capability.js";
|
|
2
|
+
import type { GitLabProject } from "./client.js";
|
|
3
|
+
|
|
4
|
+
/** Server-owned repository metadata for Settings. @public */
|
|
5
|
+
export interface GitLabRepositoryCatalog {
|
|
6
|
+
/** Reuse the last successful scan, loading it once when empty. @public */
|
|
7
|
+
list(profile: string): Promise<readonly GitLabProject[]>;
|
|
8
|
+
/** Search a cached scan or one remote page; repeated queries share cached results. @public */
|
|
9
|
+
search(profile: string, query: string): Promise<readonly GitLabProject[]>;
|
|
10
|
+
/** Read known metadata without making a remote request. @public */
|
|
11
|
+
peek(profile: string, projectId: number): GitLabProject | undefined;
|
|
12
|
+
/** Replace the catalog only after a complete successful scan. @public */
|
|
13
|
+
refresh(profile: string): Promise<readonly GitLabProject[]>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
export function createGitLabRepositoryCatalog(
|
|
18
|
+
integration: GitLabIntegration,
|
|
19
|
+
): GitLabRepositoryCatalog {
|
|
20
|
+
const profiles = new Map<
|
|
21
|
+
string,
|
|
22
|
+
{
|
|
23
|
+
origin: string;
|
|
24
|
+
projects?: readonly GitLabProject[];
|
|
25
|
+
pending?: Promise<readonly GitLabProject[]>;
|
|
26
|
+
known: Map<number, GitLabProject>;
|
|
27
|
+
searches: Map<string, Promise<readonly GitLabProject[]>>;
|
|
28
|
+
}
|
|
29
|
+
>();
|
|
30
|
+
function entry(profile: string) {
|
|
31
|
+
const client = integration.client(profile);
|
|
32
|
+
let cached = profiles.get(profile);
|
|
33
|
+
if (!cached || cached.origin !== client.baseUrl) {
|
|
34
|
+
cached = { origin: client.baseUrl, known: new Map(), searches: new Map() };
|
|
35
|
+
profiles.set(profile, cached);
|
|
36
|
+
}
|
|
37
|
+
return { client, cached };
|
|
38
|
+
}
|
|
39
|
+
async function refresh(profile: string) {
|
|
40
|
+
const { client, cached } = entry(profile);
|
|
41
|
+
if (cached.pending) return cached.pending;
|
|
42
|
+
const scan = Promise.resolve()
|
|
43
|
+
.then(() => client.listProjects())
|
|
44
|
+
.then((projects) => {
|
|
45
|
+
cached.projects = structuredClone(projects);
|
|
46
|
+
cached.searches.clear();
|
|
47
|
+
cached.known.clear();
|
|
48
|
+
return cached.projects;
|
|
49
|
+
});
|
|
50
|
+
cached.pending = scan;
|
|
51
|
+
try {
|
|
52
|
+
return await scan;
|
|
53
|
+
} finally {
|
|
54
|
+
cached.pending = undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
async search(profile, text) {
|
|
59
|
+
const query = text.trim().toLowerCase();
|
|
60
|
+
if (query.length < 2) return [];
|
|
61
|
+
const { client, cached } = entry(profile);
|
|
62
|
+
if (cached.projects)
|
|
63
|
+
return cached.projects
|
|
64
|
+
.filter((project) => project.path_with_namespace.toLowerCase().includes(query))
|
|
65
|
+
.slice(0, 100);
|
|
66
|
+
const result = cached.searches.get(query);
|
|
67
|
+
if (result) return result;
|
|
68
|
+
const scan = Promise.resolve()
|
|
69
|
+
.then(() => client.searchProjects(query))
|
|
70
|
+
.then((projects) => {
|
|
71
|
+
const snapshot = structuredClone(projects);
|
|
72
|
+
if (cached.searches.get(query) === scan)
|
|
73
|
+
for (const project of snapshot) cached.known.set(project.id, project);
|
|
74
|
+
return snapshot;
|
|
75
|
+
})
|
|
76
|
+
.catch((error) => {
|
|
77
|
+
if (cached.searches.get(query) === scan) cached.searches.delete(query);
|
|
78
|
+
throw error;
|
|
79
|
+
});
|
|
80
|
+
cached.searches.set(query, scan);
|
|
81
|
+
return scan;
|
|
82
|
+
},
|
|
83
|
+
peek(profile, projectId) {
|
|
84
|
+
const { cached } = entry(profile);
|
|
85
|
+
return cached.projects
|
|
86
|
+
? cached.projects.find((project) => project.id === projectId)
|
|
87
|
+
: cached.known.get(projectId);
|
|
88
|
+
},
|
|
89
|
+
async list(profile) {
|
|
90
|
+
return entry(profile).cached.projects ?? refresh(profile);
|
|
91
|
+
},
|
|
92
|
+
refresh,
|
|
93
|
+
};
|
|
94
|
+
}
|