From bf57be5822cd152b2dd877f472fbc359985dfddb Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Wed, 18 Dec 2024 07:45:44 +0530 Subject: [PATCH 1/6] feat: introduce a style bot. --- .github/workflows/pr_style_bot.yml | 90 ++++++++++++++++++++++++++++++ .github/workflows/pr_tests.yml | 4 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/pr_style_bot.yml diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml new file mode 100644 index 000000000000..a0c48de8df75 --- /dev/null +++ b/.github/workflows/pr_style_bot.yml @@ -0,0 +1,90 @@ + +name: PR Style Bot + +on: + issue_comment: + types: [created] + +permissions: + contents: write + pull-requests: write + +jobs: + run-style-bot: + if: > + contains(github.event.comment.body, '@bot /style') && + github.event.issue.pull_request != null + runs-on: ubuntu-latest + + steps: + - name: Extract PR details + id: pr_info + uses: actions/github-script@v6 + with: + script: | + const prNumber = context.payload.issue.number; + const { data: pr } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber + }); + core.setOutput("prNumber", prNumber); + core.setOutput("headRef", pr.head.ref); + + - name: Check out PR branch + uses: actions/checkout@v3 + with: + repository: ${{ github.repository }} + ref: ${{ steps.pr_info.outputs.headRef }} + + - name: Debug + run: echo "Full results ${{ steps.pr_info.outputs.headRef }}" + + - name: Set up Python + uses: actions/setup-python@v4 + + - name: Install dependencies + run: | + pip install .[quality] + + - name: Run make style and make quality + run: | + make style && make quality + + - name: Commit and push changes + id: commit_and_push + run: | + # Configure git + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + # If there are changes after running style/quality, commit them + if [ -n "$(git status --porcelain)" ]; then + git add . + git commit -m "Apply style fixes" + # Push back to the PR branch + git push origin HEAD:${{ steps.pr_info.outputs.headRef }} + echo "changes_pushed=true" >> $GITHUB_OUTPUT + else + echo "No changes to commit." + echo "changes_pushed=false" >> $GITHUB_OUTPUT + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Comment on PR with workflow run link + if: steps.commit_and_push.outputs.changes_pushed == 'true' + uses: actions/github-script@v6 + with: + script: | + const prNumber = parseInt(process.env.prNumber, 10); + const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}` + + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body: `Style fixes have been applied. [View the workflow run here](${runUrl}).` + }); + env: + prNumber: ${{ steps.pr_info.outputs.prNumber }} diff --git a/.github/workflows/pr_tests.yml b/.github/workflows/pr_tests.yml index 025787606a9c..6dc9ec9ff6e4 100644 --- a/.github/workflows/pr_tests.yml +++ b/.github/workflows/pr_tests.yml @@ -2,8 +2,8 @@ name: Fast tests for PRs on: pull_request: - branches: - - main + branches: [main] + types: [synchronize] paths: - "src/diffusers/**.py" - "benchmarks/**.py" From f7c715cc4d03587865c7e0ccf3369563d108cad4 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 7 Jan 2025 15:59:42 +0530 Subject: [PATCH 2/6] updates --- .github/workflows/pr_style_bot.yml | 54 +++++++++++++++++++----------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml index a0c48de8df75..3e2c6b4561b7 100644 --- a/.github/workflows/pr_style_bot.yml +++ b/.github/workflows/pr_style_bot.yml @@ -1,11 +1,10 @@ - name: PR Style Bot on: issue_comment: types: [created] -permissions: +permissions: contents: write pull-requests: write @@ -21,24 +20,35 @@ jobs: id: pr_info uses: actions/github-script@v6 with: - script: | - const prNumber = context.payload.issue.number; - const { data: pr } = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: prNumber - }); - core.setOutput("prNumber", prNumber); - core.setOutput("headRef", pr.head.ref); + script: | + const prNumber = context.payload.issue.number; + const { data: pr } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber + }); + + // We capture both the branch ref and the "full_name" of the head repo + // so that we can check out the correct repository & branch (including forks). + core.setOutput("prNumber", prNumber); + core.setOutput("headRef", pr.head.ref); + core.setOutput("headRepoFullName", pr.head.repo.full_name); - name: Check out PR branch uses: actions/checkout@v3 with: - repository: ${{ github.repository }} + # Instead of checking out the base repo, use the contributor's repo name + repository: ${{ steps.pr_info.outputs.headRepoFullName }} ref: ${{ steps.pr_info.outputs.headRef }} + # You may need fetch-depth: 0 for being able to push + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} - name: Debug - run: echo "Full results ${{ steps.pr_info.outputs.headRef }}" + run: | + echo "PR Number: ${{ steps.pr_info.outputs.prNumber }}" + echo "Head Ref: ${{ steps.pr_info.outputs.headRef }}" + echo "Head Repo Full Name: ${{ steps.pr_info.outputs.headRepoFullName }}" - name: Set up Python uses: actions/setup-python@v4 @@ -49,20 +59,24 @@ jobs: - name: Run make style and make quality run: | + # Should we be comparing the Makefile to check for any differences with `diffusers` main? make style && make quality - name: Commit and push changes id: commit_and_push run: | - # Configure git + # Configure git with the Actions bot user git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" + # Make sure your 'origin' remote is set to the contributor's fork + git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ steps.pr_info.outputs.headRepoFullName }}.git" + # If there are changes after running style/quality, commit them if [ -n "$(git status --porcelain)" ]; then git add . git commit -m "Apply style fixes" - # Push back to the PR branch + # Push to the original contributor's forked branch git push origin HEAD:${{ steps.pr_info.outputs.headRef }} echo "changes_pushed=true" >> $GITHUB_OUTPUT else @@ -79,12 +93,12 @@ jobs: script: | const prNumber = parseInt(process.env.prNumber, 10); const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}` - + await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: prNumber, - body: `Style fixes have been applied. [View the workflow run here](${runUrl}).` + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + body: `Style fixes have been applied. [View the workflow run here](${runUrl}).` }); env: prNumber: ${{ steps.pr_info.outputs.prNumber }} From 91aa46bd6bc816a67d3d4726eb7cee32dcc18fbc Mon Sep 17 00:00:00 2001 From: Sayak Paul Date: Tue, 14 Jan 2025 10:57:53 +0530 Subject: [PATCH 3/6] Apply suggestions from code review Co-authored-by: Guillaume LEGENDRE --- .github/workflows/pr_style_bot.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml index 3e2c6b4561b7..de9e52c95758 100644 --- a/.github/workflows/pr_style_bot.yml +++ b/.github/workflows/pr_style_bot.yml @@ -36,10 +36,13 @@ jobs: - name: Check out PR branch uses: actions/checkout@v3 + env: + HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} + HEADREF: ${{ steps.pr_info.outputs.headRef }} with: # Instead of checking out the base repo, use the contributor's repo name - repository: ${{ steps.pr_info.outputs.headRepoFullName }} - ref: ${{ steps.pr_info.outputs.headRef }} + repository: $HEADREPOFULLNAME + ref: $HEADREF # You may need fetch-depth: 0 for being able to push fetch-depth: 0 token: ${{ secrets.GITHUB_TOKEN }} From a7b04ff9d004793f82828998c4d2ed8ccfcf49cd Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 14 Jan 2025 11:47:11 +0530 Subject: [PATCH 4/6] apply suggestion --- .github/workflows/pr_style_bot.yml | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml index de9e52c95758..2126abb0eab5 100644 --- a/.github/workflows/pr_style_bot.yml +++ b/.github/workflows/pr_style_bot.yml @@ -36,7 +36,7 @@ jobs: - name: Check out PR branch uses: actions/checkout@v3 - env: + env: HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} HEADREF: ${{ steps.pr_info.outputs.headRef }} with: @@ -48,10 +48,14 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} - name: Debug + env: + HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} + HEADREF: ${{ steps.pr_info.outputs.headRef }} + PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} run: | - echo "PR Number: ${{ steps.pr_info.outputs.prNumber }}" - echo "Head Ref: ${{ steps.pr_info.outputs.headRef }}" - echo "Head Repo Full Name: ${{ steps.pr_info.outputs.headRepoFullName }}" + echo "PR number: $PRNUMBER" + echo "Head Ref: $HEADREF" + echo "Head Repo Full Name: $HEADREPOFULLNAME" - name: Set up Python uses: actions/setup-python@v4 @@ -67,27 +71,30 @@ jobs: - name: Commit and push changes id: commit_and_push + env: + HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} + HEADREF: ${{ steps.pr_info.outputs.headRef }} + PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | # Configure git with the Actions bot user git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" # Make sure your 'origin' remote is set to the contributor's fork - git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ steps.pr_info.outputs.headRepoFullName }}.git" + git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ steps.commit_and_push.env.HEADREPOFULLNAME }}.git" # If there are changes after running style/quality, commit them if [ -n "$(git status --porcelain)" ]; then git add . git commit -m "Apply style fixes" # Push to the original contributor's forked branch - git push origin HEAD:${{ steps.pr_info.outputs.headRef }} + git push origin HEAD:${{ steps.commit_and_push.env.HEADREF }} echo "changes_pushed=true" >> $GITHUB_OUTPUT else echo "No changes to commit." echo "changes_pushed=false" >> $GITHUB_OUTPUT fi - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Comment on PR with workflow run link if: steps.commit_and_push.outputs.changes_pushed == 'true' From fea8f84d54c16779031a6d90e2c3d8d312ad5fe2 Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 14 Jan 2025 13:17:16 +0530 Subject: [PATCH 5/6] fixes --- .github/workflows/pr_style_bot.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml index 2126abb0eab5..8af2a9da1926 100644 --- a/.github/workflows/pr_style_bot.yml +++ b/.github/workflows/pr_style_bot.yml @@ -64,9 +64,20 @@ jobs: run: | pip install .[quality] + - name: Download Makefile from main branch + run: | + curl -o main_Makefile https://raw.githubusercontent.com/huggingface/diffusers/main/Makefile + + - name: Compare Makefiles + run: | + if ! diff -q main_Makefile Makefile; then + echo "Error: The Makefile has changed. Please ensure it matches the main branch." + exit 1 + fi + echo "No changes in Makefile. Proceeding..." + - name: Run make style and make quality run: | - # Should we be comparing the Makefile to check for any differences with `diffusers` main? make style && make quality - name: Commit and push changes From 118cb2ab9e8ca99010cd4b28d82da93025cc57aa Mon Sep 17 00:00:00 2001 From: sayakpaul Date: Tue, 14 Jan 2025 13:46:36 +0530 Subject: [PATCH 6/6] updates --- .github/workflows/pr_style_bot.yml | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pr_style_bot.yml b/.github/workflows/pr_style_bot.yml index 8af2a9da1926..4c782b4fa8d2 100644 --- a/.github/workflows/pr_style_bot.yml +++ b/.github/workflows/pr_style_bot.yml @@ -41,8 +41,8 @@ jobs: HEADREF: ${{ steps.pr_info.outputs.headRef }} with: # Instead of checking out the base repo, use the contributor's repo name - repository: $HEADREPOFULLNAME - ref: $HEADREF + repository: ${{ env.HEADREPOFULLNAME }} + ref: ${{ env.HEADREF }} # You may need fetch-depth: 0 for being able to push fetch-depth: 0 token: ${{ secrets.GITHUB_TOKEN }} @@ -53,9 +53,9 @@ jobs: HEADREF: ${{ steps.pr_info.outputs.headRef }} PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} run: | - echo "PR number: $PRNUMBER" - echo "Head Ref: $HEADREF" - echo "Head Repo Full Name: $HEADREPOFULLNAME" + echo "PR number: ${{ env.PRNUMBER }}" + echo "Head Ref: ${{ env.HEADREF }}" + echo "Head Repo Full Name: ${{ env.HEADREPOFULLNAME }}" - name: Set up Python uses: actions/setup-python@v4 @@ -64,9 +64,9 @@ jobs: run: | pip install .[quality] - - name: Download Makefile from main branch - run: | - curl -o main_Makefile https://raw.githubusercontent.com/huggingface/diffusers/main/Makefile + - name: Download Makefile from main branch + run: | + curl -o main_Makefile https://raw.githubusercontent.com/huggingface/diffusers/main/Makefile - name: Compare Makefiles run: | @@ -75,6 +75,7 @@ jobs: exit 1 fi echo "No changes in Makefile. Proceeding..." + rm -rf main_Makefile - name: Run make style and make quality run: | @@ -88,19 +89,20 @@ jobs: PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | + echo "HEADREPOFULLNAME: ${{ env.HEADREPOFULLNAME }}, HEADREF: ${{ env.HEADREF }}" # Configure git with the Actions bot user git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" # Make sure your 'origin' remote is set to the contributor's fork - git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ steps.commit_and_push.env.HEADREPOFULLNAME }}.git" + git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ env.HEADREPOFULLNAME }}.git" # If there are changes after running style/quality, commit them if [ -n "$(git status --porcelain)" ]; then git add . git commit -m "Apply style fixes" # Push to the original contributor's forked branch - git push origin HEAD:${{ steps.commit_and_push.env.HEADREF }} + git push origin HEAD:${{ env.HEADREF }} echo "changes_pushed=true" >> $GITHUB_OUTPUT else echo "No changes to commit."