From 88f0bbcd4763e8abda9e98bba4fc6fbf55eaff01 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Mon, 3 Mar 2025 18:33:15 +0100 Subject: [PATCH 1/8] add style bot GH action --- .github/workflows/style-bot-action.yml | 150 +++++++++++++++++++++++++ .github/workflows/style-bot.yml | 17 +++ 2 files changed, 167 insertions(+) create mode 100644 .github/workflows/style-bot-action.yml create mode 100644 .github/workflows/style-bot.yml diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml new file mode 100644 index 0000000000..93995be8b6 --- /dev/null +++ b/.github/workflows/style-bot-action.yml @@ -0,0 +1,150 @@ +name: Style Bot Action + +on: + workflow_call: + inputs: + python_quality_dependencies: + required: false + type: string + description: "Python package extras to install for quality checks (e.g. '[quality]')" + default: "[quality]" + pre_commit_script: + required: false + type: string + description: "Optional script to run before committing changes" + secrets: + github_token: + required: true + description: "GitHub token with permissions to comment and push to PR" + +jobs: + check-permissions: + if: > + contains(github.event.comment.body, '@bot /style') && + github.event.issue.pull_request != null + runs-on: ubuntu-latest + outputs: + is_authorized: ${{ steps.check_user_permission.outputs.has_permission }} + steps: + - name: Check user permission + id: check_user_permission + uses: actions/github-script@v6 + with: + script: | + const comment_user = context.payload.comment.user.login; + const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ + owner: context.repo.owner, + repo: context.repo.repo, + username: comment_user + }); + const authorized = permission.permission === 'admin'; + console.log(`User ${comment_user} has permission level: ${permission.permission}, authorized: ${authorized} (only admins allowed)`); + core.setOutput('has_permission', authorized); + + run-style-bot: + needs: check-permissions + if: needs.check-permissions.outputs.is_authorized == 'true' + 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 + }); + + // 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 + 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: ${{ env.HEADREPOFULLNAME }} + ref: ${{ env.HEADREF }} + # You may need fetch-depth: 0 for being able to push + fetch-depth: 0 + 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: $PRNUMBER" + echo "Head Ref: $HEADREF" + echo "Head Repo Full Name: $HEADREPOFULLNAME" + + - name: Set up Python + uses: actions/setup-python@v4 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install .${{ inputs.python_quality_dependencies }} + + - name: Custom pre-commit script + if: inputs.pre_commit_script != '' + run: | + ${inputs.pre_commit_script} + + - name: Run make style and make quality + run: | + make style && make quality + + - 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: | + echo "HEADREPOFULLNAME: $HEADREPOFULLNAME, HEADREF: $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/$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:$HEADREF + echo "changes_pushed=true" >> $GITHUB_OUTPUT + else + echo "No changes to commit." + echo "changes_pushed=false" >> $GITHUB_OUTPUT + fi + + - 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 }} \ No newline at end of file diff --git a/.github/workflows/style-bot.yml b/.github/workflows/style-bot.yml new file mode 100644 index 0000000000..c5a49500e7 --- /dev/null +++ b/.github/workflows/style-bot.yml @@ -0,0 +1,17 @@ +name: Style Bot + +on: + issue_comment: + types: [created] + +permissions: + contents: write + pull-requests: write + +jobs: + style: + uses: ./.github/workflows/style-bot-action.yml + with: + python_quality_dependencies: "[quality]" + secrets: + github_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From d4fdec4b91add9fd2cff9f748d721b0110f67c82 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Mon, 3 Mar 2025 19:14:22 +0100 Subject: [PATCH 2/8] style command as an input --- .github/workflows/style-bot-action.yml | 10 +++++++--- .github/workflows/style-bot.yml | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index 93995be8b6..c149696639 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -4,10 +4,14 @@ on: workflow_call: inputs: python_quality_dependencies: - required: false + required: true type: string description: "Python package extras to install for quality checks (e.g. '[quality]')" - default: "[quality]" + style_command: + required: false + type: string + description: "Command to run for style checks or/and style fixes" + default: "make style && make quality" pre_commit_script: required: false type: string @@ -102,7 +106,7 @@ jobs: - name: Run make style and make quality run: | - make style && make quality + ${inputs.style_command} - name: Commit and push changes id: commit_and_push diff --git a/.github/workflows/style-bot.yml b/.github/workflows/style-bot.yml index c5a49500e7..2a813bf7bf 100644 --- a/.github/workflows/style-bot.yml +++ b/.github/workflows/style-bot.yml @@ -13,5 +13,6 @@ jobs: uses: ./.github/workflows/style-bot-action.yml with: python_quality_dependencies: "[quality]" + style_command: "make style" secrets: github_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From 9ced3d2af3da6dcad396c2ad3c88eb3628a7a8ec Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 10:03:45 +0100 Subject: [PATCH 3/8] add manual trigger for debugging --- .github/workflows/style-bot.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/style-bot.yml b/.github/workflows/style-bot.yml index 2a813bf7bf..f4b939a2d9 100644 --- a/.github/workflows/style-bot.yml +++ b/.github/workflows/style-bot.yml @@ -3,7 +3,8 @@ name: Style Bot on: issue_comment: types: [created] - + workflow_dispatch: + permissions: contents: write pull-requests: write From fb890a7da803b5309a636b576ce17fec2efa7534 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 10:07:27 +0100 Subject: [PATCH 4/8] fix secret name --- .github/workflows/style-bot-action.yml | 6 +++--- .github/workflows/style-bot.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index c149696639..61e597e916 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -17,7 +17,7 @@ on: type: string description: "Optional script to run before committing changes" secrets: - github_token: + bot_token: required: true description: "GitHub token with permissions to comment and push to PR" @@ -79,7 +79,7 @@ jobs: ref: ${{ env.HEADREF }} # You may need fetch-depth: 0 for being able to push fetch-depth: 0 - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{ secrets.bot_token }} - name: Debug env: @@ -114,7 +114,7 @@ jobs: HEADREPOFULLNAME: ${{ steps.pr_info.outputs.headRepoFullName }} HEADREF: ${{ steps.pr_info.outputs.headRef }} PRNUMBER: ${{ steps.pr_info.outputs.prNumber }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.bot_token }} run: | echo "HEADREPOFULLNAME: $HEADREPOFULLNAME, HEADREF: $HEADREF" # Configure git with the Actions bot user diff --git a/.github/workflows/style-bot.yml b/.github/workflows/style-bot.yml index f4b939a2d9..334eabfcb0 100644 --- a/.github/workflows/style-bot.yml +++ b/.github/workflows/style-bot.yml @@ -16,4 +16,4 @@ jobs: python_quality_dependencies: "[quality]" style_command: "make style" secrets: - github_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + bot_token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From c148c31cd1924bb3bf07258df7e1527475fcd9d4 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 10:53:39 +0100 Subject: [PATCH 5/8] fixes --- .github/workflows/style-bot-action.yml | 4 ++-- .github/workflows/style-bot.yml | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index 61e597e916..9fa5bdcda1 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -102,11 +102,11 @@ jobs: - name: Custom pre-commit script if: inputs.pre_commit_script != '' run: | - ${inputs.pre_commit_script} + ${{ inputs.pre_commit_script }} - name: Run make style and make quality run: | - ${inputs.style_command} + ${{ inputs.style_command }} - name: Commit and push changes id: commit_and_push diff --git a/.github/workflows/style-bot.yml b/.github/workflows/style-bot.yml index 334eabfcb0..1f01855447 100644 --- a/.github/workflows/style-bot.yml +++ b/.github/workflows/style-bot.yml @@ -3,7 +3,6 @@ name: Style Bot on: issue_comment: types: [created] - workflow_dispatch: permissions: contents: write From 2660ac3df2bd4e39401204feff3499d7673c280d Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 17:04:31 +0100 Subject: [PATCH 6/8] add python version as input --- .github/workflows/style-bot-action.yml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index 9fa5bdcda1..5633bce8fc 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -3,19 +3,24 @@ name: Style Bot Action on: workflow_call: inputs: + pre_commit_script: + required: false + type: string + description: "Optional script to run before committing changes" python_quality_dependencies: required: true type: string description: "Python package extras to install for quality checks (e.g. '[quality]')" + python_version: + required: false + type: string + description: "Python version to run code formatter" + default: "3.10" style_command: required: false type: string description: "Command to run for style checks or/and style fixes" default: "make style && make quality" - pre_commit_script: - required: false - type: string - description: "Optional script to run before committing changes" secrets: bot_token: required: true @@ -93,6 +98,8 @@ jobs: - name: Set up Python uses: actions/setup-python@v4 + with: + python-version: ${{ inputs.python_version }} - name: Install dependencies run: | From 04306f33c97cc47d9d16832a4da3849c759019c9 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 17:34:12 +0100 Subject: [PATCH 7/8] use env instead of {{...}} --- .github/workflows/style-bot-action.yml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index 5633bce8fc..7bf396a1c8 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -102,18 +102,27 @@ jobs: python-version: ${{ inputs.python_version }} - name: Install dependencies + env: + python_quality_dependencies: ${{ inputs.python_quality_dependencies }} run: | python -m pip install --upgrade pip - pip install .${{ inputs.python_quality_dependencies }} + pip install .$python_quality_dependencies - name: Custom pre-commit script + env: + pre_commit_script: ${{ inputs.pre_commit_script }} if: inputs.pre_commit_script != '' + shell: bash run: | - ${{ inputs.pre_commit_script }} + echo "$pre_commit_script" > pre_commit_script.sh + chmod +x pre_commit_script.sh + ./pre_commit_script.sh - name: Run make style and make quality + env: + style_command: ${{ inputs.style_command }} run: | - ${{ inputs.style_command }} + $style_command - name: Commit and push changes id: commit_and_push From 7cf9447907b3eb98c5f3a59ae741b45a8c656476 Mon Sep 17 00:00:00 2001 From: Celina Hanouti Date: Tue, 4 Mar 2025 17:45:47 +0100 Subject: [PATCH 8/8] add a pre-commit script name input --- .github/workflows/style-bot-action.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/style-bot-action.yml b/.github/workflows/style-bot-action.yml index 7bf396a1c8..be3146a4b4 100644 --- a/.github/workflows/style-bot-action.yml +++ b/.github/workflows/style-bot-action.yml @@ -7,6 +7,11 @@ on: required: false type: string description: "Optional script to run before committing changes" + pre_commit_script_name: + required: false + type: string + description: "Custom name for the pre-commit script step" + default: "Custom pre-commit script" python_quality_dependencies: required: true type: string @@ -108,7 +113,7 @@ jobs: python -m pip install --upgrade pip pip install .$python_quality_dependencies - - name: Custom pre-commit script + - name: ${{ inputs.pre_commit_script_name }} env: pre_commit_script: ${{ inputs.pre_commit_script }} if: inputs.pre_commit_script != ''