-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaction.yml
226 lines (207 loc) · 8.85 KB
/
action.yml
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
name: Python Lint Code Scanning Action
description: Runs a python linter of your choice, outputting SARIF
branding:
icon: 'check-circle'
color: 'blue'
inputs:
linter:
description: 'The linter to use'
required: true
default: 'flake8'
choices: ['ruff', 'flake8', 'pylint', 'mypy', 'pyright', 'pytype', 'fixit', 'pyre']
target:
description: 'The target to lint'
required: true
default: ${{ github.workspace }}
output:
description: 'The output file'
required: false
default: 'python_linter.sarif'
python-version:
description: 'The version of python to use'
required: false
default: '3.10'
choices: ['3.12', '3.11', '3.10', '3.9', '3.8']
ruff-version:
description: 'The version of ruff to use'
required: false
default: 'latest'
flake8-version:
description: 'The version of flake8 to use'
required: false
default: 'latest'
pylint-version:
description: 'The version of pylint to use'
required: false
default: 'latest'
mypy-version:
description: 'The version of mypy to use'
required: false
default: 'latest'
pyright-version:
description: 'The version of pyright to use'
required: false
default: 'latest'
pytype-version:
description: 'The version of pytype to use'
required: false
default: 'latest'
fixit-version:
description: 'The version of fixit to use'
required: false
default: 'latest'
pyre-version:
description: 'The version of pyre to use'
required: false
default: 'latest'
typeshed-version:
description: 'The version of typeshed to use'
required: false
default: 'main'
runs:
using: 'composite'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
id: setup-python
uses: actions/setup-python@v4
with:
python-version: ${{ inputs.python-version }}
- name: Run Python Lint
run: |
echo "::debug::Running Python Lint for ${INPUTS_LINTER} on ${INPUTS_TARGET} with Python ${INPUTS_PYTHON_VERSION}"
# set python command
if [[ "${OSTYPE}" == "msys" ]]; then
PYTHON_CMD=python
else
PYTHON_CMD="python${INPUTS_PYTHON_VERSION}"
fi
# upgrade pip
"${PYTHON_CMD}" -mpip install --upgrade pip
# set up linter variables
linters=('ruff' 'flake8' 'pylint' 'mypy' 'pyright' 'pytype' 'fixit' 'pyre')
install_flake8_formatter_linters=('ruff', 'flake8')
install_typeshed_linters=('pyre', 'pytype', 'mypy', 'pyright')
EXTRA_PIP_FLAGS=''
LINTER_VERSION_CONSTRAINT=''
EXTRA_LINTER_SCRIPT_FLAGS=''
# check we're using a valid linter
if [[ "${linters[*]}" =~ (^|[^[:alpha:]])${INPUTS_LINTER}([^[:alpha:]]|$) ]]; then
# collect version choices, if they've been made
if [[ "${INPUTS_LINTER}" == "fixit" ]]; then
if [[ "${INPUTS_FIXIT_VERSION}" == "latest" ]]; then
LINTER_VERSION_CONSTRAINT=' >1'
else
LINTER_VERSION_CONSTRAINT="==${INPUTS_FIXIT_VERSION}"
fi
EXTRA_PIP_FLAGS=" --pre"
else
if [[ "${INPUTS_LINTER}" == "ruff" ]]; then
if [[ "${INPUTS_RUFF_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_RUFF_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "flake8" ]]; then
if [[ "${INPUTS_FLAKE8_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_FLAKE8_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "pylint" ]]; then
if [[ "${INPUTS_PYLINT_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_PYLINT_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "mypy" ]]; then
if [[ "${INPUTS_MYPY_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_MYPY_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "pyright" ]]; then
if [[ "${INPUTS_PYRIGHT_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_PYRIGHT_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "pytype" ]]; then
if [[ "${INPUTS_PYTYPE_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_PYTYPE_VERSION}"
fi
elif [[ "${INPUTS_LINTER}" == "pyre" ]]; then
if [[ "${INPUTS_PYRE_VERSION}" != "latest" ]]; then
LINTER_VERSION_CONSTRAINT="==${INPUTS_PYRE_VERSION}"
fi
fi
fi
# deal with HomeBrew managed Python on GitHub Hosted MacOS runners
if [[ "${RUNNER_OS}" == "macOS" ]]; then
EXTRA_PIP_FLAGS="${EXTRA_PIP_FLAGS} --break-system-packages"
fi
echo "::debug::Installing ${INPUTS_LINTER}${LINTER_VERSION_CONSTRAINT} for Python ${INPUTS_PYTHON_VERSION}"
# install linter
LINTER_PACKAGE="${INPUTS_LINTER}"
if [[ "${INPUTS_LINTER}" == "pyre" ]]; then
LINTER_PACKAGE="pyre-check"
fi
if ! "${PYTHON_CMD}" -mpip install -q "${LINTER_PACKAGE}${LINTER_VERSION_CONSTRAINT}" ${EXTRA_PIP_FLAGS}; then
echo "::error::${LINTER_PACKAGE}${LINTER_VERSION_CONSTRAINT} failed to install for Python ${INPUTS_PYTHON_VERSION}"
# if it is fixit on 3.7, just exit 0, we know it's not available
if [[ "${INPUTS_LINTER}" == "fixit" && "${INPUTS_PYTHON_VERSION}" == "3.7" ]]; then
exit 0
fi
exit 1
fi
# install flake8-sarif-formatter if needed
if [[ "${install_flake8_formatter_linters[*]}" =~ (^|[^[:alpha:]])${INPUTS_LINTER}([^[:alpha:]]|$) ]]; then
echo "::debug::Installing flake8_sarif_formatter for ${INPUTS_LINTER}"
"${PYTHON_CMD}" -mpip install -q flake8-sarif-formatter || ( echo "::error::flake8-sarif-formatter failed to install for Python ${INPUTS_PYTHON_VERSION}" && exit 1 )
fi
# set debug output
if [[ "${RUNNER_DEBUG}" == "1" ]]; then
EXTRA_LINTER_SCRIPT_FLAGS=" --debug"
fi
# install typeshed if needed (for typecheckers)
if [[ "${install_typeshed_linters[*]}" =~ (^|[^[:alpha:]])${INPUTS_LINTER}([^[:alpha:]]|$) ]]; then
echo "::debug::Installing typeshed for ${INPUTS_LINTER}"
# clone from GitHub
(
cd ${RUNNER_TEMP}
gh repo clone python/typeshed -- --depth 1 --branch "${INPUTS_TYPESHED_VERSION}" || ( echo "::error::typeshed failed to install for Python ${INPUTS_PYTHON_VERSION}" && exit 1 )
)
EXTRA_LINTER_SCRIPT_FLAGS+=" --typeshed-path=${RUNNER_TEMP}/typeshed"
fi
# run linter
if ! "${PYTHON_CMD}" "${GITHUB_ACTION_PATH}"/python_lint.py "${INPUTS_LINTER}" --target="${INPUTS_TARGET}" --output="${GITHUB_WORKSPACE}/${INPUTS_OUTPUT}" ${EXTRA_LINTER_SCRIPT_FLAGS}; then
# don't fail "hard" if it's known failures that we cannot account for (yet)
# pytype doesn't support 3.11+ yet
if [[ "${INPUTS_LINTER}" == "pytype" && "${INPUTS_PYTHON_VERSION}" =~ ^3\.(1[1-9]|[2-9][0-9])$ ]]; then
echo "::error::pytype failed to run for Python ${INPUTS_PYTHON_VERSION}; this is likely due to pytype not yet supporting Python ${INPUTS_PYTHON_VERSION}"
exit 0
fi
echo "::error::${INPUTS_LINTER} failed to run for Python ${INPUTS_PYTHON_VERSION}" && exit 1
fi
else
echo "::error::invalid linter ${INPUTS_LINTER}; choose one of ${linters[*]}"
exit 1
fi
env:
INPUTS_LINTER: ${{ inputs.linter }}
INPUTS_TARGET: ${{ inputs.target }}
INPUTS_OUTPUT: ${{ inputs.output }}
INPUTS_PYTHON_VERSION: ${{ inputs.python-version }}
INPUTS_RUFF_VERSION: ${{ inputs.ruff-version }}
INPUTS_FLAKE8_VERSION: ${{ inputs.flake8-version }}
INPUTS_PYLINT_VERSION: ${{ inputs.pylint-version }}
INPUTS_MYPY_VERSION: ${{ inputs.mypy-version }}
INPUTS_PYRIGHT_VERSION: ${{ inputs.pyright-version }}
INPUTS_PYTYPE_VERSION: ${{ inputs.pytype-version }}
INPUTS_FIXIT_VERSION: ${{ inputs.fixit-version }}
INPUTS_PYRE_VERSION: ${{ inputs.pyre-version }}
INPUTS_TYPESHED_VERSION: ${{ inputs.typeshed-version }}
GH_TOKEN: ${{ github.token }}
shell: bash
- name: Upload SARIF
if: ${{ hashFiles(inputs.output) != '' }}
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ inputs.output }}
- name: Upload SARIF as debug artefact
if: ${{ always() && runner.debug == '1' && hashFiles(inputs.output) != '' }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.output }}
path: ${{ inputs.output }}