Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added return to postComment #55

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import { LogReturn, Metadata } from "@ubiquity-os/ubiquity-os-logger";
import { Context } from "./context";
import { PluginRuntimeInfo } from "./helpers/runtime-info";
Expand All @@ -16,8 +17,18 @@ export interface CommentOptions {
updateComment?: boolean;
}

type WithIssueNumber<T> = T & {
issueNumber: number;
};

export type PostComment = {
(context: Context, message: LogReturn | Error, options?: CommentOptions): Promise<void>;
(
context: Context,
message: LogReturn | Error,
options?: CommentOptions
): Promise<WithIssueNumber<
RestEndpointMethodTypes["issues"]["updateComment"]["response"]["data"] | RestEndpointMethodTypes["issues"]["createComment"]["response"]["data"]
> | null>;
lastCommentId?: number;
};

Expand All @@ -39,18 +50,19 @@ export const postComment: PostComment = async function (
issueNumber = context.payload.discussion.number;
} else {
context.logger.info("Cannot post comment because issue is not found in the payload.");
return;
return null;
}

if ("repository" in context.payload && context.payload.repository?.owner?.login) {
const body = await createStructuredMetadataWithMessage(context, message, options);
if (options.updateComment && postComment.lastCommentId) {
await context.octokit.rest.issues.updateComment({
const commentData = await context.octokit.rest.issues.updateComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
comment_id: postComment.lastCommentId,
body: body,
});
return { ...commentData.data, issueNumber };
} else {
const commentData = await context.octokit.rest.issues.createComment({
owner: context.payload.repository.owner.login,
Expand All @@ -59,10 +71,12 @@ export const postComment: PostComment = async function (
body: body,
});
postComment.lastCommentId = commentData.data.id;
return { ...commentData.data, issueNumber };
}
} else {
context.logger.info("Cannot post comment because repository is not found in the payload.", { payload: context.payload });
}
return null;
};

async function createStructuredMetadataWithMessage(context: Context, message: LogReturn | Error, options: CommentOptions) {
Expand Down
Loading