loading...

Automating Python Code Formatting using GitHub Actions

mritunjay394 profile image Mritunjay Sharma ・2 min read

My Workflow

It is the first time that I have built a GitHub action and I am really thankful to this Hackathon for making me learn amazing new things πŸ˜‡ !

The action that I have made is known as autoyapf. It is a GitHub action for yapf, an open-source tool that automatically formats Python code to conform to the PEP 8 style guide.

I have created a simple workflow that automates Python code formatting whenever 'push' event triggers. In simple words - every time the maintainer pushes the code from their own playground or merges a Pull Request from a contributor it will be formatted automatically with the PEP 8 Style guidelines using the yapf tool. This action is a win-win situation for both contributors and maintainers of projects involving Python!

Sample Workflow yaml File:


name: Formatting python code
on:
  push:
    paths:
    - '**.py'
jobs:
  autoyapf:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
        with:
          ref: ${{ github.head_ref }}
      - name: autoyapf
        id: autoyapf
        uses: mritunjaysharma394/autoyapf@v2
        with:
          args: --style pep8 --recursive --in-place .
      - name: Check for modified files
        id: git-check
        run: echo ::set-output name=modified::$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
      - name: Push changes
        if: steps.git-check.outputs.modified == 'true'
        run: |
          git config --global user.name 'Mritunjay Sharma'
          git config --global user.email 'mritunjaysharma394@users.noreply.github.com'
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
          git commit -am "Automated autoyapf fixes"
          git push

The autoyapf GitHub is being used in my own another Open Source Python project with the name sedpy

I have written a detailed blog explaining how the autoyapf GitHub action is being used in the above project which can be found below:

Submission Category:

Maintainer Must-Haves

Yaml File or Link to Code

GitHub logo mritunjaysharma394 / autoyapf

Automating yapf formatting.

autoyapf

GitHub Marketplace Open Source Love

Worried whether your Python code is following the style guidelines or not? Well, you don't need to do that now πŸ˜„

autoyapf is a GitHub action for yapf, an open-source tool that automatically formats Python code to conform to the PEP 8 style guide It is in essence, the algorithm that takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide.

This action is designed to be used in conjunction with the 'push' trigger. In simple words - everytime the maintainer pushes the code from their own playground or merges a Pull Request from a contributor it will be formatted automatically with the PEP 8 Style guidelines using the yapf tool. This action is a win-win situation for both contributors and maintainers of projects involving Python! Yes, you heard it right!

This…

Additional Resources / Info

[Note:] # (Be sure to link to any open source projects that are using your workflow!)

The workflow is being used in:

  • GitHub logo mritunjaysharma394 / sedpy

    Building a cross-platform alternative of sed (stream editor)

    sedpy

    Build Status Maintenance GitHub issues GitHub forks GitHub stars [PythonVersion

    sedpy is an open-source project being built as a cross-platform alternative of sed for easier and more flexible stream line editing across BSD and GNU systems.

    What is sed and what is sedpy?

    The sed command, short for stream editor, performs editing operations on text coming from standard input or a file. sed edits line-by-line and in a non-interactive way.

    This means that you make all of the editing decisions as you are calling the command, and sed executes the directions automatically. This may seem confusing or unintuitive, but it is a very powerful and fast way to transform text, especially as part of a script or automated workflow.

    The problem with sed is that it's not purely cross-platform. Linux uses GNU-based sed while macOS has the BSD version of sed.As a result - both have quite important syntactic and usage difference. At the same time, sed…

It is also being considered to be used in RTEMS:

  • GitHub logo RTEMS / rtems-source-builder

    Build tools and packages from source.

    RTEMS Tools From Source
    -----------------------
    The RTEMS Source Builder is a tool to aid building packages from source used by
    the RTEMS project. It helps consolidate the details you need to build a package
    from source in a controlled and verifiable way. The tool is aimed at developers
    of software who use tool sets for embedded type development and is not limited
    to building just for RTEMS. Embedded development typically uses cross-compiling
    tool chains, debuggers, and debugging aids. Together we call these a 'tool
    set'. The RTEMS Source Builder is not limited to this role but designed to fit
    with-in this specific niche. It can be used outside of the RTEMS project and we
    welcome this happening in other open source or commercial projects
    The project is part of the RTEMS Project. The project's websites are
     RTEMS Project Website:
       https://www.rtems.org/
    
     GIT Source Repository:
       https://git.rtems.org/rtems-source-builder.git/
    
     Documentation:
       https://docs.rtems.org/branches/master/rsb/index.html
    
     Bugs:
       https://devel.rtems.org/query?component=RSB
    
    Please refer
    …

Thank you so much for your valuable time going through this!

Please free to share feedbacks and suggestions for improvements! πŸ’ƒ

Discussion

pic
Editor guide