@defrex/autobuild 1.11.1 → 1.13.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 +1 -1
- package/SPEC.md +601 -203
- package/bin/ab-build-runner.ts +8 -0
- package/docs/answering-blocked-builds.md +32 -2
- package/docs/architecture.md +188 -57
- package/docs/assets/headline-wide.png +0 -0
- package/docs/cli.md +1 -1
- package/docs/configuration.md +120 -23
- package/docs/dispatcher.md +19 -8
- package/docs/integration-overview.md +1 -1
- package/docs/integration-tree.md +33 -18
- package/docs/merge-policy.md +7 -2
- package/docs/operating.md +5 -3
- package/docs/operator-api.md +48 -25
- package/docs/operator-surfaces.md +39 -3
- package/docs/optimization-nodes.md +10 -8
- package/docs/plugin-authoring.md +46 -18
- package/docs/remote-store-protocol.md +51 -12
- package/docs/remote-ticket-protocol.md +85 -38
- package/docs/restacking.md +48 -3
- package/docs/setup.md +2 -1
- package/docs/ticket-migration.md +2 -2
- package/docs/ticket-sources.md +23 -8
- package/docs/tickets.md +4 -4
- package/docs/troubleshooting.md +17 -2
- package/docs/verification.md +3 -1
- package/package.json +3 -3
- package/packages/core/src/cli/ambient-errors.ts +17 -0
- package/packages/core/src/cli/bounded-io.ts +174 -0
- package/packages/core/src/cli/build-control.ts +72 -6
- package/packages/core/src/cli/dashboard/actions.ts +4 -0
- package/packages/core/src/cli/dashboard/integration-row.ts +43 -19
- package/packages/core/src/cli/dashboard/model.ts +93 -29
- package/packages/core/src/cli/dashboard/poll.ts +231 -34
- package/packages/core/src/cli/dashboard/queue.ts +21 -10
- package/packages/core/src/cli/dashboard/render.ts +14 -37
- package/packages/core/src/cli/dashboard/row-text.ts +26 -8
- package/packages/core/src/cli/dashboard/tone-ansi.ts +39 -0
- package/packages/core/src/cli/dashboard/vocabulary.ts +165 -20
- package/packages/core/src/cli/dispatch-frontend.ts +2 -1
- package/packages/core/src/cli/dispatch-handoff.ts +41 -10
- package/packages/core/src/cli/dispatch-launch-contract.ts +5 -0
- package/packages/core/src/cli/dispatch-launcher-cadence.ts +80 -0
- package/packages/core/src/cli/dispatch-launcher.ts +9 -10
- package/packages/core/src/cli/dispatch-preflight.ts +12 -3
- package/packages/core/src/cli/dispatch-release-observer.ts +53 -0
- package/packages/core/src/cli/dispatch-runtime-cache.ts +204 -82
- package/packages/core/src/cli/dispatch-runtime-dependencies.ts +250 -43
- package/packages/core/src/cli/dispatch-tickets.ts +33 -12
- package/packages/core/src/cli/dispatch-upgrade-install.ts +36 -16
- package/packages/core/src/cli/dispatch-upgrade-source.ts +8 -2
- package/packages/core/src/cli/dispatch-yield-observer.ts +82 -0
- package/packages/core/src/cli/dispatch.ts +595 -149
- package/packages/core/src/cli/env.ts +2 -15
- package/packages/core/src/cli/help.ts +43 -28
- package/packages/core/src/cli/main.ts +11 -7
- package/packages/core/src/cli/production-store.ts +102 -0
- package/packages/core/src/cli/publication-settlement.ts +4 -0
- package/packages/core/src/cli/remote-poll.ts +22 -5
- package/packages/core/src/cli/repo-state.ts +2 -3
- package/packages/core/src/cli/repository-status.ts +44 -28
- package/packages/core/src/cli/sessionless-store.ts +26 -0
- package/packages/core/src/cli/status.ts +188 -47
- package/packages/core/src/cli/store-opening.ts +26 -57
- package/packages/core/src/cli/store-ref.ts +6 -1
- package/packages/core/src/cli/ticket-start.ts +3 -3
- package/packages/core/src/cli/ticket.ts +12 -11
- package/packages/core/src/cli/wait.ts +8 -28
- package/packages/core/src/cli/watch.ts +113 -50
- package/packages/core/src/config/reference-metadata.ts +7 -1
- package/packages/core/src/config/reference.ts +5 -2
- package/packages/core/src/config/schema.ts +51 -16
- package/packages/core/src/distribution-packaging.ts +156 -18
- package/packages/core/src/events/catalog-identity.ts +28 -0
- package/packages/core/src/events/catalog.ts +3 -9
- package/packages/core/src/events/compatibility.ts +2 -30
- package/packages/core/src/events/payloads.ts +85 -14
- package/packages/core/src/events/policy.ts +3 -0
- package/packages/core/src/events/repository.ts +15 -4
- package/packages/core/src/events/sessions.ts +1 -1
- package/packages/core/src/events/validation-error.ts +12 -0
- package/packages/core/src/kernel/__snapshots__/reducer-accumulator-shapes.json +36 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/0a10010ee5cf2e47.json +577 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/227701361be6d1e0.json +490 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/3064e337cc75f8d6.json +310 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/76e5043bae7a80ab.json +86 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/87a917802870bbed.json +87 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/9d85616e5b9e24c1.json +500 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/a1e90a969d32a367.json +307 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/c7c94fbe7f63cf07.json +84 -0
- package/packages/core/src/kernel/__snapshots__/reducer-identity-records/da3bc91c49211eab.json +310 -0
- package/packages/core/src/kernel/auto-merge-default.ts +2 -1
- package/packages/core/src/kernel/capacity-build.ts +21 -0
- package/packages/core/src/kernel/clean-restack-policy.ts +40 -0
- package/packages/core/src/kernel/discard-in-flight.ts +13 -0
- package/packages/core/src/kernel/dispatch-capacity.ts +578 -235
- package/packages/core/src/kernel/done-escalations.ts +269 -0
- package/packages/core/src/kernel/engine.ts +30 -17
- package/packages/core/src/kernel/integration-landing.ts +24 -4
- package/packages/core/src/kernel/integration-stack.ts +39 -0
- package/packages/core/src/kernel/janitor-failure.ts +20 -1
- package/packages/core/src/kernel/log-index-value.ts +3 -0
- package/packages/core/src/kernel/log-index.ts +41 -2
- package/packages/core/src/kernel/measurement.ts +24 -0
- package/packages/core/src/kernel/open-pr-backpressure.ts +3 -3
- package/packages/core/src/kernel/read-facts.ts +8 -370
- package/packages/core/src/kernel/reducer-registry.ts +43 -125
- package/packages/core/src/kernel/reducer-revisions.ts +18 -0
- package/packages/core/src/kernel/reducer-value-entry.ts +98 -0
- package/packages/core/src/kernel/reducer-value-index.ts +75 -0
- package/packages/core/src/kernel/reducer.ts +84 -16
- package/packages/core/src/kernel/reopen-state.ts +10 -0
- package/packages/core/src/kernel/repository-attention.ts +14 -5
- package/packages/core/src/kernel/repository-read-facts.ts +375 -0
- package/packages/core/src/kernel/ticket-starts.ts +15 -4
- package/packages/core/src/kernel/verify-stage-state.ts +145 -3
- package/packages/core/src/kernel/verify-stages.ts +46 -7
- package/packages/core/src/operator/attention.ts +28 -3
- package/packages/core/src/operator/done-escalations.ts +38 -0
- package/packages/core/src/operator/presentation.ts +14 -1
- package/packages/core/src/operator/protocol.ts +8 -5
- package/packages/core/src/operator/query.ts +72 -57
- package/packages/core/src/operator/registry.ts +19 -15
- package/packages/core/src/operator/ticket-start.ts +8 -7
- package/packages/core/src/operator/tickets.ts +28 -9
- package/packages/core/src/plugin-sdk/index.ts +10 -0
- package/packages/core/src/plugins/manifest.ts +14 -1
- package/packages/core/src/ports/forge/create.ts +8 -0
- package/packages/core/src/ports/forge/github-transport.ts +5 -1
- package/packages/core/src/ports/forge/github.ts +3 -0
- package/packages/core/src/ports/tickets/contract.ts +147 -76
- package/packages/core/src/ports/tickets/create.ts +47 -7
- package/packages/core/src/ports/tickets/fake.ts +8 -4
- package/packages/core/src/ports/tickets/file-codec.ts +4 -3
- package/packages/core/src/ports/tickets/file.ts +7 -3
- package/packages/core/src/ports/tickets/github.ts +1 -0
- package/packages/core/src/ports/tickets/grants.ts +11 -3
- package/packages/core/src/ports/tickets/hosted-index.ts +5 -0
- package/packages/core/src/ports/tickets/linear.ts +28 -9
- package/packages/core/src/ports/tickets/migration.ts +4 -4
- package/packages/core/src/ports/tickets/operations.ts +4 -2
- package/packages/core/src/ports/tickets/priority-labels.ts +213 -0
- package/packages/core/src/ports/tickets/priority.ts +127 -21
- package/packages/core/src/ports/tickets/remote-protocol.ts +10 -0
- package/packages/core/src/ports/tickets/remote.ts +104 -11
- package/packages/core/src/ports/tickets/revision.ts +5 -3
- package/packages/core/src/ports/tickets/search.ts +9 -0
- package/packages/core/src/ports/tickets/tree.ts +8 -6
- package/packages/core/src/ports/types.ts +30 -15
- package/packages/core/src/ports/workspace/build-execution.ts +4 -0
- package/packages/core/src/ports/workspace/builtin-capabilities.ts +4 -0
- package/packages/core/src/ports/workspace/create.ts +16 -3
- package/packages/core/src/ports/workspace/execution-exit-marker.ts +41 -0
- package/packages/core/src/ports/workspace/fake.ts +15 -3
- package/packages/core/src/ports/workspace/recovery-step.ts +101 -0
- package/packages/core/src/ports/workspace/vercel-sandbox.ts +510 -145
- package/packages/core/src/processes/build-execution-state.ts +4 -0
- package/packages/core/src/processes/build-runner.ts +14 -1
- package/packages/core/src/processes/dispatcher.ts +777 -288
- package/packages/core/src/processes/execution-no-progress.ts +1 -0
- package/packages/core/src/processes/execution-settlement.ts +1 -0
- package/packages/core/src/processes/harvest-runner.ts +12 -7
- package/packages/core/src/processes/infrastructure-failure-budget.ts +7 -2
- package/packages/core/src/processes/invocation-budget.ts +176 -0
- package/packages/core/src/processes/publication-loss.ts +38 -3
- package/packages/core/src/processes/workspace-preservation.ts +86 -15
- package/packages/core/src/store/contract.ts +81 -3
- package/packages/core/src/store/event-reads.ts +30 -1
- package/packages/core/src/store/local/capabilities.ts +115 -0
- package/packages/core/src/store/local/store.ts +86 -56
- package/packages/core/src/store/reduced-reads.ts +32 -1
- package/packages/core/src/store/reducer-value-read.ts +235 -0
- package/packages/core/src/store/reducer-value-shapes.json +6722 -6475
- package/packages/core/src/store/reducer-value-validation.ts +181 -33
- package/packages/core/src/store/reducer-values.ts +36 -204
- package/packages/core/src/store/remote/client.ts +169 -29
- package/packages/core/src/store/remote/compatibility.ts +113 -56
- package/packages/core/src/store/remote/credentials.ts +4 -1
- package/packages/core/src/store/remote/protocol.ts +3 -0
- package/packages/core/src/store/remote/released-clients.json +22 -0
- package/packages/core/src/store/remote/released-reducer-values/1.12.0.json +35806 -0
- package/packages/core/src/store/remote/released-reducer-values/1.13.0.json +35966 -0
- package/packages/core/src/store/remote/released-reducer-values/index.ts +20 -16
- package/packages/core/src/store/remote/released-value-policy.ts +144 -0
- package/packages/core/src/store/remote/retry.ts +406 -0
- package/packages/core/src/store/rollup-lease.ts +41 -0
- package/packages/core/src/store/rollups.ts +7 -38
- package/packages/core/src/store/snapshots.ts +1 -1
- package/packages/core/src/testing/dispatch-read-budget.ts +409 -14
- package/packages/core/src/testing/import-graph.ts +78 -0
- package/packages/core/src/testing/index.ts +2 -0
- package/packages/core/src/testing/invocation-budget.ts +42 -0
- package/packages/core/src/testing/reduced-read-budget.ts +602 -40
- package/packages/core/src/testing/released-value-coverage.ts +423 -0
- package/packages/core/src/testing/slot-agreement.ts +52 -0
- package/packages/core/src/web/assets/app.js +5 -5
- package/packages/core/src/web/overview-source.ts +19 -6
- package/skills/guide/SKILL.md +245 -103
- package/skills/guide/references/integration-tree.md +33 -18
- package/skills/guide/references/merge-policy.md +7 -2
- package/skills/guide/references/operator-api.md +48 -25
- package/skills/guide/references/optimization-nodes.md +10 -8
- package/skills/guide/references/plugin-authoring.md +46 -18
- package/skills/guide/references/remote-store-protocol.md +51 -12
- package/skills/guide/references/remote-ticket-protocol.md +85 -38
- package/skills/guide/references/restacking.md +48 -3
- package/skills/guide/references/setup.md +2 -1
- package/skills/guide/references/ticket-migration.md +2 -2
- package/skills/operate/SKILL.md +114 -65
- package/skills/plan/SKILL.md +4 -3
- package/skills/spec/SKILL.md +9 -5
- package/skills/tickets/SKILL.md +25 -17
- package/templates/hosted-service/package.json +2 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { compatibleTicketSource } from './tree'
|
|
2
|
+
import { isPriorityLabel } from './priority-labels'
|
|
2
3
|
import { configuredStore } from '../../cli/configured-store'
|
|
3
4
|
import { projectToken } from '../../cli/project-credentials'
|
|
4
5
|
import { resolveRepoOrigin } from '../../cli/repo-state'
|
|
@@ -15,7 +16,7 @@ import { createPluginRegistry, type PluginRegistry } from '../../plugins/registr
|
|
|
15
16
|
import type { PluginFactoryContext } from '../../plugins/manifest'
|
|
16
17
|
import type { TicketSource } from '../types'
|
|
17
18
|
import { DEFAULT_TICKETS_DIR, FileTicketSource } from './file'
|
|
18
|
-
import { LinearTicketSource } from './linear'
|
|
19
|
+
import { LinearTicketSource, type LinearFetch } from './linear'
|
|
19
20
|
import { GitHubTicketSource } from './github'
|
|
20
21
|
import { FileGitHubValidatorStore } from './github-validators'
|
|
21
22
|
import {
|
|
@@ -61,6 +62,16 @@ async function rawTicketSource(
|
|
|
61
62
|
/** Process label: the github source's default transport refuses requests
|
|
62
63
|
* without a credential. Absent, `AB_GITHUB_REQUIRE_CREDENTIAL` decides. */
|
|
63
64
|
requireCredential?: string
|
|
65
|
+
/** Host fetch for the builtin network sources (hosted, GitHub, Linear)
|
|
66
|
+
* and the plugin factory context; absent ⇒ global `fetch`. A bounded
|
|
67
|
+
* dispatcher invocation passes one that cancels at its deadline. */
|
|
68
|
+
fetchFn?: typeof fetch
|
|
69
|
+
/** Plugin factory context `invocationSignal`; absent ⇒ `() => undefined`. */
|
|
70
|
+
invocationSignal?: () => AbortSignal | undefined
|
|
71
|
+
/** The dispatcher's seam: a hosted source's policy synchronization and
|
|
72
|
+
* targeting negotiation are handed over to run at its first tick boundary
|
|
73
|
+
* instead of during construction. Absent, construction performs them. */
|
|
74
|
+
deferPreparation?: (prepare: () => Promise<void>) => void
|
|
64
75
|
} = {},
|
|
65
76
|
): Promise<TicketSource> {
|
|
66
77
|
const exec = options.exec ?? spawnExec
|
|
@@ -114,14 +125,16 @@ async function rawTicketSource(
|
|
|
114
125
|
teamKey: config.teamKey,
|
|
115
126
|
...(config.claimedState !== undefined ? { claimedState: config.claimedState } : {}),
|
|
116
127
|
...(config.createState !== undefined ? { createState: config.createState } : {}),
|
|
128
|
+
...(options.fetchFn !== undefined ? { fetchFn: options.fetchFn } : {}),
|
|
117
129
|
})
|
|
118
130
|
// Delegated sessions may read policy, but never redefine project authority.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
const policy =
|
|
132
|
+
!ticketSession && !env.AB_HARVEST && !env.AB_PHASE ? hostedTicketPolicy(config) : undefined
|
|
133
|
+
// The source is used with a settled targeting decision, so no observation
|
|
134
|
+
// pass ever runs on undecided support; the policy sync usually settles it.
|
|
135
|
+
if (options.deferPreparation !== undefined)
|
|
136
|
+
options.deferPreparation(() => source.prepareForDispatch(policy))
|
|
137
|
+
else await source.prepareForDispatch(policy)
|
|
125
138
|
return source
|
|
126
139
|
}
|
|
127
140
|
|
|
@@ -141,9 +154,15 @@ async function rawTicketSource(
|
|
|
141
154
|
'[tickets].source = "linear" requires teamKey — the Linear team key (e.g. "ENG")',
|
|
142
155
|
)
|
|
143
156
|
}
|
|
157
|
+
const fetchFn = options.fetchFn
|
|
144
158
|
return new LinearTicketSource({
|
|
145
159
|
apiKey,
|
|
146
160
|
teamKey: config.teamKey,
|
|
161
|
+
...(fetchFn !== undefined
|
|
162
|
+
? {
|
|
163
|
+
fetchFn: (url, init) => fetchFn(url, init) as unknown as ReturnType<LinearFetch>,
|
|
164
|
+
}
|
|
165
|
+
: {}),
|
|
147
166
|
...(config.claimedState !== undefined ? { claimedState: config.claimedState } : {}),
|
|
148
167
|
...(config.createState !== undefined ? { createState: config.createState } : {}),
|
|
149
168
|
})
|
|
@@ -167,6 +186,7 @@ async function rawTicketSource(
|
|
|
167
186
|
createGitHubFetchTransport({
|
|
168
187
|
token: githubTokenSource(env),
|
|
169
188
|
requireCredential: githubCredentialRequirement(env, options.requireCredential),
|
|
189
|
+
...(options.fetchFn !== undefined ? { fetchFn: options.fetchFn } : {}),
|
|
170
190
|
}),
|
|
171
191
|
repository: coordinates,
|
|
172
192
|
lifecycle: ticketLifecycle(config),
|
|
@@ -228,6 +248,8 @@ async function rawTicketSource(
|
|
|
228
248
|
config: config as unknown as Record<string, unknown>,
|
|
229
249
|
env,
|
|
230
250
|
repoRoot: resolve(targetRepo),
|
|
251
|
+
fetch: options.fetchFn ?? globalThis.fetch,
|
|
252
|
+
invocationSignal: options.invocationSignal ?? (() => undefined),
|
|
231
253
|
}
|
|
232
254
|
try {
|
|
233
255
|
return await registration.factory(context)
|
|
@@ -251,9 +273,27 @@ export async function createTicketSource(
|
|
|
251
273
|
const plugin = owner?.kind === 'plugin' ? owner.name : undefined
|
|
252
274
|
assertReadyAssigneeSupported(config, source, plugin)
|
|
253
275
|
assertCreateAssigneeSupported(config, source, plugin)
|
|
276
|
+
if (!source.priority) assertNoPriorityLabels(config, source.name)
|
|
254
277
|
return compatibleTicketSource(source)
|
|
255
278
|
}
|
|
256
279
|
|
|
280
|
+
/** A source without native priority stores levels as `priority:<level>`
|
|
281
|
+
* labels, so they can neither gate readiness nor be applied by default. */
|
|
282
|
+
function assertNoPriorityLabels(
|
|
283
|
+
config: { readyLabels?: string[]; createLabels?: string[] },
|
|
284
|
+
source: string,
|
|
285
|
+
): void {
|
|
286
|
+
for (const [key, labels] of [
|
|
287
|
+
['readyLabels', config.readyLabels],
|
|
288
|
+
['createLabels', config.createLabels],
|
|
289
|
+
] as const)
|
|
290
|
+
for (const label of labels ?? [])
|
|
291
|
+
if (isPriorityLabel(label))
|
|
292
|
+
throw new Error(
|
|
293
|
+
`[tickets].${key} entry "${label}" is how the ${source} ticket source stores ticket priority; set priority with --priority instead`,
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
|
|
257
297
|
/** Owner and name of a github.com origin: an `owner/name` slug, or a URL or
|
|
258
298
|
* SSH spelling whose host is github.com. Anything else is null. */
|
|
259
299
|
export function githubCoordinates(origin: string): { owner: string; name: string } | null {
|
|
@@ -39,7 +39,9 @@ function cloneTicket(ticket: Ticket, doneState: string): Ticket {
|
|
|
39
39
|
...(ticket.issueLinks?.length
|
|
40
40
|
? { issueLinks: ticket.issueLinks.map((link) => ({ ...link })) }
|
|
41
41
|
: {}),
|
|
42
|
-
...(ticket.priority
|
|
42
|
+
...(ticket.priority !== undefined && ticket.priority !== 'normal'
|
|
43
|
+
? { priority: ticket.priority }
|
|
44
|
+
: {}),
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
@@ -197,7 +199,9 @@ export class FakeTicketSource implements TicketSource {
|
|
|
197
199
|
? { blockedBy: [...draft.blockedBy] }
|
|
198
200
|
: {}),
|
|
199
201
|
...(draft.issueLinks?.length ? { issueLinks: normalizeIssueLinks(draft.issueLinks) } : {}),
|
|
200
|
-
...(draft.priority
|
|
202
|
+
...(draft.priority !== undefined && draft.priority !== 'normal'
|
|
203
|
+
? { priority: draft.priority }
|
|
204
|
+
: {}),
|
|
201
205
|
}
|
|
202
206
|
this.tickets.set(id, ticket)
|
|
203
207
|
if (opts.idempotencyKey !== undefined) {
|
|
@@ -248,8 +252,8 @@ export class FakeTicketSource implements TicketSource {
|
|
|
248
252
|
if (validated.labels !== undefined) ticket.labels = [...validated.labels]
|
|
249
253
|
if (validated.issueLinks?.length) ticket.issueLinks = validated.issueLinks
|
|
250
254
|
else if (validated.issueLinks !== undefined) delete ticket.issueLinks
|
|
251
|
-
if (validated.priority === '
|
|
252
|
-
else if (validated.priority !== undefined)
|
|
255
|
+
if (validated.priority === 'normal') delete ticket.priority
|
|
256
|
+
else if (validated.priority !== undefined) ticket.priority = validated.priority
|
|
253
257
|
|
|
254
258
|
this.updates.push({ id, patch: cloneUpdate(validated) })
|
|
255
259
|
}
|
|
@@ -2,6 +2,7 @@ import { portableAssetSchema } from './portable-assets'
|
|
|
2
2
|
import { parse as parseToml, stringify as stringifyToml } from 'smol-toml'
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
import { refineUniqueIssues } from './issue-links'
|
|
5
|
+
import { ticketPrioritySchema } from './priority'
|
|
5
6
|
|
|
6
7
|
const frontmatterSchema = z.strictObject({
|
|
7
8
|
kind: z.enum(['work', 'node']).optional(),
|
|
@@ -45,7 +46,7 @@ const frontmatterSchema = z.strictObject({
|
|
|
45
46
|
.optional(),
|
|
46
47
|
/** Dispatch class. Absent ≡ normal; a hand-written `normal` is tolerated and
|
|
47
48
|
* dropped on the next write. */
|
|
48
|
-
priority:
|
|
49
|
+
priority: ticketPrioritySchema.optional(),
|
|
49
50
|
/** Stable external-create adoption key. It travels with the ticket across
|
|
50
51
|
* state-directory moves, so retries find it in any lifecycle state. */
|
|
51
52
|
idempotencyKey: z.string().min(1).optional(),
|
|
@@ -105,8 +106,8 @@ export function serializeTicketFile(front: Frontmatter, body: string): string {
|
|
|
105
106
|
if (front.issueLinks !== undefined && front.issueLinks.length > 0) {
|
|
106
107
|
data.issueLinks = front.issueLinks
|
|
107
108
|
}
|
|
108
|
-
//
|
|
109
|
-
if (front.priority
|
|
109
|
+
// Normal is never written, so normal tickets round-trip byte-identically.
|
|
110
|
+
if (front.priority !== undefined && front.priority !== 'normal') data.priority = front.priority
|
|
110
111
|
if (front.idempotencyKey !== undefined) {
|
|
111
112
|
data.idempotencyKey = front.idempotencyKey
|
|
112
113
|
}
|
|
@@ -315,7 +315,9 @@ export class FileTicketSource implements TicketSource {
|
|
|
315
315
|
? { blockedBy: [...draft.blockedBy] }
|
|
316
316
|
: {}),
|
|
317
317
|
...(draft.issueLinks?.length ? { issueLinks: normalizeIssueLinks(draft.issueLinks) } : {}),
|
|
318
|
-
...(draft.priority
|
|
318
|
+
...(draft.priority !== undefined && draft.priority !== 'normal'
|
|
319
|
+
? { priority: draft.priority }
|
|
320
|
+
: {}),
|
|
319
321
|
...(opts.idempotencyKey !== undefined ? { idempotencyKey: opts.idempotencyKey } : {}),
|
|
320
322
|
}
|
|
321
323
|
if (taken.has(front.id)) throw new Error(`requested ticket id already occupied: ${front.id}`)
|
|
@@ -724,7 +726,9 @@ export class FileTicketSource implements TicketSource {
|
|
|
724
726
|
? { blockedBy: [...front.blockedBy] }
|
|
725
727
|
: {}),
|
|
726
728
|
...(front.issueLinks?.length ? { issueLinks: normalizeIssueLinks(front.issueLinks) } : {}),
|
|
727
|
-
...(front.priority
|
|
729
|
+
...(front.priority !== undefined && front.priority !== 'normal'
|
|
730
|
+
? { priority: front.priority }
|
|
731
|
+
: {}),
|
|
728
732
|
}
|
|
729
733
|
}
|
|
730
734
|
}
|
|
@@ -739,7 +743,7 @@ function setIssueLinks(front: Frontmatter, links: TicketUpdate['issueLinks']): v
|
|
|
739
743
|
/** Urgent sets the key; normal removes it. */
|
|
740
744
|
function setPriority(front: Frontmatter, priority: TicketUpdate['priority']): void {
|
|
741
745
|
if (priority === undefined) return
|
|
742
|
-
if (priority
|
|
746
|
+
if (priority !== 'normal') front.priority = priority
|
|
743
747
|
else delete front.priority
|
|
744
748
|
}
|
|
745
749
|
|
|
@@ -443,6 +443,7 @@ export class GitHubTicketSource implements TicketSource {
|
|
|
443
443
|
readonly readyAssignee = true
|
|
444
444
|
readonly assignees = true
|
|
445
445
|
readonly integrationTree = true
|
|
446
|
+
readonly kindLabel = NODE_LABEL
|
|
446
447
|
|
|
447
448
|
private readonly transport: GitHubRequest
|
|
448
449
|
private readonly owner: string
|
|
@@ -6,8 +6,15 @@ import {
|
|
|
6
6
|
} from '../../store/remote/version'
|
|
7
7
|
import { ticketGrantResponseSchema } from './remote-protocol'
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/** `fetchFn` is the host fetch (absent ⇒ global `fetch`); a bounded
|
|
10
|
+
* dispatcher invocation passes one that also cancels at its deadline. */
|
|
11
|
+
export async function ticketGrantRequest(
|
|
12
|
+
url: string,
|
|
13
|
+
token: string,
|
|
14
|
+
input: unknown,
|
|
15
|
+
fetchFn: typeof fetch = fetch,
|
|
16
|
+
) {
|
|
17
|
+
const response = await fetchFn(`${url.replace(/\/+$/, '')}/tickets/grants`, {
|
|
11
18
|
method: 'POST',
|
|
12
19
|
headers: {
|
|
13
20
|
authorization: `Bearer ${token}`,
|
|
@@ -40,8 +47,9 @@ export async function issueTicketGrant(
|
|
|
40
47
|
repo: string,
|
|
41
48
|
execution: string,
|
|
42
49
|
kind: 'runner' | 'phase',
|
|
50
|
+
fetchFn?: typeof fetch,
|
|
43
51
|
) {
|
|
44
52
|
return ticketGrantResponseSchema.parse(
|
|
45
|
-
await ticketGrantRequest(url, token, { action: 'issue', repo, execution, kind }),
|
|
53
|
+
await ticketGrantRequest(url, token, { action: 'issue', repo, execution, kind }, fetchFn),
|
|
46
54
|
)
|
|
47
55
|
}
|
|
@@ -13,6 +13,8 @@ export {
|
|
|
13
13
|
TICKET_ISSUE_LINKS_CAPABILITY,
|
|
14
14
|
TICKET_PRIORITY_HEADER,
|
|
15
15
|
TICKET_PRIORITY_CAPABILITY,
|
|
16
|
+
TICKET_PRIORITY_LEVELS_HEADER,
|
|
17
|
+
TICKET_PRIORITY_LEVELS_CAPABILITY,
|
|
16
18
|
TICKET_DEPENDENCY_STATE_HEADER,
|
|
17
19
|
TICKET_DEPENDENCY_STATE_CAPABILITY,
|
|
18
20
|
TICKET_POLICY_ROLES_HEADER,
|
|
@@ -53,7 +55,10 @@ export {
|
|
|
53
55
|
refuseUnsupportedPriority,
|
|
54
56
|
withoutUnsupportedPriority,
|
|
55
57
|
ticketPriority,
|
|
58
|
+
dispatchPriority,
|
|
59
|
+
byPriority,
|
|
56
60
|
urgentFirst,
|
|
61
|
+
TICKET_PRIORITIES,
|
|
57
62
|
type TicketPriority,
|
|
58
63
|
} from './priority'
|
|
59
64
|
export { TicketAssigneesUnsupported } from './assignees'
|
|
@@ -124,7 +124,8 @@ interface GqlIssue {
|
|
|
124
124
|
title: string
|
|
125
125
|
description: string | null
|
|
126
126
|
url: string
|
|
127
|
-
/** Linear's 0–4 scale
|
|
127
|
+
/** Linear's 0–4 scale: 1 Urgent, 2 High and 4 Low map to their levels;
|
|
128
|
+
* 3 Medium and 0 No priority read as `normal`. */
|
|
128
129
|
priority?: number
|
|
129
130
|
state: { name: string; type: string } | null
|
|
130
131
|
labels: { nodes: Array<{ name: string }>; pageInfo?: PageInfo }
|
|
@@ -274,12 +275,13 @@ function nextCursor(info: PageInfo | undefined, seen: Set<string>): string | nul
|
|
|
274
275
|
// nodeLabels 1 + 1.1 + parent 1 + .1 + 1.1 + 2.1 ≈ 65, so at the contract's
|
|
275
276
|
// maximum limit of 100: 100 * 65 + wrappers < 6,600. Bodies are never selected.
|
|
276
277
|
const SEARCH_LABEL_PAGE_SIZE = 50
|
|
277
|
-
const SEARCH_QUERY = `query SearchIssues($filter: IssueFilter!, $first: Int!, $after: String, $nodeLabel: IssueLabelFilter!) { issues(filter: $filter, first: $first, after: $after, includeArchived: true, orderBy: createdAt) { nodes { identifier title state { name } labels(first: ${SEARCH_LABEL_PAGE_SIZE}) { nodes { name } ${PAGE_INFO} } nodeLabels: labels(filter: $nodeLabel, first: 1) { nodes { name } } parent { identifier team { key } labels(filter: $nodeLabel, first: 1) { nodes { name } } } } ${PAGE_INFO} } }`
|
|
278
|
+
const SEARCH_QUERY = `query SearchIssues($filter: IssueFilter!, $first: Int!, $after: String, $nodeLabel: IssueLabelFilter!) { issues(filter: $filter, first: $first, after: $after, includeArchived: true, orderBy: createdAt) { nodes { identifier title priority state { name } labels(first: ${SEARCH_LABEL_PAGE_SIZE}) { nodes { name } ${PAGE_INFO} } nodeLabels: labels(filter: $nodeLabel, first: 1) { nodes { name } } parent { identifier team { key } labels(filter: $nodeLabel, first: 1) { nodes { name } } } } ${PAGE_INFO} } }`
|
|
278
279
|
const SEARCH_CURSOR_PREFIX = 'linear1.'
|
|
279
280
|
|
|
280
281
|
interface GqlSearchIssue {
|
|
281
282
|
identifier: string
|
|
282
283
|
title: string
|
|
284
|
+
priority?: number
|
|
283
285
|
state: { name: string } | null
|
|
284
286
|
labels: Connection<{ name: string }>
|
|
285
287
|
nodeLabels: { nodes: Array<{ name: string }> }
|
|
@@ -340,6 +342,18 @@ const DELETE_RELATION_MUTATION = `mutation DeleteRelation($id: String!) { issueR
|
|
|
340
342
|
// Linear priority: 0 = No priority, 1 = Urgent, 2 = High, 3 = Medium, 4 = Low.
|
|
341
343
|
const LINEAR_NO_PRIORITY = 0
|
|
342
344
|
const LINEAR_URGENT = 1
|
|
345
|
+
const LINEAR_HIGH = 2
|
|
346
|
+
const LINEAR_LOW = 4
|
|
347
|
+
/** Every level but `normal`, as Linear stores it. */
|
|
348
|
+
const LINEAR_LEVELS = { urgent: LINEAR_URGENT, high: LINEAR_HIGH, low: LINEAR_LOW } as const
|
|
349
|
+
|
|
350
|
+
/** Linear's number as a ticket level; Medium and No priority are normal. */
|
|
351
|
+
function linearLevel(priority: number | undefined): keyof typeof LINEAR_LEVELS | undefined {
|
|
352
|
+
if (priority === LINEAR_URGENT) return 'urgent'
|
|
353
|
+
if (priority === LINEAR_HIGH) return 'high'
|
|
354
|
+
if (priority === LINEAR_LOW) return 'low'
|
|
355
|
+
return undefined
|
|
356
|
+
}
|
|
343
357
|
const linearReservedIssueIdSchema = z.uuidv4()
|
|
344
358
|
|
|
345
359
|
type ReadQuery = <T>(
|
|
@@ -684,6 +698,9 @@ export class LinearTicketSource implements TicketSource {
|
|
|
684
698
|
labels: issue.labels.nodes.map((label) => label.name),
|
|
685
699
|
kind: issue.nodeLabels.nodes.length > 0 ? 'node' : 'work',
|
|
686
700
|
...(target !== undefined ? { target } : {}),
|
|
701
|
+
...(linearLevel(issue.priority) !== undefined
|
|
702
|
+
? { priority: linearLevel(issue.priority) }
|
|
703
|
+
: {}),
|
|
687
704
|
}
|
|
688
705
|
})
|
|
689
706
|
const next = nextCursor(data.issues.pageInfo, new Set())
|
|
@@ -881,7 +898,8 @@ export class LinearTicketSource implements TicketSource {
|
|
|
881
898
|
if (reservedId !== undefined) input.id = reservedId
|
|
882
899
|
if (assigneeId !== undefined) input.assigneeId = assigneeId
|
|
883
900
|
// `normal` needs nothing: Linear creates issues with No priority.
|
|
884
|
-
if (draft.priority
|
|
901
|
+
if (draft.priority !== undefined && draft.priority !== 'normal')
|
|
902
|
+
input.priority = LINEAR_LEVELS[draft.priority]
|
|
885
903
|
|
|
886
904
|
let issue: GqlIssue | null = null
|
|
887
905
|
let createError: unknown
|
|
@@ -941,12 +959,13 @@ export class LinearTicketSource implements TicketSource {
|
|
|
941
959
|
validated.target = await this.validateTree(id, { ...validated, kind: effectiveKind })
|
|
942
960
|
}
|
|
943
961
|
const input: Record<string, unknown> = {}
|
|
944
|
-
if (validated.priority
|
|
945
|
-
|
|
946
|
-
//
|
|
962
|
+
if (validated.priority !== undefined && validated.priority !== 'normal')
|
|
963
|
+
input.priority = LINEAR_LEVELS[validated.priority]
|
|
964
|
+
// `normal` clears Urgent, High or Low; Medium set by a person stays. A
|
|
965
|
+
// person setting a level between this read and the write keeps it.
|
|
947
966
|
else if (
|
|
948
967
|
validated.priority === 'normal' &&
|
|
949
|
-
(await this.currentPriority('update', id))
|
|
968
|
+
linearLevel(await this.currentPriority('update', id)) !== undefined
|
|
950
969
|
)
|
|
951
970
|
input.priority = LINEAR_NO_PRIORITY
|
|
952
971
|
const issueId = await this.resolveIssueId('update', id)
|
|
@@ -966,7 +985,7 @@ export class LinearTicketSource implements TicketSource {
|
|
|
966
985
|
)
|
|
967
986
|
}
|
|
968
987
|
}
|
|
969
|
-
// Only a priority-only `normal` patch on a
|
|
988
|
+
// Only a priority-only `normal` patch on a Medium or No priority issue gets here
|
|
970
989
|
// empty; there is nothing to write.
|
|
971
990
|
if (Object.keys(input).length === 0) return
|
|
972
991
|
|
|
@@ -1578,7 +1597,7 @@ export class LinearTicketSource implements TicketSource {
|
|
|
1578
1597
|
issue.parent.labels.nodes.some((label) => label.name === 'node')
|
|
1579
1598
|
? issue.parent.identifier
|
|
1580
1599
|
: undefined
|
|
1581
|
-
const priority = issue.priority
|
|
1600
|
+
const priority = linearLevel(issue.priority)
|
|
1582
1601
|
return {
|
|
1583
1602
|
kind,
|
|
1584
1603
|
...(target !== undefined ? { target } : {}),
|
|
@@ -19,7 +19,7 @@ import { parseTicketFile, serializeTicketFile } from './file-codec'
|
|
|
19
19
|
import { validateFileTicketId } from './requested-id'
|
|
20
20
|
import { normalizeStateName } from './states'
|
|
21
21
|
import { TicketIssueLinksUnsupported } from './issue-links'
|
|
22
|
-
import { TicketPriorityUnsupported } from './priority'
|
|
22
|
+
import { TicketPriorityUnsupported, ticketPriority } from './priority'
|
|
23
23
|
import { encodePortableAsset, decodePortableAsset, type PortableAsset } from './portable-assets'
|
|
24
24
|
|
|
25
25
|
export const MANIFEST = '.ab-migration.json'
|
|
@@ -237,7 +237,7 @@ export async function exportTickets(opts: MigrationOptions): Promise<void> {
|
|
|
237
237
|
labels: ticket.labels,
|
|
238
238
|
blockedBy: ticket.blockedBy,
|
|
239
239
|
issueLinks: ticket.issueLinks,
|
|
240
|
-
...(ticket
|
|
240
|
+
...(ticketPriority(ticket) !== 'normal' ? { priority: ticket.priority } : {}),
|
|
241
241
|
idempotencyKey: key,
|
|
242
242
|
...(ticket.comments !== undefined ? { comments: ticket.comments } : {}),
|
|
243
243
|
attachments: await currentAssets(
|
|
@@ -460,7 +460,7 @@ export async function importTickets(opts: MigrationOptions): Promise<MigrationRe
|
|
|
460
460
|
throw new Error('target source cannot preserve kind/target metadata')
|
|
461
461
|
if (!opts.source.issueLinks && tickets.some((t) => t.issueLinks?.length))
|
|
462
462
|
throw new TicketIssueLinksUnsupported(opts.source.name)
|
|
463
|
-
if (!opts.source.priority && tickets.some((t) => t
|
|
463
|
+
if (!opts.source.priority && tickets.some((t) => ticketPriority(t) !== 'normal'))
|
|
464
464
|
throw new TicketPriorityUnsupported(opts.source.name)
|
|
465
465
|
const bundles = new Map<string, PortableAsset[]>()
|
|
466
466
|
for (const ticket of tickets) {
|
|
@@ -588,7 +588,7 @@ export async function importTickets(opts: MigrationOptions): Promise<MigrationRe
|
|
|
588
588
|
labels: ticket.labels,
|
|
589
589
|
...(opts.source.integrationTree ? { kind: ticket.kind ?? 'work' } : {}),
|
|
590
590
|
...(opts.source.issueLinks && links.length ? { issueLinks: links } : {}),
|
|
591
|
-
...(opts.source.priority && ticket
|
|
591
|
+
...(opts.source.priority && ticketPriority(ticket) !== 'normal'
|
|
592
592
|
? { priority: ticket.priority }
|
|
593
593
|
: {}),
|
|
594
594
|
},
|
|
@@ -99,10 +99,12 @@ export async function createTicket(
|
|
|
99
99
|
input: TicketDraft & { state?: string },
|
|
100
100
|
options: { blockerErrorPrefix?: string; blockerErrorSuffix?: string } = {},
|
|
101
101
|
): Promise<Ticket> {
|
|
102
|
+
// Wrapped first: the facade gives a source without native priority its
|
|
103
|
+
// label fallback, so only a source that still cannot store a level refuses.
|
|
104
|
+
source = compatibleTicketSource(source)
|
|
102
105
|
refuseUnsupportedIssueLinks(source, input)
|
|
103
106
|
refuseUnsupportedPriority(source, input)
|
|
104
107
|
refuseUnsupportedAssignees(source, input)
|
|
105
|
-
source = compatibleTicketSource(source)
|
|
106
108
|
const blockedBy = await knownBlockers(
|
|
107
109
|
source,
|
|
108
110
|
input.blockedBy ?? [],
|
|
@@ -142,10 +144,10 @@ export async function updateTicket(
|
|
|
142
144
|
readAfter = true,
|
|
143
145
|
expectedRevision?: string,
|
|
144
146
|
): Promise<Ticket | null> {
|
|
147
|
+
source = compatibleTicketSource(source)
|
|
145
148
|
refuseUnsupportedIssueLinks(source, patch)
|
|
146
149
|
refuseUnsupportedPriority(source, patch)
|
|
147
150
|
refuseUnsupportedAssignees(source, patch)
|
|
148
|
-
source = compatibleTicketSource(source)
|
|
149
151
|
try {
|
|
150
152
|
if (expectedRevision !== undefined)
|
|
151
153
|
await mutateTicketIfRevision(source, id, expectedRevision, { kind: 'update', patch })
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Ticket,
|
|
3
|
+
TicketCriteria,
|
|
4
|
+
TicketDraft,
|
|
5
|
+
TicketMutation,
|
|
6
|
+
TicketPriority,
|
|
7
|
+
TicketSearchHit,
|
|
8
|
+
TicketSource,
|
|
9
|
+
TicketUpdate,
|
|
10
|
+
} from '../types'
|
|
11
|
+
import { checkTicketRevision, TicketRevisionConflict } from './revision'
|
|
12
|
+
import { priorityRank } from './priority'
|
|
13
|
+
|
|
14
|
+
/** Every priority label starts with this prefix. */
|
|
15
|
+
export const PRIORITY_LABEL_PREFIX = 'priority:'
|
|
16
|
+
|
|
17
|
+
/** The labels a source without native priority stores levels as. `normal`
|
|
18
|
+
* has none: it is the absence of every priority label. */
|
|
19
|
+
export const PRIORITY_LABELS: Readonly<Record<Exclude<TicketPriority, 'normal'>, string>> = {
|
|
20
|
+
urgent: 'priority:urgent',
|
|
21
|
+
high: 'priority:high',
|
|
22
|
+
low: 'priority:low',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The label a level is stored as; `undefined` for `normal`. */
|
|
26
|
+
export function priorityLabel(level: TicketPriority): string | undefined {
|
|
27
|
+
return level === 'normal' ? undefined : PRIORITY_LABELS[level]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function labelLevel(name: string): Exclude<TicketPriority, 'normal'> | undefined {
|
|
31
|
+
const lower = name.toLowerCase()
|
|
32
|
+
for (const [level, label] of Object.entries(PRIORITY_LABELS))
|
|
33
|
+
if (label === lower) return level as Exclude<TicketPriority, 'normal'>
|
|
34
|
+
return undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Exact, case-insensitive match against the three priority labels. */
|
|
38
|
+
export function isPriorityLabel(name: string): boolean {
|
|
39
|
+
return labelLevel(name) !== undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The highest level among the labels; `undefined` when none is a priority
|
|
43
|
+
* label. */
|
|
44
|
+
export function labelPriority(labels: readonly string[]): TicketPriority | undefined {
|
|
45
|
+
let best: TicketPriority | undefined
|
|
46
|
+
for (const name of labels) {
|
|
47
|
+
const level = labelLevel(name)
|
|
48
|
+
if (level !== undefined && (best === undefined || priorityRank(level) < priorityRank(best)))
|
|
49
|
+
best = level
|
|
50
|
+
}
|
|
51
|
+
return best
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The level read from labels, moved out of `labels` into `priority`. */
|
|
55
|
+
function fromLabels<T extends { labels: string[]; priority?: TicketPriority }>(value: T): T {
|
|
56
|
+
const { priority: _raw, ...rest } = value
|
|
57
|
+
const labels: readonly string[] = value.labels ?? []
|
|
58
|
+
const level = labelPriority(labels)
|
|
59
|
+
if (level === undefined) return 'priority' in value ? (rest as T) : value
|
|
60
|
+
return { ...rest, labels: labels.filter((name) => !isPriorityLabel(name)), priority: level } as T
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function refusePriorityLabels(source: string, labels: readonly string[] | undefined, use: string) {
|
|
64
|
+
const named = (labels ?? []).filter(isPriorityLabel)
|
|
65
|
+
if (named.length > 0)
|
|
66
|
+
throw new Error(
|
|
67
|
+
`${named.map((name) => `"${name}"`).join(', ')} ${named.length === 1 ? 'is' : 'are'} how the ${source} ticket source stores ticket priority; ${use} — nothing was written`,
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function refuseCriteria(source: string, criteria: { labels?: string[] } | undefined): void {
|
|
72
|
+
refusePriorityLabels(source, criteria?.labels, 'it cannot filter tickets or gate readiness')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function refuseWriteLabels(source: string, labels: readonly string[] | undefined): void {
|
|
76
|
+
refusePriorityLabels(source, labels, 'set priority with --priority instead')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function sameLabels(a: readonly string[], b: readonly string[]): boolean {
|
|
80
|
+
const left = new Set(a.map((name) => name.toLowerCase()))
|
|
81
|
+
const right = new Set(b.map((name) => name.toLowerCase()))
|
|
82
|
+
return left.size === right.size && [...left].every((name) => right.has(name))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The raw patch for a facade patch: the full label set rebuilt from the
|
|
87
|
+
* current raw labels. Visible labels come from the patch or the current
|
|
88
|
+
* ticket; priority labels are exactly the named level's, or the current ones
|
|
89
|
+
* untouched. `null` means the write changes nothing.
|
|
90
|
+
*/
|
|
91
|
+
function rawPatch(
|
|
92
|
+
current: Ticket,
|
|
93
|
+
patch: TicketUpdate,
|
|
94
|
+
kindLabel: string | undefined,
|
|
95
|
+
): TicketUpdate | null {
|
|
96
|
+
const { priority, ...rest } = patch
|
|
97
|
+
let visible = patch.labels ?? current.labels.filter((name) => !isPriorityLabel(name))
|
|
98
|
+
// Only a source recording kind as a label keeps that label in step with a
|
|
99
|
+
// kind change written beside priority; every other label is preserved.
|
|
100
|
+
if (kindLabel !== undefined && patch.labels === undefined && patch.kind !== undefined) {
|
|
101
|
+
visible = visible.filter((name) => name !== kindLabel)
|
|
102
|
+
if (patch.kind === 'node') visible.push(kindLabel)
|
|
103
|
+
}
|
|
104
|
+
const kept =
|
|
105
|
+
priority === undefined
|
|
106
|
+
? current.labels.filter(isPriorityLabel)
|
|
107
|
+
: [priorityLabel(priority)].filter((name): name is string => name !== undefined)
|
|
108
|
+
const labels = [...visible, ...kept]
|
|
109
|
+
const others = Object.keys(rest).filter((key) => key !== 'labels')
|
|
110
|
+
if (others.length === 0 && sameLabels(labels, current.labels)) return null
|
|
111
|
+
return { ...rest, labels }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const REVISION_RETRIES = 2
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The label fallback for a source without native priority: levels other
|
|
118
|
+
* than `normal` are stored as exactly one `priority:<level>` label. Reads
|
|
119
|
+
* map the labels a read already returned, so they cost no extra request;
|
|
120
|
+
* priority labels are reported as `priority`, never among `labels`, and the
|
|
121
|
+
* highest wins when several are present. A write naming priority or labels
|
|
122
|
+
* reads the raw ticket once and rebuilds its complete label set, so a
|
|
123
|
+
* labels replacement keeps priority and a priority write keeps every other
|
|
124
|
+
* label. Listing criteria and written labels that name a priority label are
|
|
125
|
+
* refused before any request.
|
|
126
|
+
*/
|
|
127
|
+
export function priorityLabelSource(source: TicketSource): TicketSource {
|
|
128
|
+
const name = source.name
|
|
129
|
+
async function current(id: string): Promise<Ticket> {
|
|
130
|
+
const ticket = await source.get(id)
|
|
131
|
+
if (!ticket) throw new Error(`${name} ticket source: unknown ticket "${id}"`)
|
|
132
|
+
return ticket
|
|
133
|
+
}
|
|
134
|
+
async function guarded(id: string, revision: string, patch: TicketUpdate): Promise<void> {
|
|
135
|
+
const raw = await current(id)
|
|
136
|
+
checkTicketRevision(raw, revision)
|
|
137
|
+
const write = rawPatch(raw, patch, source.kindLabel)
|
|
138
|
+
if (write === null) return
|
|
139
|
+
await source.mutateIfRevision!(id, revision, { kind: 'update', patch: write })
|
|
140
|
+
}
|
|
141
|
+
async function update(id: string, patch: TicketUpdate): Promise<void> {
|
|
142
|
+
if (patch.priority === undefined && patch.labels === undefined) return source.update(id, patch)
|
|
143
|
+
refuseWriteLabels(name, patch.labels)
|
|
144
|
+
if (!source.mutateIfRevision) {
|
|
145
|
+
const write = rawPatch(await current(id), patch, source.kindLabel)
|
|
146
|
+
if (write !== null) await source.update(id, write)
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
for (let attempt = 0; ; attempt++) {
|
|
150
|
+
const raw = await current(id)
|
|
151
|
+
try {
|
|
152
|
+
return await guarded(id, raw.revision, patch)
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (!(error instanceof TicketRevisionConflict) || attempt >= REVISION_RETRIES) throw error
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const facade = Object.create(source) as TicketSource
|
|
159
|
+
return new Proxy(facade, {
|
|
160
|
+
get(_facade, property) {
|
|
161
|
+
if (property === 'priority') return true
|
|
162
|
+
const value = Reflect.get(source, property, source)
|
|
163
|
+
if (typeof value !== 'function') return value
|
|
164
|
+
if (property === 'get')
|
|
165
|
+
return async (id: string) => {
|
|
166
|
+
const ticket = await source.get(id)
|
|
167
|
+
return ticket ? fromLabels(ticket) : null
|
|
168
|
+
}
|
|
169
|
+
if (property === 'listReady' || property === 'listPage')
|
|
170
|
+
return async (criteria: TicketCriteria, ...args: unknown[]) => {
|
|
171
|
+
refuseCriteria(name, criteria)
|
|
172
|
+
const listing = await value.call(source, criteria, ...args)
|
|
173
|
+
return { ...listing, tickets: listing.tickets.map(fromLabels) }
|
|
174
|
+
}
|
|
175
|
+
if (property === 'listAll' || property === 'listTargeting')
|
|
176
|
+
return async (...args: unknown[]) => {
|
|
177
|
+
const listing = await value.apply(source, args)
|
|
178
|
+
return { ...listing, tickets: listing.tickets.map(fromLabels) }
|
|
179
|
+
}
|
|
180
|
+
if (property === 'readExportSnapshot')
|
|
181
|
+
return async (...args: unknown[]) =>
|
|
182
|
+
((await value.apply(source, args)) as Ticket[]).map(fromLabels)
|
|
183
|
+
if (property === 'search')
|
|
184
|
+
return async (query: { labels?: string[] }) => {
|
|
185
|
+
refuseCriteria(name, query)
|
|
186
|
+
const page = await value.call(source, query)
|
|
187
|
+
return { ...page, tickets: page.tickets.map((hit: TicketSearchHit) => fromLabels(hit)) }
|
|
188
|
+
}
|
|
189
|
+
if (property === 'create')
|
|
190
|
+
return async (draft: TicketDraft, ...args: unknown[]) => {
|
|
191
|
+
refuseWriteLabels(name, draft.labels)
|
|
192
|
+
const { priority, ...rest } = draft
|
|
193
|
+
const label = priority === undefined ? undefined : priorityLabel(priority)
|
|
194
|
+
const write: TicketDraft =
|
|
195
|
+
label === undefined ? rest : { ...rest, labels: [...(rest.labels ?? []), label] }
|
|
196
|
+
return fromLabels(await value.call(source, write, ...args))
|
|
197
|
+
}
|
|
198
|
+
if (property === 'update') return update
|
|
199
|
+
if (property === 'mutateIfRevision')
|
|
200
|
+
return async (id: string, revision: string, mutation: TicketMutation) => {
|
|
201
|
+
if (
|
|
202
|
+
mutation.kind !== 'update' ||
|
|
203
|
+
(mutation.patch.priority === undefined && mutation.patch.labels === undefined)
|
|
204
|
+
)
|
|
205
|
+
return value.call(source, id, revision, mutation)
|
|
206
|
+
if (!revision.trim()) throw new Error('expected revision must be nonblank')
|
|
207
|
+
refuseWriteLabels(name, mutation.patch.labels)
|
|
208
|
+
return guarded(id, revision, mutation.patch)
|
|
209
|
+
}
|
|
210
|
+
return value.bind(source)
|
|
211
|
+
},
|
|
212
|
+
})
|
|
213
|
+
}
|