Skip to content

Commit 50085e1

Browse files
committed
chore(git): run pre commit and workflows conditionally
1 parent 08eed3f commit 50085e1

7 files changed

Lines changed: 109 additions & 98 deletions

File tree

.githooks/pre-commit

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
echo "Backend linting"
4-
golangci-lint run
5-
if [ $? -ne 0 ]; then
6-
echo "golangci-lint failed. Please fix the errors before committing."
7-
exit 1
8-
fi
9-
10-
echo "Backend testing"
11-
go test ./...
12-
if [ $? -ne 0 ]; then
13-
echo "backend tests failed. Please fix the tests before committing."
14-
exit 1
15-
fi
4+
CHECK_BACKEND=false
5+
CHECK_FRONTEND=false
6+
CHECK_SWAGGER=false
167

17-
echo "Swagger docs generation && formatting"
18-
make swagger
8+
while IFS= read -r -d '' file; do
9+
case "$file" in
10+
ui/*)
11+
CHECK_FRONTEND=true
12+
;;
13+
internal/server/api/v1/*)
14+
CHECK_BACKEND=true
15+
CHECK_SWAGGER=true
16+
;;
17+
*)
18+
CHECK_BACKEND=true
19+
;;
20+
esac
21+
done < <(git diff --cached --name-only -z --diff-filter=ACMR)
1922

20-
if ! git diff --exit-code ./docs/ ./internal/server/api/v1/; then
21-
echo "Swagger docs were not up to date."
22-
echo "They have been generated and formatted automatically."
23-
echo "Add the changes to the commit."
24-
exit 1
23+
if [ "$CHECK_BACKEND" = true ]; then
24+
echo "Checking backend"
25+
golangci-lint run
2526
fi
2627

27-
echo "Frontend linting"
28-
(cd ui && pnpm --silent run precommit:lint)
29-
if [ $? -ne 0 ]; then
30-
echo "Frontend linting failed. Please fix the errors before committing."
31-
exit 1
28+
if [ "$CHECK_SWAGGER" = true ]; then
29+
echo "Checking swagger"
30+
make swagger
31+
if ! git diff --exit-code -- ./docs/ ./internal/server/api/v1/; then
32+
echo "Swagger docs were not up to date."
33+
echo "They have been generated and formatted automatically."
34+
echo "Add the changes to the commit."
35+
exit 1
36+
fi
3237
fi
3338

34-
echo "Frontend typecheck"
35-
(cd ui && pnpm --silent run precommit:typecheck)
36-
if [ $? -ne 0 ]; then
37-
echo "Frontend type checking failed. Please fix the errors before committing."
38-
exit 1
39+
if [ "$CHECK_FRONTEND" = true ]; then
40+
echo "Checking frontend"
41+
(
42+
cd ui
43+
pnpm --silent run lint
44+
pnpm --silent run typecheck
45+
)
3946
fi

.github/workflows/backend-lint.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ on:
55
branches:
66
- main
77
pull_request:
8+
paths:
9+
- "**/*.go"
10+
- "go.mod"
11+
- "go.sum"
12+
- "golangci.yml"
813

914
permissions:
1015
contents: read

.github/workflows/backend-test.yml

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

.github/workflows/frontend-lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
branches:
66
- main
77
pull_request:
8+
paths:
9+
- "ui/**"
810

911
jobs:
1012
format-and-lint:
Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,83 @@
1-
name: Check Migration Order
1+
name: Migration order
22

33
on:
4-
push:
5-
branches:
6-
- main
74
pull_request:
8-
branches:
9-
- main
10-
types: [opened, synchronize]
5+
paths:
6+
- "db/migrations/**"
7+
push:
8+
branches: [main]
9+
10+
permissions:
11+
contents: read
1112

1213
jobs:
1314
check-migration-timestamp:
14-
name: Verify Migration Timestamp
1515
runs-on: ubuntu-latest
1616
steps:
17-
- name: Checkout code
18-
uses: actions/checkout@v4
17+
- uses: actions/checkout@v4
1918
with:
2019
fetch-depth: 0
2120

2221
- name: Verify that new migrations are the most recent
22+
shell: bash
2323
run: |
24-
set -e
24+
set -euo pipefail
2525
2626
MIGRATION_DIR="db/migrations/"
2727
2828
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
2929
BASE_REF="origin/${{ github.base_ref }}"
3030
HEAD_REF="${{ github.sha }}"
31-
32-
echo "Running on PR. Comparing HEAD with base (${{ github.base_ref }})."
33-
elif [[ "${{ github.event_name }}" == "push" ]]; then
31+
echo "PR: comparing HEAD with base (${BASE_REF})."
32+
else
3433
BASE_REF="${{ github.before }}"
3534
HEAD_REF="${{ github.sha }}"
36-
3735
if [[ "$BASE_REF" == "0000000000000000000000000000000000000000" ]]; then
38-
echo "This is the first push. No base to compare against. Skipping check."
36+
echo "First push detected. Skipping migration order check."
3937
exit 0
4038
fi
41-
42-
echo "Running on push. Comparing current commit with previous ($BASE_REF)."
39+
echo "Push: comparing ${HEAD_REF} with previous (${BASE_REF})."
4340
fi
4441
45-
LATEST_BASE_TIMESTAMP=$(git ls-tree --name-only $BASE_REF $MIGRATION_DIR 2>/dev/null | xargs -n 1 basename | cut -d '_' -f1 | sort -nr | head -n 1 || echo "0")
46-
47-
if [ -z "$LATEST_BASE_TIMESTAMP" ]; then
48-
LATEST_BASE_TIMESTAMP=0
49-
fi
42+
LATEST_BASE_TIMESTAMP="$(git ls-tree --name-only "$BASE_REF" "$MIGRATION_DIR" 2>/dev/null \
43+
| xargs -n 1 basename \
44+
| cut -d '_' -f1 \
45+
| sort -nr \
46+
| head -n 1 || true)"
5047
51-
echo "Latest migration timestamp on base branch is: $LATEST_BASE_TIMESTAMP"
48+
LATEST_BASE_TIMESTAMP="${LATEST_BASE_TIMESTAMP:-0}"
49+
echo "Latest base migration timestamp: $LATEST_BASE_TIMESTAMP"
5250
53-
ADDED_FILES=$(git diff --name-only --diff-filter=A $BASE_REF $HEAD_REF -- $MIGRATION_DIR)
54-
if [ -z "$ADDED_FILES" ]; then
55-
echo "No new migration files found. Check passed."
51+
ADDED_FILES="$(git diff --name-only --diff-filter=A "$BASE_REF" "$HEAD_REF" -- "$MIGRATION_DIR" || true)"
52+
if [[ -z "$ADDED_FILES" ]]; then
53+
echo "No new migration files added. OK."
5654
exit 0
5755
fi
5856
59-
echo "Found new migration files to check:"
57+
echo "New migration files:"
6058
echo "$ADDED_FILES"
6159
6260
HAS_ERROR=false
6361
6462
while IFS= read -r file; do
65-
filename=$(basename "$file")
66-
timestamp=$(echo "$filename" | cut -d'_' -f1)
67-
echo "-> Checking '$filename' with timestamp '$timestamp'"
63+
[[ -z "$file" ]] && continue
64+
filename="$(basename "$file")"
65+
timestamp="$(echo "$filename" | cut -d'_' -f1)"
66+
67+
echo "Checking $filename (timestamp $timestamp)"
6868
69-
# 1. Check if the timestamp is a valid number
7069
if ! [[ "$timestamp" =~ ^[0-9]+$ ]]; then
71-
echo "::error file=$file::Invalid timestamp in filename. Prefix must be numeric."
70+
echo "::error file=$file::Invalid timestamp prefix; must be numeric."
7271
HAS_ERROR=true
7372
continue
7473
fi
7574
76-
# 2. Check if the new timestamp is greater than the latest on the base branch
77-
if [ "$timestamp" -le "$LATEST_BASE_TIMESTAMP" ]; then
78-
echo "::error file=$file::Timestamp '$timestamp' is not greater than the latest on the base branch ('$LATEST_BASE_TIMESTAMP')."
75+
if [[ "$timestamp" -le "$LATEST_BASE_TIMESTAMP" ]]; then
76+
echo "::error file=$file::Timestamp '$timestamp' must be greater than base '$LATEST_BASE_TIMESTAMP'."
7977
HAS_ERROR=true
8078
fi
8179
done <<< "$ADDED_FILES"
8280
83-
if [ "$HAS_ERROR" = true ]; then
84-
exit 1
85-
fi
81+
$HAS_ERROR && exit 1
8682
87-
echo "All new migration files have valid timestamps."
83+
echo "Migration order OK."

.github/workflows/sqlc-diff.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Sqlc
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "db/**"
7+
- "pkg/sqlc/**"
8+
- "sqlc.yml"
9+
push:
10+
branches: [main]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
diff:
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- uses: sqlc-dev/setup-sqlc@v3
23+
with:
24+
sqlc-version: "1.30.0"
25+
26+
- name: sqlc diff
27+
run: sqlc diff

.github/workflows/swagger.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
branches:
66
- main
77
pull_request:
8+
paths:
9+
- "internal/server/api/v1/**"
810

911
permissions:
1012
contents: read

0 commit comments

Comments
 (0)