mirror of
https://github.com/status-im/sourcecred.git
synced 2025-01-24 03:19:06 +00:00
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
This commit is contained in:
parent
32f83ad676
commit
01248fb8f6
@ -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")];
|
||||
|
@ -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");
|
||||
|
@ -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<RegistryEntry>;
|
||||
export type RepoIdRegistryJSON = Compatible<RepoIdRegistry>;
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
@ -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());
|
||||
});
|
||||
});
|
||||
});
|
@ -8,7 +8,6 @@
|
||||
|
||||
/*::
|
||||
import type {Assets} from "../webutil/assets";
|
||||
import type {RepoIdRegistry} from "../core/repoIdRegistry";
|
||||
|
||||
type RouteDatum = {|
|
||||
+path: string,
|
||||
|
Loading…
x
Reference in New Issue
Block a user