@leed-ai/git-gateway-worker 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +49 -2
- package/dist/audit/audit-recorder.d.ts +2 -0
- package/dist/audit/audit-recorder.js +53 -0
- package/dist/audit/audit-recorder.js.map +1 -0
- package/dist/audit/push-report.d.ts +13 -0
- package/dist/audit/push-report.js +66 -0
- package/dist/audit/push-report.js.map +1 -0
- package/dist/audit-recorder-K1Ns3tfC.d.ts +31 -0
- package/dist/auth/authenticate.d.ts +14 -0
- package/dist/auth/authenticate.js +59 -0
- package/dist/auth/authenticate.js.map +1 -0
- package/dist/auth/authorize.d.ts +12 -0
- package/dist/auth/authorize.js +57 -0
- package/dist/auth/authorize.js.map +1 -0
- package/dist/auth/read-token-auth.d.ts +60 -0
- package/dist/auth/read-token-auth.js +83 -0
- package/dist/auth/read-token-auth.js.map +1 -0
- package/dist/cache/decision-cache.d.ts +2 -0
- package/dist/cache/decision-cache.js +45 -0
- package/dist/cache/decision-cache.js.map +1 -0
- package/dist/commit-workflow-schemas-BWlg7bZR.d.ts +95 -0
- package/dist/decision-cache-87U32iSj.d.ts +24 -0
- package/dist/gateway-app.d.ts +69 -0
- package/dist/gateway-app.js +80 -0
- package/dist/gateway-app.js.map +1 -0
- package/dist/gateway-context-Boh9I0mk.d.ts +54 -0
- package/dist/gateway-env-DnsBU4Pq.d.ts +40 -0
- package/dist/gateway-worker.d.ts +13 -0
- package/dist/gateway-worker.js +22 -0
- package/dist/gateway-worker.js.map +1 -0
- package/dist/git-route-Spxi09Rm.d.ts +22 -0
- package/dist/health.d.ts +6 -0
- package/dist/health.js +16 -0
- package/dist/health.js.map +1 -0
- package/dist/logger-BZ1zyKLb.d.ts +27 -0
- package/dist/logging/logger.d.ts +2 -0
- package/dist/logging/logger.js +15 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/on-push-DTREOB__.d.ts +62 -0
- package/dist/policy/branch-policy.d.ts +24 -0
- package/dist/policy/branch-policy.js +125 -0
- package/dist/policy/branch-policy.js.map +1 -0
- package/dist/policy/enforce-branch-policy.d.ts +30 -0
- package/dist/policy/enforce-branch-policy.js +214 -0
- package/dist/policy/enforce-branch-policy.js.map +1 -0
- package/dist/policy/fast-forward.d.ts +32 -0
- package/dist/policy/fast-forward.js +95 -0
- package/dist/policy/fast-forward.js.map +1 -0
- package/dist/policy/ref-pattern.d.ts +7 -0
- package/dist/policy/ref-pattern.js +25 -0
- package/dist/policy/ref-pattern.js.map +1 -0
- package/dist/policy/rule-branch-policy.d.ts +17 -0
- package/dist/policy/rule-branch-policy.js +69 -0
- package/dist/policy/rule-branch-policy.js.map +1 -0
- package/dist/proxy/defer.d.ts +8 -0
- package/dist/proxy/defer.js +15 -0
- package/dist/proxy/defer.js.map +1 -0
- package/dist/proxy/forward-read.d.ts +11 -0
- package/dist/proxy/forward-read.js +53 -0
- package/dist/proxy/forward-read.js.map +1 -0
- package/dist/proxy/gateway-deps.d.ts +39 -0
- package/dist/proxy/gateway-deps.js +0 -0
- package/dist/proxy/gateway-env.d.ts +2 -0
- package/dist/proxy/gateway-env.js +0 -0
- package/dist/proxy/git-responses.d.ts +25 -0
- package/dist/proxy/git-responses.js +83 -0
- package/dist/proxy/git-responses.js.map +1 -0
- package/dist/proxy/git-route.d.ts +2 -0
- package/dist/proxy/git-route.js +34 -0
- package/dist/proxy/git-route.js.map +1 -0
- package/dist/proxy/push-events.d.ts +28 -0
- package/dist/proxy/push-events.js +65 -0
- package/dist/proxy/push-events.js.map +1 -0
- package/dist/proxy/push-guard.d.ts +43 -0
- package/dist/proxy/push-guard.js +81 -0
- package/dist/proxy/push-guard.js.map +1 -0
- package/dist/proxy/push-options.d.ts +16 -0
- package/dist/proxy/push-options.js +33 -0
- package/dist/proxy/push-options.js.map +1 -0
- package/dist/proxy/receive-pack.d.ts +17 -0
- package/dist/proxy/receive-pack.js +244 -0
- package/dist/proxy/receive-pack.js.map +1 -0
- package/dist/proxy/request-log.d.ts +13 -0
- package/dist/proxy/request-log.js +41 -0
- package/dist/proxy/request-log.js.map +1 -0
- package/dist/proxy/resolve-route.d.ts +9 -0
- package/dist/proxy/resolve-route.js +22 -0
- package/dist/proxy/resolve-route.js.map +1 -0
- package/dist/proxy/upstream-call.d.ts +23 -0
- package/dist/proxy/upstream-call.js +63 -0
- package/dist/proxy/upstream-call.js.map +1 -0
- package/dist/proxy/upstream.d.ts +2 -0
- package/dist/proxy/upstream.js +68 -0
- package/dist/proxy/upstream.js.map +1 -0
- package/dist/push-event-f-5TsFUY.d.ts +15 -0
- package/dist/push-policy/changed-paths.d.ts +26 -0
- package/dist/push-policy/changed-paths.js +117 -0
- package/dist/push-policy/changed-paths.js.map +1 -0
- package/dist/push-policy/pushed-objects.d.ts +30 -0
- package/dist/push-policy/pushed-objects.js +76 -0
- package/dist/push-policy/pushed-objects.js.map +1 -0
- package/dist/push-policy/run-push-policy.d.ts +35 -0
- package/dist/push-policy/run-push-policy.js +77 -0
- package/dist/push-policy/run-push-policy.js.map +1 -0
- package/dist/push-policy/tree-diff.d.ts +21 -0
- package/dist/push-policy/tree-diff.js +78 -0
- package/dist/push-policy/tree-diff.js.map +1 -0
- package/dist/push-policy/uncheckable.d.ts +14 -0
- package/dist/push-policy/uncheckable.js +25 -0
- package/dist/push-policy/uncheckable.js.map +1 -0
- package/dist/rpc/branch-methods.d.ts +16 -0
- package/dist/rpc/branch-methods.js +43 -0
- package/dist/rpc/branch-methods.js.map +1 -0
- package/dist/rpc/commit-attempt.d.ts +27 -0
- package/dist/rpc/commit-attempt.js +85 -0
- package/dist/rpc/commit-attempt.js.map +1 -0
- package/dist/rpc/commit-graph.d.ts +20 -0
- package/dist/rpc/commit-graph.js +38 -0
- package/dist/rpc/commit-graph.js.map +1 -0
- package/dist/rpc/commit-method.d.ts +13 -0
- package/dist/rpc/commit-method.js +30 -0
- package/dist/rpc/commit-method.js.map +1 -0
- package/dist/rpc/commit-source.d.ts +12 -0
- package/dist/rpc/commit-source.js +9 -0
- package/dist/rpc/commit-source.js.map +1 -0
- package/dist/rpc/gateway-context.d.ts +2 -0
- package/dist/rpc/gateway-context.js +30 -0
- package/dist/rpc/gateway-context.js.map +1 -0
- package/dist/rpc/gateway-rpc.d.ts +10 -0
- package/dist/rpc/gateway-rpc.js +48 -0
- package/dist/rpc/gateway-rpc.js.map +1 -0
- package/dist/rpc/gateway-service.d.ts +24 -0
- package/dist/rpc/gateway-service.js +63 -0
- package/dist/rpc/gateway-service.js.map +1 -0
- package/dist/rpc/history-page.d.ts +35 -0
- package/dist/rpc/history-page.js +122 -0
- package/dist/rpc/history-page.js.map +1 -0
- package/dist/rpc/lifecycle-methods.d.ts +12 -0
- package/dist/rpc/lifecycle-methods.js +39 -0
- package/dist/rpc/lifecycle-methods.js.map +1 -0
- package/dist/rpc/paint-down.d.ts +17 -0
- package/dist/rpc/paint-down.js +96 -0
- package/dist/rpc/paint-down.js.map +1 -0
- package/dist/rpc/promote-attempt.d.ts +21 -0
- package/dist/rpc/promote-attempt.js +55 -0
- package/dist/rpc/promote-attempt.js.map +1 -0
- package/dist/rpc/push-ref.d.ts +21 -0
- package/dist/rpc/push-ref.js +47 -0
- package/dist/rpc/push-ref.js.map +1 -0
- package/dist/rpc/read-methods.d.ts +17 -0
- package/dist/rpc/read-methods.js +94 -0
- package/dist/rpc/read-methods.js.map +1 -0
- package/dist/rpc/read-token.d.ts +39 -0
- package/dist/rpc/read-token.js +107 -0
- package/dist/rpc/read-token.js.map +1 -0
- package/dist/rpc/repo-access.d.ts +36 -0
- package/dist/rpc/repo-access.js +75 -0
- package/dist/rpc/repo-access.js.map +1 -0
- package/dist/rpc/sync-attempt.d.ts +23 -0
- package/dist/rpc/sync-attempt.js +105 -0
- package/dist/rpc/sync-attempt.js.map +1 -0
- package/dist/sync-workflow-schemas-DCNmNE5-.d.ts +57 -0
- package/dist/upstream-DvqMCj5N.d.ts +15 -0
- package/dist/workflow-starter-BqhwX6oA.d.ts +45 -0
- package/dist/workflows/commit-workflow-schemas.d.ts +2 -0
- package/dist/workflows/commit-workflow-schemas.js +84 -0
- package/dist/workflows/commit-workflow-schemas.js.map +1 -0
- package/dist/workflows/commit-workflow.d.ts +16 -0
- package/dist/workflows/commit-workflow.js +22 -0
- package/dist/workflows/commit-workflow.js.map +1 -0
- package/dist/workflows/on-push.d.ts +2 -0
- package/dist/workflows/on-push.js +90 -0
- package/dist/workflows/on-push.js.map +1 -0
- package/dist/workflows/push-event.d.ts +2 -0
- package/dist/workflows/push-event.js +28 -0
- package/dist/workflows/push-event.js.map +1 -0
- package/dist/workflows/run-commit-workflow.d.ts +15 -0
- package/dist/workflows/run-commit-workflow.js +49 -0
- package/dist/workflows/run-commit-workflow.js.map +1 -0
- package/dist/workflows/run-sync-workflow.d.ts +14 -0
- package/dist/workflows/run-sync-workflow.js +67 -0
- package/dist/workflows/run-sync-workflow.js.map +1 -0
- package/dist/workflows/step-errors.d.ts +15 -0
- package/dist/workflows/step-errors.js +24 -0
- package/dist/workflows/step-errors.js.map +1 -0
- package/dist/workflows/step-runner.d.ts +31 -0
- package/dist/workflows/step-runner.js +6 -0
- package/dist/workflows/step-runner.js.map +1 -0
- package/dist/workflows/sync-branch-workflow.d.ts +15 -0
- package/dist/workflows/sync-branch-workflow.js +21 -0
- package/dist/workflows/sync-branch-workflow.js.map +1 -0
- package/dist/workflows/sync-workflow-schemas.d.ts +2 -0
- package/dist/workflows/sync-workflow-schemas.js +104 -0
- package/dist/workflows/sync-workflow-schemas.js.map +1 -0
- package/dist/workflows/workflow-instance-id.d.ts +12 -0
- package/dist/workflows/workflow-instance-id.js +33 -0
- package/dist/workflows/workflow-instance-id.js.map +1 -0
- package/dist/workflows/workflow-runtime.d.ts +7 -0
- package/dist/workflows/workflow-runtime.js +11 -0
- package/dist/workflows/workflow-runtime.js.map +1 -0
- package/dist/workflows/workflow-starter.d.ts +2 -0
- package/dist/workflows/workflow-starter.js +40 -0
- package/dist/workflows/workflow-starter.js.map +1 -0
- package/package.json +381 -4
- package/src/audit/audit-recorder.ts +74 -0
- package/src/audit/push-report.ts +60 -0
- package/src/auth/authenticate.ts +68 -0
- package/src/auth/authorize.ts +57 -0
- package/src/auth/read-token-auth.ts +119 -0
- package/src/cache/decision-cache.ts +57 -0
- package/src/gateway-app.ts +147 -0
- package/src/gateway-worker.ts +32 -0
- package/src/health.ts +12 -0
- package/src/logging/logger.ts +40 -0
- package/src/policy/branch-policy.ts +119 -0
- package/src/policy/enforce-branch-policy.ts +213 -0
- package/src/policy/fast-forward.ts +136 -0
- package/src/policy/ref-pattern.ts +24 -0
- package/src/policy/rule-branch-policy.ts +76 -0
- package/src/proxy/defer.ts +18 -0
- package/src/proxy/forward-read.ts +52 -0
- package/src/proxy/gateway-deps.ts +43 -0
- package/src/proxy/gateway-env.ts +43 -0
- package/src/proxy/git-responses.ts +89 -0
- package/src/proxy/git-route.ts +49 -0
- package/src/proxy/push-events.ts +91 -0
- package/src/proxy/push-guard.ts +109 -0
- package/src/proxy/push-options.ts +38 -0
- package/src/proxy/receive-pack.ts +257 -0
- package/src/proxy/request-log.ts +44 -0
- package/src/proxy/resolve-route.ts +24 -0
- package/src/proxy/upstream-call.ts +71 -0
- package/src/proxy/upstream.ts +76 -0
- package/src/push-policy/changed-paths.ts +143 -0
- package/src/push-policy/pushed-objects.ts +88 -0
- package/src/push-policy/run-push-policy.ts +99 -0
- package/src/push-policy/tree-diff.ts +107 -0
- package/src/push-policy/uncheckable.ts +27 -0
- package/src/rpc/branch-methods.ts +57 -0
- package/src/rpc/commit-attempt.ts +87 -0
- package/src/rpc/commit-graph.ts +44 -0
- package/src/rpc/commit-method.ts +36 -0
- package/src/rpc/commit-source.ts +15 -0
- package/src/rpc/gateway-context.ts +77 -0
- package/src/rpc/gateway-rpc.ts +65 -0
- package/src/rpc/gateway-service.ts +85 -0
- package/src/rpc/history-page.ts +163 -0
- package/src/rpc/lifecycle-methods.ts +50 -0
- package/src/rpc/paint-down.ts +120 -0
- package/src/rpc/promote-attempt.ts +52 -0
- package/src/rpc/push-ref.ts +54 -0
- package/src/rpc/read-methods.ts +111 -0
- package/src/rpc/read-token.ts +135 -0
- package/src/rpc/repo-access.ts +112 -0
- package/src/rpc/sync-attempt.ts +84 -0
- package/src/workflows/commit-workflow-schemas.ts +95 -0
- package/src/workflows/commit-workflow.ts +39 -0
- package/src/workflows/on-push.ts +128 -0
- package/src/workflows/push-event.ts +41 -0
- package/src/workflows/run-commit-workflow.ts +45 -0
- package/src/workflows/run-sync-workflow.ts +52 -0
- package/src/workflows/step-errors.ts +26 -0
- package/src/workflows/step-runner.ts +36 -0
- package/src/workflows/sync-branch-workflow.ts +40 -0
- package/src/workflows/sync-workflow-schemas.ts +122 -0
- package/src/workflows/workflow-instance-id.ts +26 -0
- package/src/workflows/workflow-runtime.ts +13 -0
- package/src/workflows/workflow-starter.ts +69 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { EXAMPLE_SHA_A, EXAMPLE_SHA_B } from '@leed-ai/git-gateway-contract/schemas/examples';
|
|
3
|
+
import { BranchName, FilePath, RepoName } from '@leed-ai/git-gateway-contract/schemas/names';
|
|
4
|
+
import { Sha } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
/** What a SyncBranchWorkflow instance is started with. */
|
|
8
|
+
export const SyncBranchWorkflowParams = z
|
|
9
|
+
.object({
|
|
10
|
+
repo: RepoName.describe('Repository holding both branches.'),
|
|
11
|
+
from: BranchName.describe('Branch whose commits are brought into `into`, e.g. main.'),
|
|
12
|
+
into: BranchName.describe('Branch brought up to date, e.g. staging; only fast-forwarded or merged into.'),
|
|
13
|
+
fromHead: Sha.describe(
|
|
14
|
+
'Head of `from` the push left; identifies the instance, so one push starts at most one sync.',
|
|
15
|
+
),
|
|
16
|
+
})
|
|
17
|
+
.strict()
|
|
18
|
+
.refine((params) => params.from !== params.into, { message: 'from and into must differ', path: ['into'] })
|
|
19
|
+
.meta({
|
|
20
|
+
id: 'SyncBranchWorkflowParams',
|
|
21
|
+
title: 'SyncBranchWorkflow params',
|
|
22
|
+
description:
|
|
23
|
+
'The params a SyncBranchWorkflow instance is started with by an onPush sync rule: sync `into` with `from` after a push moved `from` to `fromHead`. The sync merges whatever `from` holds when it runs.',
|
|
24
|
+
examples: [{ repo: 'acme-raw-content', from: 'main', into: 'staging', fromHead: EXAMPLE_SHA_B }],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** SyncBranchWorkflow params, inferred from {@link SyncBranchWorkflowParams}. */
|
|
28
|
+
export type SyncBranchWorkflowParams = z.infer<typeof SyncBranchWorkflowParams>;
|
|
29
|
+
|
|
30
|
+
/** `into` already contained `from`. */
|
|
31
|
+
export const SyncStepUpToDate = z
|
|
32
|
+
.object({
|
|
33
|
+
status: z
|
|
34
|
+
.literal('up_to_date')
|
|
35
|
+
.describe('`into` already contained `from` (possibly because an earlier run of the step merged it).'),
|
|
36
|
+
head: Sha.describe('Head of `into`, unchanged.'),
|
|
37
|
+
})
|
|
38
|
+
.strict()
|
|
39
|
+
.meta({
|
|
40
|
+
id: 'SyncStepUpToDate',
|
|
41
|
+
title: 'sync step: up to date',
|
|
42
|
+
description: "The SyncBranchWorkflow's sync step found nothing to do.",
|
|
43
|
+
examples: [{ status: 'up_to_date', head: EXAMPLE_SHA_A }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** `into` was fast-forwarded. */
|
|
47
|
+
export const SyncStepFastForward = z
|
|
48
|
+
.object({
|
|
49
|
+
status: z.literal('fast_forward').describe('`into` had no commits of its own and was fast-forwarded.'),
|
|
50
|
+
head: Sha.describe('New head of `into`, the head of `from`.'),
|
|
51
|
+
})
|
|
52
|
+
.strict()
|
|
53
|
+
.meta({
|
|
54
|
+
id: 'SyncStepFastForward',
|
|
55
|
+
title: 'sync step: fast-forward',
|
|
56
|
+
description: "The SyncBranchWorkflow's sync step fast-forwarded `into`.",
|
|
57
|
+
examples: [{ status: 'fast_forward', head: EXAMPLE_SHA_B }],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/** A merge commit was written. */
|
|
61
|
+
export const SyncStepMerged = z
|
|
62
|
+
.object({
|
|
63
|
+
status: z.literal('merged').describe('Both branches had commits; a file-level merge commit was written.'),
|
|
64
|
+
head: Sha.describe('New head of `into`, the merge commit.'),
|
|
65
|
+
})
|
|
66
|
+
.strict()
|
|
67
|
+
.meta({
|
|
68
|
+
id: 'SyncStepMerged',
|
|
69
|
+
title: 'sync step: merged',
|
|
70
|
+
description: "The SyncBranchWorkflow's sync step wrote a merge commit to `into`.",
|
|
71
|
+
examples: [{ status: 'merged', head: EXAMPLE_SHA_B }],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
/** The branches conflict. */
|
|
75
|
+
export const SyncStepConflict = z
|
|
76
|
+
.object({
|
|
77
|
+
status: z
|
|
78
|
+
.literal('conflict')
|
|
79
|
+
.describe(
|
|
80
|
+
'Some paths changed differently on both sides; nothing was written and retrying cannot help.',
|
|
81
|
+
),
|
|
82
|
+
paths: z
|
|
83
|
+
.array(FilePath.describe('A conflicting path.'))
|
|
84
|
+
.min(1)
|
|
85
|
+
.describe('Every conflicting path, sorted in byte order (paths only, never content).'),
|
|
86
|
+
base: Sha.describe('The merge base used.'),
|
|
87
|
+
fromHead: Sha.describe('The head of `from` merged.'),
|
|
88
|
+
intoHead: Sha.describe('The head of `into`, unchanged.'),
|
|
89
|
+
})
|
|
90
|
+
.strict()
|
|
91
|
+
.meta({
|
|
92
|
+
id: 'SyncStepConflict',
|
|
93
|
+
title: 'sync step: conflict',
|
|
94
|
+
description:
|
|
95
|
+
"The SyncBranchWorkflow's sync step could not merge; the instance completes with this result so the conflict can be reported and resolved by hand.",
|
|
96
|
+
examples: [
|
|
97
|
+
{
|
|
98
|
+
status: 'conflict',
|
|
99
|
+
paths: ['content/about.md'],
|
|
100
|
+
base: '4b825dc642cb6eb9a060e54bf8d69288fbee4904',
|
|
101
|
+
fromHead: EXAMPLE_SHA_B,
|
|
102
|
+
intoHead: EXAMPLE_SHA_A,
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* What the SyncBranchWorkflow's sync step stores and the instance returns: ids and paths only,
|
|
109
|
+
* never file content or a credential.
|
|
110
|
+
*/
|
|
111
|
+
export const SyncStepResult = z
|
|
112
|
+
.discriminatedUnion('status', [SyncStepUpToDate, SyncStepFastForward, SyncStepMerged, SyncStepConflict])
|
|
113
|
+
.meta({
|
|
114
|
+
id: 'SyncStepResult',
|
|
115
|
+
title: 'SyncBranchWorkflow result',
|
|
116
|
+
description:
|
|
117
|
+
"What the SyncBranchWorkflow's sync step stores and the instance returns, discriminated by `status`: commit ids and conflicting paths only.",
|
|
118
|
+
examples: [{ status: 'merged', head: EXAMPLE_SHA_B }],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/** A SyncBranchWorkflow result, inferred from {@link SyncStepResult}. */
|
|
122
|
+
export type SyncStepResult = z.infer<typeof SyncStepResult>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { utf8 } from '@leed-ai/git-gateway-contract/reference/bytes';
|
|
3
|
+
import { bytesToHex, sha1Bytes } from '@leed-ai/git-gateway-core/bytes';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A Workflow instance id derived from what identifies the work, so starting the same work twice
|
|
7
|
+
* names the same instance: `<prefix>-<40 hex digits>`, the SHA-1 of the parts joined by NUL.
|
|
8
|
+
* Always at most 100 characters of `[a-z0-9-]` for prefixes of that alphabet, which is what
|
|
9
|
+
* Workflows accepts as an instance id.
|
|
10
|
+
*/
|
|
11
|
+
export async function workflowInstanceId(prefix: string, parts: readonly string[]): Promise<string> {
|
|
12
|
+
if (!/^[a-z][a-z0-9-]{0,40}$/.test(prefix)) {
|
|
13
|
+
throw new RangeError(`workflowInstanceId: invalid prefix ${JSON.stringify(prefix)}`);
|
|
14
|
+
}
|
|
15
|
+
return `${prefix}-${bytesToHex(await sha1Bytes(utf8(parts.join('\0'))))}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The id of the CommitWorkflow instance for one idempotent commit. */
|
|
19
|
+
export function commitWorkflowId(repo: string, branch: string, idempotencyKey: string): Promise<string> {
|
|
20
|
+
return workflowInstanceId('commit', [repo, branch, idempotencyKey]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The id of the SyncBranchWorkflow instance that syncs `into` with `from` at one head of `from`. */
|
|
24
|
+
export function syncWorkflowId(repo: string, from: string, into: string, fromHead: string): Promise<string> {
|
|
25
|
+
return workflowInstanceId('sync', [repo, from, into, fromHead]);
|
|
26
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { WorkflowStep } from 'cloudflare:workers';
|
|
3
|
+
import { NonRetryableError } from 'cloudflare:workflows';
|
|
4
|
+
|
|
5
|
+
import type { FatalError, StepRunner } from './step-runner.ts';
|
|
6
|
+
|
|
7
|
+
/** A Workflows `WorkflowStep` as the gateway's {@link StepRunner}. */
|
|
8
|
+
export function stepRunnerOf(step: WorkflowStep): StepRunner {
|
|
9
|
+
return { do: (name, config, callback) => step.do(name, config, callback) };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Stops a Workflow instance without retries, keeping the error's message (and any GatewayError body in it). */
|
|
13
|
+
export const nonRetryable: FatalError = (error) => new NonRetryableError(error.message, error.name);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { storageUnavailable } from '@leed-ai/git-gateway-contract/storage/storage-errors';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The part of a Workflows instance handle (`WorkflowInstance`) the gateway uses: read its status,
|
|
6
|
+
* or run it again from the start with the params it was created with.
|
|
7
|
+
*/
|
|
8
|
+
export interface WorkflowHandle {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
/** The instance's current status; `status` is one of the Workflows instance states. */
|
|
11
|
+
status(): Promise<{ readonly status: string }>;
|
|
12
|
+
/** Runs the instance again from its first step, with the params it was created with. */
|
|
13
|
+
restart(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The part of a Workflows binding (`env.COMMIT_WORKFLOW`) the gateway uses: start an instance
|
|
18
|
+
* under a chosen id, or look one up. A real `Workflow<P>` binding satisfies it; tests pass a fake.
|
|
19
|
+
*/
|
|
20
|
+
export interface WorkflowStarter<P> {
|
|
21
|
+
/** Starts an instance with this id and params; rejects when the id is already taken. */
|
|
22
|
+
create(options: { id: string; params: P }): Promise<{ readonly id: string }>;
|
|
23
|
+
/** The instance with this id; rejects when there is none. */
|
|
24
|
+
get(id: string): Promise<WorkflowHandle>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What {@link startWorkflowOnce} did: started a new instance, found a live or finished one
|
|
29
|
+
* already under the id, or found a dead one (errored or terminated) and restarted it.
|
|
30
|
+
*/
|
|
31
|
+
export type WorkflowStart = 'created' | 'existing' | 'restarted';
|
|
32
|
+
|
|
33
|
+
/** Instance states after which the instance will never run again unless restarted. */
|
|
34
|
+
const DEAD_STATES: ReadonlySet<string> = new Set(['errored', 'terminated']);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Starts a Workflow instance under a deterministic id, at most once while it lives: when the id
|
|
38
|
+
* is already taken by a queued, running, waiting, paused or complete instance (the same write was
|
|
39
|
+
* handed off before), that instance stands and nothing new starts. When the instance under the id
|
|
40
|
+
* is dead — errored after its retries ran out, or terminated — it is restarted, so retrying the
|
|
41
|
+
* same write is never answered by an instance that will not run. Restarting is safe because the
|
|
42
|
+
* workflows' steps are idempotent. Throws storage_unavailable when the instance can be neither
|
|
43
|
+
* created nor found, or when its status cannot be read or it cannot be restarted.
|
|
44
|
+
*/
|
|
45
|
+
export async function startWorkflowOnce<P>(
|
|
46
|
+
starter: WorkflowStarter<P>,
|
|
47
|
+
id: string,
|
|
48
|
+
params: P,
|
|
49
|
+
): Promise<WorkflowStart> {
|
|
50
|
+
try {
|
|
51
|
+
await starter.create({ id, params });
|
|
52
|
+
return 'created';
|
|
53
|
+
} catch (createError) {
|
|
54
|
+
let handle: WorkflowHandle;
|
|
55
|
+
try {
|
|
56
|
+
handle = await starter.get(id);
|
|
57
|
+
} catch {
|
|
58
|
+
throw storageUnavailable(`start workflow instance ${id}`, createError);
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const { status } = await handle.status();
|
|
62
|
+
if (!DEAD_STATES.has(status)) return 'existing';
|
|
63
|
+
await handle.restart();
|
|
64
|
+
return 'restarted';
|
|
65
|
+
} catch (error) {
|
|
66
|
+
throw storageUnavailable(`restart workflow instance ${id}`, error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|