Skip to content

Commit

Permalink
Merge pull request #56 from ubiquity-os/development
Browse files Browse the repository at this point in the history
Merge development into main
  • Loading branch information
gentlementlegen authored Jan 23, 2025
2 parents 7cfd969 + 3f56173 commit 9914bb3
Showing 1 changed file with 17 additions and 3 deletions.
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

0 comments on commit 9914bb3

Please sign in to comment.