Skip to content

Commit

Permalink
Merge pull request #543 from reg-viz/feat/ghe-comment-function-export
Browse files Browse the repository at this point in the history
feat: Allow default exported plugin factory
  • Loading branch information
Quramy committed Oct 27, 2021
2 parents 752b16e + ae40cd5 commit 74ddce2
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/reg-notify-github-with-api-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.10.17",
"description": "Notify reg-suit result to GHE repository using API",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "ts-node e2e/script.ts",
"prepublish": "tsc -p tsconfig.build.json"
Expand Down
10 changes: 6 additions & 4 deletions packages/reg-notify-github-with-api-plugin/src/gh-gql-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ export class GhGqlClient {
repository,
branchName,
body,
markerComment = "reg-comment",
}: {
owner: string;
repository: string;
branchName: string;
body: string;
markerComment?: string;
}) {
const { data, errors } = await this._client.query<
ContextData,
Expand All @@ -123,7 +125,7 @@ export class GhGqlClient {
!data.repository.ref.associatedPullRequests.totalCount
) {
// Nothing to do
return;
return [];
}

const pullRequests = data.repository.ref.associatedPullRequests.nodes;
Expand All @@ -132,14 +134,14 @@ export class GhGqlClient {
pullRequests.map(async pullRequest => {
if (pullRequest.comments && pullRequest.comments.nodes) {
const commentToBeUpdated = pullRequest.comments.nodes.find(
c => c.body && c.body.startsWith("<!-- reg-comment -->"),
c => c.body && c.body.startsWith(`<!-- ${markerComment} -->`),
);
if (commentToBeUpdated) {
const { errors } = await this._client.mutate({
mutation: updateCommentMutation,
variables: {
id: commentToBeUpdated.id,
body: `<!-- reg-comment -->\n${body}`,
body: `<!-- ${markerComment} -->\n${body}`,
},
});
if (errors) throw errors;
Expand All @@ -150,7 +152,7 @@ export class GhGqlClient {
mutation: createCommentMutation,
variables: {
id: pullRequest.id,
body: `<!-- reg-comment -->\n${body}`,
body: `<!-- ${markerComment} -->\n${body}`,
},
});
if (errors) throw errors;
Expand Down
4 changes: 3 additions & 1 deletion packages/reg-notify-github-with-api-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { NotifierPluginFactory } from "reg-suit-interface";
import { GhApiNotifierPlugin } from "./gh-api-notifier-plugin";
import { GhApiPreparer } from "./gh-api-preparer";

export { GhGqlClient as GithubCommentClient } from "./gh-gql-client";

const factory: NotifierPluginFactory = () => {
return {
notifier: new GhApiNotifierPlugin(),
preparer: new GhApiPreparer(),
};
};

export = factory;
export default factory;
7 changes: 7 additions & 0 deletions packages/reg-suit-core/src/plugin-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ test("should return pluginHolders", () => {
assert.equal(pm._pluginHolders[0].moduleId, "./lib/testing/dummy-plugin");
});

test("should return pluginHolders2", () => {
const pm = createPluginManager({ "./lib/testing/dummy-plugin-mod": {} });
pm.loadPlugins();
assert.equal(pm._pluginHolders.length, 1);
assert.equal(pm._pluginHolders[0].moduleId, "./lib/testing/dummy-plugin-mod");
});

test("resolve locally installed module", () => {
const pm = createPluginManager({});
expect(() => pm._loadPlugin("reg-simple-keygen-plugin")).not.toThrowError();
Expand Down
2 changes: 1 addition & 1 deletion packages/reg-suit-core/src/plugin-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class PluginManager {
}
if (pluginFileName) {
const factory = require(pluginFileName);
const pluginHolder = factory();
const pluginHolder = typeof factory?.default === "function" ? factory.default() : factory();
this._pluginHolders.push({ ...pluginHolder, moduleId: name });
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/reg-suit-core/src/testing/dummy-plugin-mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const factory = () => ({
notifier: {
notify: () => Promise.resolve("notify"),
},
});

exports.default = factory;
2 changes: 1 addition & 1 deletion packages/reg-suit-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.10.17",
"description": "",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"types": "lib/index.d.ts",
"scripts": {
"test": "echo T.B.D.",
"prepublish": "tsc -p tsconfig.build.json",
Expand Down

0 comments on commit 74ddce2

Please sign in to comment.