Skip to content
Myr

lander.ts

This file

Path

apps/web/src/dev/lander.ts

273 lines · 8.1 KiB · TypeScript

Last changed by

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

lines 1–2why, one step back
1// The scripted `/memory` Lander answers in a fixed order, which a world people click through can't follow,
2// so this one does the work over the memory repo host.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 3–201why, one step back
3import { RefName, RefOutcome, RepoHost } from "@repo/artifacts/repo-host";
4import { Brand } from "@repo/brand";5import { composeRef, Lander, landedRef, Rebuild } from "@repo/project/lander";6import type { Change, LandJob } from "@repo/project/lander";7import type { CommitSha, RepoName } from "@repo/protocol/ids";8import { LandedSha, RepoPath, TreeSha } from "@repo/protocol/ids";9import { RepoBusy, StaleBase, VerifierBlocked } from "@repo/protocol/refusal";10import { sdkVersion } from "@repo/rules/rules-evaluator";11import { Array, DateTime, Effect, Layer, Option, Ref } from "effect";1213import {14 filesAt,15 overlay,16 personIdentity,17 platformIdentity,18 scratch,19 textOf,20} from "./git.ts";21import type { Files, Snapshot } from "./git.ts";2223const mainRef = RefName.make("refs/heads/main");2425const blocked = () => new VerifierBlocked({});2627const read = (repo: RepoName, commit: CommitSha) =>28 filesAt(repo, commit).pipe(29 Effect.flatMap(Effect.fromOption),30 Effect.mapError(blocked)31 );3233const changesOf = (base: Snapshot, head: Snapshot): readonly Change[] =>34 Array.dedupe([...base.keys(), ...head.keys()])35 .toSorted()36 .flatMap((path): readonly Change[] => {37 const before = base.get(path);38 const after = head.get(path);39 const at = RepoPath.make(path);4041 if (before === undefined) {42 return after === undefined43 ? []44 : [{ after: after.blob, path: at, status: "added" }];45 }4647 if (after === undefined) {48 return [{ before: before.blob, path: at, status: "deleted" }];49 }5051 return before.blob === after.blob52 ? []53 : [54 {55 after: after.blob,56 before: before.blob,57 path: at,58 status: "modified",59 },60 ];61 });6263const merged = (input: {64 readonly repo: RepoName;65 readonly main: LandedSha;66 readonly fork: RepoName;67 readonly base: CommitSha;68 readonly head: CommitSha;69}) =>70 Effect.gen(function* () {71 const base = yield* read(input.fork, input.base);72 const head = yield* read(input.fork, input.head);73 const main = yield* read(input.repo, input.main);74 const changes = changesOf(base, head);7576 // Main moved this path since the answer's base, to something else.77 const conflicts = changes.flatMap(({ path }) =>78 main.get(path)?.blob !== base.get(path)?.blob &&79 main.get(path)?.blob !== head.get(path)?.blob80 ? [path]81 : []82 );8384 if (Array.isReadonlyArrayNonEmpty(conflicts)) {85 return yield* new StaleBase({86 base: input.base,87 conflicts,88 main: input.main,89 });90 }9192 const files = new Map(textOf(main));9394 for (const { path } of changes) {95 const text = head.get(path)?.text;9697 if (text === undefined) {98 files.delete(path);99 } else {100 files.set(path, text);101 }102 }103104 return { changes, files: files satisfies Files };105 });106107const rebuilt = (job: LandJob) =>108 Rebuild.$match(job.rebuild, {109 Compose: ({ files }) =>110 Effect.map(read(job.repo, job.main), (main) =>111 overlay(112 textOf(main),113 files.map(({ content, path }) => [path, content] as const)114 )115 ),116 Existing: ({ commit }) => Effect.map(read(job.repo, commit), textOf),117 Merge: ({ base, fork, head }) =>118 Effect.map(119 merged({ base, fork, head, main: job.main, repo: job.repo }),120 ({ files }) => files121 ),122 });123124const baseOf = (job: LandJob) =>125 Rebuild.$is("Merge")(job.rebuild) ? job.rebuild.base : job.main;126127export const DevLander: Layer.Layer<Lander, never, RepoHost> = Layer.effect(128 Lander,129 Effect.gen(function* () {130 const host = yield* RepoHost;131 const provided = Effect.provideService(RepoHost, host);132133 // A request landed before: its commit, so a second land returns it.134 const landedBy = yield* Ref.make<ReadonlyMap<string, LandedSha>>(new Map());135136 const tipOf = (repo: RepoName, ref: RefName) =>137 host.refs(repo).pipe(138 Effect.map((refs) => Option.fromNullishOr(refs.get(ref))),139 Effect.mapError(blocked)140 );141142 const land = (job: LandJob) =>143 Effect.gen(function* () {144 const before = (yield* Ref.get(landedBy)).get(job.requestId);145146 if (before !== undefined) {147 return { pending: [], sha: before };148 }149150 const tip = yield* tipOf(job.repo, mainRef);151152 if (!Option.contains(tip, job.main)) {153 return yield* new StaleBase({154 base: baseOf(job),155 conflicts: [],156 main: Option.getOrElse(tip, () => job.main),157 });158 }159160 const into = yield* scratch;161 const tree = yield* into.tree(yield* rebuilt(job));162163 if (TreeSha.make(tree) !== job.tree) {164 return yield* blocked();165 }166167 const commit = yield* into.commit({168 at: yield* DateTime.now,169 author: personIdentity(job.author),170 committer: platformIdentity,171 message: job.message,172 parents: [job.main],173 tree,174 });175176 // Never forced: if main moved since `job.main`, the push is refused.177 const [outcome] = yield* host178 .push(job.repo, into.source, [179 { from: Option.some(job.main), ref: mainRef, to: commit },180 ])181 .pipe(182 Effect.catchTags({183 HostBusy: () => Effect.fail(new RepoBusy({})),184 RepoNotFound: () => Effect.fail(blocked()),185 })186 );187188 if (outcome === undefined || !RefOutcome.$is("Updated")(outcome)) {189 return yield* new StaleBase({190 base: baseOf(job),191 conflicts: [],192 main: job.main,193 });194 }195196 const sha = LandedSha.make(commit);197198 yield* Ref.update(landedBy, (all) =>199 new Map(all).set(job.requestId, sha)200 );201

Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248

line 202why, one step back
202 // The mirror follows main in a push of its own.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 203–273why, one step back
203 const mirror = yield* tipOf(job.repo, landedRef);
204205 yield* host206 .push(job.repo, into.source, [207 { from: mirror, ref: landedRef, to: commit },208 ])209 .pipe(Effect.ignore);210211 return { pending: [], sha };212 });213214 return Lander.of({215 bundleRules: ({ at, repo }) =>216 provided(217 Effect.map(read(repo, at), (files) => ({218 entry: `${Brand.configDir}/project.ts`,219 modules: Object.fromEntries(220 [...textOf(files)].filter(([path]) =>221 path.startsWith(`${Brand.configDir}/`)222 )223 ),224 sdkVersion,225 }))226 ),227 compose: ({ files, main, repo }) =>228 provided(229 Effect.gen(function* () {230 const onMain = yield* read(repo, main);231 const into = yield* scratch;232233 const tree = yield* into.tree(234 overlay(235 textOf(onMain),236 files.map(({ content, path }) => [path, content] as const)237 )238 );239240 const commit = yield* into.commit({241 at: yield* DateTime.now,242 author: platformIdentity,243 committer: platformIdentity,244 message: "Compose files onto main for a check",245 parents: [main],246 tree,247 });248249 const ref = composeRef(TreeSha.make(tree));250 const existing = yield* tipOf(repo, ref);251252 if (!Option.contains(existing, commit)) {253 yield* host254 .push(repo, into.source, [{ from: existing, ref, to: commit }])255 .pipe(Effect.mapError(blocked));256 }257258 return { commit, tree: TreeSha.make(tree) };259 })260 ),261 land: (job) => provided(land(job)),262 merge: (job) =>263 provided(264 Effect.gen(function* () {265 const { changes, files } = yield* merged(job);266 const into = yield* scratch;267268 return { changes, tree: TreeSha.make(yield* into.tree(files)) };269 })270 ),271 });272 })273);