Skip to content

Commit e5446e0

Browse files
authored
Merge pull request #8 from advanced-security/fix-typeshed-and-token
Fix typeshed and token
2 parents 6cac776 + d064d54 commit e5446e0

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

action.yml

+13-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,11 @@ runs:
164164
if [[ "${install_typeshed_linters[*]}" =~ (^|[^[:alpha:]])${INPUTS_LINTER}([^[:alpha:]]|$) ]]; then
165165
echo "::debug::Installing typeshed for ${INPUTS_LINTER}"
166166
# clone from GitHub
167-
gh repo clone python/typeshed -- --depth 1 --branch "${INPUTS_TYPESHED_VERSION}" "${GITHUB_WORKSPACE}/typeshed" || ( echo "::error::typeshed failed to install for Python ${INPUTS_PYTHON_VERSION}" && exit 1 )
168-
EXTRA_LINTER_SCRIPT_FLAGS+=" --typeshed-path=${GITHUB_WORKSPACE}/typeshed"
167+
(
168+
cd ${RUNNER_TEMP}
169+
gh repo clone python/typeshed -- --depth 1 --branch "${INPUTS_TYPESHED_VERSION}" || ( echo "::error::typeshed failed to install for Python ${INPUTS_PYTHON_VERSION}" && exit 1 )
170+
)
171+
EXTRA_LINTER_SCRIPT_FLAGS+=" --typeshed-path=${RUNNER_TEMP}/typeshed"
169172
fi
170173
171174
# run linter
@@ -197,9 +200,17 @@ runs:
197200
INPUTS_FIXIT_VERSION: ${{ inputs.fixit-version }}
198201
INPUTS_PYRE_VERSION: ${{ inputs.pyre-version }}
199202
INPUTS_TYPESHED_VERSION: ${{ inputs.typeshed-version }}
203+
GH_TOKEN: ${{ github.token }}
200204
shell: bash
201205
- name: Upload SARIF
202206
if: ${{ hashFiles(inputs.output) != '' }}
203207
uses: github/codeql-action/upload-sarif@v2
204208
with:
205209
sarif_file: ${{ inputs.output }}
210+
- name: Upload SARIF as debug artefact
211+
if: ${{ always() && runner.debug == '1' && hashFiles(inputs.output) != '' }}
212+
uses: actions/upload-artifact@v3
213+
with:
214+
name: ${{ inputs.output }}
215+
path: ${{ inputs.output }}
216+

python_lint.py

+30-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def make_sarif_run(tool_name: str) -> dict:
5555
return sarif_run
5656

5757

58-
def flake8_linter(target: Path, *args) -> None:
58+
def flake8_linter(target: Path, *_args) -> None:
5959
"""Run the flake8 linter.
6060
6161
In contrast to the other linters, flake8 has plugin architecture.
@@ -155,7 +155,7 @@ def ruff_format_sarif(results: List[Dict[str, Any]], target: Path) -> dict:
155155
return sarif_run
156156

157157

158-
def ruff_linter(target: Path, *args) -> Optional[dict]:
158+
def ruff_linter(target: Path, *_args) -> Optional[dict]:
159159
"""Run the ruff linter."""
160160
try:
161161
# pylint: disable=import-outside-toplevel
@@ -257,7 +257,7 @@ def pylint_format_sarif(results: List[Dict[str, Any]], target: Path) -> dict:
257257
return sarif_run
258258

259259

260-
def pylint_linter(target: Path, *args) -> Optional[dict]:
260+
def pylint_linter(target: Path, *_args) -> Optional[dict]:
261261
"""Run the pylint linter."""
262262
process = run(
263263
["pylint", "--output-format=json", "--recursive=y", target.absolute().as_posix()],
@@ -680,7 +680,7 @@ def fixit_format_sarif(results: str, target: Path) -> dict:
680680
return sarif_run
681681

682682

683-
def fixit_linter(target: Path) -> Optional[dict]:
683+
def fixit_linter(target: Path, *_args) -> Optional[dict]:
684684
"""Run the fixit linter, from Meta."""
685685
process = run(["fixit", "lint", target.absolute().as_posix()], capture_output=True, check=False)
686686

@@ -712,6 +712,31 @@ def make_paths_relative_to_target(runs: List[dict], target: Path) -> None:
712712
)
713713

714714

715+
def fix_sarif_locations(runs: List[dict]) -> None:
716+
"""Fix the SARIF locations.
717+
718+
Normalise values less than 1 to 1, e.g. -1 or 0.
719+
720+
Convert strings to ints.
721+
722+
For anything that can't be converted to an int, set it to 1.
723+
"""
724+
for sarif_run in runs:
725+
for result in sarif_run["results"]:
726+
for location in result["locations"]:
727+
region = location["physicalLocation"]["region"]
728+
for key in ("startLine", "endLine", "startColumn", "endColumn"):
729+
if key in region:
730+
try:
731+
region[key] = int(region[key])
732+
except ValueError:
733+
LOG.error("Unable to convert %s to int", region[key])
734+
region[key] = 1
735+
continue
736+
if region[key] < 1:
737+
region[key] = 1
738+
739+
715740
LINTERS = {
716741
"pylint": pylint_linter,
717742
"ruff": ruff_linter,
@@ -751,7 +776,7 @@ def main() -> None:
751776
sarif_runs: List[dict] = []
752777

753778
target = Path(args.target).resolve().absolute()
754-
typeshed_path = Path(args.typeshed_path).resolve().absolute()
779+
typeshed_path = Path(args.typeshed_path).resolve().absolute() if args.typeshed_path is not None else None
755780

756781
for linter in args.linter:
757782
LOG.debug("Running %s", linter)

0 commit comments

Comments
 (0)