Initiatives: pass SOURCECRED_INITIATIVES_DIRECTORY to LoadContext (#1672)

This represent the local path which we didn't include in the Project.
This commit is contained in:
Robin van Boven 2020-02-28 05:15:08 -07:00 committed by GitHub
parent 4d0a7fd60b
commit 68aac1f48d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 4 deletions

View File

@ -17,6 +17,7 @@ export type LoadOptions = {|
+plugins: $ReadOnlyArray<PluginDeclaration>, +plugins: $ReadOnlyArray<PluginDeclaration>,
+sourcecredDirectory: string, +sourcecredDirectory: string,
+githubToken: ?GithubToken, +githubToken: ?GithubToken,
+initiativesDirectory: ?string,
|}; |};
/** /**
@ -32,12 +33,14 @@ export async function load(
project, project,
params, params,
weightsOverrides, weightsOverrides,
initiativesDirectory,
} = options; } = options;
const data = new DataDirectory(sourcecredDirectory); const data = new DataDirectory(sourcecredDirectory);
const context = new LoadContext({ const context = new LoadContext({
cache: (data: CacheProvider), cache: (data: CacheProvider),
githubToken, githubToken,
reporter, reporter,
initiativesDirectory,
}); });
const result = await context.load(project, { const result = await context.load(project, {
params: params || {}, params: params || {},

View File

@ -28,6 +28,7 @@ export type LoadContextOptions = {|
+cache: CacheProvider, +cache: CacheProvider,
+reporter: TaskReporter, +reporter: TaskReporter,
+githubToken: ?GithubToken, +githubToken: ?GithubToken,
+initiativesDirectory: ?string,
|}; |};
type OptionalLoadArguments = {| type OptionalLoadArguments = {|

View File

@ -13,6 +13,7 @@ const fakes = {
contractedGraph: ({fake: "contractedGraph"}: any), contractedGraph: ({fake: "contractedGraph"}: any),
weightedGraph: ({fake: "weightedGraph"}: any), weightedGraph: ({fake: "weightedGraph"}: any),
timelineCred: ({fake: "timelineCred"}: any), timelineCred: ({fake: "timelineCred"}: any),
initiativesDirectory: ({fake: "initiativesDirectory"}: any),
}; };
const mockCacheProvider = (): CacheProvider => ({ const mockCacheProvider = (): CacheProvider => ({
@ -66,6 +67,7 @@ describe("src/backend/loadContext", () => {
const githubToken = validateToken("0".repeat(40)); const githubToken = validateToken("0".repeat(40));
const project = createProject({id: "testing-project"}); const project = createProject({id: "testing-project"});
const params = {alpha: 0.123}; const params = {alpha: 0.123};
const initiativesDirectory = fakes.initiativesDirectory;
describe("constructor", () => { describe("constructor", () => {
/** /**
@ -80,7 +82,12 @@ describe("src/backend/loadContext", () => {
const reporter = new TestTaskReporter(); const reporter = new TestTaskReporter();
// When // When
const loadContext = new LoadContext({cache, githubToken, reporter}); const loadContext = new LoadContext({
cache,
githubToken,
reporter,
initiativesDirectory,
});
// Then // Then
expect(loadContext).toMatchObject({ expect(loadContext).toMatchObject({
@ -105,7 +112,12 @@ describe("src/backend/loadContext", () => {
const cache = mockCacheProvider(); const cache = mockCacheProvider();
const reporter = new TestTaskReporter(); const reporter = new TestTaskReporter();
const weightsOverrides = Weights.empty(); const weightsOverrides = Weights.empty();
const loadContext = new LoadContext({cache, githubToken, reporter}); const loadContext = new LoadContext({
cache,
githubToken,
reporter,
initiativesDirectory,
});
const spies = mockProxyMethods(loadContext, project, cache); const spies = mockProxyMethods(loadContext, project, cache);
// When // When
@ -114,6 +126,7 @@ describe("src/backend/loadContext", () => {
// Then // Then
const cachedProject = {project, cache}; const cachedProject = {project, cache};
const expectedEnv = { const expectedEnv = {
initiativesDirectory,
githubToken, githubToken,
reporter, reporter,
cache, cache,
@ -155,7 +168,12 @@ describe("src/backend/loadContext", () => {
// Given // Given
const cache = mockCacheProvider(); const cache = mockCacheProvider();
const reporter = new TestTaskReporter(); const reporter = new TestTaskReporter();
const loadContext = new LoadContext({cache, githubToken, reporter}); const loadContext = new LoadContext({
cache,
githubToken,
reporter,
initiativesDirectory,
});
const spies = mockProxyMethods(loadContext, project, cache); const spies = mockProxyMethods(loadContext, project, cache);
// When // When
@ -163,6 +181,7 @@ describe("src/backend/loadContext", () => {
// Then // Then
const expectedEnv = { const expectedEnv = {
initiativesDirectory,
githubToken, githubToken,
reporter, reporter,
cache, cache,
@ -185,7 +204,12 @@ describe("src/backend/loadContext", () => {
const cache = mockCacheProvider(); const cache = mockCacheProvider();
const reporter = new TestTaskReporter(); const reporter = new TestTaskReporter();
const weightsOverrides = Weights.empty(); const weightsOverrides = Weights.empty();
const loadContext = new LoadContext({cache, githubToken, reporter}); const loadContext = new LoadContext({
cache,
githubToken,
reporter,
initiativesDirectory,
});
mockProxyMethods(loadContext, project, cache); mockProxyMethods(loadContext, project, cache);
// When // When

View File

@ -21,6 +21,10 @@ export function sourcecredDirectory(): string {
return env != null ? env : defaultSourcecredDirectory(); return env != null ? env : defaultSourcecredDirectory();
} }
export function initiativesDirectory(): string | null {
return process.env.SOURCECRED_INITIATIVES_DIRECTORY || null;
}
export function githubToken(): ?GithubToken { export function githubToken(): ?GithubToken {
const envToken = process.env.SOURCECRED_GITHUB_TOKEN; const envToken = process.env.SOURCECRED_GITHUB_TOKEN;
if (envToken == null || !envToken.length) { if (envToken == null || !envToken.length) {

View File

@ -13,10 +13,13 @@ import {
sourcecredDirectory, sourcecredDirectory,
githubToken, githubToken,
loadWeights, loadWeights,
initiativesDirectory,
} from "./common"; } from "./common";
describe("cli/common", () => { describe("cli/common", () => {
const exampleGithubToken = validateToken("0".repeat(40)); const exampleGithubToken = validateToken("0".repeat(40));
const exampleInitiativesDirectory = path.join(__dirname, "initiatives");
beforeEach(() => { beforeEach(() => {
jest jest
.spyOn(require("os"), "tmpdir") .spyOn(require("os"), "tmpdir")
@ -62,6 +65,17 @@ describe("cli/common", () => {
}); });
}); });
describe("initiativesDirectory", () => {
it("uses the environment variable when available", () => {
process.env.SOURCECRED_INITIATIVES_DIRECTORY = exampleInitiativesDirectory;
expect(initiativesDirectory()).toEqual(exampleInitiativesDirectory);
});
it("returns `null` if the environment variable is not set", () => {
delete process.env.SOURCECRED_INITIATIVES_DIRECTORY;
expect(initiativesDirectory()).toBe(null);
});
});
describe("loadWeights", () => { describe("loadWeights", () => {
function tmpWithContents(contents: mixed) { function tmpWithContents(contents: mixed) {
const name = tmp.tmpNameSync(); const name = tmp.tmpNameSync();

View File

@ -102,6 +102,7 @@ const command: Command = async (args, std) => {
plugins, plugins,
sourcecredDirectory: Common.sourcecredDirectory(), sourcecredDirectory: Common.sourcecredDirectory(),
githubToken: null, githubToken: null,
initiativesDirectory: null,
}, },
taskReporter taskReporter
); );

View File

@ -57,11 +57,17 @@ function usage(print: (string) => void): void {
public repositories, no special permissions are required. public repositories, no special permissions are required.
For private repositories, the 'repo' scope is required. For private repositories, the 'repo' scope is required.
SOURCECRED_INITIATIVES_DIRECTORY
Local path to a directory containing json files with
initiative declarations. Required when using the Initiatives
plugin; ignored otherwise.
SOURCECRED_DIRECTORY SOURCECRED_DIRECTORY
Directory owned by SourceCred, in which data, caches, Directory owned by SourceCred, in which data, caches,
registries, etc. are stored. Optional: defaults to a registries, etc. are stored. Optional: defaults to a
directory 'sourcecred' under your OS's temporary directory; directory 'sourcecred' under your OS's temporary directory;
namely: namely:
${Common.defaultSourcecredDirectory()} ${Common.defaultSourcecredDirectory()}
`.trimRight() `.trimRight()
); );
@ -112,6 +118,7 @@ const loadCommand: Command = async (args, std) => {
weights = await loadWeightOverrides(weightsPath); weights = await loadWeightOverrides(weightsPath);
} }
const initiativesDirectory = Common.initiativesDirectory();
const githubToken = Common.githubToken(); const githubToken = Common.githubToken();
if (githubToken == null) { if (githubToken == null) {
return die(std, "SOURCECRED_GITHUB_TOKEN not set"); return die(std, "SOURCECRED_GITHUB_TOKEN not set");
@ -142,6 +149,7 @@ const loadCommand: Command = async (args, std) => {
plugins, plugins,
sourcecredDirectory: Common.sourcecredDirectory(), sourcecredDirectory: Common.sourcecredDirectory(),
githubToken, githubToken,
initiativesDirectory,
}; };
}); });
// Deliberately load in serial because GitHub requests that their API not // Deliberately load in serial because GitHub requests that their API not

View File

@ -34,6 +34,7 @@ describe("cli/load", () => {
const dirname = tmp.dirSync().name; const dirname = tmp.dirSync().name;
process.env.SOURCECRED_DIRECTORY = dirname; process.env.SOURCECRED_DIRECTORY = dirname;
process.env.SOURCECRED_GITHUB_TOKEN = exampleGithubToken; process.env.SOURCECRED_GITHUB_TOKEN = exampleGithubToken;
process.env.SOURCECRED_INITIATIVES_DIRECTORY = tmp.dirSync().name;
return dirname; return dirname;
} }
@ -81,6 +82,7 @@ describe("cli/load", () => {
plugins: [githubDeclaration], plugins: [githubDeclaration],
sourcecredDirectory: Common.sourcecredDirectory(), sourcecredDirectory: Common.sourcecredDirectory(),
githubToken: exampleGithubToken, githubToken: exampleGithubToken,
initiativesDirectory: Common.initiativesDirectory(),
}; };
expect(await invocation).toEqual({ expect(await invocation).toEqual({
exitCode: 0, exitCode: 0,
@ -105,6 +107,7 @@ describe("cli/load", () => {
plugins: [githubDeclaration], plugins: [githubDeclaration],
sourcecredDirectory: Common.sourcecredDirectory(), sourcecredDirectory: Common.sourcecredDirectory(),
githubToken: exampleGithubToken, githubToken: exampleGithubToken,
initiativesDirectory: Common.initiativesDirectory(),
}); });
expect(await invocation).toEqual({ expect(await invocation).toEqual({
exitCode: 0, exitCode: 0,
@ -142,6 +145,7 @@ describe("cli/load", () => {
plugins: [githubDeclaration], plugins: [githubDeclaration],
sourcecredDirectory: Common.sourcecredDirectory(), sourcecredDirectory: Common.sourcecredDirectory(),
githubToken: exampleGithubToken, githubToken: exampleGithubToken,
initiativesDirectory: Common.initiativesDirectory(),
}; };
expect(await invocation).toEqual({ expect(await invocation).toEqual({
exitCode: 0, exitCode: 0,