├── .github └── workflows │ └── issue-automation.yml ├── LICENSE ├── action.yml └── README.md /.github/workflows/issue-automation.yml: -------------------------------------------------------------------------------- 1 | name: Add issues to On Call project 2 | 3 | on: 4 | issues: 5 | types: 6 | - opened 7 | - labeled 8 | 9 | jobs: 10 | add-to-project: 11 | name: Add issue to "On Call" 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/add-to-project@v0.3.0 15 | with: 16 | project-url: https://github.com/orgs/sourcery-ai/projects/3 17 | github-token: ${{ secrets.ADD_TO_PROJECT_PAT }} 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 SOURCERY.AI LIMITED 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 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: "Sourcery Action" 2 | 3 | branding: 4 | icon: 'box' 5 | color: 'orange' 6 | 7 | description: "A GitHub Action for running Sourcery." 8 | 9 | inputs: 10 | token: 11 | description: > 12 | The Sourcery token for logging in. 13 | 14 | The Sourcery token can be retrieved from 15 | [your dashboard](https://sourcery.ai/dashboard/profile) and uploaded to your 16 | GitHub repository as a 17 | [GitHub secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets). 18 | required: true 19 | target: 20 | description: "File(s) or directory(ies) to review." 21 | required: false 22 | default: "." 23 | version: 24 | description: > 25 | Sourcery CLI version, e.g. '1.5.0'. If empty, default to the latest version 26 | available. 27 | required: false 28 | default: "" 29 | verbose: 30 | description: > 31 | Verbose output with explanation and code snippets. Either 'true' or 'false'. 32 | required: false 33 | default: "true" 34 | check: 35 | description: "Fail job if issues are found. Either 'true' or 'false'." 36 | required: false 37 | default: "true" 38 | fix: 39 | description: "Automatically fix issues where possible. Either 'true' or 'false'." 40 | required: false 41 | default: "false" 42 | in_place: 43 | description: "Change files in place. Either 'true' or 'false'." 44 | required: false 45 | default: "false" 46 | deprecationMessage: > 47 | This input is deprecated and will be removed in a future release. Please update 48 | your Sourcery `version` and use `fix` instead of `in_place`. 49 | config: 50 | description: "Location of the Sourcery YAML configuration file." 51 | required: false 52 | default: "" 53 | diff_ref: 54 | description: > 55 | Reference to compare the current branch with and run Sourcery on the diff. 56 | If empty, run Sourcery on the entire codebase. 57 | To review only code that changed in a PR, use 58 | "github.event.pull_request.base.sha". 59 | required: false 60 | default: "" 61 | 62 | runs: 63 | using: composite 64 | steps: 65 | - name: Install Sourcery 66 | shell: bash 67 | run: | 68 | if [ -z ${{ inputs.version }} ] 69 | then 70 | python3 -m pip install sourcery 71 | else 72 | python3 -m pip install sourcery==${{ inputs.version }} 73 | fi 74 | 75 | - name: Authenticate Sourcery 76 | shell: bash 77 | run: | 78 | sourcery login --token ${{ inputs.token }} 79 | 80 | - name: Sourcery review 81 | shell: bash 82 | run: | 83 | if [ ${{ inputs.check }} == "true" ]; then 84 | SOURCERY_OPTIONS+=( "--check" ) 85 | elif [ ${{ inputs.check }} != "false" ]; then 86 | echo "Invalid value for input 'check'" 87 | echo "Expected either 'true' or 'false'" 88 | echo "Got '${{ inputs.check }}'" 89 | exit 1 90 | fi 91 | 92 | if [ ${{ inputs.fix }} == "true" ]; then 93 | SOURCERY_OPTIONS+=( "--fix" ) 94 | elif [ ${{ inputs.fix }} != "false" ]; then 95 | echo "Invalid value for input 'fix'" 96 | echo "Expected either 'true' or 'false'" 97 | echo "Got '${{ inputs.fix }}'" 98 | exit 1 99 | fi 100 | 101 | # this input is deprecated and will be removed in a future release 102 | # we are keeping it here because users might have pinned Sourcery to an older 103 | # version that does not support `--fix` 104 | if [ ${{ inputs.in_place }} == "true" ]; then 105 | SOURCERY_OPTIONS+=( "--in-place" ) 106 | elif [ ${{ inputs.in_place }} != "false" ]; then 107 | echo "Invalid value for input 'in_place'" 108 | echo "Expected either 'true' or 'false'" 109 | echo "Got '${{ inputs.in_place }}'" 110 | exit 1 111 | fi 112 | 113 | if [ ${{ inputs.verbose }} == "true" ]; then 114 | SOURCERY_OPTIONS+=( "--verbose" ) 115 | elif [ ${{ inputs.verbose }} != "false" ]; then 116 | echo "Invalid value for input 'verbose'" 117 | echo "Expected either 'true' or 'false'" 118 | echo "Got '${{ inputs.verbose }}'" 119 | exit 1 120 | fi 121 | 122 | if [ -n "${{ inputs.config }}" ]; then 123 | SOURCERY_OPTIONS+=( "--config ${{ inputs.config }}" ) 124 | fi 125 | 126 | if [ -n "${{ inputs.diff_ref }}" ]; then 127 | SOURCERY_OPTIONS+=( "--diff=git diff ${{ inputs.diff_ref }}" ) 128 | fi 129 | 130 | sourcery review "${SOURCERY_OPTIONS[@]}" ${{ inputs.target }} 131 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sourcery GitHub Action 2 | 3 | ## Overview 4 | 5 | Run [Sourcery](https://sourcery.ai/) as a GitHub Action that you can easily add to your 6 | Continuous Integration. 7 | 8 | This action performs the following operations: 9 | 10 | * Installs Sourcery on your worker machine 11 | * Logs in to Sourcery using your access token 12 | * Reviews your code 13 | 14 | > **Note**: the Sourcery GitHub Action is FREE for open-source projects. 15 | 16 | > **Note**: for private repositories, a 17 | > [TEAM subscription](https://docs.sourcery.ai/Product/Plans/#team) is required. 18 | > Check out our [pricing page](https://sourcery.ai/pricing/) to sign up. 19 | 20 | ## Usage 21 | 22 | ### Check your entire codebase 23 | 24 | To run Sourcery over your entire codebase whenever a push is done to the `main` branch: 25 | 26 | ```yaml 27 | name: Check codebase using Sourcery 28 | 29 | on: 30 | push: 31 | branches: [main] 32 | 33 | jobs: 34 | review-with-sourcery: 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v3 38 | 39 | - uses: actions/setup-python@v4 40 | with: 41 | python-version: '3.10' 42 | 43 | - uses: sourcery-ai/action@v1 44 | with: 45 | token: ${{ secrets.SOURCERY_TOKEN }} 46 | ``` 47 | 48 | We recommend you store your Sourcery token as a 49 | [GitHub secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets) on 50 | your repository. In the example above, that secret was stored as `SOURCERY_TOKEN`. 51 | 52 | You can retrieve your Sourcery token from your 53 | [dashboard](https://sourcery.ai/dashboard/profile). 54 | 55 | ### Check your PR 56 | 57 | To run Sourcery only on the changed code in a PR: 58 | 59 | ```yaml 60 | name: Check PR using Sourcery 61 | 62 | on: pull_request 63 | 64 | jobs: 65 | review-with-sourcery: 66 | runs-on: ubuntu-latest 67 | steps: 68 | - uses: actions/checkout@v3 69 | with: 70 | fetch-depth: 0 71 | 72 | - uses: actions/setup-python@v4 73 | with: 74 | python-version: '3.10' 75 | 76 | - uses: sourcery-ai/action@v1 77 | with: 78 | token: ${{ secrets.SOURCERY_TOKEN }} 79 | diff_ref: ${{ github.event.pull_request.base.sha }} 80 | ``` 81 | 82 | Note here that we pass the `fetch-depth: 0` option to 83 | [`actions/checkout@v3`](https://github.com/actions/checkout). This is necessary because 84 | Sourcery needs access to both the current branch and the main branch in order to compute 85 | diffs. 86 | 87 | ## Inputs 88 | 89 | ### `token` 90 | 91 | > **Type**: string 92 | > 93 | > **CLI equivalent**: `--token ` 94 | 95 | A Sourcery token. You can retrieve yours at the 96 | [Sourcery dashboard](https://sourcery.ai/dashboard/profile). 97 | 98 | We recommend you to store your token as a 99 | [GitHub secret](https://docs.github.com/en/actions/security-guides/encrypted-secrets). 100 | 101 | ### `target` 102 | 103 | > **Type**: string 104 | > 105 | > **Default**: `.` 106 | > 107 | > **CLI equivalent**: `--target ` 108 | 109 | File(s) or directory(ies) for Sourcery to review. 110 | 111 | This defaults to `"."`, meaning that Sourcery will review your entire project. 112 | 113 | To review only the directories `dir1/` and `dir2/` along with the files `file1.py` and 114 | `file2.py`, pass them as space-separated strings: 115 | 116 | ```yaml 117 | - uses: sourcery-ai/action@v1 118 | with: 119 | token: ${{ secrets.SOURCERY_TOKEN }} 120 | target: dir1 dir2 file1.py file2.py 121 | ``` 122 | 123 | ### `version` 124 | 125 | > **Type**: string 126 | > 127 | > **Default**: The latest Sourcery version available 128 | > 129 | > **CLI equivalent**: None 130 | 131 | The Sourcery CLI version to use. 132 | 133 | This defaults to the latest version available. 134 | 135 | To choose a specific version (let's say `v1.5.0`): 136 | 137 | ```yaml 138 | - uses: sourcery-ai/action@v1 139 | with: 140 | token: ${{ secrets.SOURCERY_TOKEN }} 141 | version: 1.5.0 142 | ``` 143 | 144 | We recommend you _not_ to this option unless strictly necessary. Pinning a Sourcery 145 | version may leave you out from the awesome features we are working on - as well as 146 | bugfixes ;) 147 | 148 | ### `verbose` 149 | 150 | > **Type**: either `true` or `false` 151 | > 152 | > **Default**: `true` 153 | > 154 | > **CLI equivalent**: `--verbose` 155 | 156 | Enable or disable Sourcery's verbose output. 157 | 158 | ### `check` 159 | 160 | > **Type**: either `true` or `false` 161 | > 162 | > **Default**: `true` 163 | > 164 | > **CLI equivalent**: `--check` 165 | 166 | Whether Sourcery should return an error code or not if issues are found in the code. 167 | 168 | This defaults to `true`, and hence the Sourcery CI step will fail in case Sourcery finds 169 | issues in your code. You can pass `false` to prevent that behavior. 170 | 171 | ### `fix` 172 | 173 | > **Type**: either `true` or `false` 174 | > 175 | > **Default**: `false` 176 | > 177 | > **CLI equivalent**: `--fix` 178 | 179 | Whether Sourcery should automatically fix and modify the reviewed files in-place or not. 180 | 181 | ### `config` 182 | 183 | > **Type**: string 184 | > 185 | > **Default**: None 186 | > 187 | > **CLI equivalent**: `--config ` 188 | 189 | Path to a valid Sourcery configuration file. 190 | 191 | By default, this searches the current directory and its parents for a `.sourcery.yaml` 192 | file. 193 | 194 | To use a configuration file located elsewhere (for instance, at 195 | **`config_files/.my-team-rules.yaml`**): 196 | 197 | ```yaml 198 | - uses: sourcery-ai/action@v1 199 | with: 200 | token: ${{ secrets.SOURCERY_TOKEN }} 201 | config: config_files/.my-team-rules.yaml 202 | ``` 203 | 204 | ### `diff_ref` 205 | 206 | > **Type**: string 207 | > 208 | > **Default**: None 209 | > 210 | > **CLI equivalent**: `--diff="git diff "` 211 | 212 | A reference to compute a `git diff`. 213 | 214 | This is not used by default. In this case, Sourcery will run over the entire 215 | [`target`](#target) files. 216 | 217 | To run Sourcery only on the changed lines in a PR, use: 218 | 219 | ```yaml 220 | - uses: sourcery-ai/action@v1 221 | with: 222 | token: ${{ secrets.SOURCERY_TOKEN }} 223 | diff_ref: ${{ github.event.pull_request.base.sha }} 224 | ``` 225 | --------------------------------------------------------------------------------