feat: add pre-commit-hook

This commit is contained in:
debugtalk
2022-05-09 16:05:47 +08:00
parent 0d89cda921
commit 10544f95c6
6 changed files with 82 additions and 15 deletions
+5
View File
@@ -18,6 +18,11 @@ build: ## build hrp cli tool
@echo "[info] build hrp cli tool" @echo "[info] build hrp cli tool"
@. scripts/build.sh @. scripts/build.sh
.PHONY: install-hooks
install-hooks: ## install git hooks
@find scripts -name "install-*-hook" | awk -F'-' '{s=$$2;for(i=3;i<NF;i++){s=s"-"$$i;}print s;}' | while read f; do bash "scripts/install-$$f-hook"; done
@echo "[OK] install all hooks"
.PHONY: help .PHONY: help
help: ## print make commands help: ## print make commands
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ @grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
+3 -1
View File
@@ -1,6 +1,8 @@
# Release History # Release History
## v4.1.0-alpha (2022-05-07) ## v4.1.0-alpha (2022-05-09)
- feat: add pre-commit-hook to format go/python code
**go version** **go version**
+6 -1
View File
@@ -11,7 +11,11 @@ from httprunner.models import IStep, StepResult, TStep
from httprunner.models import SqlMethodEnum, TSqlRequest from httprunner.models import SqlMethodEnum, TSqlRequest
from httprunner.response import SqlResponseObject from httprunner.response import SqlResponseObject
from httprunner.runner import HttpRunner from httprunner.runner import HttpRunner
from httprunner.step_request import (StepRequestExtraction, StepRequestValidation, call_hooks) from httprunner.step_request import (
StepRequestExtraction,
StepRequestValidation,
call_hooks,
)
try: try:
import sqlalchemy import sqlalchemy
@@ -73,6 +77,7 @@ def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult:
if not runner.db_engine: if not runner.db_engine:
ensure_sql_ready() ensure_sql_ready()
from httprunner.database.engine import DBEngine from httprunner.database.engine import DBEngine
runner.db_engine = DBEngine( runner.db_engine = DBEngine(
f'mysql+pymysql://{parsed_request_dict["db_config"]["user"]}:' f'mysql+pymysql://{parsed_request_dict["db_config"]["user"]}:'
f'{parsed_request_dict["db_config"]["password"]}@{parsed_request_dict["db_config"]["ip"]}:' f'{parsed_request_dict["db_config"]["password"]}@{parsed_request_dict["db_config"]["ip"]}:'
+1
View File
@@ -105,6 +105,7 @@ def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult:
if not runner.thrift_client: if not runner.thrift_client:
ensure_thrift_ready() ensure_thrift_ready()
from httprunner.thrift.thrift_client import ThriftClient from httprunner.thrift.thrift_client import ThriftClient
runner.thrift_client = ThriftClient( runner.thrift_client = ThriftClient(
thrift_file=parsed_request_dict["idl_path"], thrift_file=parsed_request_dict["idl_path"],
service_name=parsed_request_dict["service_name"], service_name=parsed_request_dict["service_name"],
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
echo "SCRIPT_DIR:, $SCRIPT_DIR"
# assume the script is always in <repository>/scripts
pushd "$SCRIPT_DIR/.." >/dev/null
PRE_COMMIT_FILE=.git/hooks/pre-commit
# install pre-commit hook and make it executable
function install() {
go get mvdan.cc/gofumpt
go get github.com/incu6us/goimports-reviser/v2@latest
cat > $PRE_COMMIT_FILE <<'EOF'
#!/bin/bash
# What does this script do?
# 1. gofumpt go files automatically
# 2. goimports-reviser go files automatically
# 3. black python files automatically
# make sure gofumpt is installed
# What does each letter mean in "ACMRTUXB"?
# Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink,
# submodule, ...) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B)
for file in $(git diff --name-only --cached --diff-filter=ACMRTUXB | grep '.go$')
do
echo "(gofumpt) $file"
gofumpt -w "$file"
echo "(goimports-reviser) $file"
goimports-reviser -file-path "$file" -rm-unused
git add "$file"
done
for file in $(git diff --name-only --cached --diff-filter=ACMRTUXB | grep '.py$')
do
echo "(black) $file"
black "$file"
git add "$file"
done
EOF
chmod +x $PRE_COMMIT_FILE
}
if [[ -f $PRE_COMMIT_FILE ]]; then
echo "Backing up $PRE_COMMIT_FILE to ${PRE_COMMIT_FILE}.bak"
mv $PRE_COMMIT_FILE ${PRE_COMMIT_FILE}.bak
install
else
install
fi
popd >/dev/null