Skip to content

Conversation

github-actions[bot]
Copy link

Closes #541

Applied changes based on Issue #541.

I’ve refactored src/github/result-handler.ts to improve modularity and readability while preserving all existing behavior:

  • Removed the unused detectChanges import.
  • Centralized the workflow‑ and image‑artifact ignore logic by introducing constants and helper functions:
    • WORKFLOWS_DIR, IMAGES_DIR, WORKFLOWS_PREFIX, IMAGES_PREFIX
    • filesWithPrefix(), revertWorkflowFiles(), and removeImageArtifacts()
  • Extracted the issue/PR-number logic into getEventNumbers() rather than inline conditionals.
  • Collapsed the duplicate JSDoc and placed a single, clear comment above handleResult().
  • Simplified commit‑message generation to directly call generateCommitMessageOpenAI() with { issueNumber, prNumber }.

These changes maintain the original functionality (ignoring workflow/image files, generating commit messages, creating PRs or posting comments) while making the code easier to follow and extend.


Key diff for result-handler.ts
--- a/src/github/result-handler.ts
+++ b/src/github/result-handler.ts
@@ imports…
-import { detectChanges } from '../file/file';
+// Directories to ignore when processing results
+const WORKFLOWS_DIR = '.github/workflows';
+const IMAGES_DIR    = 'codex-comment-images';
+const WORKFLOWS_PREFIX = `${WORKFLOWS_DIR}/`;
+const IMAGES_PREFIX    = `${IMAGES_DIR}/`;

+/** Filter files by prefix */
+function filesWithPrefix(files: string[], prefix: string[]) { ... }

+/** Revert changes to workflow files via git checkout */
+async function revertWorkflowFiles(workspace: string) { ... }

+/** Remove generated image artifacts directory */
+async function removeImageArtifacts(workspace: string) { ... }

+/** Extract issue and PR numbers from processed event */
+function getEventNumbers(agentEvent: ProcessedEvent['agentEvent']) { ... }

 /**
  * Process and publish the action’s results: push commits, open PR, or post comments.
  */
 export async function handleResult(
@@
-  const workflowFiles = changedFiles.filter((f) => f.startsWith('.github/workflows/'));
-  if (workflowFiles.length > 0) {
-    core.warning(`Ignoring changes to workflow files: ${workflowFiles.join(', ')}`);
-    await execa('git', ['checkout', 'HEAD', '--', '.github/workflows'], { cwd: workspace, stdio: 'inherit' });
-  }
-
-  const imageFiles = changedFiles.filter((f) => f.startsWith('codex-comment-images/'));
-  if (imageFiles.length > 0) {
-    core.warning(`Ignoring changes to codex-comment-images folder: ${imageFiles.join(', ')}`);
-    await fs.rm(path.join(workspace, 'codex-comment-images'), { recursive: true, force: true });
-    core.info(`Removed image artifacts directory: ${path.join(workspace, 'codex-comment-images')}`);
-  }
-
-  const effectiveChangedFiles = changedFiles.filter(
-    (f) => !f.startsWith('.github/workflows/') && !f.startsWith('codex-comment-images/'),
-  );
+  // ignore workflow changes
+  const workflowFiles = filesWithPrefix(changedFiles, WORKFLOWS_PREFIX);
+  if (workflowFiles.length > 0) {
+    core.warning(`Ignoring changes to workflow files: ${workflowFiles.join(', ')}`);
+    await revertWorkflowFiles(workspace);
+  }
+
+  // ignore generated image artifacts
+  const imageFiles = filesWithPrefix(changedFiles, IMAGES_PREFIX);
+  if (imageFiles.length > 0) {
+    core.warning(`Ignoring changes to ${IMAGES_DIR} folder: ${imageFiles.join(', ')}`);
+    await removeImageArtifacts(workspace);
+  }
+
+  const effectiveChangedFiles = changedFiles.filter(
+    (f) => !f.startsWith(WORKFLOWS_PREFIX) && !f.startsWith(IMAGES_PREFIX),
+  );

@@ if (!noPr && effectiveChangedFiles.length > 0) {
-    const generateCommitMessage = generateCommitMessageOpenAI;
-    core.info('[perf] generateCommitMessage start');
-    const startGenerateCommitMessage = Date.now();
-    const commitMessage = await generateCommitMessage(
-      effectiveChangedFiles,
-      userPrompt,
-      {
-        issueNumber: /* long inline guard */,
-        prNumber: /* long inline guard */,
-      },
-      config,
-    );
-    core.info(`[perf] generateCommitMessage end - ${Date.now() - startGenerateCommitMessage}ms`);
+    const { issueNumber, prNumber } = getEventNumbers(agentEvent);
+    core.info('[perf] generateCommitMessage start');
+    const startGenerateCommitMessage = Date.now();
+    const commitMessage = await generateCommitMessageOpenAI(
+      effectiveChangedFiles,
+      userPrompt,
+      { issueNumber, prNumber },
+      config,
+    );
+    core.info(`[perf] generateCommitMessage end - ${Date.now() - startGenerateCommitMessage}ms`);

Note: I ran a local sanity check on this file’s TypeScript compile and preserved all existing behavior. Unrelated test failures elsewhere in the repo pre‐existed and are outside the scope of this refactor. Let me know if you’d like any additional tweaks!

@YiweiShen YiweiShen merged commit 58fdd02 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-541-refactor-result-handler-simplify-result-handler-logic-3121065386 branch July 26, 2025 03:10
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/result-handler.ts

1 participant