Automating Python Code Formatting using GitHub Actions
Tags: actionshackathon, python, codequality, github
Published: 2020-09-08 20:40:29

[Instructions]: # (To submit to the GitHub Actions x DEV Hackathon, please fill out all sections.)


### My Workflow
[Note]: # (Make sure to include the name of your GitHub Action and share where it's being used!)

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 :innocent: ! 

The action that I have made is known as [autoyapf](https://github.com/mritunjaysharma394/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:
```yaml

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](https://github.com/mritunjaysharma394/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:
{% link https://dev.to/mritunjay394/how-to-automate-code-formatting-for-python-projects-with-github-actions-a-study-47g8 %}

### Submission Category: 

[Note]: # (Maintainer Must-Haves, DIY Deployments, Interesting IoT, Phone Friendly, or Wacky Wildcards)
Maintainer Must-Haves

### Yaml File or Link to Code

[Note]: # (Our markdown editor supports pretty embeds. Try this syntax: `{% github link_to_your_repo %}` to share a GitHub repository)

{% github mritunjaysharma394/autoyapf %}

### Additional Resources / Info

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

[Note]: # (Be sure to include the DEV usernames of your collaborators, if any. )

[Reminder]: # (Submissions are due on September 12th (11:59 PM PDT) and winners will be announced on September 23!)

The workflow is being used in: 

* {% github mritunjaysharma394/sedpy %}
* {% github NAMANJAIN2606/clean-up/ no-readme %} 

It is also being considered to be used in RTEMS: 
* {% github rtems/rtems-source-builder %}

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

Please free to share feedbacks and suggestions for improvements! :dancer: