From 01248fb8f6ccf0a0fe7fea8d8ef6d43def14567d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Wed, 8 Jan 2020 12:58:04 -0800 Subject: [PATCH] Remove the repoIdRegistry (#1534) Before we added the concept of "SourceCred Projects", we tracked cred instances via their GitHub repostiory id. The replacement for this system was added in #1238, I missed the RepoIdRegistry in the cleanup. This commit removes all code pertaining to the now-obsolete RepoIdRegistry. Test plan: - `yarn test --full` passes - manual inspection of `yarn start`; it still loads properly - manual inspection of the output for build_static_site.sh - `git grep repoIdRegistry` returns no hits --- config/env.js | 1 - config/webpack.config.web.js | 1 - src/core/repoIdRegistry.js | 115 ---------------------------- src/core/repoIdRegistry.test.js | 130 -------------------------------- src/homepage/routeData.js | 1 - 5 files changed, 248 deletions(-) delete mode 100644 src/core/repoIdRegistry.js delete mode 100644 src/core/repoIdRegistry.test.js diff --git a/config/env.js b/config/env.js index ddf5524..66ffca5 100644 --- a/config/env.js +++ b/config/env.js @@ -7,7 +7,6 @@ const path = require("path"); const paths = require("./paths"); /*:: import type {GitState} from "../src/core/version"; */ -/*:: import type {RepoIdRegistry} from "../src/core/repoIdRegistry"; */ // Make sure that including paths.js after env.js will read .env variables. delete require.cache[require.resolve("./paths")]; diff --git a/config/webpack.config.web.js b/config/webpack.config.web.js index 00b9a75..f473649 100644 --- a/config/webpack.config.web.js +++ b/config/webpack.config.web.js @@ -5,7 +5,6 @@ import type { $Application as ExpressApp, $Response as ExpressResponse, } from "express"; -import type {RepoIdRegistry} from "../src/core/repoIdRegistry"; */ const os = require("os"); const path = require("path"); diff --git a/src/core/repoIdRegistry.js b/src/core/repoIdRegistry.js deleted file mode 100644 index d2085d9..0000000 --- a/src/core/repoIdRegistry.js +++ /dev/null @@ -1,115 +0,0 @@ -// @flow - -/** - * The RepoIdRegistry is a small JSON file that SourceCred uses to track which - * RepoIds have been loaded. - * - * The registry consists of Entries. The entry contains the RepoId of the - * loaded repository, and may contain additional metadata (e.g. the last-loaded - * timestamp). - */ -import deepEqual from "lodash.isequal"; -import {toCompat, fromCompat, type Compatible} from "../util/compat"; -import type {RepoId} from "../core/repoId"; -import fs from "fs"; -import path from "path"; -import stringify from "json-stable-stringify"; - -export const REPO_ID_REGISTRY_FILE = "repositoryRegistry.json"; -export const REPO_ID_REGISTRY_API = "/api/v1/data/repositoryRegistry.json"; - -const REPO_ID_REGISTRY_COMPAT = {type: "REPO_ID_REGISTRY", version: "0.2.0"}; - -export type RegistryEntry = {| - +repoId: RepoId, -|}; -export type RepoIdRegistry = $ReadOnlyArray; -export type RepoIdRegistryJSON = Compatible; - -/** - * Adds a new entry to an existing RepoIdRegistry. - */ -export function addEntry( - registry: RepoIdRegistry, - entry: RegistryEntry -): RepoIdRegistry { - return [ - ...registry.filter( - (x: RegistryEntry) => !deepEqual(x.repoId, entry.repoId) - ), - entry, - ]; -} - -/** - * Get an entry from a RepoIdRegistry, using the RepoId as a key. - * - * Returns `undefined` if there is no corresponding entry. - */ -export function getEntry( - registry: RepoIdRegistry, - repoId: RepoId -): RegistryEntry | typeof undefined { - return registry.find((entry) => deepEqual(entry.repoId, repoId)); -} - -/** - * Create a new, empty RepoIdRegistry. - */ -export function emptyRegistry(): RepoIdRegistry { - return []; -} - -/** - * Load the RepoIdRegistry from its conventional location within the - * provided sourcecredDirectory. - * - * The conventional location is the REPO_ID_REGISTRY_FILE within - * the sourcecred directory. - * - * If no registry file is present, an empty registry will be returned - * (but not written to disk). - */ -export function getRegistry(sourcecredDirectory: string): RepoIdRegistry { - const registryFile = path.join(sourcecredDirectory, REPO_ID_REGISTRY_FILE); - if (fs.existsSync(registryFile)) { - const contents = fs.readFileSync(registryFile); - const registryJSON: RepoIdRegistryJSON = JSON.parse(contents.toString()); - return fromJSON(registryJSON); - } else { - return emptyRegistry(); - } -} - -/** - * Write the RepoIdRegistry to its conventional location within the - * provided sourcecredDirectory. - * - * The conventional location is the REPO_ID_REGISTRY_FILE within - * the sourcecred directory. - * - * If a registry is already present, it will be overwritten. - */ -export function writeRegistry( - registry: RepoIdRegistry, - sourcecredDirectory: string -) { - const registryFile = path.join(sourcecredDirectory, REPO_ID_REGISTRY_FILE); - fs.writeFileSync(registryFile, stringify(toJSON(registry))); -} - -/** - * Convert a RepoIdRegistry to JSON. - * Exported for testing purposes. - */ -export function toJSON(r: RepoIdRegistry): RepoIdRegistryJSON { - return toCompat(REPO_ID_REGISTRY_COMPAT, r); -} - -/** - * Convert a RepoIdRegistryJSON to a RepoIdRegistry. - * Exported for testing purposes. - */ -export function fromJSON(j: RepoIdRegistryJSON): RepoIdRegistry { - return fromCompat(REPO_ID_REGISTRY_COMPAT, j); -} diff --git a/src/core/repoIdRegistry.test.js b/src/core/repoIdRegistry.test.js deleted file mode 100644 index 1e11576..0000000 --- a/src/core/repoIdRegistry.test.js +++ /dev/null @@ -1,130 +0,0 @@ -// @flow - -import { - toJSON, - fromJSON, - addEntry, - getEntry, - emptyRegistry, - type RepoIdRegistry, - getRegistry, - writeRegistry, - REPO_ID_REGISTRY_FILE, -} from "./repoIdRegistry"; - -import {makeRepoId} from "../core/repoId"; -import tmp from "tmp"; -import path from "path"; -import fs from "fs"; - -describe("core/repoIdRegistry", () => { - describe("fromJSON compose on", () => { - function checkExample(x: RepoIdRegistry) { - expect(fromJSON(toJSON(x))).toEqual(x); - expect(toJSON(fromJSON(toJSON(x)))).toEqual(toJSON(x)); - } - it("empty registry", () => { - checkExample(emptyRegistry()); - }); - it("nonempty registry", () => { - checkExample([ - {repoId: makeRepoId("foo", "bar")}, - {repoId: makeRepoId("zoo", "zod")}, - ]); - }); - }); - - describe("addEntry", () => { - it("adds to empty registry", () => { - expect( - addEntry(emptyRegistry(), {repoId: makeRepoId("foo", "bar")}) - ).toEqual([{repoId: makeRepoId("foo", "bar")}]); - }); - it("adds to nonempty registry", () => { - const registry = [{repoId: makeRepoId("foo", "bar")}]; - expect(addEntry(registry, {repoId: makeRepoId("zoo", "zod")})).toEqual([ - {repoId: makeRepoId("foo", "bar")}, - {repoId: makeRepoId("zoo", "zod")}, - ]); - }); - it("adding repoId that is already the last has no effect", () => { - const registry = [ - {repoId: makeRepoId("zoo", "zod")}, - {repoId: makeRepoId("foo", "bar")}, - ]; - expect(addEntry(registry, {repoId: makeRepoId("foo", "bar")})).toEqual( - registry - ); - }); - it("adding already-existing repoId shifts it to the end", () => { - const registry = [ - {repoId: makeRepoId("zoo", "zod")}, - {repoId: makeRepoId("foo", "bar")}, - ]; - expect(addEntry(registry, {repoId: makeRepoId("zoo", "zod")})).toEqual([ - {repoId: makeRepoId("foo", "bar")}, - {repoId: makeRepoId("zoo", "zod")}, - ]); - }); - }); - - describe("getEntry", () => { - it("returns the matching entry by RepoId", () => { - const entry = {repoId: makeRepoId("zoo", "zod")}; - const registry = addEntry(emptyRegistry(), entry); - expect(getEntry(registry, entry.repoId)).toBe(entry); - }); - it("returns undefined if there is no matching entry", () => { - expect(getEntry(emptyRegistry(), makeRepoId("foo", "bar"))).toBe( - undefined - ); - }); - }); - - it("empty registry is empty", () => { - expect(emptyRegistry()).toEqual([]); - }); - - describe("{get,write}Registry", () => { - const repoId = () => makeRepoId("foo", "bar"); - const entry = () => ({repoId: repoId()}); - const registry = () => addEntry(emptyRegistry(), entry()); - - it("getRegistry returns empty registry if nothing present", () => { - const dirname = tmp.dirSync().name; - expect(getRegistry(dirname)).toEqual(emptyRegistry()); - }); - - it("writeRegistry writes a repoIdRegistry to the directory", () => { - const dirname = tmp.dirSync().name; - const registryFile = path.join(dirname, REPO_ID_REGISTRY_FILE); - - expect(fs.existsSync(registryFile)).toBe(false); - writeRegistry(registry(), dirname); - expect(fs.existsSync(registryFile)).toBe(true); - - const contents = fs.readFileSync(registryFile); - const registryJSON = JSON.parse(contents.toString()); - expect(toJSON(registry())).toEqual(registryJSON); - }); - - it("getRegistry returns the registry written by writeRegistry", () => { - const dirname = tmp.dirSync().name; - const repoId = makeRepoId("foo", "bar"); - const entry = {repoId}; - const registry = addEntry(emptyRegistry(), entry); - writeRegistry(registry, dirname); - expect(getRegistry(dirname)).toEqual(registry); - }); - - it("writeRegistry overwrites any existing registry", () => { - const dirname = tmp.dirSync().name; - const repoId = makeRepoId("foo", "bar"); - const entry = {repoId}; - const registry = addEntry(emptyRegistry(), entry); - writeRegistry(registry, dirname); - writeRegistry(emptyRegistry(), dirname); - expect(getRegistry(dirname)).toEqual(emptyRegistry()); - }); - }); -}); diff --git a/src/homepage/routeData.js b/src/homepage/routeData.js index 6530632..b98d899 100644 --- a/src/homepage/routeData.js +++ b/src/homepage/routeData.js @@ -8,7 +8,6 @@ /*:: import type {Assets} from "../webutil/assets"; -import type {RepoIdRegistry} from "../core/repoIdRegistry"; type RouteDatum = {| +path: string,