Skip to content

feat: output pr_comments to github action #132

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

Closed
wants to merge 10 commits into from

Conversation

shenxianpeng
Copy link
Contributor

@shenxianpeng shenxianpeng commented Aug 19, 2025

Summary by CodeRabbit

  • New Features

    • CI now exposes formatted PR comment text as a top-level workflow/action output and saves it for downstream steps.
    • Workflow automatically posts PR comments containing commit-check results.
  • Chores

    • CI step IDs and outputs added so other steps can reference PR comment text.
    • VCS ignores Python bytecode artifacts.
  • Behavior

    • PR comment output is always produced in the workflow outputs (even when no comment change).

@shenxianpeng shenxianpeng requested a review from a team as a code owner August 19, 2025 20:31
@shenxianpeng shenxianpeng added the enhancement New feature or request label Aug 19, 2025
Copy link

coderabbitai bot commented Aug 19, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds a new exported function to always write formatted PR comment text to GITHUB_OUTPUT, expands PR-commenting logic to fetch/update/delete comments via the GitHub API, and exposes the comment text as an action output and workflow artifact. Also adjusts workflow triggers and ignores Python bytecode.

Changes

Cohort / File(s) Summary
PR comment logic & output
main.py
Added set_pr_comments_output() -> None that reads results and appends pr_comments={pr_comments} to the GITHUB_OUTPUT file. Expanded add_pr_comments() to use PyGithub: derive PR number from GITHUB_REF, fetch repo/pr, prepare SUCCESS/FAILURE titles, find/update/delete existing matching comments or create a new one, return 0 on success/empty result and 1 on failure/error, and handle exceptions robustly. main() now always calls set_pr_comments_output() so output is always produced.
Action interface outputs
action.yml
Added id: commit-check to the run step and declared a new top-level output pr_comments exposing ${{ steps.commit-check.outputs.pr_comments }} so the action exposes the aggregated PR comment body.
Workflow adjustments
.github/workflows/commit-check.yml
Changed on.pull_request.branches to an array, set the self-test step id: commit-check, added steps to save ${{ steps.commit-check.outputs.pr_comments }} to pr-comments.txt and post it as a comment via actions/github-script@v6.
VCS ignores
.gitignore
Added Python bytecode ignores: __pycache__/, *.pyc, *.pyo (keeps existing venv ignores).

Sequence Diagram(s)

sequenceDiagram
    participant Runner as GHA Runner
    participant Action as commit-check Action (main.py)
    participant GitHub as GitHub API
    participant GHAOUT as GITHUB_OUTPUT
    participant Workflow as Workflow Steps

    Runner->>Action: start action
    Action->>GitHub: init client (GITHUB_TOKEN), resolve repo & PR from GITHUB_REF
    alt PR present
        Action->>GitHub: list PR comments
        GitHub-->>Action: comments
        alt Matching comment(s) found
            Action->>GitHub: update latest matching comment (delete older duplicates)
            GitHub-->>Action: update confirmation
        else No matching comment
            Action->>GitHub: create new PR comment
            GitHub-->>Action: create confirmation
        end
    else No PR (or error)
        Action-->>Action: skip API calls / handle error
    end
    rect rgba(230,246,255,0.6)
    note right of Action: New — always export aggregated text
    Action->>GHAOUT: append `pr_comments={pr_comments}`
    end
    Action->>Workflow: set step output `pr_comments`
    Workflow->>Workflow: write `pr-comments.txt` and post/upload as needed
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

I nibble through the code at night,
I fetch, I write, I make things right.
Comments tidy, outputs bright,
A tiny hop, a glowing light —
CI hums softly, checks take flight 🥕

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bugfix/fork-repo-add-comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

Commit-Check ✔️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
main.py (2)

154-157: Output not set when the comment is already up-to-date (early return skips it)

If the last comment matches, you return before exporting pr_comments. If downstream steps rely on this output, they’ll miss it. Move the output block to immediately after computing pr_comments so it runs regardless of whether a comment is created/updated.

Place this block right after building pr_comments (after Line 137) and remove the later occurrence:

# Immediately after pr_comments is computed
gh_output = os.getenv("GITHUB_OUTPUT")
if gh_output:
    with open(gh_output, "a") as gh_out:
        delimiter = "PR_COMMENTS_EOF"
        gh_out.write(f"pr_comments<<{delimiter}\n{pr_comments}\n{delimiter}\n")
else:
    print(f"pr_comments:\n{pr_comments}")

Would you like me to update the PR to reposition the output and avoid the early-return gap?


111-115: Consider exporting pr_comments even when PR_COMMENTS is disabled

Currently, add_pr_comments returns immediately when PR_COMMENTS == "false", so pr_comments is never exported in that configuration. If the goal is to always surface the computed text for later steps (even when you choose not to post to GitHub), factor the output setting to occur irrespective of PR_COMMENTS.

If you intend the output to be available regardless of comment-posting, I can refactor add_pr_comments to:

  • always compute result_text/pr_comments and export them, and
  • only gate the GitHub API interaction on PR_COMMENTS.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1a0695b and 459a669.

📒 Files selected for processing (1)
  • main.py (1 hunks)

main.py Outdated
Comment on lines 171 to 173
# output pr_comments to GitHub Actions
print(f"::set-output name=pr_comments::{pr_comments}")

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Replace deprecated ::set-output with GITHUB_OUTPUT and handle multiline safely

GitHub deprecated ::set-output. Use the GITHUB_OUTPUT environment file to set step outputs. This also handles multiline values (your pr_comments includes code fences and newlines) without brittle escaping.

Apply this diff:

-        # output pr_comments to GitHub Actions
-        print(f"::set-output name=pr_comments::{pr_comments}")
+        # Output pr_comments to GitHub Actions using the env file (set-output is deprecated)
+        gh_output = os.getenv("GITHUB_OUTPUT")
+        if gh_output:
+            with open(gh_output, "a") as gh_out:
+                delimiter = "PR_COMMENTS_EOF"
+                gh_out.write(f"pr_comments<<{delimiter}\n{pr_comments}\n{delimiter}\n")
+        else:
+            # Fallback for local runs where GITHUB_OUTPUT is unset
+            print(f"pr_comments:\n{pr_comments}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# output pr_comments to GitHub Actions
print(f"::set-output name=pr_comments::{pr_comments}")
# Output pr_comments to GitHub Actions using the env file (set-output is deprecated)
gh_output = os.getenv("GITHUB_OUTPUT")
if gh_output:
with open(gh_output, "a") as gh_out:
delimiter = "PR_COMMENTS_EOF"
gh_out.write(f"pr_comments<<{delimiter}\n{pr_comments}\n{delimiter}\n")
else:
# Fallback for local runs where GITHUB_OUTPUT is unset
print(f"pr_comments:\n{pr_comments}")
🤖 Prompt for AI Agents
In main.py around lines 171 to 173, replace the deprecated GitHub Actions
"::set-output" print with writing to the GITHUB_OUTPUT environment file to
safely handle multiline pr_comments: import os if not already, get the output
path via os.environ["GITHUB_OUTPUT"], and append a heredoc-style entry for
pr_comments (for example: write 'pr_comments<<EOF\n' + pr_comments + '\nEOF\n')
to that file so multiline content and code fences are preserved; ensure the
write uses text mode and handle KeyError with a clear fallback or raise if
GITHUB_OUTPUT is missing.

@shenxianpeng shenxianpeng requested a review from Copilot August 19, 2025 21:19
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
action.yml (1)

89-92: Top-level output wiring looks correct; ensure multiline-safe export in main.py

The output mapping to steps.run.outputs.pr_comments is set up properly. Please verify that main.py writes multiline output to $GITHUB_OUTPUT using a unique heredoc delimiter to avoid truncation or delimiter collisions.

Example (outside this file) for Python:

# in main.py
import os, secrets, string
body = pr_comments  # the markdown to export
delim = "EOF_" + "".join(secrets.choice(string.ascii_letters) for _ in range(12))
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
    f.write(f"pr_comments<<{delim}\n")
    f.write(body)
    if not body.endswith("\n"):
        f.write("\n")
    f.write(f"{delim}\n")

Optional: even when no update occurs, consider exporting an empty value for consistency (consumers then don’t need to special-case unset outputs).

.github/workflows/commit-check.yml (2)

34-36: Preserve multiline formatting when saving comment body; guard against empty output

Using echo may mishandle very large/multiline content and special characters. Prefer a heredoc and skip the step when no output is produced.

Apply:

-      - name: Save PR comments to pr-comments.txt
-        run: echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt
+      - name: Save PR comments to pr-comments.txt
+        if: ${{ steps.commit-check.outputs.pr_comments != '' }}
+        run: |
+          # Preserve exact multiline content without further shell interpolation
+          cat > pr-comments.txt <<'EOF'
+          ${{ steps.commit-check.outputs.pr_comments }}
+          EOF

37-41: Upload artifact only when content exists

Add the same guard so we don’t upload an empty file. Optionally set retention-days.

-      - name: Upload pr-comments.txt
-        uses: actions/upload-artifact@v4
-        with:
-          name: commit-check-pr-comments
-          path: pr-comments.txt
+      - name: Upload pr-comments.txt
+        if: ${{ steps.commit-check.outputs.pr_comments != '' }}
+        uses: actions/upload-artifact@v4
+        with:
+          name: commit-check-pr-comments
+          path: pr-comments.txt
+          # retention-days: 7
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between c388ea5 and 1c123d7.

📒 Files selected for processing (3)
  • .github/workflows/commit-check.yml (3 hunks)
  • action.yml (2 hunks)
  • main.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • main.py
🔇 Additional comments (3)
action.yml (1)

51-52: Good call: assign a stable step id to surface outputs

Adding id: run is necessary to map composite outputs to steps.run.outputs.*. Looks correct.

.github/workflows/commit-check.yml (2)

5-5: No-op normalization of branches config

Switching to an array form is fine and conventional.


20-20: Correctly assigned step id to consume action outputs

id: commit-check enables referencing ${{ steps.commit-check.outputs.pr_comments }} in later steps. Looks good.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds output functionality to expose PR comment text from the commit-check action as both a workflow output and a build artifact. This enables downstream workflow steps to access the generated PR comments.

  • Adds PR comment output to GitHub Actions workflow output via $GITHUB_OUTPUT
  • Configures action.yml to expose pr_comments as an output parameter
  • Updates the workflow to save PR comments as a build artifact

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
main.py Writes PR comments to $GITHUB_OUTPUT for workflow consumption
action.yml Defines pr_comments output and adds step ID for referencing
.github/workflows/commit-check.yml Adds artifact upload for PR comments and formatting fix

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

main.py Outdated
# output pr_comments to $GITHUB_OUTPUT
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file:
output_file.write(f"pr_comments={pr_comments}\n")

Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes GITHUB_OUTPUT environment variable exists without checking. This could raise a KeyError if the environment variable is not set. Consider using os.environ.get("GITHUB_OUTPUT") with appropriate error handling.

Suggested change
github_output = os.environ.get("GITHUB_OUTPUT")
if github_output is None:
raise EnvironmentError("GITHUB_OUTPUT environment variable is not set")
with open(github_output, "a", encoding="utf-8") as output_file:
output_file.write(f"pr_comments={pr_comments}\n")

Copilot uses AI. Check for mistakes.

main.py Outdated
@@ -168,6 +168,10 @@ def add_pr_comments() -> int:
print(f"Creating a new comment on PR #{pr_number}.")
pull_request.create_comment(body=pr_comments)

# output pr_comments to $GITHUB_OUTPUT
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file:
output_file.write(f"pr_comments={pr_comments}\n")
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pr_comments variable could contain newlines or special characters that might break the GitHub Actions output format. Consider escaping or encoding the content to ensure proper parsing.

Suggested change
output_file.write(f"pr_comments={pr_comments}\n")
def escape_github_output(value: str) -> str:
"""Escape value for GitHub Actions output."""
return value.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A")
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file:
output_file.write(f"pr_comments={escape_github_output(pr_comments)}\n")

Copilot uses AI. Check for mistakes.

@shenxianpeng
Copy link
Contributor Author

@copilot echo "" > pr-comments.txt why? steps.commit-check.outputs.pr_comments is empty. Help me fix it!

Copilot AI and others added 2 commits August 20, 2025 00:30
…tting

* Initial plan

* Fix pr_comments output always being set regardless of PR_COMMENTS setting

Co-authored-by: shenxianpeng <[email protected]>

* Update .gitignore to exclude Python cache files

Co-authored-by: shenxianpeng <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: shenxianpeng <[email protected]>
Copy link
Contributor

Commit-Check ✔️

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
.github/workflows/commit-check.yml (1)

37-49: Upgrade to actions/github-script@v7 and skip when comment is empty

Static analysis is correct: v6 is deprecated on current runners. Also, guard against posting empty comments if output parsing fails.

Apply this diff:

-      - name: Post PR comments
-        uses: actions/github-script@v6
+      - name: Post PR comments
+        if: ${{ steps.commit-check.outputs.pr_comments != '' }}
+        uses: actions/github-script@v7
         with:
           script: |
             const fs = require('fs');
             const content = fs.readFileSync('pr-comments.txt', 'utf8');
             github.rest.issues.createComment({
               issue_number: context.issue.number,
               owner: context.repo.owner,
               repo: context.repo.repo,
               body: content
             });
♻️ Duplicate comments (1)
main.py (1)

111-124: Fix GITHUB_OUTPUT writing: current format drops multiline outputs (causing empty pr_comments)

You’re writing name=value without escaping or the multiline heredoc format. Since pr_comments contains newlines and code fences, GitHub fails to parse it and the step output becomes empty. Also guard for missing GITHUB_OUTPUT in local runs.

Apply this diff:

 def set_pr_comments_output() -> None:
     """Sets the pr_comments output regardless of PR_COMMENTS setting."""
     result_text = read_result_file()
     pr_comments = (
         SUCCESS_TITLE
         if result_text is None
         else f"{FAILURE_TITLE}\n```\n{result_text}\n```"
     )

-    # output pr_comments to $GITHUB_OUTPUT
-    with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output_file:
-        output_file.write(f"pr_comments={pr_comments}\n")
+    # Output pr_comments to $GITHUB_OUTPUT (handle multiline safely)
+    gh_output = os.getenv("GITHUB_OUTPUT")
+    if gh_output:
+        with open(gh_output, "a", encoding="utf-8") as output_file:
+            delimiter = "PR_COMMENTS_EOF"
+            output_file.write(f"pr_comments<<{delimiter}\n{pr_comments}\n{delimiter}\n")
+    else:
+        # Fallback for local runs where GITHUB_OUTPUT is unset
+        print(f"pr_comments:\n{pr_comments}")
🧹 Nitpick comments (2)
.github/workflows/commit-check.yml (2)

34-36: Write multiline output to file safely (avoid echo pitfalls)

echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt can mangle content in edge cases and may not preserve trailing newlines. Prefer a heredoc to faithfully persist multiline content.

Apply this diff:

-      - name: Save PR comments to pr-comments.txt
-        run: echo "${{ steps.commit-check.outputs.pr_comments }}" > pr-comments.txt
+      - name: Save PR comments to pr-comments.txt
+        run: |
+          # Preserve multiline content exactly
+          cat > pr-comments.txt <<'PR_COMMENTS_EOF'
+          ${{ steps.commit-check.outputs.pr_comments }}
+          PR_COMMENTS_EOF

32-33: Avoid double-posting PR comments (choose one mechanism)

You currently:

  • post via Python (add_pr_comments when pr-comments is true), and
  • post again via github-script using the output.

This will create duplicates. Consider disabling one path. If you want to standardize on the workflow path, set the action input to false:

-          pr-comments: ${{ github.event_name == 'pull_request' }}
+          pr-comments: false

Alternatively, remove the github-script step and rely on the Python poster.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 1c123d7 and e263dec.

📒 Files selected for processing (4)
  • .github/workflows/commit-check.yml (3 hunks)
  • .gitignore (1 hunks)
  • action.yml (1 hunks)
  • main.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .gitignore
🚧 Files skipped from review as they are similar to previous changes (1)
  • action.yml
🧰 Additional context used
🪛 actionlint (1.7.7)
.github/workflows/commit-check.yml

38-38: the runner of "actions/github-script@v6" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)

🔇 Additional comments (3)
main.py (1)

217-219: LGTM: Good call to set pr_comments output unconditionally

This ensures the output is always available to downstream workflow steps, independent of whether comments are posted.

.github/workflows/commit-check.yml (2)

5-5: LGTM

Converting to array form for branches is fine and future-proof.


20-20: LGTM

Adding id: commit-check enables referencing the action’s outputs. Correct.

@shenxianpeng shenxianpeng deleted the bugfix/fork-repo-add-comment branch August 19, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants