|  | 
|  | 1 | +# Adapted from https://github.com/huggingface/diffusers/blob/main/.github/workflows/pr_style_bot.yml | 
|  | 2 | +name: PR Style Bot | 
|  | 3 | + | 
|  | 4 | +on: | 
|  | 5 | +  issue_comment: | 
|  | 6 | +    types: [created] | 
|  | 7 | + | 
|  | 8 | +permissions: | 
|  | 9 | +  contents: write | 
|  | 10 | +  pull-requests: write | 
|  | 11 | + | 
|  | 12 | +jobs: | 
|  | 13 | +  run-style-bot: | 
|  | 14 | +    if: > | 
|  | 15 | +      contains(github.event.comment.body, '@bot /style') && | 
|  | 16 | +      github.event.issue.pull_request != null | 
|  | 17 | +    runs-on: ubuntu-latest | 
|  | 18 | + | 
|  | 19 | +    steps: | 
|  | 20 | +      - name: Extract PR details | 
|  | 21 | +        id: pr_info | 
|  | 22 | +        uses: actions/github-script@v6 | 
|  | 23 | +        with: | 
|  | 24 | +          script: | | 
|  | 25 | +            const prNumber = context.payload.issue.number; | 
|  | 26 | +            const { data: pr } = await github.rest.pulls.get({ | 
|  | 27 | +              owner: context.repo.owner, | 
|  | 28 | +              repo: context.repo.repo, | 
|  | 29 | +              pull_number: prNumber | 
|  | 30 | +            }); | 
|  | 31 | +
 | 
|  | 32 | +            // We capture both the branch ref and the "full_name" of the head repo | 
|  | 33 | +            // so that we can check out the correct repository & branch (including forks). | 
|  | 34 | +            core.setOutput("prNumber", prNumber); | 
|  | 35 | +            core.setOutput("headRef", pr.head.ref); | 
|  | 36 | +            core.setOutput("headRepoFullName", pr.head.repo.full_name); | 
|  | 37 | +
 | 
|  | 38 | +      - name: Check out PR branch | 
|  | 39 | +        uses: actions/checkout@v4 | 
|  | 40 | +        env: | 
|  | 41 | +          HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} | 
|  | 42 | +          HEADREF: ${{ steps.pr_info.outputs.headRef }} | 
|  | 43 | +        with: | 
|  | 44 | +          persist-credentials: false | 
|  | 45 | +          # Instead of checking out the base repo, use the contributor's repo name | 
|  | 46 | +          repository: ${{ env.HEADREPOFULLNAME }} | 
|  | 47 | +          ref: ${{ env.HEADREF }} | 
|  | 48 | +          # You may need fetch-depth: 0 for being able to push | 
|  | 49 | +          fetch-depth: 0 | 
|  | 50 | +          token: ${{ secrets.GITHUB_TOKEN }} | 
|  | 51 | + | 
|  | 52 | +      - name: Debug | 
|  | 53 | +        env: | 
|  | 54 | +          HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} | 
|  | 55 | +          HEADREF: ${{ steps.pr_info.outputs.headRef }} | 
|  | 56 | +          PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} | 
|  | 57 | +        run: | | 
|  | 58 | +          echo "PR number: $PRNUMBER" | 
|  | 59 | +          echo "Head Ref: $HEADREF" | 
|  | 60 | +          echo "Head Repo Full Name: $HEADREPOFULLNAME" | 
|  | 61 | +
 | 
|  | 62 | +      - name: Set up Python | 
|  | 63 | +        uses: actions/setup-python@v4 | 
|  | 64 | + | 
|  | 65 | +      - name: Get Ruff Version from pre-commit-config.yaml | 
|  | 66 | +        id: get-ruff-version | 
|  | 67 | +        run: | | 
|  | 68 | +          RUFF_VERSION=$(awk '/repo: https:\/\/github.com\/astral-sh\/ruff-pre-commit/{flag=1;next}/rev:/{if(flag){print $2;exit}}' .pre-commit-config.yaml) | 
|  | 69 | +          echo "ruff_version=${RUFF_VERSION}" >> $GITHUB_OUTPUT | 
|  | 70 | +
 | 
|  | 71 | +      - name: Install Ruff | 
|  | 72 | +        env: | 
|  | 73 | +          RUFF_VERSION: ${{ steps.get-ruff-version.outputs.ruff_version }} | 
|  | 74 | +        run: python -m pip install "ruff==${RUFF_VERSION}" | 
|  | 75 | + | 
|  | 76 | +      - name: Ruff check | 
|  | 77 | +        run: ruff check --fix | 
|  | 78 | + | 
|  | 79 | +      - name: Ruff format | 
|  | 80 | +        run: ruff format | 
|  | 81 | + | 
|  | 82 | +      - name: Commit and push changes | 
|  | 83 | +        id: commit_and_push | 
|  | 84 | +        env: | 
|  | 85 | +          HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} | 
|  | 86 | +          HEADREF: ${{ steps.pr_info.outputs.headRef }} | 
|  | 87 | +          PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} | 
|  | 88 | +          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | 
|  | 89 | +        run: | | 
|  | 90 | +          echo "HEADREPOFULLNAME: $HEADREPOFULLNAME, HEADREF: $HEADREF" | 
|  | 91 | +          # Configure git with the Actions bot user | 
|  | 92 | +          git config user.name "github-actions[bot]" | 
|  | 93 | +          git config user.email "github-actions[bot]@users.noreply.github.com" | 
|  | 94 | +
 | 
|  | 95 | +          # Make sure your 'origin' remote is set to the contributor's fork | 
|  | 96 | +          git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/$HEADREPOFULLNAME.git" | 
|  | 97 | +
 | 
|  | 98 | +          # If there are changes after running style/quality, commit them | 
|  | 99 | +          if [ -n "$(git status --porcelain)" ]; then | 
|  | 100 | +            git add . | 
|  | 101 | +            git commit -m "Apply style fixes" | 
|  | 102 | +            # Push to the original contributor's forked branch | 
|  | 103 | +            git push origin HEAD:$HEADREF | 
|  | 104 | +            echo "changes_pushed=true" >> $GITHUB_OUTPUT | 
|  | 105 | +          else | 
|  | 106 | +            echo "No changes to commit." | 
|  | 107 | +            echo "changes_pushed=false" >> $GITHUB_OUTPUT | 
|  | 108 | +          fi | 
|  | 109 | +
 | 
|  | 110 | +      - name: Comment on PR with workflow run link | 
|  | 111 | +        if: steps.commit_and_push.outputs.changes_pushed == 'true' | 
|  | 112 | +        uses: actions/github-script@v6 | 
|  | 113 | +        with: | 
|  | 114 | +          script: | | 
|  | 115 | +            const prNumber = parseInt(process.env.prNumber, 10); | 
|  | 116 | +            const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}` | 
|  | 117 | +
 | 
|  | 118 | +            await github.rest.issues.createComment({ | 
|  | 119 | +              owner: context.repo.owner, | 
|  | 120 | +              repo: context.repo.repo, | 
|  | 121 | +              issue_number: prNumber, | 
|  | 122 | +              body: `Style fixes have been applied. [View the workflow run here](${runUrl}).` | 
|  | 123 | +            }); | 
|  | 124 | +        env: | 
|  | 125 | +          prNumber: ${{ steps.pr_info.outputs.prNumber }} | 
0 commit comments