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 action will immensely help in increasing the speed of development cycle of projects as you will no longer need to worry about whether clean code is being produced or not as autoyapf action will take care of that 🎊
Create a .github/workflows
directory in your repository and inside this directory create a workflow .yml
file (give the file a name like autoyapf.yml
). An example workflow is available below. For more information, reference the GitHub Help Documentation for Creating a workflow file.
Copy the workflow example below in the .yml
file you created in .github/workflows
directory.
The following workflow is a simple example to demonstrate how the action works.
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 'github-actions'
git config --global user.email 'github-actions@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
Note: The GITHUB_TOKEN
is provided by Actions; you do not need to create your own token.
This configuration will work something like this. As an example here, my friend Naman is contributing to the Project with a Pull Request of a code that is not formatted or styled. On
merging this PR, autoyapf
action will be triggered which will automatically style the code according to PEP-8 Guidelines.:
1. Pull Request Initiated by Naman which I later merged
2. GitHub Action worked successfully
3. Automated code fixes commit by autoyapf action
4. Visible changes in code after the autoyapf fix
We would love you to contribute to @actions/autoyapf
, pull requests are welcome! Please see the CONTRIBUTING.md for more information.
The scripts and documentation in this project are released under the MIT License