@leitwerk-dev/coding 0.2.1 → 0.3.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.
Files changed (85) hide show
  1. package/README.md +51 -12
  2. package/dist/actions.d.ts +0 -5
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/auto-work-branch-internal.d.ts +2 -18
  5. package/dist/auto-work-branch-internal.d.ts.map +1 -1
  6. package/dist/auto-work-branch.d.ts +1 -2
  7. package/dist/auto-work-branch.d.ts.map +1 -1
  8. package/dist/auto-work-branch.js +4 -102
  9. package/dist/auto-work-branch.js.map +1 -1
  10. package/dist/chunk-6E5VQPC4.js +28 -0
  11. package/dist/chunk-6E5VQPC4.js.map +1 -0
  12. package/dist/chunk-GO44KCUS.js +154 -0
  13. package/dist/chunk-GO44KCUS.js.map +1 -0
  14. package/dist/chunk-KFELB7ZC.js +70 -0
  15. package/dist/chunk-KFELB7ZC.js.map +1 -0
  16. package/dist/finalization-git-internal.d.ts +6 -45
  17. package/dist/finalization-git-internal.d.ts.map +1 -1
  18. package/dist/finalization-git.js +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +48 -91
  22. package/dist/index.js.map +1 -1
  23. package/dist/profile-bindings-internal.d.ts +3 -0
  24. package/dist/profile-bindings-internal.d.ts.map +1 -0
  25. package/dist/pull-request-publication-internal.d.ts +73 -0
  26. package/dist/pull-request-publication-internal.d.ts.map +1 -0
  27. package/dist/repository-change-launch-internal-Dd-AONUs.d.ts +67 -0
  28. package/dist/repository-change-launch-internal.d.ts +31 -82
  29. package/dist/repository-change-launch-internal.d.ts.map +1 -1
  30. package/dist/repository-change-launch.d.ts +4 -2
  31. package/dist/repository-change-launch.d.ts.map +1 -1
  32. package/dist/repository-change-launch.js +205 -133
  33. package/dist/repository-change-launch.js.map +1 -1
  34. package/dist/repository-change-process.d.ts +7 -19
  35. package/dist/repository-change-process.d.ts.map +1 -1
  36. package/dist/repository-change-publication.d.ts +229 -0
  37. package/dist/repository-change-publication.d.ts.map +1 -0
  38. package/dist/repository-change-publication.js +770 -0
  39. package/dist/repository-change-publication.js.map +1 -0
  40. package/dist/repository-change-state-internal.d.ts +2 -11
  41. package/dist/repository-change-state-internal.d.ts.map +1 -1
  42. package/dist/repository-change-state.d.ts +0 -1
  43. package/dist/repository-change-state.d.ts.map +1 -1
  44. package/dist/repository-change-state.js +0 -7
  45. package/dist/repository-change-ui-launcher-internal.d.ts +53 -0
  46. package/dist/repository-change-ui-launcher-internal.d.ts.map +1 -0
  47. package/dist/repository-rebase/git.d.ts +31 -0
  48. package/dist/repository-rebase/git.d.ts.map +1 -0
  49. package/dist/repository-rebase/index.d.ts +42 -0
  50. package/dist/repository-rebase/index.d.ts.map +1 -0
  51. package/dist/repository-rebase/index.js +16 -0
  52. package/dist/repository-rebase/policy.d.ts +37 -0
  53. package/dist/repository-rebase/policy.d.ts.map +1 -0
  54. package/dist/repository-rebase/prompt.d.ts +3 -0
  55. package/dist/repository-rebase/prompt.d.ts.map +1 -0
  56. package/package.json +14 -4
  57. package/src/actions.ts +0 -13
  58. package/src/auto-work-branch-internal.ts +22 -130
  59. package/src/auto-work-branch.ts +1 -7
  60. package/src/finalization-git-internal.ts +60 -697
  61. package/src/index.ts +0 -1
  62. package/src/profile-bindings-internal.ts +39 -0
  63. package/src/pull-request-publication-internal.ts +286 -0
  64. package/src/repository-change-launch-internal.ts +79 -267
  65. package/src/repository-change-launch.ts +10 -6
  66. package/src/repository-change-process.ts +8 -126
  67. package/src/repository-change-publication.ts +673 -0
  68. package/src/repository-change-state-internal.ts +4 -34
  69. package/src/repository-change-state.ts +0 -1
  70. package/src/repository-change-ui-launcher-internal.ts +225 -0
  71. package/src/repository-rebase/git.ts +219 -0
  72. package/src/repository-rebase/index.ts +144 -0
  73. package/src/repository-rebase/policy.ts +75 -0
  74. package/src/repository-rebase/prompt.ts +2 -0
  75. package/dist/chunk-HUVXPZSF.js +0 -61
  76. package/dist/chunk-HUVXPZSF.js.map +0 -1
  77. package/dist/chunk-MY6XFGHM.js +0 -614
  78. package/dist/chunk-MY6XFGHM.js.map +0 -1
  79. package/dist/chunk-OERAYSVI.js +0 -27
  80. package/dist/chunk-OERAYSVI.js.map +0 -1
  81. package/dist/turns/resolve-merge-conflict.d.ts +0 -4
  82. package/dist/turns/resolve-merge-conflict.d.ts.map +0 -1
  83. package/dist/turns/resolve-merge-conflict.js +0 -8
  84. package/src/turns/resolve-merge-conflict.ts +0 -26
  85. /package/dist/{turns/resolve-merge-conflict.js.map → repository-rebase/index.js.map} +0 -0
package/src/index.ts CHANGED
@@ -14,7 +14,6 @@ export default extension;
14
14
  export {
15
15
  acceptReviewForm,
16
16
  codingActionIds,
17
- createFinalizeChangeForm,
18
17
  requestReviewChangesForm,
19
18
  requestRevisionForm,
20
19
  } from "./actions.js";
@@ -0,0 +1,39 @@
1
+ function record(value: unknown, label: string): Record<string, unknown> {
2
+ if (!value || typeof value !== "object" || Array.isArray(value))
3
+ throw new Error(`${label} must be an object`);
4
+ return value as Record<string, unknown>;
5
+ }
6
+
7
+ /** Strict profile wiring shared by repository-change extensions. @internal */
8
+ export function parseRepositoryProfileBindings<K extends string>(
9
+ raw: unknown,
10
+ extensionId: string,
11
+ fields: Record<K, string>,
12
+ ): Readonly<Record<string, Readonly<Record<K, string>>>> {
13
+ const config = raw === undefined ? {} : record(raw, `${extensionId} configuration`);
14
+ if (config.profile_bindings === undefined) return Object.freeze({});
15
+ const mappings = record(config.profile_bindings, "profile_bindings");
16
+ return Object.freeze(
17
+ Object.fromEntries(
18
+ Object.entries(mappings).map(([profile, value]) => {
19
+ if (!profile.trim() || profile.trim() !== profile)
20
+ throw new Error(
21
+ "profile_bindings keys must be non-empty profile names without surrounding whitespace",
22
+ );
23
+ const mapping = record(value, `profile_bindings.${profile}`);
24
+ for (const key of Object.keys(mapping))
25
+ if (!Object.values(fields).includes(key))
26
+ throw new Error(`Unknown profile_bindings.${profile}.${key}`);
27
+ const binding = Object.fromEntries(
28
+ Object.entries<string>(fields).map(([field, key]) => {
29
+ const value = mapping[key];
30
+ if (value !== undefined && (typeof value !== "string" || !value.trim()))
31
+ throw new Error(`profile_bindings.${profile}.${key} must be a non-empty string`);
32
+ return [field, value === undefined ? profile : (value as string).trim()];
33
+ }),
34
+ ) as Record<K, string>;
35
+ return [profile, Object.freeze(binding)];
36
+ }),
37
+ ),
38
+ );
39
+ }
@@ -0,0 +1,286 @@
1
+ import type {
2
+ defineExternalActionSource,
3
+ RepositoryFeedbackSourceConfig,
4
+ RepositoryIssueCancelledSourceConfig,
5
+ RepositoryPullRequestSourceConfig,
6
+ } from "@leitwerk-dev/process-sdk";
7
+ import type { GitIdentity } from "./finalization-git.js";
8
+ import type { RepositoryIssueOriginParams } from "./repository-change-launch-internal.js";
9
+ import type {
10
+ PublicationContext,
11
+ PublicationEvidence,
12
+ PublicationParams,
13
+ PublicationRequest,
14
+ PublicationSource,
15
+ PublicationState,
16
+ RepositoryChangePublicationAdapter,
17
+ } from "./repository-change-publication.js";
18
+ import {
19
+ publicationObject as object,
20
+ readPublicationState,
21
+ } from "./repository-change-publication.js";
22
+ import type { RepositoryChangeState } from "./repository-change-state.js";
23
+
24
+ const callFor =
25
+ <P>(ctx: PublicationContext<P>, provider: string) =>
26
+ <T>(name: string, args: Record<string, unknown>) =>
27
+ ctx.callIntegrationTool(`${provider}_${name}`, { projectKey: "repo", ...args }) as Promise<T>;
28
+
29
+ /** @internal */
30
+ export async function resolvePullRequestGitIdentity<P>(
31
+ ctx: PublicationContext<P>,
32
+ provider: string,
33
+ profile: string,
34
+ ): Promise<GitIdentity> {
35
+ const project = ctx.projects.find((candidate) => candidate.key === "repo");
36
+ const identity = object(object(project?.metadata)["leitwerk.gitIdentity"]);
37
+ if (
38
+ identity.provider === provider &&
39
+ identity.profile === profile &&
40
+ [identity.login, identity.name, identity.email].every(
41
+ (value) => typeof value === "string" && value.trim(),
42
+ )
43
+ )
44
+ return identity as unknown as GitIdentity;
45
+ return callFor(ctx, provider)<GitIdentity>("resolve_git_identity", {});
46
+ }
47
+
48
+ /** @internal */
49
+ export function readPullRequestFeedback(
50
+ current: PublicationState,
51
+ event: unknown,
52
+ ): Extract<PublicationEvidence, { kind: "feedback" }> {
53
+ const value = object(event);
54
+ const cursors = object(value.cursors);
55
+ return {
56
+ kind: "feedback",
57
+ conversationCursor: Number(cursors.conversationCursor ?? current.conversationCursor),
58
+ reviewCursor: Number(cursors.reviewCursor ?? current.reviewCursor),
59
+ inlineCursor: Number(cursors.inlineCursor ?? current.inlineCursor),
60
+ feedbackIds: (value.feedbackIds ?? []) as PublicationState["feedbackIds"],
61
+ };
62
+ }
63
+
64
+ /** @internal */
65
+ export function reconcilePullRequestSourceIssue<P extends PublicationParams>(
66
+ provider: string,
67
+ labelKey: "id" | "name",
68
+ issueOrigin: (params: P) =>
69
+ | {
70
+ /** @internal */
71
+ issueNumber: number;
72
+ /** @internal */
73
+ triggerLabel: string;
74
+ /** @internal */
75
+ doneLabel: string;
76
+ }
77
+ | undefined,
78
+ ): RepositoryChangePublicationAdapter<P>["reconcileTerminal"] {
79
+ return async (ctx, _current, pr) => {
80
+ const params = issueOrigin(ctx.params);
81
+ if (!params) return;
82
+ const call = callFor(ctx, provider);
83
+ const issue = await call<{ labels: Array<{ id: number; name: string }> }>("get_issue", {
84
+ issueNumber: params.issueNumber,
85
+ });
86
+ let labels = issue.labels
87
+ .filter((label) => label.name !== params.triggerLabel)
88
+ .map((label) => label[labelKey]);
89
+ if (pr.merged) {
90
+ const done = await call<{ id: number; name: string }>("ensure_label", {
91
+ name: params.doneLabel,
92
+ });
93
+ labels = [...new Set([...labels, done[labelKey]])];
94
+ }
95
+ await call("update_issue", {
96
+ issueNumber: params.issueNumber,
97
+ patch: { labels, ...(pr.merged ? { state: "closed" } : {}) },
98
+ writeKey: `${provider}:${ctx.process.id}:${pr.merged ? "complete-source-issue" : "remove-source-trigger"}`,
99
+ });
100
+ await call("add_issue_comment", {
101
+ issueNumber: params.issueNumber,
102
+ body: pr.merged
103
+ ? `Merged ${pr.html_url}${pr.merge_commit_sha ? ` at ${pr.merge_commit_sha}` : ""}.`
104
+ : `Leitwerk stopped because ${pr.html_url} was closed without merge.`,
105
+ writeKey: `${provider}:${ctx.process.id}:${pr.merged ? "merged" : "closed"}-pr-comment`,
106
+ });
107
+ };
108
+ }
109
+
110
+ /** @internal */
111
+ type SourceFactory<C> = ReturnType<typeof defineExternalActionSource<C>>;
112
+
113
+ /** Shared wiring for providers implementing the pull-request source protocol. @internal */
114
+ export function pullRequestPublicationSources<P extends PublicationParams>(
115
+ provider: string,
116
+ label: string,
117
+ external: {
118
+ /** @internal */
119
+ pullRequestFeedback: SourceFactory<
120
+ Omit<RepositoryFeedbackSourceConfig, "disabled" | "terminalOutcome">
121
+ >;
122
+ /** @internal */
123
+ pullRequestTerminal: SourceFactory<Omit<RepositoryPullRequestSourceConfig, "disabled">>;
124
+ /** @internal */
125
+ issueCancelled: SourceFactory<RepositoryIssueCancelledSourceConfig>;
126
+ },
127
+ profile: (params: P) => string,
128
+ issueOrigin: (params: P) => RepositoryIssueOriginParams | undefined,
129
+ ) {
130
+ const remote = (state: RepositoryChangeState) =>
131
+ readPublicationState(state, `${provider}RepoChange`);
132
+ /** @internal */
133
+ function requirePr(state: RepositoryChangeState) {
134
+ const value = remote(state);
135
+ if (!value.headSha || !value.prNumber || !value.prUrl)
136
+ throw new Error("Pull request delivery state is incomplete");
137
+ return {
138
+ ...value,
139
+ /** @internal */ headSha: value.headSha,
140
+ /** @internal */ prNumber: value.prNumber,
141
+ /** @internal */ prUrl: value.prUrl,
142
+ };
143
+ }
144
+ const feedback: PublicationSource<P> = {
145
+ id: `${provider}_feedback`,
146
+ kind: "feedback",
147
+ label: `${label} pull request feedback`,
148
+ source: external.pullRequestFeedback<P, RepositoryChangeState>(({ params, state }) => ({
149
+ profile: profile(params),
150
+ owner: params.owner,
151
+ repo: params.repo,
152
+ prNumber: requirePr(state).prNumber,
153
+ conversationCursor: remote(state).conversationCursor,
154
+ reviewCursor: remote(state).reviewCursor,
155
+ inlineCursor: remote(state).inlineCursor,
156
+ quietPeriodMs: 120000,
157
+ pollInterval: "30s",
158
+ })),
159
+ read: ({ state, event }) => readPullRequestFeedback(remote(state), event),
160
+ };
161
+ const terminal: PublicationSource<P>[] = (["merged", "closed"] as const).map(
162
+ (terminalOutcome) => ({
163
+ id: `${provider}_pr_${terminalOutcome}`,
164
+ kind: "terminal",
165
+ label: `${label} pull request ${terminalOutcome}`,
166
+ source: external.pullRequestTerminal<P, RepositoryChangeState>(({ params, state }) => ({
167
+ profile: profile(params),
168
+ owner: params.owner,
169
+ repo: params.repo,
170
+ prNumber: requirePr(state).prNumber,
171
+ terminalOutcome,
172
+ pollInterval: "30s",
173
+ })),
174
+ read: ({ event }) => ({
175
+ kind: "terminal",
176
+ request: object(event).pullRequest as PublicationRequest,
177
+ }),
178
+ }),
179
+ );
180
+ const cancelled: PublicationSource<P> = {
181
+ id: "source_cancelled",
182
+ kind: "cancelled",
183
+ label: "Source issue cancelled",
184
+ enabled: (params) => issueOrigin(params) !== undefined,
185
+ source: external.issueCancelled<P, RepositoryChangeState>(({ params }) => {
186
+ const issue = issueOrigin(params);
187
+ if (!issue) throw new Error(`${label} source issue metadata is unavailable`);
188
+ return {
189
+ profile: profile(params),
190
+ owner: params.owner,
191
+ repo: params.repo,
192
+ issueNumber: issue.issueNumber,
193
+ triggerLabel: issue.triggerLabel,
194
+ pollInterval: "30s",
195
+ };
196
+ }),
197
+ read: () => ({ kind: "cancelled" }),
198
+ };
199
+ return {
200
+ /** @internal */ requirePr,
201
+ /** @internal */ feedback,
202
+ /** @internal */ terminal,
203
+ /** @internal */ cancelled,
204
+ };
205
+ }
206
+
207
+ /** Callbacks for providers implementing the shared pull-request tool protocol. @internal */
208
+ export function pullRequestPublicationCallbacks<P extends PublicationParams>(
209
+ provider: string,
210
+ label: string,
211
+ issueOrigin: (params: P) =>
212
+ | {
213
+ /** @internal */
214
+ issueNumber: number;
215
+ /** @internal */
216
+ issueUrl: string;
217
+ }
218
+ | undefined,
219
+ ): Pick<
220
+ RepositoryChangePublicationAdapter<P>,
221
+ "ensureRequest" | "linkIssue" | "reply" | "acknowledge" | "head" | "afterRebase"
222
+ > {
223
+ const call = (ctx: PublicationContext<P>) => callFor(ctx, provider);
224
+ return {
225
+ async ensureRequest(ctx) {
226
+ const project = ctx.repo.get("repo");
227
+ const issue = issueOrigin(ctx.params);
228
+ const pr = await call(ctx)<PublicationRequest>("ensure_pull_request", {
229
+ title: ctx.process.title ?? (issue ? `Issue #${issue.issueNumber}` : "Leitwerk change"),
230
+ body: issue
231
+ ? `Implements ${issue.issueUrl}\n\nLeitwerk process: ${ctx.process.id}`
232
+ : `Leitwerk process: ${ctx.process.id}`,
233
+ head: project.workBranch,
234
+ base: project.baseBranch,
235
+ });
236
+ if (!pr) throw new Error(`${label} pull request could not be created or found`);
237
+ return pr;
238
+ },
239
+ async linkIssue(ctx, current) {
240
+ const issue = issueOrigin(ctx.params);
241
+ if (issue)
242
+ await call(ctx)("add_issue_comment", {
243
+ issueNumber: issue.issueNumber,
244
+ body: `Leitwerk opened pull request ${current.prUrl}.`,
245
+ writeKey: `${provider}:${ctx.process.id}:source-pr-link:${current.prNumber}`,
246
+ });
247
+ },
248
+ async reply(ctx, current) {
249
+ const invoke = call(ctx);
250
+ for (const feedback of current.feedbackIds)
251
+ await invoke("reply_to_pull_request_feedback", {
252
+ pullRequestNumber: current.prNumber,
253
+ feedbackKind: feedback.kind,
254
+ feedbackId: feedback.id,
255
+ body: `Addressed in ${current.headSha?.slice(0, 8) ?? "the current revision"}.`,
256
+ writeKey: `${provider}:${ctx.process.id}:feedback-reply:${feedback.kind}:${feedback.id}:${current.headSha}`,
257
+ });
258
+ },
259
+ async acknowledge(ctx, current) {
260
+ const invoke = call(ctx);
261
+ for (const feedback of current.feedbackIds) {
262
+ if (feedback.kind === "review") continue;
263
+ await invoke("add_pull_request_feedback_reaction", {
264
+ pullRequestNumber: current.prNumber,
265
+ feedbackKind: feedback.kind,
266
+ feedbackId: feedback.id,
267
+ writeKey: `${provider}:${ctx.process.id}:feedback-eyes:${feedback.kind}:${feedback.id}`,
268
+ });
269
+ }
270
+ },
271
+ async head(ctx, current) {
272
+ return (
273
+ await call(ctx)<{ head: { sha: string } }>("get_pull_request", {
274
+ pullRequestNumber: current.prNumber,
275
+ })
276
+ ).head.sha;
277
+ },
278
+ async afterRebase(ctx, current) {
279
+ await call(ctx)("add_pull_request_comment", {
280
+ pullRequestNumber: current.prNumber,
281
+ body: `Merge conflict repair completed at ${current.headSha}; base ${current.conflict?.baseSha}.`,
282
+ writeKey: `rebase:${ctx.process.id}:${current.lastConflictKey}:${current.headSha}`,
283
+ });
284
+ },
285
+ };
286
+ }