Public: anyone can read this; the crew can change it.
An origin's reconnect and storage-limit sentences live in one module, read by credentials, sync, storage and mirror #5640
mason/forge:mason/5623-an-origin-s-reconnect-and-storage-limit → main 2 commits fork
8 files
changed +64
−9
src/actions/migrate.ts +2 −1
| @@ -63,6 +63,7 @@ import { | ||
| 63 | 63 | type MirrorJob, |
| 64 | 64 | } from "../origins/jobs.ts"; |
| 65 | 65 | import type { Provider } from "../origins/types.ts"; |
| 66 | import { AT_STORAGE_LIMIT } from "../origins/words.ts"; | |
| 66 | 67 | import { requireUser } from "./access.ts"; |
| 67 | 68 | import { enableApp } from "./apps.ts"; |
| 68 | 69 | import { addToCrew } from "./crew.ts"; |
| @@ -287,7 +288,7 @@ export function migrateMessage(error: MigrateError, limit = "at the repositories | ||
| 287 | 288 | case "repository_limit": |
| 288 | 289 | return `Not made: ${limit}.`; |
| 289 | 290 | case "storage_limit": |
| 290 | return "Not made: this account is at the forge's storage limit. Delete a repository or app data first."; | |
| 291 | return `Not made: ${AT_STORAGE_LIMIT}. Delete a repository or app data first.`; | |
| 291 | 292 | } |
| 292 | 293 | } |
| 293 | 294 |
src/core/storage.ts +3 −2
| @@ -17,6 +17,7 @@ import { appsRoot } from "../apps/data.ts"; | ||
| 17 | 17 | import { dirBytes } from "../apps/bytes.ts"; |
| 18 | 18 | import { repoDir } from "../git/repos.ts"; |
| 19 | 19 | import { getQuota } from "../settings/quotas.ts"; |
| 20 | import { AT_STORAGE_LIMIT } from "../origins/words.ts"; | |
| 20 | 21 | import type { User } from "./users.ts"; |
| 21 | 22 | |
| 22 | 23 | /** The limit is set in megabytes (decimal), the unit people say it in. */ |
| @@ -177,8 +178,8 @@ export async function storageRefusal( | ||
| 177 | 178 | export function storageLimitMessage(app: App, ownerId: string): string { |
| 178 | 179 | const sentence = storageSentence(app, ownerId); |
| 179 | 180 | return sentence === undefined |
| 180 | ? "this account is at the forge's storage limit" | |
| 181 | : `this account is at the forge's storage limit: ${sentence}`; | |
| 181 | ? AT_STORAGE_LIMIT | |
| 182 | : `${AT_STORAGE_LIMIT}: ${sentence}`; | |
| 182 | 183 | } |
| 183 | 184 | |
| 184 | 185 | /** What a refused push says, over HTTP and SSH alike. */ |
src/origins/credential.ts +3 −2
| @@ -29,6 +29,7 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 29 | 29 | import { appFor, refresh, type AppRegistration } from "./oauth.ts"; |
| 30 | 30 | import { providerFor } from "./providers/index.ts"; |
| 31 | 31 | import type { Provider, ProviderCredential, ProviderResult } from "./types.ts"; |
| 32 | import { NEEDS_RECONNECT } from "./words.ts"; | |
| 32 | 33 | |
| 33 | 34 | const log = createLog("origins"); |
| 34 | 35 | |
| @@ -88,7 +89,7 @@ async function resolveToken( | ||
| 88 | 89 | const current = getOriginById(app.db, originId); |
| 89 | 90 | if (!current) return authFailure("this origin is no longer connected"); |
| 90 | 91 | if (current.needsReauth) |
| 91 | return authFailure("this origin needs to be reconnected"); | |
| 92 | return authFailure(NEEDS_RECONNECT); | |
| 92 | 93 | |
| 93 | 94 | if (current.authKind === "oauth" && expiringSoon(current)) { |
| 94 | 95 | const pending = refreshing.get(originId); |
| @@ -96,7 +97,7 @@ async function resolveToken( | ||
| 96 | 97 | else await runRefresh(app, originId); |
| 97 | 98 | const after = getOriginById(app.db, originId); |
| 98 | 99 | if (!after || after.needsReauth) |
| 99 | return authFailure("this origin needs to be reconnected"); | |
| 100 | return authFailure(NEEDS_RECONNECT); | |
| 100 | 101 | } |
| 101 | 102 | |
| 102 | 103 | const sealed = sealedSecrets(app.db, originId); |
src/origins/mirror.ts +2 −1
| @@ -32,6 +32,7 @@ import { adoptDefaultBranch, configureMirror } from "./git-mirror.ts"; | ||
| 32 | 32 | import { advance, createJob, runningJob, type MirrorJob } from "./jobs.ts"; |
| 33 | 33 | import { syncOrigin } from "./sync.ts"; |
| 34 | 34 | import type { RemoteRepo } from "./types.ts"; |
| 35 | import { AT_STORAGE_LIMIT } from "./words.ts"; | |
| 35 | 36 | |
| 36 | 37 | export type MirrorError = |
| 37 | 38 | | "not_found" |
| @@ -301,7 +302,7 @@ export function mirrorMessage(error: MirrorError): string { | ||
| 301 | 302 | case "repository_limit": |
| 302 | 303 | return "this account is at the forge's repositories limit"; |
| 303 | 304 | case "storage_limit": |
| 304 | return "this account is at the forge's storage limit"; | |
| 305 | return AT_STORAGE_LIMIT; | |
| 305 | 306 | } |
| 306 | 307 | } |
| 307 | 308 |
src/origins/push-through.ts +3 −2
| @@ -40,6 +40,7 @@ import { err, ok, type Result } from "../kernel/result.ts"; | ||
| 40 | 40 | import { providerForOrigin } from "./credential.ts"; |
| 41 | 41 | import { redact, secretsIn } from "./credentials.ts"; |
| 42 | 42 | import { pushToOrigin, snapshotRefs } from "./git-mirror.ts"; |
| 43 | import { NEEDS_RECONNECT } from "./words.ts"; | |
| 43 | 44 | |
| 44 | 45 | /** A push-through must not outlive a pusher's patience. */ |
| 45 | 46 | const TIMEOUT_MS = 60_000; |
| @@ -194,13 +195,13 @@ async function wayThrough( | ||
| 194 | 195 | const provider = providerForOrigin(app, link.origin); |
| 195 | 196 | if (!provider.ok) |
| 196 | 197 | return err({ |
| 197 | message: `${link.origin.webUrl}: this origin needs to be reconnected`, | |
| 198 | message: `${link.origin.webUrl}: ${NEEDS_RECONNECT}`, | |
| 198 | 199 | }); |
| 199 | 200 | |
| 200 | 201 | const authEnv = await provider.value.gitAuthEnv(link.cloneUrl); |
| 201 | 202 | if (Object.keys(authEnv).length === 0) |
| 202 | 203 | return err({ |
| 203 | message: `${link.origin.webUrl}: this origin needs to be reconnected`, | |
| 204 | message: `${link.origin.webUrl}: ${NEEDS_RECONNECT}`, | |
| 204 | 205 | }); |
| 205 | 206 | return ok({ link, authEnv }); |
| 206 | 207 | } |
src/origins/sync.ts +2 −1
| @@ -53,6 +53,7 @@ import { | ||
| 53 | 53 | importReviewStates, |
| 54 | 54 | } from "./import.ts"; |
| 55 | 55 | import type { Provider, ProviderFailure, RemoteIssue } from "./types.ts"; |
| 56 | import { NEEDS_RECONNECT } from "./words.ts"; | |
| 56 | 57 | |
| 57 | 58 | const log = createLog("origins"); |
| 58 | 59 | |
| @@ -153,7 +154,7 @@ async function runOrigin( | ||
| 153 | 154 | }; |
| 154 | 155 | |
| 155 | 156 | if (origin.needsReauth) { |
| 156 | report.error = "this origin needs to be reconnected"; | |
| 157 | report.error = NEEDS_RECONNECT; | |
| 157 | 158 | recordSync(app.db, origin.id, { at: startedAt, error: report.error }); |
| 158 | 159 | schedule(app, origin.id, SYNC_INTERVAL_MS); |
| 159 | 160 | return report; |
src/origins/words.ts added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | /** | |
| 2 | * What an origin's state says, on the web and in the terminal alike: one | |
| 3 | * place for each sentence, so a refusal reads the same wherever it is said. | |
| 4 | */ | |
| 5 | ||
| 6 | /** An origin whose credential was revoked or expired past refreshing. */ | |
| 7 | export const NEEDS_RECONNECT = "this origin needs to be reconnected"; | |
| 8 | ||
| 9 | /** An account refused a create, fork or mirror for the bytes it holds. */ | |
| 10 | export const AT_STORAGE_LIMIT = "this account is at the forge's storage limit"; |
test/origin-state-words.test.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * An origin that needs reconnecting, or an account at its storage limit, | |
| 3 | * reads the same wherever it is said, because it is said from one place: | |
| 4 | * src/origins/words.ts. A copy typed back into a file that says it fails here. | |
| 5 | */ | |
| 6 | ||
| 7 | import { describe, expect, test } from "bun:test"; | |
| 8 | import { readFileSync } from "node:fs"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { AT_STORAGE_LIMIT, NEEDS_RECONNECT } from "../src/origins/words.ts"; | |
| 11 | ||
| 12 | const root = join(import.meta.dir, ".."); | |
| 13 | const read = (file: string): string => readFileSync(join(root, file), "utf8"); | |
| 14 | ||
| 15 | const sayers = [ | |
| 16 | "src/origins/credential.ts", | |
| 17 | "src/origins/sync.ts", | |
| 18 | "src/core/storage.ts", | |
| 19 | "src/origins/mirror.ts", | |
| 20 | "src/origins/push-through.ts", | |
| 21 | "src/actions/migrate.ts", | |
| 22 | ]; | |
| 23 | ||
| 24 | describe("an origin's state words", () => { | |
| 25 | test("the sentences read as they always have", () => { | |
| 26 | expect(NEEDS_RECONNECT).toBe("this origin needs to be reconnected"); | |
| 27 | expect(AT_STORAGE_LIMIT).toBe( | |
| 28 | "this account is at the forge's storage limit", | |
| 29 | ); | |
| 30 | }); | |
| 31 | ||
| 32 | for (const file of sayers) | |
| 33 | test(`${file} imports the words and types neither sentence`, () => { | |
| 34 | const text = read(file); | |
| 35 | expect(text).toMatch(/from "(\.\/|\.\.\/origins\/)words\.ts"/); | |
| 36 | expect(text).not.toContain(NEEDS_RECONNECT); | |
| 37 | expect(text).not.toContain(AT_STORAGE_LIMIT); | |
| 38 | }); | |
| 39 | }); |
new above
offline