From cd6c869d84c730e2b9d2a7ccbcace9c1ba5b7622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Tue, 24 Jul 2018 12:33:58 -0700 Subject: [PATCH] Add a registry of loaded repositories (#516) We want the UI to offer a list of available repositories, rather than using a text input box. To do this, we first need the backend to include a registry of all available repositories. Test plan: Sadly we don't have CLI testing, so I manually verified this by doing the following: ``` $ yarn backend $ rm -r $SOURCECRED_DIRECTORY $ node bin/sourcecred.js load sourcecred example-github $ cat $SOURCECRED_DIRECTORY/repositoryRegistry.json {"sourcecred/example-github":true} $ node bin/sourcecred.js load sourcecred example-github $ cat $SOURCECRED_DIRECTORY/repositoryRegistry.json {"sourcecred/example-github":true} $ node bin/sourcecred.js load sourcecred example-git $ cat $SOURCECRED_DIRECTORY/repositoryRegistry.json {"sourcecred/example-git":true,"sourcecred/example-github":true} ``` --- src/cli/commands/load.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/load.js b/src/cli/commands/load.js index b41aae5..fc05e2f 100644 --- a/src/cli/commands/load.js +++ b/src/cli/commands/load.js @@ -3,6 +3,8 @@ import {Command, flags} from "@oclif/command"; import mkdirp from "mkdirp"; import path from "path"; +import fs from "fs"; +import stringify from "json-stable-stringify"; import {loadGithubData} from "../../plugins/github/loadGithubData"; import {loadGitData} from "../../plugins/git/loadGitData"; @@ -72,7 +74,13 @@ export default class PluginGraphCommand extends Command { } } -function loadAllPlugins({repoOwner, repoName, githubToken, maxOldSpaceSize}) { +function loadAllPlugins({ + basedir, + repoOwner, + repoName, + githubToken, + maxOldSpaceSize, +}) { if (githubToken == null) { // TODO: This check should be abstracted so that plugins can // specify their argument dependencies and get nicely @@ -100,6 +108,9 @@ function loadAllPlugins({repoOwner, repoName, githubToken, maxOldSpaceSize}) { })), ]; execDependencyGraph(tasks, {taskPassLabel: "DONE"}).then(({success}) => { + if (success) { + addToRepoRegistry({basedir, repoOwner, repoName}); + } process.exitCode = success ? 0 : 1; }); } @@ -140,3 +151,22 @@ function loadPlugin({basedir, plugin, repoOwner, repoName, githubToken}) { return; } } + +const REPO_REGISTRY_FILE = "repositoryRegistry.json"; + +function addToRepoRegistry(options) { + // TODO: Make this function transactional before loading repositories in + // parallel. + const {basedir, repoOwner, repoName} = options; + const outputFile = path.join(basedir, REPO_REGISTRY_FILE); + let registry = null; + if (fs.existsSync(outputFile)) { + const contents = fs.readFileSync(outputFile); + registry = JSON.parse(contents.toString()); + } else { + registry = {}; + } + + registry[`${repoOwner}/${repoName}`] = true; + fs.writeFileSync(outputFile, stringify(registry)); +}