├── .github ├── CODEOWNERS └── workflows │ ├── slash-command-dispatch.yml │ ├── dockerhub-description.yml │ └── autopep8.yml ├── requirements.txt ├── autopep8-example-pr.png ├── entrypoint.sh ├── action.yml ├── Dockerfile ├── example └── example.py ├── LICENSE └── README.md /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @peter-evans 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | autopep8==2.0.1 2 | -------------------------------------------------------------------------------- /autopep8-example-pr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peter-evans/autopep8/HEAD/autopep8-example-pr.png -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -l 2 | set -uo pipefail 3 | 4 | autopep8 $* 5 | echo "exit-code=$?" >> $GITHUB_OUTPUT 6 | -------------------------------------------------------------------------------- /.github/workflows/slash-command-dispatch.yml: -------------------------------------------------------------------------------- 1 | name: Slash Command Dispatch 2 | on: 3 | issue_comment: 4 | types: [created] 5 | jobs: 6 | slashCommandDispatch: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Slash Command Dispatch 10 | uses: peter-evans/slash-command-dispatch@v2 11 | with: 12 | token: ${{ secrets.REPO_ACCESS_TOKEN }} 13 | commands: autopep8-example 14 | permission: admin 15 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'autopep8' 2 | author: 'Peter Evans' 3 | description: 'Automatically formats Python code to conform to the PEP 8 style guide.' 4 | inputs: 5 | args: 6 | description: 'Arguments to pass to autopep8' 7 | required: true 8 | default: '--help' 9 | outputs: 10 | exit-code: 11 | description: 'The exit code output by autopep8' 12 | runs: 13 | using: 'docker' 14 | image: 'docker://peterevans/autopep8:2.0.0' 15 | args: 16 | - ${{ inputs.args }} 17 | branding: 18 | icon: 'code' 19 | color: 'blue' 20 | -------------------------------------------------------------------------------- /.github/workflows/dockerhub-description.yml: -------------------------------------------------------------------------------- 1 | name: Update Docker Hub Description 2 | on: 3 | push: 4 | branches: 5 | - master 6 | paths: 7 | - README.md 8 | - .github/workflows/dockerhub-description.yml 9 | jobs: 10 | dockerHubDescription: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: Docker Hub Description 15 | uses: peter-evans/dockerhub-description@v2.4.1 16 | env: 17 | DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} 18 | DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }} 19 | DOCKERHUB_REPOSITORY: peterevans/autopep8 20 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.8.3-alpine3.10 2 | 3 | LABEL maintainer="Peter Evans " 4 | LABEL repository="https://github.com/peter-evans/autopep8" 5 | LABEL homepage="https://github.com/peter-evans/autopep8" 6 | 7 | LABEL com.github.actions.name="autopep8" 8 | LABEL com.github.actions.description="Automatically formats Python code to conform to the PEP 8 style guide." 9 | LABEL com.github.actions.icon="code" 10 | LABEL com.github.actions.color="blue" 11 | 12 | COPY LICENSE README.md / 13 | 14 | COPY requirements.txt /tmp/ 15 | RUN pip install --requirement /tmp/requirements.txt 16 | 17 | COPY entrypoint.sh /entrypoint.sh 18 | ENTRYPOINT ["/entrypoint.sh"] 19 | CMD ["--help"] -------------------------------------------------------------------------------- /example/example.py: -------------------------------------------------------------------------------- 1 | import math, sys; 2 | 3 | def example1(): 4 | ####This is a long comment. This should be wrapped to fit within 72 characters. 5 | some_tuple=( 1,2, 3,'a' ); 6 | some_variable={'long':'Long code lines should be wrapped within 79 characters.', 7 | 'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'], 8 | 'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1, 9 | 20,300,40000,500000000,60000000000000000]}} 10 | return (some_tuple, some_variable) 11 | def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key('')); 12 | class Example3( object ): 13 | def __init__ ( self, bar ): 14 | #Comments should have a space after the hash. 15 | if bar : bar+=1; bar=bar* bar ; return bar 16 | else: 17 | some_string = """ 18 | Indentation in multiline strings should not be touched. 19 | Only actual code should be reindented. 20 | """ 21 | return (sys.path, some_string) 22 | -------------------------------------------------------------------------------- /.github/workflows/autopep8.yml: -------------------------------------------------------------------------------- 1 | name: Format python code 2 | on: 3 | repository_dispatch: 4 | types: [autopep8-example-command] 5 | jobs: 6 | autopep8: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | - name: autopep8 11 | uses: ./ 12 | with: 13 | args: --recursive --in-place --aggressive --aggressive . 14 | - name: Create Pull Request 15 | uses: peter-evans/create-pull-request@v6 16 | with: 17 | commit-message: autopep8 action fixes 18 | title: Fixes by autopep8 action 19 | body: This is an auto-generated PR with fixes by autopep8. 20 | labels: autopep8, automated pr 21 | reviewers: peter-evans 22 | branch: autopep8-patches 23 | - name: Add reaction 24 | uses: peter-evans/create-or-update-comment@v4 25 | with: 26 | repository: ${{ github.event.client_payload.github.payload.repository.full_name }} 27 | comment-id: ${{ github.event.client_payload.github.payload.comment.id }} 28 | reaction-type: hooray 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Peter Evans 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # autopep8 2 | [![GitHub Marketplace](https://img.shields.io/badge/Marketplace-autopep8-blue.svg?colorA=24292e&colorB=0366d6&style=flat&longCache=true&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAM6wAADOsB5dZE0gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAERSURBVCiRhZG/SsMxFEZPfsVJ61jbxaF0cRQRcRJ9hlYn30IHN/+9iquDCOIsblIrOjqKgy5aKoJQj4O3EEtbPwhJbr6Te28CmdSKeqzeqr0YbfVIrTBKakvtOl5dtTkK+v4HfA9PEyBFCY9AGVgCBLaBp1jPAyfAJ/AAdIEG0dNAiyP7+K1qIfMdonZic6+WJoBJvQlvuwDqcXadUuqPA1NKAlexbRTAIMvMOCjTbMwl1LtI/6KWJ5Q6rT6Ht1MA58AX8Apcqqt5r2qhrgAXQC3CZ6i1+KMd9TRu3MvA3aH/fFPnBodb6oe6HM8+lYHrGdRXW8M9bMZtPXUji69lmf5Cmamq7quNLFZXD9Rq7v0Bpc1o/tp0fisAAAAASUVORK5CYII=)](https://github.com/marketplace/actions/autopep8) 3 | 4 | A GitHub action for [autopep8](https://github.com/hhatto/autopep8), a tool that automatically formats Python code to conform to the PEP 8 style guide. 5 | 6 | This action is designed to be used in conjunction with [Create Pull Request](https://github.com/peter-evans/create-pull-request). This will automatically create a pull request to merge fixes that autopep8 makes to python code in your repository. 7 | 8 | ## Usage 9 | 10 | This action is a simple wrapper around [autopep8](https://github.com/hhatto/autopep8). Arguments should be passed to the action via the `args` parameter. 11 | This example fixes all python files in your repository with aggressive level 2. 12 | 13 | ```yml 14 | - name: autopep8 15 | id: autopep8 16 | uses: peter-evans/autopep8@v2 17 | with: 18 | args: --recursive --in-place --aggressive --aggressive . 19 | ``` 20 | 21 | The action outputs the exit code from autopep8. This can be useful in combination with the autopep8 flag `--exit-code` for pull request checks. 22 | 23 | ```yml 24 | - name: Fail if autopep8 made changes 25 | if: steps.autopep8.outputs.exit-code == 2 26 | run: exit 1 27 | ``` 28 | 29 | See [autopep8 documentation](https://github.com/hhatto/autopep8) for further argument details. 30 | 31 | ## Automated pull requests 32 | 33 | On its own this action is not very useful. Please use it in conjunction with [Create Pull Request](https://github.com/peter-evans/create-pull-request) or a [direct push to branch workflow](#direct-push-with-on-pull_request-workflows). 34 | 35 | The following workflow is a simple example to demonstrate how the two actions work together. 36 | 37 | ```yml 38 | name: Format python code 39 | on: push 40 | jobs: 41 | autopep8: 42 | runs-on: ubuntu-latest 43 | steps: 44 | - uses: actions/checkout@v2 45 | - name: autopep8 46 | uses: peter-evans/autopep8@v2 47 | with: 48 | args: --recursive --in-place --aggressive --aggressive . 49 | - name: Create Pull Request 50 | uses: peter-evans/create-pull-request@v3 51 | with: 52 | commit-message: autopep8 action fixes 53 | title: Fixes by autopep8 action 54 | body: This is an auto-generated PR with fixes by autopep8. 55 | labels: autopep8, automated pr 56 | reviewers: peter-evans 57 | branch: autopep8-patches 58 | ``` 59 | 60 | This configuration will create pull requests that look like this: 61 | 62 | ![Pull Request Example](https://github.com/peter-evans/autopep8/blob/master/autopep8-example-pr.png?raw=true) 63 | 64 | ## Automated pull requests with "on: pull_request" workflows 65 | 66 | **Update**: While the following approach does work in some cases, my strong recommendation would be to use a slash command style "ChatOps" solution for operations on pull requests. See [slash-command-dispatch](https://github.com/peter-evans/slash-command-dispatch) for such a solution. 67 | 68 | The following is an example workflow for a use-case where autopep8 runs as both a check on pull requests and raises a further pull request to apply fixes. 69 | 70 | How it works: 71 | 1. When a pull request is raised the workflow executes as a check. 72 | 2. If autopep8 makes any fixes a pull request will be raised for those fixes to be merged into the current pull request branch. The workflow then deliberately causes the check to fail. 73 | 3. When the pull request containing the fixes is merged the workflow runs again. This time autopep8 makes no changes and the check passes. 74 | 4. The original pull request can now be merged. 75 | 76 | Note that due to [token restrictions on public repository forks](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#permissions-for-the-github_token), this workflow does not work for pull requests raised from forks. 77 | Private repositories can be configured to [enable workflows](https://docs.github.com/en/github/administering-a-repository/disabling-or-limiting-github-actions-for-a-repository#enabling-workflows-for-private-repository-forks) from forks to run without restriction. 78 | 79 | ```yml 80 | name: autopep8 81 | on: pull_request 82 | jobs: 83 | autopep8: 84 | # Check if the PR is not raised by this workflow and is not from a fork 85 | if: startsWith(github.head_ref, 'autopep8-patches') == false && github.event.pull_request.head.repo.full_name == github.repository 86 | runs-on: ubuntu-latest 87 | steps: 88 | - uses: actions/checkout@v2 89 | with: 90 | ref: ${{ github.head_ref }} 91 | - name: autopep8 92 | id: autopep8 93 | uses: peter-evans/autopep8@v2 94 | with: 95 | args: --exit-code --recursive --in-place --aggressive --aggressive . 96 | - name: Set autopep8 branch name 97 | id: vars 98 | run: | 99 | branch-name="autopep8-patches/${{ github.head_ref }}" 100 | echo "branch-name=$branch-name" >> $GITHUB_OUTPUT 101 | - name: Create Pull Request 102 | if: steps.autopep8.outputs.exit-code == 2 103 | uses: peter-evans/create-pull-request@v3 104 | with: 105 | commit-message: autopep8 action fixes 106 | title: Fixes by autopep8 action 107 | body: This is an auto-generated PR with fixes by autopep8. 108 | labels: autopep8, automated pr 109 | reviewers: peter-evans 110 | branch: ${{ steps.vars.outputs.branch-name }} 111 | - name: Fail if autopep8 made changes 112 | if: steps.autopep8.outputs.exit-code == 2 113 | run: exit 1 114 | ``` 115 | 116 | ## Direct push with "on: pull_request" workflows 117 | 118 | The following workflow is an alternative to the previous workflow. Instead of raising a second pull request it commits the changes made by autopep8 directly to the pull request branch. 119 | 120 | **Important caveat:** If you have other pull request checks besides the following workflow then you must use a [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) instead of the default `GITHUB_TOKEN`. 121 | This is due to a deliberate limitation imposed by GitHub Actions that events raised by a workflow (such as `push`) cannot trigger further workflow runs. 122 | This is to prevent accidental "infinite loop" situations, and as an anti-abuse measure. 123 | Using a `repo` scoped [Personal Access Token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token) is an approved workaround. See [here](https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#triggering-further-workflow-runs) for further detail. 124 | 125 | How it works: 126 | 1. When a pull request is raised the workflow executes as a check. 127 | 2. If autopep8 makes any fixes they will be committed directly to the current pull request branch. 128 | 3. The `push` triggers all pull request checks to run again. 129 | 130 | Note that due to [token restrictions on public repository forks](https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token#permissions-for-the-github_token), this workflow does not work for pull requests raised from forks. 131 | Private repositories can be configured to [enable workflows](https://docs.github.com/en/github/administering-a-repository/disabling-or-limiting-github-actions-for-a-repository#enabling-workflows-for-private-repository-forks) from forks to run without restriction. 132 | 133 | ```yml 134 | name: autopep8 135 | on: pull_request 136 | jobs: 137 | autopep8: 138 | # Check if the PR is not from a fork 139 | if: github.event.pull_request.head.repo.full_name == github.repository 140 | runs-on: ubuntu-latest 141 | steps: 142 | - uses: actions/checkout@v2 143 | with: 144 | token: ${{ secrets.REPO_ACCESS_TOKEN }} 145 | ref: ${{ github.head_ref }} 146 | - name: autopep8 147 | id: autopep8 148 | uses: peter-evans/autopep8@v2 149 | with: 150 | args: --exit-code --recursive --in-place --aggressive --aggressive . 151 | - name: Commit autopep8 changes 152 | if: steps.autopep8.outputs.exit-code == 2 153 | run: | 154 | git config --global user.name 'Peter Evans' 155 | git config --global user.email 'peter-evans@users.noreply.github.com' 156 | git commit -am "Automated autopep8 fixes" 157 | git push 158 | ``` 159 | 160 | ## License 161 | 162 | MIT License - see the [LICENSE](LICENSE) file for details 163 | --------------------------------------------------------------------------------