From 7e064bb99dd687529c12b336036db01a83334b6a Mon Sep 17 00:00:00 2001 From: marshall_lee Date: Wed, 18 Dec 2024 14:16:25 +0800 Subject: [PATCH] replace basic checks with pre-commit via action --- .github/actions/basics/action.yml | 46 ++++++++++++++++++ .github/workflows/ci.yml | 58 +---------------------- .github/workflows/pr-cargo-deny.yml | 31 ------------ .github/workflows/pr-check-link.yml | 14 ------ .github/workflows/pr.yml | 12 +++++ .pre-commit-config.yaml | 46 ++++++++++++++++++ implementations/q146_lru_cache/Cargo.toml | 2 +- implementations/q460_lfu_cache/Cargo.toml | 2 +- utilities/pre-commit | 12 ----- 9 files changed, 108 insertions(+), 115 deletions(-) create mode 100644 .github/actions/basics/action.yml delete mode 100644 .github/workflows/pr-cargo-deny.yml delete mode 100644 .github/workflows/pr-check-link.yml create mode 100644 .github/workflows/pr.yml create mode 100644 .pre-commit-config.yaml delete mode 100755 utilities/pre-commit diff --git a/.github/actions/basics/action.yml b/.github/actions/basics/action.yml new file mode 100644 index 0000000..bd53e46 --- /dev/null +++ b/.github/actions/basics/action.yml @@ -0,0 +1,46 @@ +name: "Basic Checks" + +runs: + using: "composite" + steps: + # 1. Check out repository + - uses: actions/checkout@v4 + + # 2. Set up Rust nightly toolchain + - name: Install Rust nightly + uses: dtolnay/rust-toolchain@stable + with: + toolchain: ${{ inputs.rust_nightly }} + components: clippy, rustfmt + + - uses: Swatinem/rust-cache@v2 + + # 3. Set up Node.js for markdownlint + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: "latest" + + - name: Install pre-commit + run: | + python3 -m venv venv + source venv/bin/activate + pip install pre-commit + + # 4. Run pre-commit hooks on all files + - name: Run pre-commit + run: pre-commit run --all-files + + # 5. Run linkspector + - name: Run linkspector + uses: umbrelladocs/action-linkspector@v1 + with: + github_token: ${{ secrets.github_token }} + reporter: github-pr-review + fail_on_error: true + +inputs: + rust_nightly: + description: "Rust nightly toolchain version" + required: false + default: "nightly" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9faafd..c5f748f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -287,62 +287,8 @@ jobs: # Basic actions that must pass before we kick off more expensive tests. basics: - name: basic checks - runs-on: ubuntu-latest - needs: - - clippy - - rustfmt - - markdownlint - steps: - - run: exit 0 - - rustfmt: - name: rustfmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: Install Rust ${{ env.rust_nightly }} - uses: dtolnay/rust-toolchain@stable + - uses: ./.github/actions/basics with: - toolchain: ${{ env.rust_nightly }} - components: rustfmt - - uses: Swatinem/rust-cache@v2 - # Check rustfmt - - name: "rustfmt --check" - # Workaround for rust-lang/cargo#7732 - run: | - if ! rustfmt --check --edition 2024 $(git ls-files '*.rs'); then - printf "Please run \`rustfmt --edition 2024 \$(git ls-files '*.rs')\` to fix rustfmt errors.\n" >&2 - exit 1 - fi - - clippy: - name: clippy - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Install Rust ${{ env.rust_nightly }} - uses: dtolnay/rust-toolchain@stable - with: - toolchain: ${{ env.rust_nightly }} - components: clippy - - uses: Swatinem/rust-cache@v2 - # Run clippy - - name: "clippy --all" - run: cargo clippy --all --tests --all-features --no-deps - - markdownlint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: latest - - - name: Install markdownlint-cli - run: npm install -g markdownlint-cli - - - name: Run markdownlint - run: markdownlint '**/*.md' --ignore node_modules + rust_nightly: ${{ env.rust_nightly }} diff --git a/.github/workflows/pr-cargo-deny.yml b/.github/workflows/pr-cargo-deny.yml deleted file mode 100644 index 4ed1bcf..0000000 --- a/.github/workflows/pr-cargo-deny.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: Pull Request Security Audit - -on: - push: - paths: - - "**/Cargo.toml" - pull_request: - paths: - - "**/Cargo.toml" - -concurrency: - group: - ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} - cancel-in-progress: true - -permissions: - contents: read - -env: - rust_nightly: nightly-2024-12-02 - -jobs: - cargo-deny: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Install Rust ${{ env.rust_nightly }} - uses: dtolnay/rust-toolchain@stable - with: - toolchain: ${{ env.rust_nightly }} - - uses: EmbarkStudios/cargo-deny-action@v2 diff --git a/.github/workflows/pr-check-link.yml b/.github/workflows/pr-check-link.yml deleted file mode 100644 index c297adb..0000000 --- a/.github/workflows/pr-check-link.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Linkspector -on: [pull_request] -jobs: - check-links: - name: runner / linkspector - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Run linkspector - uses: umbrelladocs/action-linkspector@v1 - with: - github_token: ${{ secrets.github_token }} - reporter: github-pr-review - fail_on_error: true diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..bd85ad4 --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,12 @@ +name: PR Checks + +on: + pull_request: + +jobs: + basics: + runs-on: ubuntu-latest + steps: + - uses: ./.github/actions/basics + with: + rust_nightly: nightly-2024-12-02 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..043953e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,46 @@ +repos: + - repo: local + hooks: + # Rustfmt formatting check + - id: rustfmt + name: Rustfmt + language: system + entry: rustfmt + args: ["--edition", "2024", "--check"] + pass_filenames: true + files: \.rs$ + + # Cargo Clippy linting + - id: cargo-clippy + name: Cargo Clippy + language: system + entry: cargo + args: + [ + "clippy", + "--all", + "--tests", + "--all-features", + "--no-deps", + "--", + "-D", + "warnings", + ] + pass_filenames: false + + # Cargo deny check + - id: cargo-deny + name: Cargo Deny + language: system + entry: cargo + args: ["deny", "check"] + pass_filenames: false + + # Markdown linting + - id: markdownlint + name: Markdownlint + language: node + entry: markdownlint + args: ["--ignore", "node_modules"] + pass_filenames: true + files: \.md$ diff --git a/implementations/q146_lru_cache/Cargo.toml b/implementations/q146_lru_cache/Cargo.toml index 2400b60..488f5ad 100644 --- a/implementations/q146_lru_cache/Cargo.toml +++ b/implementations/q146_lru_cache/Cargo.toml @@ -10,7 +10,7 @@ intrusive-collections = "0.9.7" rand = "0.8.5" proptest = "1.5.0" mockall = "0.13.1" -cache_util = { path = "../../utilities/cache_util" } +cache_util = { path = "../../utilities/cache_util", version = "0.1.0" } [dev-dependencies] rstest = "0.23.0" diff --git a/implementations/q460_lfu_cache/Cargo.toml b/implementations/q460_lfu_cache/Cargo.toml index 0a17300..9f2e0c0 100644 --- a/implementations/q460_lfu_cache/Cargo.toml +++ b/implementations/q460_lfu_cache/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT" [dependencies] priority-queue = "2.1.1" intrusive-collections = "0.9.7" -cache_util = { path = "../../utilities/cache_util" } +cache_util = { path = "../../utilities/cache_util", version = "0.1.0" } [dev-dependencies] rstest = "0.23.0" diff --git a/utilities/pre-commit b/utilities/pre-commit deleted file mode 100755 index 7710911..0000000 --- a/utilities/pre-commit +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -# [Run before committing via hook] -# ln -s ../../utilities/pre-commit .git/hooks/ - -set -e # Exit on error - -rustfmt --check --edition 2024 $(git ls-files '*.rs') - -cargo clippy --all --tests --all-features --no-deps - -markdownlint '**/*.md' --ignore node_modules \ No newline at end of file