Skip to content

Commit b8dc0b5

Browse files
Align infra/test.sh & infra/lint.sh (#88)
The idea with this commit is that `infra/lint.sh` and `infra/test.sh` are the "source of truth" for how we want to run linting and tests respectively. We then use these scripts in `precommit`, `make`, Github Actions, and wherever else we want to run tests and linting. For Github Actions, we also now run linting and tests directly in the action runner, rather than running through Docker. This resulted in a speed-up of the checks. Fixes #79, #67 Other changes include: * Do not install postgresql client * Install git and use precommit for linting This should fail if it can't run * Use development image for tests and lint * Fix lint as reported by `pre-commit run --all-files`, `black`, and `isort` * Prepend test commands with `poetry run` * Ignore missing imports for ruamel * Update .secrets.baseline to exclude poetry.lock * Add make commands for format, lint and test * Remove git check for detect-secrets lint check Co-authored-by: Mathieu Leplatre <[email protected]>
1 parent 42bdd0a commit b8dc0b5

20 files changed

+189
-214
lines changed

.github/workflows/lint-build.yaml

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,14 @@ on: pull_request
55
jobs:
66
run_test:
77
runs-on: ubuntu-latest
8-
98
steps:
10-
- name: Checkout code
11-
uses: actions/checkout@v2
12-
13-
- name: Build lint image
14-
uses: docker/build-push-action@v2
9+
- uses: actions/checkout@v3
10+
- name: Install poetry
11+
run: pipx install poetry
12+
- uses: actions/setup-python@v4
1513
with:
16-
context: .
17-
file: infra/Dockerfile
18-
push: false
19-
target: "lint"
20-
tags: ghcr.io/${{ github.repository }}-lint:${{ github.sha }}
21-
22-
- name: Run lint
23-
run: |-
24-
docker run --rm \
25-
-e JIRA_USERNAME \
26-
-e JIRA_API_KEY \
27-
-e BUGZILLA_API_KEY \
28-
ghcr.io/${{ github.repository }}-lint:${{ github.sha }}
14+
python-version: "3.9"
15+
cache: "poetry"
16+
- name: Install dependencies
17+
run: poetry install
18+
- run: infra/lint.sh

.github/workflows/test-build.yaml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@ on: pull_request
55
jobs:
66
run_test:
77
runs-on: ubuntu-latest
8-
8+
env:
9+
JIRA_USERNAME: fake_jira_username
10+
JIRA_API_KEY: fake_jira_api_key
11+
BUGZILLA_API_KEY: fake_bugzilla_api_key
912
steps:
10-
- name: Checkout code
11-
uses: actions/checkout@v2
12-
13-
- name: Build test image
14-
uses: docker/build-push-action@v2
13+
- uses: actions/checkout@v3
14+
- name: Install poetry
15+
run: pipx install poetry
16+
- uses: actions/setup-python@v4
1517
with:
16-
context: .
17-
file: infra/Dockerfile
18-
push: false
19-
target: "test"
20-
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
21-
18+
python-version: "3.9"
19+
cache: "poetry"
20+
- name: Install dependencies
21+
run: poetry install
2222
- name: Run tests
23-
run: |-
24-
docker run --rm \
25-
-e JIRA_USERNAME \
26-
-e JIRA_API_KEY \
27-
-e BUGZILLA_API_KEY \
28-
ghcr.io/${{ github.repository }}:${{ github.sha }}
29-
env:
30-
JIRA_USERNAME: "fake_username"
31-
JIRA_API_KEY: "fake_api_key"
32-
BUGZILLA_API_KEY: "fake_api_key"
23+
run: infra/test.sh

.pre-commit-config.yaml

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
exclude: |
2-
(?x)^(
3-
.*/gunicorn_conf.py|
4-
.*/test_gunicorn_conf.py
5-
)$
61
repos:
7-
- repo: local
8-
hooks:
9-
- id: pylint
10-
name: pylint
11-
entry: poetry run pylint
12-
language: system
13-
exclude: ^tests/
14-
types: [python]
152
- repo: https://github.com/pre-commit/pre-commit-hooks
163
rev: v2.1.0
174
hooks:
@@ -27,57 +14,51 @@ repos:
2714
]
2815
exclude: "infra/k8s/secret.json"
2916
- id: trailing-whitespace
17+
- repo: local
18+
hooks:
19+
- id: pylint
20+
name: pylint
21+
entry: infra/lint.sh pylint
22+
language: script
23+
types: [python]
3024
- repo: local
3125
hooks:
3226
- id: mypy
3327
name: mypy
34-
entry: poetry run mypy
35-
language: system
28+
entry: infra/lint.sh mypy
29+
language: script
3630
types: [python]
3731
- repo: local
3832
hooks:
3933
- id: bandit
4034
name: bandit
41-
entry: poetry run bandit
42-
args: [-lll, --recursive]
43-
language: system
35+
entry: infra/lint.sh bandit
36+
language: script
37+
types: [python]
4438
- repo: local
4539
hooks:
4640
- id: detect-secrets
4741
name: detect-secrets
48-
entry: poetry run detect-secrets-hook
49-
args: ['--baseline', '.secrets.baseline']
50-
exclude: "poetry.lock"
51-
language: system
52-
- repo: https://github.com/asottile/seed-isort-config
53-
rev: v1.9.3
54-
hooks:
55-
- id: seed-isort-config
42+
entry: infra/lint.sh detect-secrets
43+
language: script
5644
- repo: local
5745
hooks:
5846
- id: isort
5947
name: isort
60-
entry: poetry run isort
61-
args: ["--recursive", "--settings-path", "./pyproject.toml", "."]
62-
language: system
48+
entry: infra/lint.sh isort
49+
language: script
6350
types: [python]
64-
exclude: |
65-
(?x)^(
66-
.*/gunicorn_conf.py|
67-
.*/test_gunicorn_conf.py
68-
)$
6951
- repo: local
7052
hooks:
7153
- id: black
7254
name: black
73-
entry: poetry run black
55+
entry: infra/lint.sh black
56+
language: script
7457
types: [python]
75-
language: system
7658
- repo: local
7759
hooks:
7860
- id: yamllint
7961
name: yamllint
80-
entry: poetry run yamllint
81-
args: ["-c", ".yamllint", 'config/']
82-
types: [file, yaml]
83-
language: system
62+
entry: infra/lint.sh yamllint
63+
language: script
64+
types: [yaml]

.secrets.baseline

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"custom_plugin_paths": [],
33
"exclude": {
4-
"files": "^.secrets.baseline$",
4+
"files": "poetry.lock",
55
"lines": null
66
},
7-
"generated_at": "2022-03-18T00:00:55Z",
7+
"generated_at": "2022-07-10T01:39:12Z",
88
"plugins_used": [
99
{
1010
"name": "AWSKeyDetector"
@@ -59,31 +59,11 @@
5959
}
6060
],
6161
"results": {
62-
".github/workflows/deploy-build.yaml": [
62+
"README.md": [
6363
{
64-
"hashed_secret": "24b04b46d769e6f6b8d10c943738ac5c968dea1c",
64+
"hashed_secret": "04e78d6e804f2b59e6cb282cb9ed2c7bfd8a9737",
6565
"is_verified": false,
66-
"line_number": 94,
67-
"type": "Secret Keyword"
68-
},
69-
{
70-
"hashed_secret": "1481d0a0ceb16ea4672fed76a0710306eb9f3a33",
71-
"is_verified": false,
72-
"line_number": 96,
73-
"type": "Secret Keyword"
74-
},
75-
{
76-
"hashed_secret": "30a1dfa6d09c177dcd147a5f31a5627becd96b23",
77-
"is_verified": false,
78-
"line_number": 96,
79-
"type": "Secret Keyword"
80-
}
81-
],
82-
"poetry.lock": [
83-
{
84-
"hashed_secret": "d97b148b47121abb687af133c28a3932202426b0",
85-
"is_verified": false,
86-
"line_number": 899,
66+
"line_number": 152,
8767
"type": "Hex High Entropy String"
8868
}
8969
]

infra/Dockerfile renamed to Dockerfile

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ RUN mkdir -p $POETRY_HOME && \
3131

3232
RUN apt-get update && \
3333
apt-get install --assume-yes apt-utils && \
34-
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
35-
apt-get install --no-install-recommends -y \
36-
libpq5
34+
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
3735

3836
# builder-base is used to build dependencies
3937
FROM python-base as builder-base
4038
RUN apt-get install --no-install-recommends -y \
4139
curl \
42-
build-essential \
43-
libpq-dev
40+
build-essential
4441

4542
# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME
4643
USER app
@@ -57,6 +54,9 @@ RUN poetry install --no-dev --no-root
5754
# 'development' stage installs all dev deps and can be used to develop code.
5855
# For example using docker-compose to mount local volume under /app
5956
FROM python-base as development
57+
# to run detect-secrets
58+
RUN apt-get install --no-install-recommends -y git
59+
6060
ENV FASTAPI_ENV=development
6161

6262
# Copying poetry and venv into image
@@ -80,24 +80,6 @@ ENTRYPOINT ["/docker-entrypoint.sh"]
8080
CMD uvicorn src.app.api:app --reload --host=0.0.0.0 --port=$PORT
8181

8282

83-
# 'lint' stage runs similar checks to pre-commit
84-
# running in check mode means build will fail if any linting errors occur
85-
FROM development AS lint
86-
RUN bandit -lll --recursive src --exclude "src/poetry.lock,src/.venv,src/.mypy,src/build"
87-
RUN mypy src
88-
RUN black --config ./pyproject.toml --check src tests
89-
RUN isort --recursive --settings-path ./pyproject.toml --check-only src
90-
RUN pylint src tests
91-
RUN yamllint -d "{extends: default, rules: {key-duplicates: enable, key-ordering: enable}}" ./config
92-
CMD ./infra/detect_secrets_helper.sh
93-
94-
95-
# 'test' stage runs our unit tests with pytest and
96-
# coverage.
97-
FROM development AS test
98-
CMD ./infra/test.sh
99-
100-
10183
# 'production' stage uses the clean 'python-base' stage and copyies
10284
# in only our runtime deps that were installed in the 'builder-base'
10385
FROM python-base as production

Makefile

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ help:
1212
@echo ""
1313
@echo " build - build docker containers"
1414
@echo " lint - lint check for code"
15+
@echo " format - run formatters (black, isort), fix in place"
1516
@echo " start - run the API service"
1617
@echo ""
1718
@echo " test - run test suite"
@@ -25,32 +26,30 @@ help:
2526

2627
.PHONY: build
2728
build:
28-
docker-compose -f ./docker-compose.yaml -f ./tests/infra/docker-compose.test.yaml build \
29+
docker-compose build \
2930
--build-arg userid=${_UID} --build-arg groupid=${_GID}
3031

32+
.PHONY: format
33+
format:
34+
infra/lint.sh black --fix
35+
infra/lint.sh isort --fix
36+
3137
.PHONY: lint
3238
lint:
33-
docker-compose -f ./docker-compose.yaml -f ./tests/infra/docker-compose.lint.yaml build \
34-
--build-arg userid=${_UID} --build-arg groupid=${_GID} lint
35-
39+
docker-compose run --rm web infra/lint.sh
3640

3741
.PHONY: shell
3842
shell:
39-
docker-compose -f ./docker-compose.yaml run web
43+
docker-compose run web /bin/sh
4044

4145
.PHONY: start
4246
start:
4347
docker-compose up
4448

4549
.PHONY: test
4650
test:
47-
docker-compose -f ./docker-compose.yaml -f ./tests/infra/docker-compose.test.yaml run tests
48-
ifneq (1, ${MK_KEEP_DOCKER_UP})
49-
# Due to https://github.com/docker/compose/issues/2791 we have to explicitly
50-
# rm all running containers
51-
docker-compose down
52-
endif
51+
docker-compose run --rm web infra/test.sh
5352

5453
.PHONY: test-shell
5554
test-shell:
56-
docker-compose -f ./docker-compose.yaml -f ./tests/infra/docker-compose.test.yaml run web
55+
docker-compose run web

docker-compose.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ services:
33
web:
44
build:
55
context: .
6-
dockerfile: ./infra/Dockerfile
76
target: development
87
volumes:
9-
- type: bind
10-
source: .
11-
target: /app
8+
- .:/app
129
ports:
1310
- ${PORT:-8000}:${PORT:-8000}
1411
# Let the init system handle signals for us.

infra/detect_secrets_helper.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)