Skip to content

Commit 1062f55

Browse files
committed
ci: Enforce Ruff lint and formatting
1 parent 809722c commit 1062f55

72 files changed

Lines changed: 3699 additions & 3283 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/lint.yml‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Lint
2+
3+
env:
4+
PYTHON_VERSION: "3.12"
5+
6+
# Report on every PR, including changes outside the Python test paths.
7+
# Required checks need a result even when a PR changes no Python files.
8+
on:
9+
push:
10+
branches: [main]
11+
pull_request:
12+
workflow_dispatch:
13+
14+
permissions:
15+
contents: read
16+
17+
concurrency:
18+
group: lint-${{ github.event.pull_request.number || github.ref }}
19+
cancel-in-progress: true
20+
21+
jobs:
22+
ruff:
23+
runs-on: ubuntu-latest
24+
timeout-minutes: 10
25+
steps:
26+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
27+
with:
28+
fetch-depth: 1
29+
persist-credentials: false
30+
- name: 🐍 setup python
31+
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
32+
with:
33+
python-version: ${{ env.PYTHON_VERSION }}
34+
- name: 🛠️ install deps
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install uv
38+
uv sync --locked --extra dev
39+
- name: 🧹 ruff check
40+
run: uv run --locked --extra dev ruff check
41+
- name: 🎨 ruff format
42+
run: uv run --locked --extra dev ruff format --check

‎.github/workflows/package-check.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: python -m pip install ".[test]" "twine>=4.0.0"
3535

3636
- name: Run unit tests
37-
run: python -m pytest -q tests/unit
37+
run: python -m pytest -q tests/unit tests/integration/test_all_endpoints.py::TestAllEndpointsMocked
3838

3939
- name: Build distributions
4040
run: hatch build

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dist
1515
*.dist
1616
*.egg-info
1717
*.cpython-312.pyc
18-
example-socket-export.py
1918
__pycache__/
2019
.coverage
2120
.coverage.*

‎.hooks/sync_version.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python3
2-
import subprocess
2+
import json
33
import pathlib
44
import re
5+
import subprocess
56
import sys
67
import urllib.request
7-
import json
88

99
VERSION_FILE = pathlib.Path("socketdev/version.py")
1010
PYPROJECT_FILE = pathlib.Path("pyproject.toml")
@@ -16,6 +16,7 @@
1616
PYPI_PROD_API = "https://pypi.org/pypi/socketdev/json"
1717
PYPI_TEST_API = "https://test.pypi.org/pypi/socketdev/json"
1818

19+
1920
def read_version_from_version_file(path: pathlib.Path) -> str:
2021
content = path.read_text()
2122
match = VERSION_PATTERN.search(content)
@@ -24,6 +25,7 @@ def read_version_from_version_file(path: pathlib.Path) -> str:
2425
sys.exit(1)
2526
return match.group(1)
2627

28+
2729
def read_version_from_git(path: str) -> str:
2830
try:
2931
output = subprocess.check_output(["git", "show", f"HEAD:{path}"], text=True)
@@ -34,13 +36,15 @@ def read_version_from_git(path: str) -> str:
3436
except subprocess.CalledProcessError:
3537
return None
3638

39+
3740
def bump_patch_version(version: str) -> str:
3841
if ".dev" in version:
3942
version = version.split(".dev")[0]
4043
parts = version.split(".")
4144
parts[-1] = str(int(parts[-1]) + 1)
4245
return ".".join(parts)
4346

47+
4448
def parse_stable_version(version: str):
4549
if not STABLE_VERSION_PATTERN.fullmatch(version):
4650
return None
@@ -188,16 +192,21 @@ def main():
188192
inject_version(new_version)
189193
uv_lock_changed = run_uv_lock()
190194
lock_hint = " and uv.lock" if uv_lock_changed else ""
191-
print(f"⚠️ Version {current_version} is already published on PyPI — auto-bumped to {new_version}. Please git add{lock_hint} + commit again.")
195+
print(
196+
f"⚠️ Version {current_version} is already published on PyPI — auto-bumped to {new_version}. Please git add{lock_hint} + commit again."
197+
)
192198
sys.exit(1)
193199

194200
uv_lock_changed = run_uv_lock()
195201
if uv_lock_changed:
196-
print("⚠️ Version already bumped, but uv.lock was out of date and has been updated. Please git add uv.lock + commit again.")
202+
print(
203+
"⚠️ Version already bumped, but uv.lock was out of date and has been updated. Please git add uv.lock + commit again."
204+
)
197205
sys.exit(1)
198206

199207
print("✅ Version already bumped and uv.lock is up to date — proceeding.")
200208
sys.exit(0)
201209

210+
202211
if __name__ == "__main__":
203212
main()

‎.pre-commit-config.yaml‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
5+
# Use the project's locked Ruff version, shared with CI and make lint.
6+
- id: ruff-check
7+
name: ruff check
8+
entry: uv run --locked --extra dev ruff check --force-exclude --fix
9+
language: system
10+
types_or: [python, pyi]
11+
require_serial: true
12+
13+
- id: ruff-format
14+
name: ruff format
15+
entry: uv run --locked --extra dev ruff format --force-exclude
16+
language: system
17+
types_or: [python, pyi]
18+
require_serial: true

‎CHANGELOG.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# Changelog
22

3+
## 3.6.3
4+
5+
### Changed
6+
7+
- Enforce Ruff linting and formatting in CI and pre-commit, using the locked
8+
development tool version. Add `make lint`, `make lint-fix`, `make format`,
9+
and `make hooks` for contributors.
10+
- Bound function complexity at 12 independent paths and eight arguments, with
11+
explicit exceptions for existing functions and checks for obsolete suppressions.
12+
- Full-scan parameter normalization uses debug logging instead of stdout.
13+
- Refresh mocked endpoint tests for the supported API methods and include them
14+
in the default local and CI test runs.
15+
16+
### Fixed
17+
18+
- Repository updates correctly serialize keyword arguments into the request body.
19+
- Report uploads close files opened from paths after the request, including on
20+
failure. Streams supplied by callers remain open.
21+
- Lazy-file validation remains active under `python -O`.
22+
323
## 3.6.2
424

525
### Fixed: reject retired API credentials before use

‎CONTRIBUTING.md‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,43 @@
22

33
## Development setup
44

5-
Use Python 3.9 or newer. Create and activate a virtual environment, then
6-
install the package with its development and test dependencies:
5+
Use Python 3.10 or newer and install [uv](https://docs.astral.sh/uv/). From the
6+
repository root, install the locked development and test dependencies:
77

88
```bash
9-
python -m venv .venv
10-
source .venv/bin/activate
11-
python -m pip install -e ".[dev,test]"
9+
uv sync --locked --extra dev --extra test
10+
make hooks
1211
```
1312

14-
Before opening a pull request, run the focused checks for your change. The
15-
complete local check is:
13+
Before opening a pull request, run:
1614

1715
```bash
18-
python -m pytest
19-
hatch build
20-
python -m twine check dist/*
16+
make lint
17+
make test
18+
uv run --locked hatch build
19+
uv run --locked python -m twine check dist/*
2120
```
2221

22+
## Linting and formatting
23+
24+
Ruff checks Python source, tests, hooks, examples, and developer scripts. The
25+
rule set lives in `pyproject.toml`; CI, Make targets, and pre-commit use the same
26+
Ruff version from `uv.lock`.
27+
28+
`make lint-fix` applies safe lint fixes. `make format` formats files with a line
29+
length of 120. Pre-commit applies both to staged Python files. Review and stage
30+
any edits before committing again. CI checks the full repository on every pull
31+
request and push to `main`.
32+
33+
Functions are limited to cyclomatic complexity 12 and eight arguments. Existing
34+
over-limit functions carry explicit suppressions; split new functions that exceed
35+
the limits. `RUF100` rejects suppressions that become unnecessary. Avoid running
36+
`ruff check --fix` with a narrow `--select` that includes `RUF100`, because it
37+
can remove markers for rules outside that selection.
38+
39+
Integration tests requiring live credentials are opt-in; see
40+
[tests/README.md](tests/README.md). The default `make test` runs unit tests and mocked endpoint tests.
41+
2342
## Pull request validation
2443

2544
The `Package Check` workflow runs automatically for pull requests. It runs the

‎Makefile‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.PHONY: lint lint-fix format format-check hooks test
2+
3+
lint:
4+
uv run --locked --extra dev ruff check
5+
uv run --locked --extra dev ruff format --check
6+
7+
lint-fix:
8+
uv run --locked --extra dev ruff check --fix
9+
10+
format:
11+
uv run --locked --extra dev ruff format
12+
13+
format-check:
14+
uv run --locked --extra dev ruff format --check
15+
16+
hooks:
17+
uv run --locked --extra dev pre-commit install
18+
19+
test:
20+
uv run --locked --extra test pytest tests/unit tests/integration/test_all_endpoints.py::TestAllEndpointsMocked

‎example-socket-export.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import json
2-
import os
32
import logging
3+
import os
4+
import sys
5+
46
from socketdev import socketdev
7+
58
logging.basicConfig(level=logging.INFO)
69

710

@@ -11,13 +14,13 @@
1114
if len(orgs) > 0:
1215
org_id = None
1316
org_slug = None
14-
for org_key in orgs['organizations']:
15-
org = orgs['organizations'][org_key]
17+
for org_key in orgs["organizations"]:
18+
org = orgs["organizations"][org_key]
1619
org_id = org_key
17-
org_slug = org['slug']
20+
org_slug = org["slug"]
1821
else:
1922
print("Something went wrong with getting org info")
20-
exit(1)
23+
sys.exit(1)
2124
per_page = 100
2225
response = sdk.repos.get(org_slug, per_page=per_page)
2326
next_page = response.get("nextPage")
@@ -47,7 +50,7 @@
4750
"pull_request_url": full_scan_metadata.get("pull_request_url"),
4851
"committers": full_scan_metadata.get("committers"),
4952
"created_at": full_scan_metadata.get("created_at"),
50-
"results": sdk.fullscans.stream(org_slug=org_slug, full_scan_id=head_full_scan_id) or None
53+
"results": sdk.fullscans.stream(org_slug=org_slug, full_scan_id=head_full_scan_id) or None,
5154
}
5255
socket_results[head_full_scan_id] = full_scan_result
5356

0 commit comments

Comments
 (0)