Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add make format command #568

Merged
merged 6 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,6 @@
"line_number": 39
}
],
"bin/lint.sh": [
{
"type": "Secret Keyword",
"filename": "bin/lint.sh",
"hashed_secret": "fd336c21216d202878615f06d5fd8528d187f37e",
"is_verified": false,
"line_number": 16
}
],
"docs/developer_setup.md": [
{
"type": "Secret Keyword",
Expand Down Expand Up @@ -203,5 +194,5 @@
}
]
},
"generated_at": "2023-01-30T15:11:07Z"
"generated_at": "2023-03-03T14:31:04Z"
}
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ help:
@echo " build - build docker containers"
@echo " db-only - run PostgreSQL server"
@echo " lint - lint check for code"
@echo " format - run formatters (black, isort), fix in place"
@echo " setup - (re)create the database"
@echo " shell - open a shell in the web container"
@echo " start - run the API service"
Expand Down Expand Up @@ -47,6 +48,11 @@ build: .env
lint: $(INSTALL_STAMP)
bin/lint.sh

.PHONY: format
format: $(INSTALL_STAMP)
bin/lint.sh black --fix
bin/lint.sh isort --fix

.PHONY: db-only
db-only: .env
docker-compose up postgres-admin
Expand Down
88 changes: 73 additions & 15 deletions bin/lint.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,81 @@
#!/bin/bash
#!/bin/sh

set -euo pipefail
set -e
Comment on lines -1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the motivation for these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, initially I was going to suggest keeping this the way it is and migrating JBI to "bash strict mode".

But turns out there are some gotchas with that approach as well.

So, maybe -euo pipefail is "safer", if not too safe, but this could also be fine as is, since I'm less convinced about the safety that these flags provide.


POETRY_RUN="poetry run"

CURRENT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
BASE_DIR="$(dirname "$CURRENT_DIR")"
HAS_GIT="$(command -v git || echo '')"
echo $HAS_GIT

$POETRY_RUN bandit -lll --recursive "${BASE_DIR}" --exclude "${BASE_DIR}/poetry.lock,${BASE_DIR}/.venv,${BASE_DIR}/.mypy,${BASE_DIR}/build"
bandit () {
$POETRY_RUN bandit -lll --recursive "${BASE_DIR}" --exclude "${BASE_DIR}/poetry.lock,${BASE_DIR}/.venv,${BASE_DIR}/.mypy,${BASE_DIR}/build"
}
black () {
$POETRY_RUN black ${check:+--check} "${BASE_DIR}"
}
detect_secrets () {
# Scan only files fixed into the repo, omit poetry.lock
FILES_TO_SCAN=`git ls-tree --full-tree -r --name-only HEAD | grep -v poetry.lock`
$POETRY_RUN detect-secrets-hook $FILES_TO_SCAN --baseline .secrets.baseline
}
isort () {
$POETRY_RUN isort ${check:+--check-only} "${BASE_DIR}"
}
pylint () {
$POETRY_RUN pylint "${BASE_DIR}/ctms" "${BASE_DIR}/tests/unit"
}
mypy () {
$POETRY_RUN mypy "${BASE_DIR}/ctms"
}
all () {
echo "running black"
black
echo "running isort"
isort
echo "running mypy"
mypy
echo "running pylint"
pylint
echo "running bandit"
bandit
echo "running detect_secrets"
detect_secrets
}

if [ -n "$HAS_GIT" ]; then
# Scan only files checked into the repo, omit poetry.lock
SECRETS_TO_SCAN=`git ls-tree --full-tree -r --name-only HEAD | grep -v poetry.lock`
$POETRY_RUN detect-secrets-hook $SECRETS_TO_SCAN --baseline .secrets.baseline
fi
usage () {
echo "Usage: bin/lint.sh [OPTION]"
echo " run linting checks"
echo "Options":
echo " bandit"
echo " black [--fix]"
echo " detect-secrets"
echo " isort [--fix]"
echo " mypy"
echo " pylint"
}

$POETRY_RUN isort --check-only "${BASE_DIR}"
$POETRY_RUN black --check "${BASE_DIR}"
$POETRY_RUN mypy "${BASE_DIR}/ctms"
$POETRY_RUN pylint "${BASE_DIR}/ctms" "${BASE_DIR}/tests/unit"
subcommand='';
check="true"
if [ -z $1 ]; then
all
else
subcommand=$1; shift
case $subcommand in
"black" | "isort")
case $1 in
"--fix")
check=""
;;
esac
case $subcommand in
"isort") isort;;
"black") black;;
esac
;;
grahamalama marked this conversation as resolved.
Show resolved Hide resolved

"pylint") pylint;;
"mypy") mypy;;
"bandit") bandit;;
"detect-secrets") detect_secrets;;
*) usage;;
esac
fi