From e779ee2cebfbe0aa77210e17c008eec2a1d880f9 Mon Sep 17 00:00:00 2001 From: feathercyc Date: Tue, 16 Jul 2024 10:49:32 +0800 Subject: [PATCH] feat: add ci Signed-off-by: feathercyc --- .github/workflows/pr_check.yml | 64 +++++++++++++++++++++++++ ci/scripts/check-trailing-spaces.sh | 74 +++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 .github/workflows/pr_check.yml create mode 100644 ci/scripts/check-trailing-spaces.sh diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml new file mode 100644 index 0000000..811e05e --- /dev/null +++ b/.github/workflows/pr_check.yml @@ -0,0 +1,64 @@ +name: PR Checks + +on: + push: + branches: [main] + pull_request: + types: [opened, synchronize, reopened] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: clippy + override: true + + - name: Make script executable + run: chmod +x ci/scripts/check-trailing-spaces.sh + + - name: Trailing spaces check + run: ci/scripts/check-trailing-spaces.sh + + - name: Audit + run: cargo audit + + - name: Format + run: cargo fmt --all -- --check + + - name: Clippy + run: cargo clippy --all-targets --all-features -- -D warnings + + - name: Test + run: cargo test --verbose + + commit: + name: Commit Message Validation + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: git show-ref + - uses: actions-rs/install@v0.1 + with: + crate: git-cz + version: latest + - name: Validate commit messages + run: git-cz check ${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }} + + spell-check: + name: Spell Check + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Check Spelling + uses: crate-ci/typos@v1.23.1 diff --git a/ci/scripts/check-trailing-spaces.sh b/ci/scripts/check-trailing-spaces.sh new file mode 100644 index 0000000..4c2f51f --- /dev/null +++ b/ci/scripts/check-trailing-spaces.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash + +# Script Author: https://github.com/stdrc +# Repo: https://github.com/risingwavelabs/risingwave/blame/main/scripts/check/check-trailing-spaces.sh + +# Exits as soon as any line fails. +set -euo pipefail + +self=$0 + +# Shell colors +RED='\033[0;31m' +BLUE='\033[0;34m' +GREEN='\033[0;32m' +ORANGE='\033[0;33m' +BOLD='\033[1m' +NONE='\033[0m' + +print_help() { + echo "Usage: $self [-f|--fix]" + echo + echo "Options:" + echo " -f, --fix Fix trailing spaces." + echo " -h, --help Show this help message and exit." +} + +fix=false +while [ $# -gt 0 ]; do + case $1 in + -f | --fix) + fix=true + ;; + -h | --help) + print_help + exit 0 + ;; + *) + echo -e "${RED}${BOLD}$self: invalid option \`$1\`\n${NONE}" + print_help + exit 1 + ;; + esac + shift +done + +temp_file=$(mktemp) + +echo -ne "${BLUE}" +git config --global --add safe.directory '*' +git grep -nIP --untracked '[[:space:]]+$' | tee $temp_file || true +echo -ne "${NONE}" + +bad_files=$(cat $temp_file | cut -f1 -d ':' | sort -u) +rm $temp_file + +if [ ! -z "$bad_files" ]; then + if [[ $fix == true ]]; then + for file in $bad_files; do + sed -i '' -e's/[[:space:]]*$//' "$file" + done + + echo + echo -e "${GREEN}${BOLD}All trailing spaces listed above have been cleaned.${NONE}" + exit 0 + else + echo + echo -e "${RED}${BOLD}Please clean all the trailing spaces listed above.${NONE}" + echo -e "${BOLD}You can run '$self --fix' for convenience.${NONE}" + exit 1 + fi +else + echo -e "${GREEN}${BOLD}No trailing spaces found.${NONE}" + exit 0 +fi