rmuir commented on code in PR #14927:
URL: https://github.com/apache/lucene/pull/14927#discussion_r2194852380


##########
.github/workflows/auto-format.yml:
##########
@@ -0,0 +1,359 @@
+name: Lucene Auto Format Bot
+
+on:
+  issue_comment:
+    types: [created]
+
+env:
+  DEVELOCITY_ACCESS_KEY: ${{ secrets.DEVELOCITY_ACCESS_KEY }}
+
+jobs:
+  format-check:
+    # Only run on pull requests when the bot is mentioned
+    if: |
+      github.event.issue.pull_request &&
+      contains(github.event.comment.body, '/format-fix apply')
+    runs-on: ubuntu-latest
+    timeout-minutes: 30
+
+    permissions:
+      contents: write
+      pull-requests: write
+      issues: write
+
+    steps:
+      - name: Check permissions
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const actorPermission = await 
github.rest.repos.getCollaboratorPermissionLevel({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              username: context.actor
+            });
+
+            const permission = actorPermission.data.permission;
+            const hasPermission = ['admin', 'write'].includes(permission);
+
+            if (!hasPermission) {
+              core.setFailed(`User @${context.actor} does not have permission 
to run the format bot. Required: admin or write access.`);
+              return;
+            }
+
+      - name: Add workflow started reaction
+        uses: actions/github-script@v7
+        with:
+          script: |
+            await github.rest.reactions.createForIssueComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              comment_id: context.payload.comment.id,
+              content: 'eyes'
+            });
+
+      - name: Get PR details
+        id: pr_details
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const prNumber = context.issue.number;
+            const { data: pr } = await github.rest.pulls.get({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              pull_number: prNumber
+            });
+
+            return {
+              head_ref: pr.head.ref,
+              head_sha: pr.head.sha,
+              base_ref: pr.base.ref,
+              base_sha: pr.base.sha,
+              clone_url: pr.head.repo.clone_url,
+              ssh_url: pr.head.repo.ssh_url,
+              repo_full_name: pr.head.repo.full_name
+            };
+
+      - name: Checkout PR code
+        uses: actions/checkout@v4
+        with:
+          repository: ${{ 
fromJson(steps.pr_details.outputs.result).repo_full_name }}
+          ref: ${{ fromJson(steps.pr_details.outputs.result).head_ref }}
+          token: ${{ secrets.GITHUB_TOKEN }}
+          fetch-depth: 0
+
+      - name: Set up JDK 21
+        uses: actions/setup-java@v4
+        with:
+          java-version: '21'
+          distribution: 'temurin'
+
+      - uses: ./.github/actions/prepare-for-build
+
+      - name: Get changed files
+        id: changed-files
+        uses: tj-actions/changed-files@v46
+        with:
+          files: |
+            **/*.java
+            **/*.gradle
+            **/*.groovy
+            **/*.md
+            **/*.properties
+            **/*.xml
+            **/*.py
+            **/*.sh
+            **/*.bat
+            **/*.cmd
+
+      - name: Initial validation
+        id: initial-validation
+        continue-on-error: true
+        run: |
+          echo "Running initial validation..."
+          ./gradlew check -x test
+
+      - name: Fix formatting issues
+        if: steps.initial-validation.outcome == 'failure'
+        run: |
+          echo "Fixing formatting issues..."
+          ./gradlew tidy
+
+      - name: Check if formatting fixes were made
+        if: steps.initial-validation.outcome == 'failure'
+        id: check_changes
+        run: |
+          git add .
+          if git diff --staged --quiet; then
+            echo "No formatting changes were made"
+            echo "changes_made=false" >> $GITHUB_OUTPUT
+          else
+            echo "Formatting changes were made"
+            echo "changes_made=true" >> $GITHUB_OUTPUT
+            echo "Changed files:"
+            git diff --staged --name-only
+          fi
+
+      - name: Create fix branch and commit
+        if: steps.initial-validation.outcome == 'failure' && 
steps.check_changes.outputs.changes_made == 'true'
+        id: create_fix_branch
+        run: |
+          # Create a new branch for the fixes
+          fix_branch="format-fixes-${{ 
fromJson(steps.pr_details.outputs.result).head_ref }}-$(date +%s)"
+          echo "fix_branch=$fix_branch" >> $GITHUB_OUTPUT
+
+          git config --local user.email "act...@github.com"
+          git config --local user.name "Lucene Format Bot"
+
+          git checkout -b "$fix_branch"
+          git commit -m "Apply automatic formatting fixes
+
+          Fixes applied by @lucene-format-bot in response to:
+          ${{ github.event.comment.html_url }}
+
+          Original PR: #${{ github.event.issue.number }}
+
+          Changes applied:
+          $(git diff --name-only HEAD~1)"
+
+          git push origin "$fix_branch"
+
+      - name: Final validation
+        run: |
+          echo "Running final validation..."
+          ./gradlew check -x test
+
+      - name: Create PR for fixes
+        if: steps.initial-validation.outcome == 'failure' && 
steps.check_changes.outputs.changes_made == 'true'
+        id: create_pr
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const { data: fixPr } = await github.rest.pulls.create({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              title: `Apply formatting fixes to #${{ github.event.issue.number 
}}`,
+              head: '${{ steps.create_fix_branch.outputs.fix_branch }}',
+              base: '${{ fromJson(steps.pr_details.outputs.result).head_ref 
}}',
+              body: `## Automatic Formatting Fixes
+
+            This PR applies automatic formatting fixes to address validation 
failures in #${{ github.event.issue.number }}.
+
+            ### 📝 Details
+            - **Triggered by**: ${{ github.event.comment.html_url }}
+            - **Original PR**: #${{ github.event.issue.number }}
+            - **Fix branch**: \`${{ steps.create_fix_branch.outputs.fix_branch 
}}\`
+            - **Files checked**: ${{ 
steps.changed-files.outputs.all_changed_files_count || '0' }} files
+
+            ### 🚀 Next Steps
+            Review and merge this PR to apply the formatting fixes to the 
original branch.
+
+            ---
+            *This PR was created automatically by the Auto Format Bot*`
+            });
+
+            return {
+              pr_number: fixPr.number,
+              pr_url: fixPr.html_url
+            };
+
+      - name: Remove workflow started reaction and add success reaction
+        if: success()
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const reactions = await github.rest.reactions.listForIssueComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              comment_id: context.payload.comment.id
+            });
+
+            const eyesReaction = reactions.data.find(r => r.content === 'eyes' 
&& r.user.type === 'Bot');
+            if (eyesReaction) {
+              await github.rest.reactions.deleteForIssueComment({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                comment_id: context.payload.comment.id,
+                reaction_id: eyesReaction.id
+              });
+            }
+
+            await github.rest.reactions.createForIssueComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              comment_id: context.payload.comment.id,
+              content: '+1'
+            });
+
+      - name: Comment on PR with success (fixes applied)
+        if: success() && steps.initial-validation.outcome == 'failure' && 
steps.check_changes.outputs.changes_made == 'true'
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const prNumber = '${{ 
fromJson(steps.create_pr.outputs.result).pr_number }}';
+            const comment = `## Formatting Fixes Applied
+
+            The formatting bot has successfully created a PR with formatting 
fixes!
+
+            ### 📝 Details
+            - **Triggered by**: ${{ github.event.comment.html_url }}
+            - **Files checked**: ${{ 
steps.changed-files.outputs.all_changed_files_count || '0' }} files
+            - **Fix PR**: #${prNumber}
+
+            ### 🚀 Next Steps
+            Review and merge PR #${prNumber} to apply the formatting fixes to 
this branch.
+
+            ---
+            *This was performed automatically by the Auto Format Bot*`;
+
+            await github.rest.issues.createComment({
+              issue_number: context.issue.number,
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              body: comment
+            });
+
+      - name: Comment on PR with success (no fixes needed)
+        if: success() && steps.initial-validation.outcome == 'success'
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const comment = `## No Formatting Issues Found
+
+            The formatting bot has validated this PR and found no issues!
+
+            ### 📝 Details
+            - **Triggered by**: ${{ github.event.comment.html_url }}
+            - **Files checked**: ${{ 
steps.changed-files.outputs.all_changed_files_count || '0' }} files
+
+            The PR is ready for review!
+
+            ---
+            *This was performed automatically by the Auto Format Bot*`;
+
+            await github.rest.issues.createComment({
+              issue_number: context.issue.number,
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              body: comment
+            });
+
+      - name: Comment on PR with success (fixes made but no changes)
+        if: success() && steps.initial-validation.outcome == 'failure' && 
steps.check_changes.outputs.changes_made == 'false'
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const comment = `## Formatting Issues Detected (No Auto-fixes 
Available)
+
+            The formatting bot found issues but was unable to automatically 
fix them.
+
+            ### 📝 Details
+            - **Triggered by**: ${{ github.event.comment.html_url }}
+            - **Files checked**: ${{ 
steps.changed-files.outputs.all_changed_files_count || '0' }} files
+
+            **Recommendation**: Review the validation failures manually and 
apply fixes as needed.
+
+            ---
+            *This was performed automatically by the Auto Format Bot*`;
+
+            await github.rest.issues.createComment({
+              issue_number: context.issue.number,
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              body: comment
+            });
+
+      - name: Remove workflow started reaction and add failure reaction
+        if: failure()
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const reactions = await github.rest.reactions.listForIssueComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              comment_id: context.payload.comment.id
+            });
+
+            const eyesReaction = reactions.data.find(r => r.content === 'eyes' 
&& r.user.type === 'Bot');
+            if (eyesReaction) {
+              await github.rest.reactions.deleteForIssueComment({
+                owner: context.repo.owner,
+                repo: context.repo.repo,
+                comment_id: context.payload.comment.id,
+                reaction_id: eyesReaction.id
+              });
+            }
+
+            await github.rest.reactions.createForIssueComment({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              comment_id: context.payload.comment.id,
+              content: '-1'
+            });
+
+      - name: Comment on PR with failure
+        if: failure()
+        uses: actions/github-script@v7
+        with:
+          script: |
+            const comment = `## Formatting Failed
+
+            The formatting bot encountered issues while processing this PR.
+
+            ### Next Steps:
+            1. Check the workflow logs for specific error details
+            2. Fix any issues manually if needed
+            3. Re-trigger the bot with \`/format-fix apply\`
+
+            ### 📝 Details
+            - **Triggered by**: ${{ github.event.comment.html_url }}
+            - **Files processed**: ${{ 
steps.changed-files.outputs.all_changed_files || 'None detected' }}
+
+            ---
+            *This was performed automatically by the Auto Format Bot*`;
+
+            await github.rest.issues.createComment({
+              issue_number: context.issue.number,
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              body: comment
+            });

Review Comment:
   for example, for this entire set of javascript, i'd just use `gh pr comment`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to