Skip to content

Automate isort, flake8 etc through GitHub workflows #1626

@0x0elliot

Description

@0x0elliot

While pre-commit exists, it would be very cool if we can find a way to simply automate most of these things since they are pretty simple to automate.

I use a workflow file like this for my projects. Let me know if you would want me to implement this here as well.

name: Auto-Format Python Code

on:
  push:
  pull_request:
  
jobs:
  format:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      
    - name: Set up Python environment
      uses: actions/setup-python@v2
      with:
        python-version: '3.9'
        
    - name: Install dependencies
      run: |
        pip install black isort flake8 autoflake
      
    - name: Remove unused imports with autoflake
      run: autoflake --remove-unused-variables --in-place --recursive .
        
    - name: Format code with isort
      run: isort .
      
    - name: Format code with Black
      run: black .
      
    - name: Commit the generated schema
      uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: 🤖 Auto-formated Python code
        # this gives the commit author the github bot pfp and username
        commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
        commit_user_email: 41898282+github-actions[bot]@users.noreply.github.com
        file_pattern: "*.py"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions