mirror of
https://github.com/status-im/sourcecred.git
synced 2025-02-27 11:40:26 +00:00
Backend: implement PluginLoaders.declarations (#1616)
This is the first of several commits to create the PluginLoaders abstraction. Using this allows us to define "for all plugins" semantics, while keeping each underlying plugin interface flexible.
This commit is contained in:
parent
ca63ea00fb
commit
8838f856ab
39
src/backend/pluginLoaders.js
Normal file
39
src/backend/pluginLoaders.js
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
//@flow
|
||||||
|
|
||||||
|
import {type Project} from "../core/project";
|
||||||
|
import {type PluginDeclaration} from "../analysis/pluginDeclaration";
|
||||||
|
import {type Loader as GithubLoader} from "../plugins/github/loader";
|
||||||
|
import {type Loader as IdentityLoader} from "../plugins/identity/loader";
|
||||||
|
import {type Loader as DiscourseLoader} from "../plugins/discourse/loader";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A type combining all known plugin Loader interfaces.
|
||||||
|
*
|
||||||
|
* Using this allows us to define "for all plugins" semantics, while keeping
|
||||||
|
* each underlying plugin's interface flexible.
|
||||||
|
*/
|
||||||
|
export type PluginLoaders = {|
|
||||||
|
+github: GithubLoader,
|
||||||
|
+discourse: DiscourseLoader,
|
||||||
|
+identity: IdentityLoader,
|
||||||
|
|};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all relevant PluginDeclarations for a given Project.
|
||||||
|
*/
|
||||||
|
export function declarations(
|
||||||
|
{github, discourse, identity}: PluginLoaders,
|
||||||
|
project: Project
|
||||||
|
): $ReadOnlyArray<PluginDeclaration> {
|
||||||
|
const plugins: PluginDeclaration[] = [];
|
||||||
|
if (project.repoIds.length) {
|
||||||
|
plugins.push(github.declaration());
|
||||||
|
}
|
||||||
|
if (project.discourseServer != null) {
|
||||||
|
plugins.push(discourse.declaration());
|
||||||
|
}
|
||||||
|
if (project.identities.length) {
|
||||||
|
plugins.push(identity.declaration());
|
||||||
|
}
|
||||||
|
return plugins;
|
||||||
|
}
|
72
src/backend/pluginLoaders.test.js
Normal file
72
src/backend/pluginLoaders.test.js
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {createProject} from "../core/project";
|
||||||
|
import {makeRepoId} from "../plugins/github/repoId";
|
||||||
|
import * as PluginLoaders from "./pluginLoaders";
|
||||||
|
|
||||||
|
const fakeGithubDec = ("fake-github-dec": any);
|
||||||
|
const fakeDiscourseDec = ("fake-discourse-dec": any);
|
||||||
|
const fakeIdentityDec = ("fake-identity-dec": any);
|
||||||
|
|
||||||
|
const mockPluginLoaders = () => ({
|
||||||
|
github: {
|
||||||
|
declaration: jest.fn().mockReturnValue(fakeGithubDec),
|
||||||
|
},
|
||||||
|
discourse: {
|
||||||
|
declaration: jest.fn().mockReturnValue(fakeDiscourseDec),
|
||||||
|
},
|
||||||
|
identity: {
|
||||||
|
declaration: jest.fn().mockReturnValue(fakeIdentityDec),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("src/backend/pluginLoaders", () => {
|
||||||
|
const exampleRepoId = makeRepoId("sourcecred-test", "example-github");
|
||||||
|
|
||||||
|
describe("declarations", () => {
|
||||||
|
it("should include discourse declaration", async () => {
|
||||||
|
// Given
|
||||||
|
const loaders = mockPluginLoaders();
|
||||||
|
const project = createProject({
|
||||||
|
id: "has-discourse",
|
||||||
|
discourseServer: {serverUrl: "http://foo.bar"},
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
const decs = PluginLoaders.declarations(loaders, project);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(decs).toEqual([fakeDiscourseDec]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should include github declaration", async () => {
|
||||||
|
// Given
|
||||||
|
const loaders = mockPluginLoaders();
|
||||||
|
const project = createProject({
|
||||||
|
id: "has-github",
|
||||||
|
repoIds: [exampleRepoId],
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
const decs = PluginLoaders.declarations(loaders, project);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(decs).toEqual([fakeGithubDec]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should include identity declaration", async () => {
|
||||||
|
// Given
|
||||||
|
const loaders = mockPluginLoaders();
|
||||||
|
const project = createProject({
|
||||||
|
id: "has-identity",
|
||||||
|
identities: [{username: "foo", aliases: ["github/foo"]}],
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
const decs = PluginLoaders.declarations(loaders, project);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(decs).toEqual([fakeIdentityDec]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
12
src/plugins/discourse/loader.js
Normal file
12
src/plugins/discourse/loader.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {type PluginDeclaration} from "../../analysis/pluginDeclaration";
|
||||||
|
import {declaration} from "./declaration";
|
||||||
|
|
||||||
|
export interface Loader {
|
||||||
|
declaration(): PluginDeclaration;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ({
|
||||||
|
declaration: () => declaration,
|
||||||
|
}: Loader);
|
12
src/plugins/github/loader.js
Normal file
12
src/plugins/github/loader.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {type PluginDeclaration} from "../../analysis/pluginDeclaration";
|
||||||
|
import {declaration} from "./declaration";
|
||||||
|
|
||||||
|
export interface Loader {
|
||||||
|
declaration(): PluginDeclaration;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ({
|
||||||
|
declaration: () => declaration,
|
||||||
|
}: Loader);
|
12
src/plugins/identity/loader.js
Normal file
12
src/plugins/identity/loader.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {type PluginDeclaration} from "../../analysis/pluginDeclaration";
|
||||||
|
import {declaration} from "./declaration";
|
||||||
|
|
||||||
|
export interface Loader {
|
||||||
|
declaration(): PluginDeclaration;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ({
|
||||||
|
declaration: () => declaration,
|
||||||
|
}: Loader);
|
Loading…
x
Reference in New Issue
Block a user