-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
Note Other AI code review bot(s) detectedCodeRabbit 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. WalkthroughAdds 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
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
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
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 unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
Commit-Check ✔️ |
There was a problem hiding this 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 disabledCurrently, 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.
📒 Files selected for processing (1)
main.py
(1 hunks)
main.py
Outdated
# output pr_comments to GitHub Actions | ||
print(f"::set-output name=pr_comments::{pr_comments}") | ||
|
There was a problem hiding this comment.
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.
# 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.
There was a problem hiding this 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.pyThe output mapping to
steps.run.outputs.pr_comments
is set up properly. Please verify thatmain.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 outputUsing
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 existsAdd 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.
📒 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 outputsAdding
id: run
is necessary to map composite outputs tosteps.run.outputs.*
. Looks correct..github/workflows/commit-check.yml (2)
5-5
: No-op normalization of branches configSwitching 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.
There was a problem hiding this 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") | ||
|
There was a problem hiding this comment.
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.
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") |
There was a problem hiding this comment.
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.
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.
@copilot |
…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]>
Commit-Check ✔️ |
There was a problem hiding this 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 emptyStatic 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. Sincepr_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: falseAlternatively, 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.
📒 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 unconditionallyThis ensures the output is always available to downstream workflow steps, independent of whether comments are posted.
.github/workflows/commit-check.yml (2)
5-5
: LGTMConverting to array form for branches is fine and future-proof.
20-20
: LGTMAdding
id: commit-check
enables referencing the action’s outputs. Correct.
Summary by CodeRabbit
New Features
Chores
Behavior