From 56752d4df3066703e55d1af35cbbc23b56cfb297 Mon Sep 17 00:00:00 2001 From: Aqib Nawab Date: Wed, 18 Nov 2026 11:13:49 +0500 Subject: [PATCH] Support a different instructor per course in repository publishing A course can name its own instructor with creatorEmail in its course.yaml. The course is published under, and bound to, that account. A course without creatorEmail uses the default instructor in courses/publishing.yaml, exactly as before. Before this change a creatorEmail that differed from publishing.yaml made the publish check fail with "creatorEmail conflicts with courses/publishing.yaml". Every instructor used in a release is checked (verified, Mentor or Admin, ZIP import enabled) before any course changes, so one missing account stops the whole release. Ownership binding keeps the previous owner as COURSE_CREATOR, as before. Same change as devweekends/Edorax-core#152, which edits the vendored copy of this script. --- server/scripts/repository-course-publish.ts | 84 ++++++++++++------- .../scripts/repository-course-publish.test.ts | 35 +++++++- 2 files changed, 90 insertions(+), 29 deletions(-) diff --git a/server/scripts/repository-course-publish.ts b/server/scripts/repository-course-publish.ts index 6fb5e17e..6e1d0597 100644 --- a/server/scripts/repository-course-publish.ts +++ b/server/scripts/repository-course-publish.ts @@ -63,6 +63,25 @@ export function ownershipSlugGroups(packages: Package[]): string[][] { return packages.map(pkg => [pkg.slug, ...(pkg.manifest.repositoryTargetSlug ? [pkg.manifest.repositoryTargetSlug] : [])]); } +/** + * The instructor who owns a course: the `creatorEmail` in its course.yaml, or else the + * default instructor in courses/publishing.yaml. A course without `creatorEmail` behaves + * exactly as before. + */ +export function courseInstructorEmail(pkg: Package, defaultEmail: string): string { + return pkg.manifest.creatorEmail?.trim() || defaultEmail; +} + +/** The distinct instructor emails a set of packages needs, compared without regard to case. */ +export function instructorEmailsFor(packages: Package[], defaultEmail: string): string[] { + const seen = new Map(); + for (const pkg of packages) { + const email = courseInstructorEmail(pkg, defaultEmail); + if (!seen.has(email.toLowerCase())) seen.set(email.toLowerCase(), email); + } + return [...seen.values()]; +} + function filesForZip(dir: string): Record { const files: Record = {}; function visit(current: string) { @@ -129,11 +148,8 @@ export function packageZip(pkg: Package, publication: "create" | "update", instr return zip; } -function validate(pkg: Package, instructorEmail: string) { - if (pkg.manifest.creatorEmail && pkg.manifest.creatorEmail.toLowerCase() !== instructorEmail.toLowerCase()) { - throw new Error(`${pkg.slug}: creatorEmail conflicts with courses/publishing.yaml`); - } - packageZip(pkg, "create", instructorEmail); +function validate(pkg: Package, defaultInstructorEmail: string) { + packageZip(pkg, "create", courseInstructorEmail(pkg, defaultInstructorEmail)); const env = { ...process.env }; delete env.COURSE_IMPORT_SOURCE; delete env.COURSE_IMPORT_RESULT; @@ -163,7 +179,7 @@ function validate(pkg: Package, instructorEmail: string) { } } -async function publish(packages: Package[], instructorEmail: string) { +async function publish(packages: Package[], defaultInstructorEmail: string) { if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL must be configured in the backend release environment"); const { prisma } = await import("../src/db/prisma.js"); const { Prisma } = await import("../src/generated/prisma/index.js"); @@ -171,32 +187,44 @@ async function publish(packages: Package[], instructorEmail: string) { const { previewCourseZipUpdate, approveCourseZipUpdate } = await import("../src/services/teach/course-update.js"); const { publishCourse } = await import("../src/services/teach/courses.js"); try { - const owner = await prisma.user.findUnique({ where: { email: instructorEmail }, - select: { id: true, email: true, name: true, image: true, role: true, emailVerified: true, zipImportEnabled: true, testerEnabled: true } }); - if (!owner || !["MENTOR", "ADMIN"].includes(owner.role) || !owner.emailVerified || !owner.zipImportEnabled) { - throw new Error(`${instructorEmail} must be a verified instructor with ZIP import access before repository publishing runs`); + // Check every instructor before any course changes, so one missing account stops the whole release. + const owners = new Map(); + for (const email of instructorEmailsFor(packages, defaultInstructorEmail)) { + const owner = await prisma.user.findUnique({ where: { email }, + select: { id: true, email: true, name: true, image: true, role: true, emailVerified: true, zipImportEnabled: true, testerEnabled: true } }); + if (!owner || !["MENTOR", "ADMIN"].includes(owner.role) || !owner.emailVerified || !owner.zipImportEnabled) { + throw new Error(`${email} must be a verified instructor with ZIP import access before repository publishing runs`); + } + owners.set(email.toLowerCase(), owner); } - const user: SessionUser = owner; + const instructorFor = (pkg: Package) => { + const email = courseInstructorEmail(pkg, defaultInstructorEmail); + return { email, user: owners.get(email.toLowerCase())! }; + }; // Bind only opted-in repository courses. The previous owner keeps // COURSE_CREATOR access; unpublished source packages must not mutate DB ownership. - for (const slugs of ownershipSlugGroups(packages)) { - const courses = await prisma.course.findMany({ where: { slug: { in: slugs } }, select: { id: true, slug: true, mentorId: true } }); - for (const course of courses) { - if (course.mentorId === user.id) continue; - await prisma.$transaction(async tx => { - await tx.courseInstructor.upsert({ - where: { courseId_userId: { courseId: course.id, userId: course.mentorId } }, - update: { role: "COURSE_CREATOR" }, - create: { courseId: course.id, userId: course.mentorId, role: "COURSE_CREATOR" }, + for (const pkg of packages) { + const { email: instructorEmail, user } = instructorFor(pkg); + for (const slugs of ownershipSlugGroups([pkg])) { + const courses = await prisma.course.findMany({ where: { slug: { in: slugs } }, select: { id: true, slug: true, mentorId: true } }); + for (const course of courses) { + if (course.mentorId === user.id) continue; + await prisma.$transaction(async tx => { + await tx.courseInstructor.upsert({ + where: { courseId_userId: { courseId: course.id, userId: course.mentorId } }, + update: { role: "COURSE_CREATOR" }, + create: { courseId: course.id, userId: course.mentorId, role: "COURSE_CREATOR" }, + }); + await tx.courseInstructor.deleteMany({ where: { courseId: course.id, userId: user.id } }); + const changed = await tx.course.updateMany({ where: { id: course.id, mentorId: course.mentorId }, data: { mentorId: user.id } }); + if (changed.count !== 1) throw new Error(`${course.slug}: course ownership changed during binding; retry the workflow`); }); - await tx.courseInstructor.deleteMany({ where: { courseId: course.id, userId: user.id } }); - const changed = await tx.course.updateMany({ where: { id: course.id, mentorId: course.mentorId }, data: { mentorId: user.id } }); - if (changed.count !== 1) throw new Error(`${course.slug}: course ownership changed during binding; retry the workflow`); - }); - console.log(`${course.slug}: assigned course ownership to ${instructorEmail}`); + console.log(`${course.slug}: assigned course ownership to ${instructorEmail}`); + } } } for (const pkg of packages) { + const { email: instructorEmail, user } = instructorFor(pkg); const targetSlug = pkg.manifest.repositoryTargetSlug ?? pkg.slug; const sourceHash = courseSourceHash(pkg); const prior = await prisma.course.findUnique({ where: { slug: targetSlug }, select: { @@ -257,11 +285,11 @@ async function main() { else console.log("No private course checkout configured; platform is ready for content publishing."); return; } - const { instructorEmail } = publishingSettings(); + const { instructorEmail: defaultInstructorEmail } = publishingSettings(); const packages = selectedPackages(); if (mode === "--count") { console.log(packages.length); return; } - for (const pkg of packages) validate(pkg, instructorEmail); - if (mode === "--publish") await publish(packages, instructorEmail); + for (const pkg of packages) validate(pkg, defaultInstructorEmail); + if (mode === "--publish") await publish(packages, defaultInstructorEmail); else console.log(`Validated ${packages.length} opted-in course package(s).`); } diff --git a/server/tests/scripts/repository-course-publish.test.ts b/server/tests/scripts/repository-course-publish.test.ts index 8ebfa8f3..7dc9d1aa 100644 --- a/server/tests/scripts/repository-course-publish.test.ts +++ b/server/tests/scripts/repository-course-publish.test.ts @@ -5,7 +5,7 @@ import path from "node:path"; import { spawnSync } from "node:child_process"; import { unzipSync } from "fflate"; import { load as loadYaml } from "js-yaml"; -import { courseDetailsUpdate, courseSourceHash, ownershipSlugGroups, packageZip, publishingSettings, selectedPackages } from "../../scripts/repository-course-publish.js"; +import { courseDetailsUpdate, courseInstructorEmail, courseSourceHash, instructorEmailsFor, ownershipSlugGroups, packageZip, publishingSettings, selectedPackages } from "../../scripts/repository-course-publish.js"; import { coursesRootForServer } from "../../scripts/course-source.js"; const temporary: string[] = []; @@ -149,3 +149,36 @@ describe("repository course publishing selection", () => { expect(courseDetailsUpdate(manifest, { ...base, assistant: { effort: "low" } })).toEqual({ assistant: null }); }); }); + +describe("per-course instructor", () => { + const DEFAULT = "devweekends@gmail.com"; + const OTHER = "aqibnawab1100@gmail.com"; + + function packagesWith(extras: Record) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "buildment-instructor-test-")); + temporary.push(root); + fs.writeFileSync(path.join(root, "publishing.yaml"), `instructorEmail: ${DEFAULT}\n`); + for (const [slug, extra] of Object.entries(extras)) course(root, slug, `repositoryPublish: true\n${extra}`); + return selectedPackages(root); + } + + it("uses the default instructor unless a course names its own", () => { + const packages = packagesWith({ plain: "", own: `creatorEmail: ${OTHER}\n` }); + const bySlug = (slug: string) => packages.find(pkg => pkg.slug === slug)!; + expect(courseInstructorEmail(bySlug("plain"), DEFAULT)).toBe(DEFAULT); + expect(courseInstructorEmail(bySlug("own"), DEFAULT)).toBe(OTHER); + }); + + it("puts the course's own instructor in its transport ZIP", () => { + const [own] = packagesWith({ own: `creatorEmail: ${OTHER}\n` }); + const files = unzipSync(packageZip(own!, "create", courseInstructorEmail(own!, DEFAULT))); + const manifest = loadYaml(Buffer.from(files["course.yaml"]!).toString("utf8")) as Record; + expect(manifest.creatorEmail).toBe(OTHER); + }); + + it("lists each instructor once, without regard to case", () => { + const packages = packagesWith({ a: "", b: `creatorEmail: ${OTHER}\n`, c: "", d: `creatorEmail: ${OTHER.toUpperCase()}\n` }); + expect(instructorEmailsFor(packages, DEFAULT)).toEqual([DEFAULT, OTHER]); + }); +}); +