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