Skip to content

Conversation

github-actions[bot]
Copy link

Closes #537

Applied changes based on Issue #537.

I’ve refactored src/github/comments.ts to improve clarity and modularity while preserving its existing behavior:

What Changed

  • Removed dead import of GitHubError.
  • Introduced type‐guard helpers (isIssueEvent / isReviewCommentEvent) for clear narrowing of the GitHubEvent union.
  • Extracted two small helpers:
    • postIssueComment for creating issue/PR comments via the issues API (with truncation and logging).
    • postReviewReply for replying to PR review comments, with a built‑in fallback to a regular PR comment.
  • Refactored postComment to simply dispatch to those helpers via early‐returns, flattening nested if/try blocks.
  • Refactored upsertComment to use the same issue‐vs‐PR branching via early‐returns and invoke postComment as needed.
  • Kept all existing behavior (body truncation, logging levels, error handling) exactly the same.
--- a/src/github/comments.ts
+++ b/src/github/comments.ts
@@ -1,6 +1,7 @@
 /** 
  * Utilities for posting and updating comments on GitHub issues and pull requests.
  */
+import * as core from '@actions/core';
 import type { Octokit } from 'octokit';
-import { GitHubError } from '../utils/errors';
 import type {
   GitHubEvent,
   GitHubEventPullRequestReviewCommentCreated,
@@ -10,6 +11,13 @@ import type {
 } from './types';
 import { truncateOutput } from './utils';
 
+// Type guards for GitHubEvent type narrowing
+function isIssueEvent(event: GitHubEvent): event is { issue: { number: number } } {
+  return 'issue' in event;
+}
+
+function isReviewCommentEvent(
+  event: GitHubEvent,
+): event is GitHubEventPullRequestReviewCommentCreated {
+  return 'pull_request' in event && 'comment' in event;
+}
+
 /** 
  * Post a comment to an issue (or pull request via issues API).
  */
@@ -21,6 +29,26 @@ async function postIssueComment(
   core.info(`Comment posted to Issue/PR #${issueNumber}`);
 }
 
+/**
+ * Post a reply to a pull request review comment, or fallback to a regular PR comment.
+ */
+async function postReviewReply(
+  octokit: Octokit,
+  repo: RepoContext,
+  prNumber: number,
+  commentId: number,
+  inReplyToId: number | undefined,
+  body: string,
+): Promise<void> {
+  try {
+    await octokit.rest.pulls.createReplyForReviewComment({
+      ...repo,
+      pull_number: prNumber,
+      comment_id: inReplyToId ?? commentId,
+      body: truncateOutput(body),
+    });
+    core.info(`Comment posted to PR #${prNumber} reply to comment #${commentId}`);
+  } catch (error) {
+    core.warning(
+      `Failed to post reply comment: ${error instanceof Error ? error.message : error}`,
+    );
+    await postIssueComment(octokit, repo, prNumber, body);
+    core.info(`Regular comment posted to PR #${prNumber}`);
+  }
+}
+
 /** 
  * Posts a comment to an issue or pull request.
  * @param octokit - Authenticated Octokit client.
@@ -37,48 +65,24 @@ export async function postComment(
   body: string,
 ): Promise<void> {
-  try {
-    if ('issue' in event) {
-      const issueNumber = event.issue.number;
-      await octokit.rest.issues.createComment({
-        ...repo,
-        issue_number: issueNumber,
-        body: truncateOutput(body),
-      });
-      core.info(`Comment posted to Issue/PR #${issueNumber}`);
-    } else if ('pull_request' in event && 'comment' in event) {
-      const prNumber = event.pull_request.number;
-      const commentId = event.comment.id;
-      const inReplyTo = event.comment.in_reply_to_id;
-      try {
-        await octokit.rest.pulls.createReplyForReviewComment({
-          ...repo,
-          pull_number: prNumber,
-          comment_id: inReplyTo ?? commentId,
-          body: truncateOutput(body),
-        });
-        core.info(
-          `Comment posted to PR #${prNumber} Reply to comment #${commentId}`,
-        );
-      } catch (error) {
-        core.warning(
-          `Failed to post reply comment: ${
-            error instanceof Error ? error.message : error
-          }`,
-        );
-        await octokit.rest.issues.createComment({
-          ...repo,
-          issue_number: prNumber,
-          body: truncateOutput(body),
-        });
-        core.info(`Regular comment posted to PR #${prNumber}`);
-      }
-    }
-  } catch (error) {
-    core.error(
-      `Failed to post comment: ${
-        error instanceof Error ? error.message : error
-      }`,
-    );
-  }
-}
+  try {
+    if (isIssueEvent(event)) {
+      await postIssueComment(octokit, repo, event.issue.number, body);
+      return;
+    }
+    if (isReviewCommentEvent(event)) {
+      await postReviewReply(
+        octokit,
+        repo,
+        event.pull_request.number,
+        event.comment.id,
+        event.comment.in_reply_to_id,
+        body,
+      );
+      return;
+    }
+  } catch (error) {
+    core.error(
+      `Failed to post comment: ${
+        error instanceof Error ? error.message : error
+      }`,
+    );
+  }
+}
 
 /**
  * Creates or updates a comment based on the presence of commentId.
@@ -87,45 +91,38 @@ export async function upsertComment(
   body: string,
 ): Promise<void> {
-  try {
-    if (!commentId) {
-      await postComment(octokit, repo, event, body);
-    } else if ('issue' in event) {
-      await octokit.rest.issues.updateComment({
-        ...repo,
-        comment_id: commentId,
-        body,
-      });
-    } else if ('pull_request' in event) {
-      await octokit.rest.pulls.updateReviewComment({
-        ...repo,
-        comment_id: commentId,
-        body,
-      });
-    } else {
-      await postComment(octokit, repo, event, body);
-    }
-  } catch (error) {
-    core.warning(
-      `Failed to upsert comment: ${
-        error instanceof Error ? error.message : String(error)
-      }`,
-    );
-  }
-}
+  try {
+    if (!commentId) {
+      await postComment(octokit, repo, event, body);
+      return;
+    }
+    if (isIssueEvent(event)) {
+      await octokit.rest.issues.updateComment({
+        ...repo,
+        comment_id: commentId,
+        body,
+      });
+      return;
+    }
+    if ('pull_request' in event) {
+      await octokit.rest.pulls.updateReviewComment({
+        ...repo,
+        comment_id: commentId,
+        body,
+      });
+      return;
+    }
+    await postComment(octokit, repo, event, body);
+  } catch (error) {
+    core.warning(
+      `Failed to upsert comment: ${
+        error instanceof Error ? error.message : String(error)
+      }`,
+    );
+  }
+}

No behavioral changes were introduced—just clarity, DRY‑ness, and more idiomatic TypeScript. Let me know if you’d like any further tweaks!

@YiweiShen YiweiShen merged commit 51f1e81 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-537-refactor-github-comments-simplify-comments-module-3121099277 branch July 26, 2025 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Done] Refactor src/github/comments.ts

1 participant