-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-package-format-and-pr.yml
More file actions
181 lines (179 loc) · 6.61 KB
/
python-package-format-and-pr.yml
File metadata and controls
181 lines (179 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
---
name: Formatting for Python
on:
workflow_call:
inputs:
package-path:
required: false
type: string
description: Path to a Python package or project
default: .
python-version:
required: false
type: string
description: Python version to use (not applicable if uv.lock is present)
default: 3.x
uv-version:
required: false
type: string
description: Version of uv to use (applicable only if uv.lock is present)
default: latest
use-black:
required: false
type: boolean
description: Use black to format the code
default: false
use-isort:
required: false
type: boolean
description: Use isort to sort import definitions
default: false
lint-before-pr:
required: false
type: boolean
description: Run lint before creating a pull request
default: true
requirements-txt:
required: false
type: string
description: Path to the requirements.txt file (not applicable if uv.lock is present)
default: null
runs-on:
required: false
type: string
description: GitHub Actions runner to use
default: ubuntu-slim
secrets:
GH_TOKEN:
required: false
description: GitHub token
permissions:
contents: write
pull-requests: write
defaults:
run:
shell: bash -euo pipefail {0}
working-directory: .
jobs:
format-and-pr:
runs-on: ${{ inputs.runs-on }}
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 1
persist-credentials: false
- name: Detect uv.lock or poetry.lock
working-directory: ${{ inputs.package-path }}
run: |
if [[ -f uv.lock ]]; then
echo "LOCK_FILE=uv.lock" >> "${GITHUB_ENV}"
elif [[ -f poetry.lock ]]; then
echo "LOCK_FILE=poetry.lock" >> "${GITHUB_ENV}"
else
echo "LOCK_FILE=" >> "${GITHUB_ENV}"
fi
- name: Set up uv
if: env.LOCK_FILE == 'uv.lock'
uses: astral-sh/setup-uv@cec208311dfd045dd5311c1add060b2062131d57 # v8.0.0
with:
version: ${{ inputs.uv-version }}
- name: Install packages using uv
if: env.LOCK_FILE == 'uv.lock'
working-directory: ${{ inputs.package-path }}
run: |
uv sync --all-groups
uv add --dev \
${{ inputs.use-black && 'black' || 'ruff' }} \
${{ inputs.use-isort && 'isort' || '' }}
tee -a "${GITHUB_ENV}" <<< "EXECUTOR=uv run --directory ${PWD}"
- name: Set up Python
if: env.LOCK_FILE != 'uv.lock'
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: ${{ inputs.python-version }}
- name: Install packages
if: env.LOCK_FILE != 'uv.lock'
env:
REQUIREMENTS_TXT_PATH: ${{ inputs.requirements-txt }}
working-directory: ${{ inputs.package-path }}
run: |
pip install -U --no-cache-dir pip
if [[ -n "${REQUIREMENTS_TXT_PATH}" ]]; then
pip install -U --no-cache-dir -r "${REQUIREMENTS_TXT_PATH}"
fi
if [[ "${LOCK_FILE}" == "poetry.lock" ]]; then
pip install --no-cache-dir poetry
poetry lock --no-interaction
poetry add --group=dev --no-interaction \
${{ inputs.use-black && 'black' || 'ruff' }} \
${{ inputs.use-isort && 'isort' || '' }}
poetry install --no-interaction
tee -a "${GITHUB_ENV}" <<< "EXECUTOR=poetry run -C ${PWD}"
else
pip install --no-cache-dir \
${{ inputs.use-black && 'black' || 'ruff' }} \
${{ inputs.use-isort && 'isort' || '' }}
tee -a "${GITHUB_ENV}" <<< "EXECUTOR="
fi
- name: Format the code using ruff
if: (! inputs.use-black)
working-directory: ${{ inputs.package-path }}
run: > # zizmor: ignore[template-injection]
${{ env.EXECUTOR }} ruff format .
- name: Format the code using black
if: inputs.use-black
working-directory: ${{ inputs.package-path }}
run: > # zizmor: ignore[template-injection]
${{ env.EXECUTOR }} black .
- name: Sort Python import definitions using isort
if: inputs.use-isort
working-directory: ${{ inputs.package-path }}
run: > # zizmor: ignore[template-injection]
${{ env.EXECUTOR }} isort .
- name: Lint the code using ruff
if: inputs.lint-before-pr
working-directory: ${{ inputs.package-path }}
run: > # zizmor: ignore[template-injection]
${{ env.EXECUTOR }} ruff check --fix --output-format=github .
- name: Restore project files
if: startsWith(env.EXECUTOR, 'poetry') || startsWith(env.EXECUTOR, 'uv')
working-directory: ${{ inputs.package-path }}
run: |
git restore pyproject.toml "${LOCK_FILE}"
- name: Create output parameters
id: parameters
env:
USE_BLACK: ${{ inputs.use-black }}
USE_ISORT: ${{ inputs.use-isort }}
PR_BASE: ${{ github.head_ref || github.ref_name }}
run: |
if ${USE_BLACK}; then
formatters='black'
else
formatters='ruff'
fi
if ${USE_ISORT}; then
formatters="${formatters} and isort"
fi
{
echo "commit_message=Reformat Python code using ${formatters}"
echo "pr_base=${PR_BASE}"
echo "pr_branch=automatedpr/${PR_BASE}"
} >> "${GITHUB_OUTPUT}"
- name: Detect changed files and create a pull request
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
with:
token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} # zizmor: ignore[secrets-outside-env] caller-provided secret
commit-message: ${{ steps.parameters.outputs.commit_message }}
title: ${{ steps.parameters.outputs.commit_message }}
branch: ${{ steps.parameters.outputs.pr_branch }}
base: ${{ steps.parameters.outputs.pr_base }}
labels: automated pr
body: |
This PR contains the following updates:
- ${{ steps.parameters.outputs.commit_message }}
This PR has been generated by
[${{ github.workflow_ref }}](https://github.com/${{ github.repository }}/).
delete-branch: true
draft: false