Skip to content

Commit

Permalink
ci: Add automation (#2)
Browse files Browse the repository at this point in the history
In this PR, we add GitHub Actions and configure pre-commit hooks to format the codebase explicitly.

* ci: Update pre-commit hooks

Replace flake8 & black with ruff, and bump dependencies

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* fix: Enforce new pre-commit

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Configure Dependabot

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Add GHA on PRs

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Drop pydocstyle as project is deprecated

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Drop bandit (use Ruff instead)

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* chore: Bump supported Python version to 3.12

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Configure Ruff

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* chore: Fix codebase formatting

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* test: Add placeholder for now

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

* ci: Allow 'assert' statements

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>

---------

Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>
  • Loading branch information
EBoisseauSierra authored Oct 7, 2024
1 parent 179eae7 commit 3f0d62a
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 124 deletions.
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
- package-ecosystem: pip
directory: "/"
schedule:
interval: weekly
groups:
# Update minor and patch versions of Python packages together.
# Major version bumps are raised as separate PRs for each package.
python:
update-types:
- "minor"
- "patch"
21 changes: 21 additions & 0 deletions .github/workflows/format_codebase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint & format codebase

on:
pull_request:
workflow_dispatch:
push:
branches:
- main
- release/**
- pre-release/**

jobs:
run_hooks:
name: Enforce pre-commit hooks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Enforce pre-commits hooks
uses: pre-commit/action@v3.0.1
18 changes: 11 additions & 7 deletions .github/workflows/pr-lint.yml → .github/workflows/format_pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PR Lint
name: PR lint

on:
pull_request:
Expand All @@ -10,15 +10,19 @@ on:

jobs:
check:
name: Validate PR title
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: morrisoncole/pr-lint-action@v1.5.1
- name: Checkout
uses: actions/checkout@v4

- name: Validate PR title
uses: morrisoncole/pr-lint-action@v1.7.1
with:
title-regex: "^(build|chore|ci|docs|feat|fix|perf|refactor|style|test|other)(\\(.+\\))?(\\!)?:\\s.+"
on-failed-regex-fail-action: true
on-failed-regex-request-changes: false
on-failed-regex-create-review: false
on-failed-regex-comment:
"Please format your PR title to match: `%regex%`!"
on-failed-regex-request-changes: true
on-failed-regex-create-review: true
on-failed-regex-comment: "Please format your PR title to match: `%regex%`!"
on-succeeded-regex-dismiss-review-comment: "The PR title is now correctly formatted. Thanks!"
repo-token: "${{ secrets.GITHUB_TOKEN }}"
39 changes: 39 additions & 0 deletions .github/workflows/run_python_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Run Python unit tests

on:
push:
branches:
- main
- release/**
- pre-release/**
pull_request:
types: [opened, edited, synchronize, reopened]

jobs:
unit_tests:
name: Run unit tests
runs-on: ubuntu-latest
permissions:
# Allow the coverage report to be generated and posted
pull-requests: write
contents: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Python
id: python_setup
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Install dependencies
id: dependencies
run: |
pip install --upgrade pip
pip install -e '.[test, build]'
- name: Run unit tests
id: pytest
run: |
pytest tests
65 changes: 26 additions & 39 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-json
- id: check-merge-conflict
- id: check-toml
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md] # allow markdown linebreak at EOL
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.6.9"
hooks:
- id: ruff
name: "Ruff linting"
args:
- --fix
- --exit-non-zero-on-fix
- id: ruff-format
name: "Ruff formatting"
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.4.0
hooks:
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md] # allow markdown linebreak at EOL
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.7.0
hooks:
- id: python-check-mock-methods
- id: python-use-type-annotations
- repo: https://github.com/PyCQA/bandit
rev: 1.7.0
hooks:
- id: bandit
args: ["--skip", "B101"] # don't check `assert`, as all tests would then raise errors
- repo: https://github.com/pycqa/pydocstyle
rev: 5.1.1
hooks:
- id: pydocstyle
exclude: ^tests/ # do not force detailed docstrings in tests
args: [--convention=google]
- repo: https://github.com/pycqa/isort
rev: 5.7.0
hooks:
- id: isort
args: ["--profile", "black", "--line-length", "88"] # make isort compliant with our code style
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v1.2.0
hooks:
- id: conventional-pre-commit
- id: conventional-pre-commit
stages: [commit-msg]
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.0
0.3.0
34 changes: 17 additions & 17 deletions gsbparse/account_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import logging
from functools import cached_property
from typing import TextIO, Union
from typing import TextIO

import defusedxml.ElementTree as ET
import defusedxml.ElementTree as Et
import pandas as pd

from gsbparse.account_section import (
GsbSectionAccount,
GsbSectionBudgetary,
GsbSectionCategory,
GsbSectionCurrency,
GsbSectionFinancial_year,
GsbSectionFinancialYear,
GsbSectionParty,
GsbSectionPayment,
GsbSectionReconcile,
Expand All @@ -27,7 +27,7 @@
class AccountFile:
"""Representation of a parsed `.gsb` file."""

def __init__(self, source: Union[str, TextIO]) -> None:
def __init__(self, source: str | TextIO) -> None:
"""Initialize an AccountFile object, given its source (file object or path)."""
self.source = source

Expand All @@ -49,42 +49,42 @@ def sections(self) -> list[dict]:
sections = {}

# Read the .gsb XML content
tree = ET.parse(self.source)
tree = Et.parse(self.source)
root = tree.getroot()

# Instantiate each GsbSection with tags of the relevant type
sections["Account"] = GsbSectionAccount(
[child.attrib for child in root if child.tag == "Account"]
[child.attrib for child in root if child.tag == "Account"],
)
sections["Currency"] = GsbSectionCurrency(
[child.attrib for child in root if child.tag == "Currency"]
[child.attrib for child in root if child.tag == "Currency"],
)
sections["Party"] = GsbSectionParty(
[child.attrib for child in root if child.tag == "Party"]
[child.attrib for child in root if child.tag == "Party"],
)
sections["Category"] = GsbSectionCategory(
[child.attrib for child in root if child.tag == "Category"]
[child.attrib for child in root if child.tag == "Category"],
)
sections["Sub_category"] = GsbSectionSubCategory(
[child.attrib for child in root if child.tag == "Sub_category"]
[child.attrib for child in root if child.tag == "Sub_category"],
)
sections["Budgetary"] = GsbSectionBudgetary(
[child.attrib for child in root if child.tag == "Budgetary"]
[child.attrib for child in root if child.tag == "Budgetary"],
)
sections["Sub_budgetary"] = GsbSectionSubBudgetary(
[child.attrib for child in root if child.tag == "Sub_budgetary"]
[child.attrib for child in root if child.tag == "Sub_budgetary"],
)
sections["Transaction"] = GsbSectionTransaction(
[child.attrib for child in root if child.tag == "Transaction"]
[child.attrib for child in root if child.tag == "Transaction"],
)
sections["Financial_year"] = GsbSectionFinancial_year(
[child.attrib for child in root if child.tag == "Financial_year"]
sections["Financial_year"] = GsbSectionFinancialYear(
[child.attrib for child in root if child.tag == "Financial_year"],
)
sections["Reconcile"] = GsbSectionReconcile(
[child.attrib for child in root if child.tag == "Reconcile"]
[child.attrib for child in root if child.tag == "Reconcile"],
)
sections["Payment"] = GsbSectionPayment(
[child.attrib for child in root if child.tag == "Payment"]
[child.attrib for child in root if child.tag == "Payment"],
)

return sections
Expand Down
Loading

0 comments on commit 3f0d62a

Please sign in to comment.