-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Post code suggestions on PRs with format issues #15477
Open
timtebeek
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
timtebeek:pr-format-code-suggestion-comments
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
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 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,55 @@ | ||
# Description: This workflow applies the formatter against the opened pull request and upload the patch. | ||
# Since this pull request receives untrusted code, we should **NOT** have any secrets in the environment. | ||
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
--- | ||
name: pr-format-workflow | ||
|
||
on: | ||
pull_request: | ||
types: [opened, synchronize] | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: '${{ github.workflow }} @ ${{ github.ref }}' | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
upload-patch: | ||
runs-on: ubuntu-latest | ||
if: ${{ github.repository == 'spring-projects/spring-security' }} | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{github.event.pull_request.head.ref}} | ||
repository: ${{github.event.pull_request.head.repo.full_name}} | ||
- name: Set up gradle | ||
uses: spring-io/spring-gradle-build-action@v2 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Capture the PR number | ||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow | ||
- name: Create pr_number.txt | ||
run: echo "${{ github.event.number }}" > pr_number.txt | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: pr_number | ||
path: pr_number.txt | ||
- name: Remove pr_number.txt | ||
run: rm -f pr_number.txt | ||
|
||
# Format code | ||
- name: Format with Gradle | ||
run: ./gradlew format | ||
|
||
# Capture the diff | ||
- name: Create patch | ||
run: | | ||
git diff | tee git-diff.patch | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: patch | ||
path: git-diff.patch |
This file contains 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,59 @@ | ||
# Description: This workflow is triggered when the `pr-format-workflow` completes to post suggestions on the PR. | ||
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code. | ||
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/ | ||
--- | ||
name: pr-suggestions-workflow | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["pr-format-workflow"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
post-suggestions: | ||
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-a-workflow-based-on-the-conclusion-of-another-workflow | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
runs-on: ubuntu-latest | ||
permissions: | ||
actions: read | ||
pull-requests: write | ||
env: | ||
# https://docs.github.com/en/actions/reference/authentication-in-a-workflow#permissions-for-the-github_token | ||
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
timeout-minutes: 10 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{github.event.workflow_run.head_branch}} | ||
repository: ${{github.event.workflow_run.head_repository.full_name}} | ||
|
||
# Download the patch | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: patch | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
run-id: ${{ github.event.workflow_run.id }} | ||
- name: Apply patch | ||
run: | | ||
git apply git-diff.patch --allow-empty | ||
rm git-diff.patch | ||
|
||
# Download the PR number | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: pr_number | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
run-id: ${{ github.event.workflow_run.id }} | ||
- name: Read pr_number.txt | ||
run: | | ||
PR_NUMBER=$(cat pr_number.txt) | ||
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV | ||
rm pr_number.txt | ||
|
||
# Post suggestions as a comment on the PR | ||
- uses: googleapis/code-suggester@v4 | ||
with: | ||
command: review | ||
pull_number: ${{ env.PR_NUMBER }} | ||
git_dir: '.' |
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.
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.
Is there a way to include a comment in the patch suggestion? If so, it would be nice to inform the OP of
./gradlew format
. I realize they can just accept the PR suggestion instead, I just see it as informative for the future so they remove one step from their submissions.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.
We had wondered the same internall, but unfortunately it does not appear to be an option exposed when using the code-suggester review command: https://github.com/googleapis/code-suggester?tab=readme-ov-file#review-a-pull-request
There is an alternative in the form of reviewdog that's more malleable, but also adds branding and token requirements that I'm not too pleased with, which is why I stuck with the minimal API offered by code-suggester.