Skip to content

Refactor code and enhance maintainability. #61

Refactor code and enhance maintainability.

Refactor code and enhance maintainability. #61

Workflow file for this run

name: CI Formatting Check
on:
push:
branches: [main, develop]
pull_request:
branches: [main, develop]
workflow_dispatch:
jobs:
formatting_check:
runs-on: ubuntu-latest
env:
CODE_DIR: code
WEB_FRONTEND_DIR: web-frontend
MOBILE_FRONTEND_DIR: mobile-frontend
INFRASTRUCTURE_DIR: infrastructure
steps:
# -------------------------------
# Checkout repository
# -------------------------------
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
# -------------------------------
# Python Setup + Caching
# -------------------------------
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Cache pip
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Python formatters
run: pip install --upgrade pip && pip install autoflake black
# -------------------------------
# Run Python formatting checks
# -------------------------------
- name: Run Python formatting checks (code directory)
shell: bash
run: |
set -euo pipefail
if [ ! -d "${{ env.CODE_DIR }}" ]; then
echo "Code directory '${{ env.CODE_DIR }}' not found — skipping Python formatting."
exit 0
fi
echo "=== Preparing temporary copy of code/ for autoflake ==="
TMP_COPY="$(mktemp -d)"
cp -a "${{ env.CODE_DIR }}/." "$TMP_COPY/"
echo "=== Running autoflake (will modify temp copy) ==="
autoflake \
--remove-all-unused-imports \
--remove-unused-variables \
--recursive \
--ignore-init-module-imports \
--in-place \
"$TMP_COPY"
echo "=== Comparing code/ with autoflake-modified copy ==="
if ! diff -r "${{ env.CODE_DIR }}" "$TMP_COPY" > /dev/null; then
echo "autoflake found formatting/unused-imports issues in ${CODE_DIR}."
echo "Run locally: autoflake --remove-all-unused-imports --remove-unused-variables --recursive --ignore-init-module-imports --in-place ${CODE_DIR}"
rm -rf "$TMP_COPY"
exit 1
fi
rm -rf "$TMP_COPY"
echo "=== Running black (check mode) on ${CODE_DIR} ==="
black "${{ env.CODE_DIR }}" --check
# -------------------------------
# Node + npm caching
# -------------------------------
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
cache-dependency-path: tools/package-lock.json # ← tells the action where the lock file lives
- name: Install Node dependencies
working-directory: tools # ← run npm ci from tools/
run: npm ci
# -------------------------------
# Prettier checks
# -------------------------------
# 1) Web frontend
- name: Run Prettier Checks (web-frontend)
if: ${{ env.WEB_FRONTEND_DIR != '' }}
working-directory: tools # ← run npx from tools/
run: |
echo "=== Running Prettier (web-frontend) ==="
npx --no-install prettier --check "../${{ env.WEB_FRONTEND_DIR }}/**/*.{js,jsx,ts,tsx,json,html,css,md}" --ignore-path ../.prettierignore || {
echo "Prettier issues found in ${WEB_FRONTEND_DIR}."
exit 1
}
# 2) Mobile frontend
- name: Run Prettier Checks (mobile-frontend)
if: ${{ env.MOBILE_FRONTEND_DIR != '' }}
working-directory: tools
run: |
echo "=== Running Prettier (mobile-frontend) ==="
npx --no-install prettier --check "../${{ env.MOBILE_FRONTEND_DIR }}/**/*.{js,jsx,ts,tsx,json,html,css,md}" --ignore-path ../.prettierignore || {
echo "Prettier issues found in ${MOBILE_FRONTEND_DIR}."
exit 1
}
# 3) Repository-wide Markdown check (ALL .md files)
- name: Run Prettier Checks (all .md files in repo)
working-directory: tools
run: |
echo "=== Running Prettier (all .md files) ==="
npx --no-install prettier --check "../**/*.md" --ignore-path ../.prettierignore || {
echo "Prettier formatting issues detected in markdown files across the repo."
exit 1
}
# 4) Infrastructure YAML/YML files only (uses env.INFRASTRUCTURE_DIR)
- name: Run Prettier Checks (infrastructure YAML)
working-directory: tools
run: |
if [ -n "${{ env.INFRASTRUCTURE_DIR }}" ] && [ -d "../${{ env.INFRASTRUCTURE_DIR }}" ]; then
echo "=== Running Prettier (infrastructure YAML/YML files) in ${INFRASTRUCTURE_DIR} ==="
npx --no-install prettier --check "../${{ env.INFRASTRUCTURE_DIR }}/**/*.{yml,yaml}" --ignore-path ../.prettierignore || {
echo "Prettier formatting issues detected in ${INFRASTRUCTURE_DIR} YAML/YML files."
exit 1
}
else
echo "No infrastructure directory '${{ env.INFRASTRUCTURE_DIR }}' found or INFRASTRUCTURE_DIR unset — skipping infra YAML checks."
fi
# -------------------------------
# Finalize
# -------------------------------
- name: Finalize Check
run: echo "All formatting checks completed successfully."