Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: migrate to a modern Python stack #460

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
157d614
migrate from setuptools to hatch with `hatch new --init`
lvaylet May 15, 2024
86a8b3b
add maintainers
lvaylet May 15, 2024
4c4c09c
set static project version
lvaylet May 15, 2024
69af1f6
fix license codename
lvaylet May 15, 2024
e1b3a73
update supported Python versions based on current Python release cycle
lvaylet May 15, 2024
d7ad0fe
remove unused field, now that project version is set statically
lvaylet May 15, 2024
5b4dc8b
remove unused dependency on `setuptools`
lvaylet May 15, 2024
5d00349
add pre-commit hook for `bandit`
lvaylet May 15, 2024
9c9a143
move package sources to src/
lvaylet May 15, 2024
f956dcf
add comments on minimum package version requirements
lvaylet May 15, 2024
1226f74
add useful project URLs
lvaylet May 15, 2024
85b22ba
configure build
lvaylet May 15, 2024
dedd1e1
configure hatch envs in `hatch.toml`
lvaylet May 15, 2024
d0c7016
rename `test` GitHub Workflow to `ci`
lvaylet May 15, 2024
4ffe7e3
list Hatch environments in CI
lvaylet May 15, 2024
ed3e596
update GitHub Actions
lvaylet May 15, 2024
672cbd0
refactor Docker tests
lvaylet May 15, 2024
8232611
fix Docker image definition and CI workflow
lvaylet May 15, 2024
7b8c005
replace Hatch matrices with GitHub matrices
lvaylet May 15, 2024
4decc49
delete setuptools config files
lvaylet May 15, 2024
bbd31e1
ruff
lvaylet May 15, 2024
f30c309
fix formatting of Hatch table
lvaylet May 15, 2024
1ce3b22
add TODOs and FIXMEs
lvaylet May 16, 2024
2ef0262
ignore Ruff cache
lvaylet May 16, 2024
8174a9c
update Makefile for Hatch
lvaylet May 16, 2024
c22038a
configure `bandit` in `pyproject.toml`
lvaylet May 16, 2024
2d57ba0
update `mypy` configuration
lvaylet May 16, 2024
991140d
configure Hatch environments
lvaylet May 16, 2024
7346a9d
update `pytype` configuration
lvaylet May 16, 2024
bf9cc8b
remove unneeded empty line
lvaylet May 16, 2024
91be9e2
format with `ruff`
lvaylet May 16, 2024
7afb0e4
ignore type linting
lvaylet May 16, 2024
a337c0a
add TODOs
lvaylet May 16, 2024
54a2595
ci: scan for CVEs in dedicated `audit` job with `bandit` and `safety`…
lvaylet May 16, 2024
fe78f27
ci: use `ruff` for linting and formatting (#466)
lvaylet May 16, 2024
cd9d514
migrate from setuptools to hatch with `hatch new --init`
lvaylet May 15, 2024
7505e24
configure build
lvaylet May 15, 2024
51f77e0
add TODOs and FIXMEs
lvaylet May 16, 2024
61ce34d
update Makefile for Hatch
lvaylet May 16, 2024
148c733
update `pytype` configuration
lvaylet May 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 82 additions & 42 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,76 +1,116 @@
name: Continous Integration (CI)
name: Continuous Integration

on:
pull_request:

jobs:
lint:
unit:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
architecture: ['x64']
python-version: ['3.8', '3.9', '3.10', '3.11']
runs-on: ${{ matrix.os }}
# TODO: Test Hatch matrices again.
# They could be as fast as concurrent GitHub jobs thanks to caching of dependencies and type check results.
# Then it is always possible to target a specific version of Python, for example with +py3.9 in the Dockerfile.
# Hatch matrices also vastly reduce the size of this workflow, with a single step for lint, test and audit.
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}

- name: Install dependencies
run: make install

- name: Lint
run: make lint
- name: Set up pip cache
if: runner.os == 'Linux'
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install Hatch
run: pipx install hatch
- name: List Hatch environments
run: hatch env show --ascii
- name: Run unit tests with coverage
run: hatch run test:unit-cov
env:
MIN_VALID_EVENTS: 10
GOOGLE_APPLICATION_CREDENTIALS: tests/unit/fixtures/fake_credentials.json

unit:
lint:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
architecture: ['x64']
python-version: ['3.8', '3.9', '3.10', '3.11']
runs-on: ${{ matrix.os }}
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.architecture }}

- name: Install dependencies
run: make install

- name: Run unit tests
run: make unit
env:
MIN_VALID_EVENTS: "10"
GOOGLE_APPLICATION_CREDENTIALS: tests/unit/fixtures/fake_credentials.json

- name: Run coverage report
run: make coverage
- name: Set up pip cache
if: runner.os == 'Linux'
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install Hatch
run: pipx install hatch
- name: List Hatch environments
run: hatch env show --ascii
- name: Lint
run: hatch run lint:all

audit:
runs-on: ubuntu-latest
# TODO: Not sure `audit` is required to run on multiple versions of Python.
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.9" # same as Dockerfile

- name: Install dependencies
run: make install

- name: Audit for CVEs
run: make audit
python-version: ${{ matrix.python-version }}
- name: Set up pip cache
if: runner.os == 'Linux'
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install Hatch
run: pipx install hatch
- name: List Hatch environments
run: hatch env show --ascii
- name: Audit
run: hatch run audit:all

docker:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v4
- uses: docker-practice/actions-setup-docker@master
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Set up pip cache
if: runner.os == 'Linux'
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Install Hatch
run: pipx install hatch
- name: List Hatch environments
run: hatch env show --ascii
- name: Build
run: hatch build

docker:
runs-on: ubuntu-latest
steps:
- uses: docker/build-push-action@v5
- name: Build Docker image
run: make docker_build
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# FIXME: Clean up unused entries and sort by categories
__pycache__/
*.py[cod]
*$py.class
Expand Down Expand Up @@ -50,4 +51,4 @@ reports/
.pytest_cache/
.pytype/
.pip-wheel-metadata/
.ruff_cache
.ruff_cache/
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ repos:
- id: ruff
# Run the formatter.
- id: ruff-format
- repo: https://github.com/PyCQA/bandit
rev: 1.7.8
hooks:
- id: bandit
70 changes: 48 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/make
#!/usr/bin/env make
# WARN: gmake syntax
########################################################
# Makefile for $(NAME)
Expand All @@ -12,25 +12,29 @@
# make audit -- run CVE scan separately
# make integration -- run integration tests
########################################################
# variable section

# Variables

NAME = slo_generator

HATCH = hatch
PIP = pip3
PYTHON = python3
TWINE = twine
COVERAGE = coverage
SITELIB = $(shell $(PYTHON) -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")

VERSION ?= $(shell grep "version = " setup.cfg | cut -d ' ' -f 3)
VERSION ?= $(shell grep "version = " pyproject.toml | cut -d ' ' -f 3)

########################################################

all: clean install test

##############

info:
@echo "slo-generator version: ${VERSION}"

##############

clean:
@echo "Cleaning up distutils stuff"
rm -rf build
Expand All @@ -46,46 +50,64 @@ clean:
@echo "Cleaning up test reports"
rm -rf report/*

##############

build: clean
$(PYTHON) setup.py sdist bdist_wheel
$(HATCH) build

deploy: clean install_twine build
$(TWINE) upload dist/*
deploy: build
$(HATCH) publish

install_twine:
$(PIP) install twine

develop: install
pre-commit install
##############

#TODO: Replace with Hatch.
install: clean
#FIXME: Are all dependencies and features requested for all these targets?
$(PIP) install -e ."[api, datadog, prometheus, elasticsearch, opensearch, splunk, pubsub, cloud_monitoring, bigquery, dev]"

uninstall: clean
$(PIP) freeze --exclude-editable | xargs $(PIP) uninstall -y
$(HATCH) env prune

##############

#TODO: How to handle pre-commit with Hatch environments?
develop: install
pre-commit install

##############

format:
$(HATCH) fmt

##############

#FIXME: Are all dependencies and features requested for all these targets?
test: install unit lint audit

##############

unit: clean
pytest --cov=$(NAME) tests -p no:warnings
$(HATCH) run test:unit

coverage:
$(COVERAGE) report --rcfile=".coveragerc"
$(HATCH) run test:cov

format:
ruff format
##############

lint: ruff pytype mypy

ruff:
ruff format --check
ruff check
$(HATCH) fmt

pytype:
pytype
$(HATCH) run lint:pytype

mypy:
mypy --show-error-codes $(NAME)
$(HATCH) run lint:mypy

##############

audit: bandit safety

audit: bandit safety

Expand All @@ -97,6 +119,8 @@ safety:
# We do not use the `--extra-index-url` option, and the behavior is intended anyway.
safety check --ignore 67599

##############

integration: int_cm int_csm int_custom int_dd int_dt int_es int_prom int_sp int_os

int_cm:
Expand Down Expand Up @@ -126,6 +150,8 @@ int_sp:
int_os:
slo-generator compute -f samples/opensearch -c samples/config.yaml

##############

# Run API locally
run_api:
slo-generator api --target=run_compute --signature-type=http -c samples/config.yaml
Expand Down
Loading
Loading