Skip to content

Commit

Permalink
Add trunk, fix autofixable lint & format errors & check for FIXMEs on…
Browse files Browse the repository at this point in the history
… PR (#96)
  • Loading branch information
katybaulch authored Mar 13, 2024
1 parent fceac82 commit 6ce2761
Show file tree
Hide file tree
Showing 127 changed files with 505 additions and 387 deletions.
42 changes: 14 additions & 28 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,33 @@ name: CI/CD

on:
push:
tags: ['v*']
tags: ["v*"]
branches:
- main
pull_request:
branches:
- main

permissions: read-all

# https://github.com/marketplace/actions/docker-layer-caching
jobs:
code-quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

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

- name: Install dependencies
run: |
python -m pip install "poetry==1.6.1" && poetry install && poetry run pre-commit install
- name: Run pre-commit checks
run: |
poetry run pre-commit run --all-files
- name: Check code contains no FIXME's
run: |
git grep -r --no-color ${case_sensitive} --line-number -e "FIXME" :^.github
if [[ $? -eq 0 ]]; then
# if we found any FIXME entries in checked in files, fail on main
exit 1
else
exit 0
fi
shell: bash {0}
permissions:
# For trunk to post annotations
checks: write
# For repo checkout
contents: read
uses: climatepolicyradar/reusable-workflows/.github/workflows/python-precommit-validator.yml@main

test:
runs-on: ubuntu-latest
steps:
- name: Install latest Docker Compose
uses: ndeloof/[email protected]
with:
legacy: false

- uses: actions/checkout@v4

- name: Configure test env variables
Expand Down
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,15 @@ load_blank.txt
load_default.txt

# PIP (because we want to use Poetry)
requirements.txt
requirements.txt

# Trunk miscellaneous
*out
*logs
*actions
*notifications
*tools
plugins
user_trunk.yaml
user.yaml
tmp
34 changes: 0 additions & 34 deletions .pre-commit-config.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions .trunk/configs/.hadolint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Following source doesn't work in most setups
ignored:
- SC1090
- SC1091
2 changes: 2 additions & 0 deletions .trunk/configs/.isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=black
11 changes: 11 additions & 0 deletions .trunk/configs/.markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Autoformatter friendly markdownlint config (all formatting rules disabled)
default: true
blank_lines: false
bullet: false
html: false
indentation: false
line_length: false
spaces: false
url: false
whitespace: false
tables: false
7 changes: 7 additions & 0 deletions .trunk/configs/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enable=all
source-path=SCRIPTDIR
disable=SC2154

# If you're having issues with shellcheck following source, disable the errors via:
# disable=SC1090
# disable=SC1091
10 changes: 10 additions & 0 deletions .trunk/configs/.yamllint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
rules:
quoted-strings:
required: only-when-needed
extra-allowed: ["{|}"]
empty-values:
forbid-in-block-mappings: true
forbid-in-flow-mappings: true
key-duplicates: {}
octal-values:
forbid-implicit-octal: true
1 change: 1 addition & 0 deletions .trunk/configs/bandit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude_dirs: ["unit_tests", "integration_tests"]
4 changes: 4 additions & 0 deletions .trunk/configs/cspell.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "0.2"
# Suggestions can sometimes take longer on CI machines,
# leading to inconsistent results.
suggestionsTimeout: 5000 # ms
67 changes: 67 additions & 0 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This file controls the behavior of Trunk: https://docs.trunk.io/cli
#
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
version: 1.20.1

# Trunk provides extensibility via plugins.
# (https://docs.trunk.io/plugins)
plugins:
sources:
- id: trunk
ref: v1.4.4
uri: https://github.com/trunk-io/plugins

# Many linters and tools depend on runtimes - configure them here.
# (https://docs.trunk.io/runtimes)
runtimes:
enabled:
- [email protected]
- [email protected]
- [email protected]

# This is the section where you manage your linters.
# (https://docs.trunk.io/check/configuration)
lint:
definitions:
- name: bandit
direct_configs: [bandit.yaml]
commands:
- name: lint
run: bandit --exit-zero -c bandit.yaml --format json --output ${tmpfile} ${target}

enabled:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- git-diff-check
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]:
commands:
- end-of-file-fixer
- check-json
- detect-aws-credentials
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]

actions:
disabled:
- trunk-announce
- trunk-check-pre-push
enabled:
- trunk-check-pre-commit
- trunk-fmt-pre-commit
- trunk-upgrade-available
49 changes: 25 additions & 24 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,47 @@ The approach taken is influenced by:
- [The Twelve Factor App](https://12factor.net/)
- [SOLID principles](https://www.baeldung.com/solid-principles)
- [Domain Driven Design (DDD)](https://martinfowler.com/tags/domain%20driven%20design.html)
particularly [folder structure](https://dev.to/stevescruz/domain-driven-design-ddd-file-structure-4pja)
particularly [folder structure](https://dev.to/stevescruz/domain-driven-design-ddd-file-structure-4pja)

## Overview

There are three main layers to the application:

- **Routing Layer** - The responsibility here is to manage the network payloads
and any authentication middleware. All business logic is handed off to...
and any authentication middleware. All business logic is handed off to...
- **Service Layer** - Contains all validation and business logic for the
application. This in turn uses the...
application. This in turn uses the...
- **Repository Layer** - With the sole responsibility of managing how data is
stored and retrieved to/from the database.
**Note**, this split into responsibilities for separate entities, should the
need arise to create a transaction (for example creating two separate entities
atomically) - then this is the responsibility of the service layer to manage the
transaction.

Router
Service
┌───────┴───────┐
│ │
▼ ▼
Repository ──────► Client
(ext)
stored and retrieved to/from the database.
**Note**, this split into responsibilities for separate entities, should the
need arise to create a transaction (for example creating two separate entities
atomically) - then this is the responsibility of the service layer to manage the
transaction.

Router
Service
┌───────┴───────┐
│ │
▼ ▼

Repository ──────► Client
(ext)

## Testing Strategy

### Unit tests

- Routing Layer - this is tested my mocking out the required services by each
individual route. The tests should alter how the service behaves to test out the
routing layer responds.
individual route. The tests should alter how the service behaves to test out the
routing layer responds.

- Service Layer - the required repositories are mocked out so that the tests can
check the service returns/raises what is expected.
check the service returns/raises what is expected.

- Repository Layer - there are no unit tests.

Expand Down
21 changes: 11 additions & 10 deletions DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ At the moment we are using PyRight pegged at version 1.1.294.
## VS Code

- Although we use PyRight for linting (see [.pre-commit-config.yaml](./.pre-commit-config.yaml))
[PyRight recommends that you use PyLance when using VS Code](https://microsoft.github.io/pyright/#/installation?id=vs-code).
[PyRight recommends that you use PyLance when using VS Code](https://microsoft.github.io/pyright/#/installation?id=vs-code).

### Extensions

| Extension | Author | Description | Recommended/Optional |
|-----------|--------|-------------|-------------------------------|
| [Black Formatter](https://github.com/) | Microsoft | | Optional |
| [Code Spell Checker](https://github.com/) | Street Side Software | | Recommended |
| [Makefile Tools](https://github.com/) | Microsoft | | Recommended |
| [markdownlint](https://github.com/) | David Anson | | Recommended |
| [Markdown All in One](https://github.com/) | Yu Zhang | | Optional |
| [PyLance](https://github.com/) | Microsoft | | Recommended |
| [YAML](https://github.com/) | Red Hat | | Optional |
| Extension | Author | Recommended/Optional |
| ------------------------------------------ | -------------------- | -------------------- |
| [Black Formatter](https://github.com/) | Microsoft | Optional |
| [Code Spell Checker](https://github.com/) | Street Side Software | Recommended |
| [Makefile Tools](https://github.com/) | Microsoft | Recommended |
| [markdownlint](https://github.com/) | David Anson | Recommended |
| [Markdown All in One](https://github.com/) | Yu Zhang | Optional |
| [PyLance](https://github.com/) | Microsoft | Recommended |
| [YAML](https://github.com/) | Red Hat | Optional |
| [Ruff](https://github.com/) | Charlie R Marsh | Recommended |

### Recommended User Settings JSON

Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ WORKDIR /usr/src
ENV PYTHONPATH=/usr/src

# Requirements
RUN pip install poetry
RUN pip install --no-cache-dir poetry==1.6.1
COPY poetry.lock pyproject.toml ./
RUN poetry config virtualenvs.create false && poetry install --no-cache
RUN poetry config virtualenvs.create false && poetry install --no-directory --no-root

# Now code
COPY ./app ./app
Expand Down
2 changes: 1 addition & 1 deletion GETTING_STARTED.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Currently the deployment is manual, this required the following steps:

- Create a new tagged release [here](https://github.com/climatepolicyradar/navigator-admin-backend/releases)
- Wait for the `semver` Action to run in github - this creates and pushes the
image into ECR
image into ECR
- Log into the AWS console in the environment you wish to deploy.
- In AppRunner - find the running images and hit the `Deploy` button.

Expand Down
19 changes: 15 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ bootstrap:
pip3 install poetry
poetry install

git_hooks:
# Install & run git pre-commit hooks
poetry run pre-commit install --install-hooks
pre-commit run --all-files
install_trunk:
$(eval trunk_installed=$(shell trunk --version > /dev/null 2>&1 ; echo $$? ))
ifneq ($(trunk_installed),0)
$(eval OS_NAME=$(shell uname -s | tr A-Z a-z))
ifeq ($(OS_NAME),linux)
curl https://get.trunk.io -fsSL | bash
endif
ifeq ($(OS_NAME),darwin)
brew install trunk-io
endif
endif

git_hooks: install_trunk
trunk fmt
trunk check

build:
docker build -t navigator-admin-backend .
Expand Down
8 changes: 4 additions & 4 deletions app/api/api_v1/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .family import families_router
from .collection import collections_router
from .analytics import analytics_router
from .auth import auth_router
from .document import document_router
from .collection import collections_router
from .config import config_router
from .analytics import analytics_router
from .document import document_router
from .event import event_router
from .family import families_router
Loading

0 comments on commit 6ce2761

Please sign in to comment.