Repository navigation
docs: add a recipe for PR comments on pull requests from forks #520
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
Open
shenxianpeng
wants to merge
4
commits into
main
Choose a base branch
from
claude/project-thread-4hzd70
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
07703db
docs: add a recipe for PR comments on pull requests from forks
shenxianpeng d723a18
docs: say that reviews also need a write token on fork pull requests
shenxianpeng 2f5d117
docs: note that pull_request_target is blocked by default from Decemb…
shenxianpeng 13eefc2
docs: fork PR comments are skipped, not failed; harden the recipe
shenxianpeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| name: cpp-linter # (1)! | ||
| on: | ||
| pull_request: | ||
| branches: [main, master, develop] | ||
| paths: ['**.c', '**.cpp', '**.h', '**.hpp', '**.cxx', '**.hxx', '**.cc', '**.hh', '**CMakeLists.txt', 'meson.build', '**.cmake'] | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| cpp-linter: | ||
| runs-on: ubuntu-latest | ||
| permissions: # (2)! | ||
| contents: read | ||
| pull-requests: read | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| # ... optionally setup build env to create a compilation database | ||
|
|
||
| - uses: cpp-linter/cpp-linter-action@v2 | ||
| id: linter | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| with: | ||
| style: 'file' # Use .clang-format config file. (3) | ||
| tidy-checks: '' # Use .clang-tidy config file. (4) | ||
| # No thread-comments here: this job's token is read-only. (5) | ||
| summary-output-file: ${{ runner.temp }}/cpp-linter-comment/comment.md | ||
|
|
||
| - name: Save the pull request number | ||
| env: | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| run: | | ||
| mkdir -p "$RUNNER_TEMP/cpp-linter-comment" | ||
| echo "$PR_NUMBER" > "$RUNNER_TEMP/cpp-linter-comment/pr-number.txt" | ||
|
|
||
| - uses: actions/upload-artifact@v7 | ||
| with: | ||
| name: cpp-linter-comment | ||
| path: ${{ runner.temp }}/cpp-linter-comment/ | ||
| retention-days: 7 | ||
|
|
||
| - name: Fail fast?! | ||
| if: steps.linter.outputs.checks-failed > 0 | ||
| run: exit 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| name: cpp-linter comment | ||
| on: | ||
| workflow_run: | ||
| workflows: [cpp-linter] # (1)! | ||
| types: [completed] | ||
|
|
||
| permissions: {} | ||
|
|
||
| # Post one run at a time per branch, so two runs cannot both add a new comment. | ||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| comment: | ||
| if: github.event.workflow_run.event == 'pull_request' | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| actions: read # (2)! | ||
| pull-requests: write | ||
| steps: | ||
| # No checkout: this job never runs code from the pull request. (3) | ||
| - uses: actions/download-artifact@v8 | ||
| id: download | ||
| continue-on-error: true # The lint run may have stopped before uploading. | ||
| with: | ||
| name: cpp-linter-comment | ||
| path: ${{ runner.temp }}/cpp-linter-comment | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ github.token }} | ||
|
|
||
| - name: Post or update the comment | ||
| if: steps.download.outcome == 'success' | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| GH_REPO: ${{ github.repository }} | ||
| HEAD_SHA: ${{ github.event.workflow_run.head_sha }} | ||
| HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} | ||
| RUN_URL: ${{ github.event.workflow_run.html_url }} | ||
| run: | | ||
| dir="$RUNNER_TEMP/cpp-linter-comment" | ||
| # The artifact was made while linting the pull request: use it only as data. | ||
| if [ ! -f "$dir/pr-number.txt" ] || [ ! -f "$dir/comment.md" ]; then | ||
| echo "::warning::The artifact has no pull request number or no comment" | ||
| exit 0 | ||
| fi | ||
| pr="$(tr -cd '0-9' < "$dir/pr-number.txt" | head -c 10)" | ||
| if [ -z "$pr" ]; then | ||
| echo "::warning::The artifact has no pull request number" | ||
| exit 0 | ||
| fi | ||
| # Only comment on the pull request whose head the lint run checked. (4) | ||
| if [ "$(gh api "repos/$GH_REPO/pulls/$pr" --jq '.head.sha + " " + .head.repo.full_name')" != "$HEAD_SHA $HEAD_REPO" ]; then | ||
| echo "::notice::Pull request #$pr has new commits since this run; not commenting" | ||
| exit 0 | ||
| fi | ||
| marker='<!-- cpp-linter-action workflow_run comment -->' | ||
| body="$RUNNER_TEMP/body.md" | ||
| if [ "$(wc -c < "$dir/comment.md")" -gt 60000 ]; then | ||
| # A comment holds at most 65536 characters. | ||
| printf '%s\nToo many findings to show here. See the [workflow run](%s).\n' "$marker" "$RUN_URL" > "$body" | ||
| else | ||
| { echo "$marker"; cat "$dir/comment.md"; } > "$body" | ||
| fi | ||
| # Update the comment from an earlier run instead of adding another one. | ||
| id="$(gh api --paginate "repos/$GH_REPO/issues/$pr/comments" \ | ||
| --jq ".[] | select(.user.login == \"github-actions[bot]\" and (.body | startswith(\"$marker\"))) | .id" \ | ||
| | head -n 1)" | ||
| if [ -n "$id" ]; then | ||
| gh api -X PATCH "repos/$GH_REPO/issues/comments/$id" -F "body=@$body" > /dev/null | ||
| else | ||
| gh api "repos/$GH_REPO/issues/$pr/comments" -F "body=@$body" > /dev/null | ||
| fi | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.