Skip to content

Conversation

github-actions[bot]
Copy link

@github-actions github-actions bot commented Jul 9, 2025

Closes #383

Applied changes based on Issue #383.

I’ve centralized all of the “create‑or‑update” comment logic into a new upsertComment helper in src/github/github.ts, and updated src/github/action.ts to use it instead of the two almost‑identical inline functions (updateFinalComment and updateOrCreateComment).


Changes in src/github/github.ts

Added a new helper that wraps postComment, issues.updateComment, and pulls.updateReviewComment behind a single upsertComment API:

/**
 * Create or update a comment: if commentId is provided, update existing comment,
 * otherwise create a new comment.
 */
export async function upsertComment(
  octokit: Octokit,
  repo: RepoContext,
  event: GitHubEvent,
  commentId: number | undefined,
  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)
      }`,
    );
  }
}

【F:src/github/github.ts†L914-L954】


Changes in src/github/action.ts

  1. Import upsertComment and remove the now‑unused postComment import.
  2. Removed the inline updateFinalComment and updateOrCreateComment functions.
  3. Replaced all calls to those inlined helpers with upsertComment(...).
-import {
-  cloneRepository,
-  addEyeReaction,
-  createPullRequest,
-  commitAndPush,
-  postComment,
-  generatePrompt,
-  removeEyeReaction,
-  addThumbUpReaction,
-} from './github.js';
+import {
+  cloneRepository,
+  addEyeReaction,
+  createPullRequest,
+  commitAndPush,
+  generatePrompt,
+  removeEyeReaction,
+  addThumbUpReaction,
+  upsertComment,
+} from './github.js';

@@ runAction
-  /**
-   * Updates the progress comment in place to display the final result.
-   */
-  async function updateFinalComment(body: string): Promise<void> {
-
-  }

@@ runAction (error handling)
-    await updateFinalComment(`CLI execution failed: ${msg}`);
+    await upsertComment(
+      octokit,
+      repo,
+      agentEvent.github,
+      progressCommentId,
+      `CLI execution failed: ${msg}`,
+    );

@@ async function handleResult
-  /** Update the progress comment in place or create a new comment if none exists. */
-  async function updateOrCreateComment(body: string): Promise<void> {
-
-  }

@@ handleResult (post–no‑PR diff path)
-    await updateOrCreateComment(commentBody);
+    await upsertComment(octokit, repo, event, progressCommentId, commentBody);

@@ handleResult (no‑changes‑left path)
-    await updateOrCreateComment(output);
+    await upsertComment(octokit, repo, event, progressCommentId, output);

【F:src/github/action.ts†L9-L20】【F:src/github/action.ts†L630-L642】【F:src/github/action.ts†L234-L260】【F:src/github/action.ts†L377-L387】


Summary of refactoring

  • DRY: Eliminated two almost‑duplicate inline helpers (updateFinalComment & updateOrCreateComment).
  • Single API: All “create‑or‑update” comment paths now funnel through upsertComment.
  • Cleaner: action.ts is leaner, focusing on orchestration rather than GitHub API branching.

These changes keep behavior identical while centralizing comment upsert logic for progress and final‐output comments. Let me know if you’d like any tweaks!

@YiweiShen YiweiShen merged commit c461405 into main Jul 10, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-383-refactor-github-extract-github-comment-update-create-helper-3054117138 branch July 10, 2025 00:56
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.

Extract update/create comment helper

1 participant