Skip to content

Commit

Permalink
chore: Add pre-commit, ci & release-please workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
qgerome authored Jul 25, 2023
1 parent cfe3160 commit 28d51d3
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 47 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI
on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- uses: pre-commit/[email protected]
test:
name: Run test suite
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: "Install dependencies"
run: pip install ".[dev]"

- name: Run tests
run: pytest --cov=. --cov-report html --cov-report term
24 changes: 24 additions & 0 deletions .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
push:
branches:
- main

permissions:
contents: write
pull-requests: write

name: release-please

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v3
with:
token: ${{ secrets.RELEASE_GH_TOKEN }}
release-type: python
package-name: openhexa.toolbox
bump-minor-pre-major: true
bump-patch-for-minor-pre-major: true
include-v-in-tag: false
changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":false}]'
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.280
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
language_version: python3
2 changes: 2 additions & 0 deletions openhexa/toolbox/dhis2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .api import Api
from .dhis2 import DHIS2

__all__ = ["DHIS2", "Api"]
9 changes: 2 additions & 7 deletions openhexa/toolbox/dhis2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def raise_if_error(response: requests.Response):
if response.status_code != 200 and "json" in response.headers["content-type"]:
msg = response.json()
if msg.get("status") == "ERROR":
raise DHIS2Error(
f"{msg.get('status')} {msg.get('httpStatusCode')}:"
f" {msg.get('message')}"
)
raise DHIS2Error(f"{msg.get('status')} {msg.get('httpStatusCode')}:" f" {msg.get('message')}")

# raise with requests if no error message provided
response.raise_for_status()
Expand All @@ -74,9 +71,7 @@ def get(self, endpoint: str, params: dict = None) -> requests.Response:
self.raise_if_error(r)
return r

def get_paged(
self, endpoint: str, params: dict = None, page_size: 1000 = None
) -> Iterable[requests.Response]:
def get_paged(self, endpoint: str, params: dict = None, page_size: 1000 = None) -> Iterable[requests.Response]:
"""Iterate over all response pages."""
if params is None:
params = {}
Expand Down
42 changes: 10 additions & 32 deletions openhexa/toolbox/dhis2/dhis2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ def organisation_units(self) -> List[dict]:
"name": ou.get("name"),
"level": ou.get("level"),
"path": ou.get("path"),
"geometry": json.dumps(ou.get("geometry"))
if ou.get("geometry")
else None,
"geometry": json.dumps(ou.get("geometry")) if ou.get("geometry") else None,
}
)
return org_units
Expand Down Expand Up @@ -160,9 +158,7 @@ def organisation_unit_groups(self) -> List[dict]:
{
"id": group.get("id"),
"name": group.get("name"),
"organisation_units": [
ou.get("id") for ou in group["organisationUnits"]
],
"organisation_units": [ou.get("id") for ou in group["organisationUnits"]],
}
)
org_unit_groups += groups
Expand All @@ -187,9 +183,7 @@ def datasets(self) -> List[dict]:
):
for ds in page.json()["dataSets"]:
row = {"id": ds.get("id"), "name": ds.get("name")}
row["data_elements"] = [
dx["dataElement"]["id"] for dx in ds["dataSetElements"]
]
row["data_elements"] = [dx["dataElement"]["id"] for dx in ds["dataSetElements"]]
row["indicators"] = [indicator["id"] for indicator in ds["indicators"]]
row["organisation_units"] = [ou["id"] for ou in ds["organisationUnits"]]
datasets.append(row)
Expand Down Expand Up @@ -250,9 +244,7 @@ def category_option_combos(self) -> List[dict]:
Id and name of all category option combos.
"""
combos = []
for page in self.client.api.get_paged(
"categoryOptionCombos", params={"fields": "id,name"}, page_size=1000
):
for page in self.client.api.get_paged("categoryOptionCombos", params={"fields": "id,name"}, page_size=1000):
combos += page.json().get("categoryOptionCombos")
return combos

Expand Down Expand Up @@ -461,9 +453,7 @@ def add_org_unit_parent_columns(

for lvl in range(1, len(levels)):
org_units = org_units.with_columns(
pl.col("path")
.apply(lambda path: self._get_uid_from_level(path, lvl))
.alias(f"parent_level_{lvl}_id")
pl.col("path").apply(lambda path: self._get_uid_from_level(path, lvl)).alias(f"parent_level_{lvl}_id")
)

org_units = org_units.join(
Expand All @@ -478,9 +468,7 @@ def add_org_unit_parent_columns(
)

df = df.join(
other=org_units.select(
["id"] + [col for col in org_units.columns if col.startswith("parent_")]
),
other=org_units.select(["id"] + [col for col in org_units.columns if col.startswith("parent_")]),
how="left",
left_on=org_unit_id_column,
right_on="id",
Expand Down Expand Up @@ -524,10 +512,7 @@ def split_params(self, params: dict) -> List[dict]:

chunks = []
for chunk in itertools.product(
*[
_split_list(params.get(param), max_length)
for param, max_length in params_to_chunk
]
*[_split_list(params.get(param), max_length) for param, max_length in params_to_chunk]
):
p = params.copy()
for i, (param, _) in enumerate(params_to_chunk):
Expand Down Expand Up @@ -598,9 +583,7 @@ def get(
raise DHIS2Error("No temporal dimension provided")

if data_elements and not self.client.version >= 2.39:
raise DHIS2Error(
"Data elements parameter not supported for DHIS2 versions < 2.39"
)
raise DHIS2Error("Data elements parameter not supported for DHIS2 versions < 2.39")

params = {
"dataElement": data_elements,
Expand Down Expand Up @@ -696,13 +679,8 @@ def split_params(self, params: dict) -> List[dict]:
for dim in dimension:
dim_id, dim_items = self.split_dimension_param(dim)
if dim_id in MAX_DIM_ITEMS:
dim_item_chunks = [
item
for item in _split_list(dim_items, MAX_DIM_ITEMS.get(dim_id, 50))
]
dim_item_chunks = [
f"{dim_id}:{';'.join(dim_items)}" for dim_items in dim_item_chunks
]
dim_item_chunks = [item for item in _split_list(dim_items, MAX_DIM_ITEMS.get(dim_id, 50))]
dim_item_chunks = [f"{dim_id}:{';'.join(dim_items)}" for dim_items in dim_item_chunks]
dim_chunks.append(dim_item_chunks)
else:
dim_chunks.append([dim])
Expand Down
6 changes: 1 addition & 5 deletions openhexa/toolbox/dhis2/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ def __init__(self, period: Union[str, datetime]):

@staticmethod
def check_period(period: str):
if (
(len(period) != 6 and len(period) != 7)
or period[4] != "W"
or period[5] == "0"
):
if (len(period) != 6 and len(period) != 7) or period[4] != "W" or period[5] == "0":
raise ValueError(f'"{period}" is not valid DHIS2 week')

@staticmethod
Expand Down
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
]

[project.optional-dependencies]
dev = ["pytest", "build"]
dev = ["ruff~=0.0.278", "pytest~=7.3.0", "build~=0.10.0", "pytest-cov~=4.0.0" , "black~=23.7.0", "pre-commit"]

[tool.setuptools.packages.find]
where = ["."]
Expand All @@ -40,7 +40,10 @@ namespaces = true
"Bug Tracker" = "https://github.com/blsq/openhexa-toolbox/issues"

[tool.black]
line-length = 88
line-length = 120

[tool.ruff]
line-length = 88
line-length = 120

[tool.ruff.pycodestyle]
max-doc-length = 120
Empty file added tests/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import unittest


class ToolboxTest(unittest.TestCase):
def test_true(self):
self.assertTrue(True)

0 comments on commit 28d51d3

Please sign in to comment.