Skip to content

Commit

Permalink
feat: Export client to comment PR via branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Oct 27, 2021
1 parent b86a5b3 commit ae40cd5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 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;

0 comments on commit ae40cd5

Please sign in to comment.