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}
```
This commit is contained in:
Dandelion Mané 2018-07-24 12:33:58 -07:00 committed by GitHub
parent 94a023ef6f
commit cd6c869d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 1 deletions

View File

@ -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));
}