- 
                Notifications
    You must be signed in to change notification settings 
- Fork 837
add style bot GitHub action #2898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 7 commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      88f0bbc
              
                add style bot GH action
              
              
                hanouticelina d4fdec4
              
                style command as an input
              
              
                hanouticelina 9ced3d2
              
                add manual trigger for debugging
              
              
                hanouticelina fb890a7
              
                fix secret name
              
              
                hanouticelina c148c31
              
                fixes
              
              
                hanouticelina 9f4b1df
              
                Merge branch 'main' into style-bot-gh-action
              
              
                hanouticelina d89e657
              
                Merge branch 'main' into style-bot-gh-action
              
              
                hanouticelina 2660ac3
              
                add python version as input
              
              
                hanouticelina 202d2a0
              
                Merge branch 'style-bot-gh-action' of github.com:huggingface/huggingf…
              
              
                hanouticelina 04306f3
              
                use env instead of {{...}}
              
              
                hanouticelina 7cf9447
              
                add a pre-commit script name input
              
              
                hanouticelina File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| name: Style Bot Action | ||
|  | ||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| python_quality_dependencies: | ||
| required: true | ||
| type: string | ||
| description: "Python package extras to install for quality checks (e.g. '[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 | ||
| description: "Optional script to run before committing changes" | ||
| secrets: | ||
| bot_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.bot_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 | ||
|         
                  hanouticelina marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| - 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: | | ||
| ${{ inputs.style_command }} | ||
|  | ||
| - 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.bot_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 }} | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. love it! | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 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]" | ||
| style_command: "make style" | ||
| secrets: | ||
| bot_token: ${{ secrets.GITHUB_TOKEN }} | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.