Skip to content

Commit

Permalink
ci(.github): revamp workflows and add hatch
Browse files Browse the repository at this point in the history
Improve the GitHub action workflows and start to use hatch and hatchling as build backend.
  • Loading branch information
yeisonvargasf committed Oct 20, 2023
1 parent 5b9cb59 commit 217ce56
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 106 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: CI

on:
push:
branches:
- 'main'
pull_request:
types: [opened, synchronize, reopened]

jobs:
test:
name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install Hatch
run: pip install --upgrade hatch

- name: Run tests and track code coverage
run: hatch run test:cov

# TODO: Enforce quality, security and style checks.

bump:
needs: test
name: Bump version
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade hatch
- name: Bump version
if: github.ref == 'refs/heads/main'
run: hatch run bump

- name: Local bump
if: github.ref != 'refs/heads/main'
run: hatch run local-bump

build:
needs: bump
name: Build ${{ github.ref }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade hatch
- name: Build package
run: hatch build
- uses: actions/upload-artifact@v3
with:
name: "dist-${{ github.ref }}"
path: dist/*
if-no-files-found: error
retention-days: 7
26 changes: 0 additions & 26 deletions .github/workflows/code_analysis.yml

This file was deleted.

63 changes: 0 additions & 63 deletions .github/workflows/main.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: PyPi Release

on:
workflow_run:
workflows: [CI]
branches: [main]
types:
- completed

jobs:
pypi-publish:
name: upload release to PyPI
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment: release
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v3
with:
name: dist-refs/heads/main
path: dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
print-hash: true
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- hooks:
- id: commitizen
- id: commitizen-branch
stages:
- push
repo: https://github.com/commitizen-tools/commitizen
rev: v3.12.0
36 changes: 36 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
import sys
import pytest


def pytest_collection_modifyitems(items, config):
for item in items:
if not any(item.iter_markers()):
item.add_marker("basic")

markexpr = config.getoption("markexpr", 'False')

expr = "basic"

if markexpr:
expr += f" or ({markexpr})"

config.option.markexpr = expr


def pytest_deselected(items):
if not items:
return
config = items[0].session.config
config.deselected = items


def pytest_terminal_summary(terminalreporter, exitstatus, config):
reports = terminalreporter.getreports('')
content = os.linesep.join(text for report in reports for secname, text in report.sections)
deselected = getattr(config, "deselected", [])
if deselected:
terminalreporter.ensure_newline()
terminalreporter.section('Deselected tests', sep='-', yellow=True, bold=True)
content = os.linesep.join(item.nodeid for item in deselected)
terminalreporter.line(content)
1 change: 1 addition & 0 deletions dparse/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def update(cls, content, dependency, version, spec="==", hashes=()):
"Updating a Pipfile requires the pipenv extra to be installed."
" Install it with pip install dparse[pipenv]")
pipfile = tempfile.NamedTemporaryFile(delete=False)
pipfile.close()
p = Project(chdir=False)
p.write_toml(data=data, path=pipfile.name)
data = open(pipfile.name).read()
Expand Down
78 changes: 78 additions & 0 deletions hatch.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
[envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest",
"commitizen"
]
[envs.default.scripts]
test = "pytest {args:tests}"
test-cov = 'coverage run -m pytest -m "conda or poetry or pipenv" {args:tests}'
cov-report = [
"coverage combine",
"coverage report --show-missing",
]
cov = [
"test-cov",
"cov-report",
]
bump = "cz bump --check-consistency --changelog"
local-bump = "cz bump --check-consistency --changelog --local-version --files-only"


[envs.test]
features = [
"all"
]
[envs.test.scripts]
test = 'pytest -m "conda or poetry or pipenv" {args:tests}'
test-cov = 'coverage run -m pytest -m "conda or poetry or pipenv" {args:tests}'


[envs.all]
type = "container"

[envs.all.overrides]
matrix.optionals.features = [
{ value = "all", if = ["all-extras"] },
{ value = "conda", if = ["conda"] },
{ value = "pipenv", if = ["pipenv"] },
{ value = "poetry", if = ["poetry"] },
]
matrix.optionals.scripts = [
{key = "test", value = 'pytest -m "conda" {args:tests}', if = ["conda"] },
{key = "test", value = 'pytest -m "pipenv" {args:tests}', if = ["pipenv"] },
{key = "test", value = 'pytest -m "poetry" {args:tests}', if = ["poetry"] },
{key = "test", value = 'pytest -m "not conda and not poetry and not pipenv" {args:tests}', if = ["no-extras"] },
{key = "test", value = 'pytest -m "conda or poetry or pipenv" {args:tests}', if = ["all-extras"] },
]

[[envs.all.matrix]]
python = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
optionals = ["conda", "pipenv", "poetry", "all-extras", "no-extras"]

[envs.lint]
detached = true
dependencies = [
"black",
"mypy",
"ruff",
]
[envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:dparse tests}"
style = [
"ruff {args:.}",
"black --check --diff {args:.}",
]
fmt = [
"black {args:.}",
"ruff --fix {args:.}",
"style",
]
all = [
"style",
"typing",
]


[build.targets.wheel]
packages = ["dparse"]
40 changes: 39 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down Expand Up @@ -42,3 +41,42 @@ pipenv = [
conda = [
"pyyaml",
]
poetry = [
"poetry",
]
all = [
"dparse[poetry]",
"dparse[pipenv]",
"dparse[conda]"
]

[tool.pytest.ini_options]
addopts = "--strict-markers"
markers = [
"conda: requires the conda extra",
"pipenv: requires the pipenv extra",
"poetry: requires the poetry extra",
"basic: requires no extras",
]

[tool.coverage.run]
source_pkgs = ["dparse"]
branch = true
parallel = true
omit = [
]

[tool.coverage.paths]
source = ["dparse", "*/dparse"]

[tool.coverage.report]
exclude_lines = [
]
[tool.commitizen]
name = "cz_conventional_commits"
tag_format = "$version"
version_scheme = "pep440"
version_provider = "pep621"
update_changelog_on_bump = true
annotated_tag = true
changelog_incremental = true
Loading

0 comments on commit 217ce56

Please sign in to comment.