@learnpack/learnpack 5.0.352 → 5.0.353
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/lib/commands/publish.js +6 -0
- package/lib/commands/serve.js +85 -259
- package/lib/models/creator.d.ts +6 -0
- package/lib/scripts/descriptionsGcsBackfill.d.ts +1 -0
- package/lib/scripts/descriptionsGcsBackfill.js +141 -0
- package/lib/scripts/descriptionsS3Backfill.d.ts +1 -0
- package/lib/scripts/descriptionsS3Backfill.js +157 -0
- package/lib/scripts/descriptionsSweep.d.ts +1 -0
- package/lib/scripts/descriptionsSweep.js +142 -0
- package/lib/utils/api.d.ts +7 -0
- package/lib/utils/api.js +8 -1
- package/lib/utils/creatorUtilities.js +2 -1
- package/lib/utils/descriptionHash.d.ts +66 -0
- package/lib/utils/descriptionHash.js +173 -0
- package/lib/utils/descriptions/gcsStorage.d.ts +16 -0
- package/lib/utils/descriptions/gcsStorage.js +60 -0
- package/lib/utils/descriptions/generateCourseDescriptions.d.ts +59 -0
- package/lib/utils/descriptions/generateCourseDescriptions.js +173 -0
- package/lib/utils/descriptions/mirrorDescriptions.d.ts +49 -0
- package/lib/utils/descriptions/mirrorDescriptions.js +109 -0
- package/lib/utils/descriptions/publishStage.d.ts +69 -0
- package/lib/utils/descriptions/publishStage.js +234 -0
- package/lib/utils/descriptions/resumePublication.d.ts +36 -0
- package/lib/utils/descriptions/resumePublication.js +113 -0
- package/lib/utils/descriptions/s3Storage.d.ts +30 -0
- package/lib/utils/descriptions/s3Storage.js +134 -0
- package/lib/utils/descriptions/workList.d.ts +75 -0
- package/lib/utils/descriptions/workList.js +177 -0
- package/lib/utils/gcsBucketName.d.ts +10 -0
- package/lib/utils/gcsBucketName.js +19 -0
- package/lib/utils/packageManifest.d.ts +14 -0
- package/lib/utils/packageManifest.js +49 -5
- package/lib/utils/publishEvents.d.ts +66 -0
- package/lib/utils/publishEvents.js +111 -0
- package/lib/utils/publishJournal.d.ts +119 -0
- package/lib/utils/publishJournal.js +275 -0
- package/lib/utils/rigoActions.d.ts +44 -0
- package/lib/utils/rigoActions.js +75 -1
- package/lib/utils/s3/packageManifestBackfill.d.ts +2 -0
- package/lib/utils/s3/packageManifestBackfill.js +2 -0
- package/lib/utils/syllabusSync.d.ts +71 -0
- package/lib/utils/syllabusSync.js +273 -0
- package/package.json +1 -1
- package/src/commands/publish.ts +7 -0
- package/src/commands/serve.ts +144 -335
- package/src/models/creator.ts +9 -0
- package/src/scripts/descriptionsGcsBackfill.ts +193 -0
- package/src/scripts/descriptionsS3Backfill.ts +208 -0
- package/src/scripts/descriptionsSweep.ts +185 -0
- package/src/ui/_app/app.css +1 -1
- package/src/ui/_app/app.js +142 -140
- package/src/ui/app.tar.gz +0 -0
- package/src/utils/api.ts +9 -0
- package/src/utils/creatorUtilities.ts +2 -1
- package/src/utils/descriptionHash.ts +196 -0
- package/src/utils/descriptions/gcsStorage.ts +67 -0
- package/src/utils/descriptions/generateCourseDescriptions.ts +301 -0
- package/src/utils/descriptions/mirrorDescriptions.ts +191 -0
- package/src/utils/descriptions/publishStage.ts +382 -0
- package/src/utils/descriptions/resumePublication.ts +200 -0
- package/src/utils/descriptions/s3Storage.ts +206 -0
- package/src/utils/descriptions/workList.ts +283 -0
- package/src/utils/gcsBucketName.ts +19 -0
- package/src/utils/packageManifest.ts +54 -4
- package/src/utils/publishEvents.ts +181 -0
- package/src/utils/publishJournal.ts +383 -0
- package/src/utils/rigoActions.ts +130 -0
- package/src/utils/s3/packageManifestBackfill.ts +2 -2
- package/src/utils/syllabusSync.ts +390 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Syllabus } from "../../models/creator"
|
|
2
|
+
import { fingerprintReadme } from "../descriptionHash"
|
|
3
|
+
import { DESCRIPTION_PROMPT_VERSION } from "../packageManifest"
|
|
4
|
+
import {
|
|
5
|
+
CourseDescriptionsStorage,
|
|
6
|
+
readCourseReadmes,
|
|
7
|
+
DEFAULT_DESCRIPTIONS_CONCURRENCY,
|
|
8
|
+
} from "./generateCourseDescriptions"
|
|
9
|
+
import { CourseExercise, CourseReadmes, findSyllabusLesson } from "./workList"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copy descriptions from the published syllabus back into the draft one.
|
|
13
|
+
*
|
|
14
|
+
* Descriptions are generated against the published package because it is an
|
|
15
|
+
* immutable snapshot. The draft in GCS, meanwhile, keeps moving: between the
|
|
16
|
+
* publication and the moment the descriptions land, steps may have been added,
|
|
17
|
+
* removed, renamed, reordered or edited.
|
|
18
|
+
*
|
|
19
|
+
* The guard against that drift is the content hash, not a timestamp or a
|
|
20
|
+
* position: a description is copied only when the draft README hashes to the
|
|
21
|
+
* very content the description was generated from. Anything that moved simply
|
|
22
|
+
* misses and waits for the next publication — a stale description is never
|
|
23
|
+
* written onto content it does not describe.
|
|
24
|
+
*
|
|
25
|
+
* Without this mirror the draft would never hold a description, and every
|
|
26
|
+
* publication would regenerate the whole course from scratch: the freshness
|
|
27
|
+
* check reads the fingerprints stored in the syllabus that travels inside the
|
|
28
|
+
* zip.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
export type MirrorResult = {
|
|
32
|
+
/** (step, language) pairs copied into the draft. */
|
|
33
|
+
copied: number;
|
|
34
|
+
/** Pairs with no reusable description: content moved on, or none published. */
|
|
35
|
+
missed: number;
|
|
36
|
+
/** Pairs the draft already had up to date. */
|
|
37
|
+
fresh: number;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type MirrorOptions = {
|
|
41
|
+
promptVersion?: number;
|
|
42
|
+
dryRun?: boolean;
|
|
43
|
+
concurrency?: number;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function emptyMirrorResult(): MirrorResult {
|
|
47
|
+
return { copied: 0, missed: 0, fresh: 0 }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Pure core: writes into `targetSyllabus` in place. Callers own the I/O, which
|
|
52
|
+
* keeps the drift rules testable without a bucket.
|
|
53
|
+
*/
|
|
54
|
+
export function mirrorDescriptionsIntoSyllabus(input: {
|
|
55
|
+
sourceSyllabus: Syllabus | null;
|
|
56
|
+
targetSyllabus: Syllabus;
|
|
57
|
+
exercises: CourseExercise[];
|
|
58
|
+
/** READMEs of the TARGET (draft), which is what must match the hash. */
|
|
59
|
+
readmes: CourseReadmes;
|
|
60
|
+
promptVersion?: number;
|
|
61
|
+
}): MirrorResult {
|
|
62
|
+
const result = emptyMirrorResult()
|
|
63
|
+
if (!input.sourceSyllabus) {
|
|
64
|
+
return result
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const promptVersion = input.promptVersion ?? DESCRIPTION_PROMPT_VERSION
|
|
68
|
+
|
|
69
|
+
for (const exercise of input.exercises) {
|
|
70
|
+
const targetLesson = findSyllabusLesson(
|
|
71
|
+
input.targetSyllabus,
|
|
72
|
+
exercise.slug
|
|
73
|
+
)
|
|
74
|
+
if (!targetLesson) {
|
|
75
|
+
// The step is not in the draft (removed, or renamed after publishing).
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const sourceLesson = findSyllabusLesson(
|
|
80
|
+
input.sourceSyllabus,
|
|
81
|
+
exercise.slug
|
|
82
|
+
)
|
|
83
|
+
const exerciseReadmes = input.readmes[exercise.slug] || {}
|
|
84
|
+
|
|
85
|
+
for (const lang of Object.keys(exercise.translations || {})) {
|
|
86
|
+
const content = exerciseReadmes[lang]
|
|
87
|
+
if (typeof content !== "string") {
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const fingerprint = fingerprintReadme(content)
|
|
92
|
+
const targetSlot = targetLesson.translations?.[lang]
|
|
93
|
+
|
|
94
|
+
if (targetSlot?.descriptionSource === "human") {
|
|
95
|
+
result.fresh += 1
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (
|
|
100
|
+
targetSlot?.sourceContentHash === fingerprint.sha256 &&
|
|
101
|
+
(targetSlot?.descriptionPromptVersion ?? 0) >= promptVersion
|
|
102
|
+
) {
|
|
103
|
+
result.fresh += 1
|
|
104
|
+
continue
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const sourceSlot = sourceLesson?.translations?.[lang]
|
|
108
|
+
// The hash guard: only reuse a description generated from exactly this
|
|
109
|
+
// content. Note a published `description: null` is reusable too — it is a
|
|
110
|
+
// real answer ("not enough content to describe"), not a missing one.
|
|
111
|
+
const reusable =
|
|
112
|
+
sourceSlot !== undefined &&
|
|
113
|
+
sourceSlot.sourceContentHash === fingerprint.sha256 &&
|
|
114
|
+
(sourceSlot.descriptionPromptVersion ?? 0) >= promptVersion &&
|
|
115
|
+
sourceSlot.descriptionStatus !== "error"
|
|
116
|
+
|
|
117
|
+
if (!reusable) {
|
|
118
|
+
result.missed += 1
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!targetLesson.translations) {
|
|
123
|
+
targetLesson.translations = {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!targetLesson.translations[lang]) {
|
|
127
|
+
targetLesson.translations[lang] = {
|
|
128
|
+
completionId: 0,
|
|
129
|
+
startedAt: Date.now(),
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const destination = targetLesson.translations[lang]
|
|
134
|
+
destination.description = sourceSlot.description ?? null
|
|
135
|
+
destination.descriptionStatus = sourceSlot.description ?
|
|
136
|
+
"generated" :
|
|
137
|
+
"none"
|
|
138
|
+
destination.descriptionSource = "auto"
|
|
139
|
+
destination.descriptionPromptVersion =
|
|
140
|
+
sourceSlot.descriptionPromptVersion ?? promptVersion
|
|
141
|
+
destination.sourceContentHash = fingerprint.sha256
|
|
142
|
+
destination.sourceSimHash = fingerprint.simhash
|
|
143
|
+
result.copied += 1
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return result
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Read the draft, mirror into it, and save it when something changed. */
|
|
151
|
+
export async function mirrorDescriptionsToDraft(
|
|
152
|
+
storage: CourseDescriptionsStorage,
|
|
153
|
+
courseSlug: string,
|
|
154
|
+
sourceSyllabus: Syllabus | null,
|
|
155
|
+
options: MirrorOptions = {}
|
|
156
|
+
): Promise<MirrorResult> {
|
|
157
|
+
if (!sourceSyllabus) {
|
|
158
|
+
return emptyMirrorResult()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const targetSyllabus = await storage.readSyllabus(courseSlug)
|
|
162
|
+
if (!targetSyllabus) {
|
|
163
|
+
return emptyMirrorResult()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const exercises = await storage.listExercises(courseSlug)
|
|
167
|
+
if (exercises.length === 0) {
|
|
168
|
+
return emptyMirrorResult()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const readmes = await readCourseReadmes(
|
|
172
|
+
storage,
|
|
173
|
+
courseSlug,
|
|
174
|
+
exercises,
|
|
175
|
+
options.concurrency ?? DEFAULT_DESCRIPTIONS_CONCURRENCY
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
const result = mirrorDescriptionsIntoSyllabus({
|
|
179
|
+
sourceSyllabus,
|
|
180
|
+
targetSyllabus,
|
|
181
|
+
exercises,
|
|
182
|
+
readmes,
|
|
183
|
+
promptVersion: options.promptVersion,
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
if (result.copied > 0 && !options.dryRun) {
|
|
187
|
+
await storage.writeSyllabus(courseSlug, targetSyllabus)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return result
|
|
191
|
+
}
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import * as fs from "fs"
|
|
2
|
+
import * as path from "path"
|
|
3
|
+
import { Bucket } from "@google-cloud/storage"
|
|
4
|
+
import { Syllabus } from "../../models/creator"
|
|
5
|
+
import {
|
|
6
|
+
finalizeJournal,
|
|
7
|
+
JournalStorage,
|
|
8
|
+
markStage,
|
|
9
|
+
PublishJournal,
|
|
10
|
+
} from "../publishJournal"
|
|
11
|
+
import {
|
|
12
|
+
CourseDescriptionsStorage,
|
|
13
|
+
generateCourseDescriptions,
|
|
14
|
+
GenerateCourseDescriptionsOptions,
|
|
15
|
+
GenerateCourseDescriptionsResult,
|
|
16
|
+
} from "./generateCourseDescriptions"
|
|
17
|
+
import { createGcsDescriptionsStorage } from "./gcsStorage"
|
|
18
|
+
import { mirrorDescriptionsToDraft, MirrorResult } from "./mirrorDescriptions"
|
|
19
|
+
import { createS3DescriptionsStorageFromEnv } from "./s3Storage"
|
|
20
|
+
import {
|
|
21
|
+
buildPackageManifestUpdatedEvent,
|
|
22
|
+
PublishEventContext,
|
|
23
|
+
sendPublishEvent,
|
|
24
|
+
} from "../publishEvents"
|
|
25
|
+
import {
|
|
26
|
+
buildDescriptionWorkList,
|
|
27
|
+
countWorkItems,
|
|
28
|
+
CourseExercise,
|
|
29
|
+
CourseReadmes,
|
|
30
|
+
} from "./workList"
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Stage B of a publication: generate the descriptions the new content needs.
|
|
34
|
+
*
|
|
35
|
+
* It runs in the background, after the HTTP response, because a single
|
|
36
|
+
* completion takes ~15s and a course may need several — far past any request
|
|
37
|
+
* limit. The publication itself is already finished and durable by then; this
|
|
38
|
+
* stage only enriches the published package.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether the freshly built package needs any description, decided from the
|
|
43
|
+
* build directory that is still on disk after zipping.
|
|
44
|
+
*
|
|
45
|
+
* Reading from disk (instead of re-reading GCS or S3) is free and, at this
|
|
46
|
+
* exact moment, all three hold the same content: the answer is what the publish
|
|
47
|
+
* response promises to breathecode, so it must be cheap enough to compute
|
|
48
|
+
* before responding.
|
|
49
|
+
*/
|
|
50
|
+
/** Reads a path relative to the build root; null when it does not exist. */
|
|
51
|
+
export type BuildFileReader = (relativePath: string) => string | null;
|
|
52
|
+
|
|
53
|
+
export function createBuildFileReader(buildRoot: string): BuildFileReader {
|
|
54
|
+
return relativePath => {
|
|
55
|
+
try {
|
|
56
|
+
return fs.readFileSync(
|
|
57
|
+
path.join(buildRoot, ...relativePath.split("/")),
|
|
58
|
+
"utf8"
|
|
59
|
+
)
|
|
60
|
+
} catch {
|
|
61
|
+
return null
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function countPendingDescriptions(
|
|
67
|
+
buildRoot: string,
|
|
68
|
+
exercises: CourseExercise[],
|
|
69
|
+
readFile: BuildFileReader = createBuildFileReader(buildRoot)
|
|
70
|
+
): number {
|
|
71
|
+
const rawSyllabus = readFile(".learn/initialSyllabus.json")
|
|
72
|
+
if (!rawSyllabus) {
|
|
73
|
+
return 0
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let syllabus: Syllabus
|
|
77
|
+
try {
|
|
78
|
+
syllabus = JSON.parse(rawSyllabus) as Syllabus
|
|
79
|
+
} catch {
|
|
80
|
+
return 0
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const readmes: CourseReadmes = {}
|
|
84
|
+
for (const exercise of exercises) {
|
|
85
|
+
for (const [lang, fileName] of Object.entries(
|
|
86
|
+
exercise.translations || {}
|
|
87
|
+
)) {
|
|
88
|
+
const content = readFile(`exercises/${exercise.slug}/${fileName}`)
|
|
89
|
+
if (content === null) {
|
|
90
|
+
// Missing translation file: nothing to fingerprint, nothing to ask for.
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!readmes[exercise.slug]) {
|
|
95
|
+
readmes[exercise.slug] = {}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
readmes[exercise.slug][lang] = content
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return countWorkItems(
|
|
103
|
+
buildDescriptionWorkList({ syllabus, exercises, readmes })
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type PublishDescriptionsStageResult = {
|
|
108
|
+
generation: GenerateCourseDescriptionsResult | null;
|
|
109
|
+
mirror: MirrorResult | null;
|
|
110
|
+
/** False when generation failed outright; drives the event status. */
|
|
111
|
+
succeeded: boolean;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Generate against the published package, then mirror what still matches back
|
|
116
|
+
* into the draft. Stages are recorded as they go so an interrupted run can be
|
|
117
|
+
* resumed by the sweep.
|
|
118
|
+
*/
|
|
119
|
+
export async function runPublishDescriptionsStage(params: {
|
|
120
|
+
courseSlug: string;
|
|
121
|
+
rigobotToken: string;
|
|
122
|
+
gcsBucket?: Bucket;
|
|
123
|
+
journal: PublishJournal;
|
|
124
|
+
journalStorage: JournalStorage;
|
|
125
|
+
/** Injection points for tests; production resolves them from env/bucket. */
|
|
126
|
+
publishedStorage?: CourseDescriptionsStorage;
|
|
127
|
+
draftStorage?: CourseDescriptionsStorage;
|
|
128
|
+
generate?: GenerateCourseDescriptionsOptions["generate"];
|
|
129
|
+
}): Promise<PublishDescriptionsStageResult> {
|
|
130
|
+
const { courseSlug, journal, journalStorage } = params
|
|
131
|
+
const publishedStorage =
|
|
132
|
+
params.publishedStorage ?? createS3DescriptionsStorageFromEnv()
|
|
133
|
+
|
|
134
|
+
await markStage(journalStorage, journal, "descriptions", "running")
|
|
135
|
+
|
|
136
|
+
let generation: GenerateCourseDescriptionsResult | null = null
|
|
137
|
+
try {
|
|
138
|
+
generation = await generateCourseDescriptions(
|
|
139
|
+
publishedStorage,
|
|
140
|
+
courseSlug,
|
|
141
|
+
{
|
|
142
|
+
token: params.rigobotToken,
|
|
143
|
+
reconcile: true,
|
|
144
|
+
generate: params.generate,
|
|
145
|
+
}
|
|
146
|
+
)
|
|
147
|
+
} catch (error) {
|
|
148
|
+
await markStage(journalStorage, journal, "descriptions", "failed", {
|
|
149
|
+
error: (error as Error).message,
|
|
150
|
+
})
|
|
151
|
+
return { generation: null, mirror: null, succeeded: false }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const succeeded = generation.status !== "failed"
|
|
155
|
+
await markStage(
|
|
156
|
+
journalStorage,
|
|
157
|
+
journal,
|
|
158
|
+
"descriptions",
|
|
159
|
+
succeeded ? "done" : "failed",
|
|
160
|
+
{
|
|
161
|
+
error: succeeded ? undefined : generation.errors.join("; "),
|
|
162
|
+
meta: {
|
|
163
|
+
steps: generation.steps,
|
|
164
|
+
generated: generation.generated,
|
|
165
|
+
failed: generation.failed,
|
|
166
|
+
missing: generation.missing,
|
|
167
|
+
durationsSeconds: generation.durationsSeconds,
|
|
168
|
+
},
|
|
169
|
+
}
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
if (!succeeded) {
|
|
173
|
+
return { generation, mirror: null, succeeded: false }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Mirror back so the draft keeps the fingerprints: without it the next
|
|
177
|
+
// publication would regenerate the whole course.
|
|
178
|
+
await markStage(journalStorage, journal, "gcsMirror", "running")
|
|
179
|
+
try {
|
|
180
|
+
const draftStorage =
|
|
181
|
+
params.draftStorage ??
|
|
182
|
+
createGcsDescriptionsStorage(params.gcsBucket as Bucket)
|
|
183
|
+
const publishedSyllabus = await publishedStorage.readSyllabus(courseSlug)
|
|
184
|
+
const mirror = await mirrorDescriptionsToDraft(
|
|
185
|
+
draftStorage,
|
|
186
|
+
courseSlug,
|
|
187
|
+
publishedSyllabus
|
|
188
|
+
)
|
|
189
|
+
await markStage(journalStorage, journal, "gcsMirror", "done", {
|
|
190
|
+
meta: mirror,
|
|
191
|
+
})
|
|
192
|
+
return { generation, mirror, succeeded: true }
|
|
193
|
+
} catch (error) {
|
|
194
|
+
// The published package is already correct; only the draft lags behind.
|
|
195
|
+
await markStage(journalStorage, journal, "gcsMirror", "failed", {
|
|
196
|
+
error: (error as Error).message,
|
|
197
|
+
})
|
|
198
|
+
return { generation, mirror: null, succeeded: true }
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Entry point for the detached run after publishing. Never throws: it is not
|
|
204
|
+
* awaited by anyone, so an escaping rejection would only surface as an
|
|
205
|
+
* unhandled promise.
|
|
206
|
+
*/
|
|
207
|
+
export async function runDescriptionsStageAfterPublish(params: {
|
|
208
|
+
courseSlug: string;
|
|
209
|
+
gcsBucket?: Bucket;
|
|
210
|
+
journal: PublishJournal;
|
|
211
|
+
journalStorage: JournalStorage;
|
|
212
|
+
hasWork: boolean;
|
|
213
|
+
/** Present when the caller announced `descriptions: "queued"`. */
|
|
214
|
+
eventContext?: PublishEventContext;
|
|
215
|
+
breathecodeToken?: string;
|
|
216
|
+
publishedStorage?: CourseDescriptionsStorage;
|
|
217
|
+
draftStorage?: CourseDescriptionsStorage;
|
|
218
|
+
generate?: GenerateCourseDescriptionsOptions["generate"];
|
|
219
|
+
/** Injection point for tests. */
|
|
220
|
+
emit?: typeof sendPublishEvent;
|
|
221
|
+
}): Promise<PublishDescriptionsStageResult> {
|
|
222
|
+
const { courseSlug, journal, journalStorage } = params
|
|
223
|
+
let outcome: PublishDescriptionsStageResult = {
|
|
224
|
+
generation: null,
|
|
225
|
+
mirror: null,
|
|
226
|
+
succeeded: true,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Resolved on demand: the common "nothing to describe" path must not build
|
|
230
|
+
// AWS clients it will never use.
|
|
231
|
+
let resolved = params.publishedStorage
|
|
232
|
+
const publishedStorage = () => {
|
|
233
|
+
if (!resolved) {
|
|
234
|
+
resolved = createS3DescriptionsStorageFromEnv()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return resolved
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
if (!params.hasWork) {
|
|
242
|
+
// Nothing changed enough to describe; both stages are settled, and no
|
|
243
|
+
// second event was promised, so none is sent.
|
|
244
|
+
await markStage(journalStorage, journal, "descriptions", "done", {
|
|
245
|
+
meta: { reason: "up_to_date" },
|
|
246
|
+
})
|
|
247
|
+
await markStage(journalStorage, journal, "gcsMirror", "done", {
|
|
248
|
+
meta: { reason: "up_to_date" },
|
|
249
|
+
})
|
|
250
|
+
await markStage(journalStorage, journal, "manifestEvent", "done", {
|
|
251
|
+
meta: { reason: "not_promised" },
|
|
252
|
+
})
|
|
253
|
+
return outcome
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Descriptions are platform enrichment, not something the publisher is
|
|
257
|
+
// billed for, so they always run under the service account.
|
|
258
|
+
const token = process.env.RIGOBOT_SYSTEM_TOKEN
|
|
259
|
+
if (!token) {
|
|
260
|
+
await markStage(journalStorage, journal, "descriptions", "failed", {
|
|
261
|
+
error: "RIGOBOT_SYSTEM_TOKEN is not configured",
|
|
262
|
+
})
|
|
263
|
+
console.error(
|
|
264
|
+
`[descriptions] Skipping stage for "${courseSlug}": RIGOBOT_SYSTEM_TOKEN is not configured`
|
|
265
|
+
)
|
|
266
|
+
outcome = { generation: null, mirror: null, succeeded: false }
|
|
267
|
+
return outcome
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
outcome = await runPublishDescriptionsStage({
|
|
271
|
+
courseSlug,
|
|
272
|
+
rigobotToken: token,
|
|
273
|
+
gcsBucket: params.gcsBucket,
|
|
274
|
+
journal,
|
|
275
|
+
journalStorage,
|
|
276
|
+
publishedStorage: publishedStorage(),
|
|
277
|
+
draftStorage: params.draftStorage,
|
|
278
|
+
generate: params.generate,
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
if (outcome.succeeded) {
|
|
282
|
+
console.log(
|
|
283
|
+
`[descriptions] "${courseSlug}": ${
|
|
284
|
+
outcome.generation?.generated ?? 0
|
|
285
|
+
} description(s), ${outcome.mirror?.copied ?? 0} mirrored`
|
|
286
|
+
)
|
|
287
|
+
} else {
|
|
288
|
+
console.error(
|
|
289
|
+
`[descriptions] "${courseSlug}": stage failed${
|
|
290
|
+
outcome.generation?.errors.length ?
|
|
291
|
+
` (${outcome.generation.errors.join("; ")})` :
|
|
292
|
+
""
|
|
293
|
+
}`
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return outcome
|
|
298
|
+
} catch (error) {
|
|
299
|
+
console.error(
|
|
300
|
+
`[descriptions] Stage failed for "${courseSlug}":`,
|
|
301
|
+
(error as Error).message
|
|
302
|
+
)
|
|
303
|
+
outcome = { generation: null, mirror: null, succeeded: false }
|
|
304
|
+
return outcome
|
|
305
|
+
} finally {
|
|
306
|
+
// The contract: a publication that announced `descriptions: "queued"` is
|
|
307
|
+
// always followed by exactly one manifest event, success or failure.
|
|
308
|
+
// Emitting from `finally` is what makes "exactly one, on every exit path"
|
|
309
|
+
// true by construction rather than by remembering to call it.
|
|
310
|
+
if (params.hasWork) {
|
|
311
|
+
await emitManifestUpdated({
|
|
312
|
+
courseSlug,
|
|
313
|
+
journal,
|
|
314
|
+
journalStorage,
|
|
315
|
+
outcome,
|
|
316
|
+
eventContext: params.eventContext,
|
|
317
|
+
breathecodeToken: params.breathecodeToken,
|
|
318
|
+
publishedStorage: publishedStorage(),
|
|
319
|
+
emit: params.emit ?? sendPublishEvent,
|
|
320
|
+
})
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
await finalizeJournal(journalStorage, journal)
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async function emitManifestUpdated(params: {
|
|
328
|
+
courseSlug: string;
|
|
329
|
+
journal: PublishJournal;
|
|
330
|
+
journalStorage: JournalStorage;
|
|
331
|
+
outcome: PublishDescriptionsStageResult;
|
|
332
|
+
eventContext?: PublishEventContext;
|
|
333
|
+
breathecodeToken?: string;
|
|
334
|
+
publishedStorage: CourseDescriptionsStorage;
|
|
335
|
+
emit: typeof sendPublishEvent;
|
|
336
|
+
}): Promise<void> {
|
|
337
|
+
const { journal, journalStorage } = params
|
|
338
|
+
|
|
339
|
+
if (!params.eventContext || !params.breathecodeToken) {
|
|
340
|
+
// Nothing was announced to breathecode, so nothing is owed.
|
|
341
|
+
await markStage(journalStorage, journal, "manifestEvent", "done", {
|
|
342
|
+
meta: { reason: "no_event_context" },
|
|
343
|
+
})
|
|
344
|
+
return
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
// Report the manifest as it is NOW: the descriptions are the whole point of
|
|
349
|
+
// this event, so the one captured at publish time would be stale.
|
|
350
|
+
const updatedManifest =
|
|
351
|
+
(await params.publishedStorage.readManifest?.(params.courseSlug)) ??
|
|
352
|
+
params.eventContext.manifest
|
|
353
|
+
|
|
354
|
+
const generation = params.outcome.generation
|
|
355
|
+
const delivered = await params.emit(
|
|
356
|
+
buildPackageManifestUpdatedEvent(
|
|
357
|
+
{ ...params.eventContext, manifest: updatedManifest },
|
|
358
|
+
params.outcome.succeeded ? "success" : "failed",
|
|
359
|
+
generation ?
|
|
360
|
+
{
|
|
361
|
+
generated: generation.generated,
|
|
362
|
+
failed: generation.failed,
|
|
363
|
+
missing: generation.missing,
|
|
364
|
+
} :
|
|
365
|
+
undefined
|
|
366
|
+
),
|
|
367
|
+
params.breathecodeToken
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
await markStage(
|
|
371
|
+
journalStorage,
|
|
372
|
+
journal,
|
|
373
|
+
"manifestEvent",
|
|
374
|
+
delivered ? "done" : "failed",
|
|
375
|
+
{ error: delivered ? undefined : "delivery failed" }
|
|
376
|
+
)
|
|
377
|
+
} catch (error) {
|
|
378
|
+
await markStage(journalStorage, journal, "manifestEvent", "failed", {
|
|
379
|
+
error: (error as Error).message,
|
|
380
|
+
})
|
|
381
|
+
}
|
|
382
|
+
}
|