From 5ef59e4e9ece8935dde13f71854bcfe98cbc7314 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Fri, 20 Dec 2024 11:37:31 -0500 Subject: [PATCH] feat(workflow): make common linter re-usable --- .github/workflows/common-lint.yml | 252 +------------------------- .github/workflows/common-linter.yml | 265 ++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+), 249 deletions(-) create mode 100644 .github/workflows/common-linter.yml diff --git a/.github/workflows/common-lint.yml b/.github/workflows/common-lint.yml index 10692ad..45e0062 100644 --- a/.github/workflows/common-lint.yml +++ b/.github/workflows/common-lint.yml @@ -5,7 +5,7 @@ # Common linting. -name: common lint +name: Common Lint on: pull_request: @@ -17,251 +17,5 @@ concurrency: cancel-in-progress: true jobs: - lint: - name: Common Lint - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Set up Python - uses: actions/setup-python@v5 - with: - python-version: '3.12' - - - name: Install dependencies - run: | - python -m pip install --upgrade \ - pip \ - setuptools \ - wheel \ - cmakelang \ - flake8 \ - nb-clean \ - nbqa[toolchain] - - - name: C++ - find files - id: cpp_files - run: | - # find files - found_files=$(find . -type f \ - -iname "*.c" -o \ - -iname "*.cpp" -o \ - -iname "*.h" -o \ - -iname "*.hpp" -o \ - -iname "*.m" -o \ - -iname "*.mm" \ - ) - ignore_files=$(find . -type f -iname ".clang-format-ignore") - - # Loop through each C++ file - for file in $found_files; do - for ignore_file in $ignore_files; do - ignore_directory=$(dirname "$ignore_file") - # if directory of ignore_file is beginning of file - if [[ "$file" == "$ignore_directory"* ]]; then - echo "ignoring file: ${file}" - found_files="${found_files//${file}/}" - break 1 - fi - done - done - - # remove empty lines - found_files=$(echo "$found_files" | sed '/^\s*$/d') - - echo "found cpp files: ${found_files}" - - # do not quote to keep this as a single line - echo found_files=${found_files} >> $GITHUB_OUTPUT - - - name: C++ - Clang format lint - if: always() && steps.cpp_files.outputs.found_files - uses: DoozyX/clang-format-lint-action@v0.18 - with: - source: ${{ steps.cpp_files.outputs.found_files }} - extensions: 'c,cpp,h,hpp,m,mm' - style: file - inplace: false - - - name: CMake - find files - id: cmake_files - if: always() - run: | - # find files - found_files=$(find . -type f -iname "CMakeLists.txt" -o -iname "*.cmake") - ignore_files=$(find . -type f -iname ".cmake-lint-ignore") - - # Loop through each C++ file - for file in $found_files; do - for ignore_file in $ignore_files; do - ignore_directory=$(dirname "$ignore_file") - # if directory of ignore_file is beginning of file - if [[ "$file" == "$ignore_directory"* ]]; then - echo "ignoring file: ${file}" - found_files="${found_files//${file}/}" - break 1 - fi - done - done - - # remove empty lines - found_files=$(echo "$found_files" | sed '/^\s*$/d') - - echo "found cmake files: ${found_files}" - - # do not quote to keep this as a single line - echo found_files=${found_files} >> $GITHUB_OUTPUT - - - name: CMake - cmake-lint - if: always() && steps.cmake_files.outputs.found_files - run: | - cmake-lint --line-width 120 --tab-size 4 ${{ steps.cmake_files.outputs.found_files }} - - - name: Docker - find files - id: dokcer_files - if: always() - run: | - found_files=$(find . -type f -iname "Dockerfile" -o -iname "*.dockerfile") - - echo "found_files: ${found_files}" - - # do not quote to keep this as a single line - echo found_files=${found_files} >> $GITHUB_OUTPUT - - - name: Docker - hadolint - if: always() && steps.dokcer_files.outputs.found_files - run: | - docker pull hadolint/hadolint - - # create hadolint config file - cat < .hadolint.yaml - --- - ignored: - - DL3008 - - DL3013 - - DL3016 - - DL3018 - - DL3028 - - DL3059 - EOF - - failed=0 - failed_files="" - - for file in ${{ steps.dokcer_files.outputs.found_files }}; do - echo "::group::${file}" - docker run --rm -i \ - -e "NO_COLOR=0" \ - -e "HADOLINT_VERBOSE=1" \ - -v $(pwd)/.hadolint.yaml:/.config/hadolint.yaml \ - hadolint/hadolint < $file || { - failed=1 - failed_files="$failed_files $file" - } - echo "::endgroup::" - done - - if [ $failed -ne 0 ]; then - echo "::error:: hadolint failed for the following files: $failed_files" - exit 1 - fi - - - name: Python - flake8 - if: always() - run: | - python -m flake8 \ - --color=always \ - --verbose - - - name: Python - nbqa flake8 - if: always() - run: | - python -m nbqa flake8 \ - --color=always \ - --verbose \ - . - - - name: Python - nb-clean - if: always() - run: | - output=$(find . -name '*.ipynb' -exec nb-clean check {} \;) - - # fail if there are any issues - if [ -n "$output" ]; then - echo "$output" - exit 1 - fi - - - name: Rust - find Cargo.toml - id: run_cargo - if: always() - run: | - # check if Cargo.toml exists - if [ -f "Cargo.toml" ]; then - echo "found_cargo=true" >> $GITHUB_OUTPUT - else - echo "found_cargo=false" >> $GITHUB_OUTPUT - fi - - - name: Rust - setup toolchain - if: always() && steps.run_cargo.outputs.found_cargo == 'true' - uses: dtolnay/rust-toolchain@stable - with: - components: rustfmt - - - name: Rust - cargo fmt - if: always() && steps.run_cargo.outputs.found_cargo == 'true' - run: | - cargo fmt -- --check - - - name: YAML - find files - id: yaml_files - if: always() - run: | - # space separated list of files - FILES=.clang-format - - # empty placeholder - found_files="" - - for FILE in ${FILES}; do - if [ -f "$FILE" ] - then - found_files="$found_files $FILE" - fi - done - - echo "found_files=${found_files}" >> $GITHUB_OUTPUT - - - name: YAML - yamllint - id: yamllint - if: always() - uses: ibiqlik/action-yamllint@v3 - with: - # https://yamllint.readthedocs.io/en/stable/configuration.html#default-configuration - config_data: | - extends: default - rules: - comments: - level: error - document-start: - level: error - line-length: - max: 120 - new-line-at-end-of-file: - level: error - new-lines: - type: unix - truthy: - # GitHub uses "on" for workflow event triggers - # .clang-format file has options of "Yes" "No" that will be caught by this, so changed to "warning" - allowed-values: ['true', 'false', 'on'] - check-keys: true - level: warning - file_or_dir: . ${{ steps.yaml_files.outputs.found_files }} - - - name: YAML - log - if: always() && steps.yamllint.outcome == 'failure' - run: | - cat "${{ steps.yamllint.outputs.logfile }}" >> $GITHUB_STEP_SUMMARY + call-common-lint: + uses: LizardByte/.github/.github/workflows/common-linter.yml@feat/workflow/make-common-linter-re-usable diff --git a/.github/workflows/common-linter.yml b/.github/workflows/common-linter.yml new file mode 100644 index 0000000..613ebfa --- /dev/null +++ b/.github/workflows/common-linter.yml @@ -0,0 +1,265 @@ +--- +name: Common Linter + +on: + workflow_call: + inputs: + runs-on: + default: 'ubuntu-latest' + required: false + type: string + container: + default: '' + required: false + type: string + +jobs: + lint: + name: Common Lint + runs-on: ${{ inputs.runs-on }} + container: ${{ inputs.container }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade \ + pip \ + setuptools \ + wheel \ + cmakelang \ + flake8 \ + nb-clean \ + nbqa[toolchain] + + - name: C++ - find files + id: cpp_files + run: | + # find files + found_files=$(find . -type f \ + -iname "*.c" -o \ + -iname "*.cpp" -o \ + -iname "*.h" -o \ + -iname "*.hpp" -o \ + -iname "*.m" -o \ + -iname "*.mm" \ + ) + ignore_files=$(find . -type f -iname ".clang-format-ignore") + + # Loop through each C++ file + for file in $found_files; do + for ignore_file in $ignore_files; do + ignore_directory=$(dirname "$ignore_file") + # if directory of ignore_file is beginning of file + if [[ "$file" == "$ignore_directory"* ]]; then + echo "ignoring file: ${file}" + found_files="${found_files//${file}/}" + break 1 + fi + done + done + + # remove empty lines + found_files=$(echo "$found_files" | sed '/^\s*$/d') + + echo "found cpp files: ${found_files}" + + # do not quote to keep this as a single line + echo found_files=${found_files} >> $GITHUB_OUTPUT + + - name: C++ - Clang format lint + if: always() && steps.cpp_files.outputs.found_files + uses: DoozyX/clang-format-lint-action@v0.18 + with: + source: ${{ steps.cpp_files.outputs.found_files }} + extensions: 'c,cpp,h,hpp,m,mm' + style: file + inplace: false + + - name: CMake - find files + id: cmake_files + if: always() + run: | + # find files + found_files=$(find . -type f -iname "CMakeLists.txt" -o -iname "*.cmake") + ignore_files=$(find . -type f -iname ".cmake-lint-ignore") + + # Loop through each C++ file + for file in $found_files; do + for ignore_file in $ignore_files; do + ignore_directory=$(dirname "$ignore_file") + # if directory of ignore_file is beginning of file + if [[ "$file" == "$ignore_directory"* ]]; then + echo "ignoring file: ${file}" + found_files="${found_files//${file}/}" + break 1 + fi + done + done + + # remove empty lines + found_files=$(echo "$found_files" | sed '/^\s*$/d') + + echo "found cmake files: ${found_files}" + + # do not quote to keep this as a single line + echo found_files=${found_files} >> $GITHUB_OUTPUT + + - name: CMake - cmake-lint + if: always() && steps.cmake_files.outputs.found_files + run: | + cmake-lint --line-width 120 --tab-size 4 ${{ steps.cmake_files.outputs.found_files }} + + - name: Docker - find files + id: dokcer_files + if: always() + run: | + found_files=$(find . -type f -iname "Dockerfile" -o -iname "*.dockerfile") + + echo "found_files: ${found_files}" + + # do not quote to keep this as a single line + echo found_files=${found_files} >> $GITHUB_OUTPUT + + - name: Docker - hadolint + if: always() && steps.dokcer_files.outputs.found_files + run: | + docker pull hadolint/hadolint + + # create hadolint config file + cat < .hadolint.yaml + --- + ignored: + - DL3008 + - DL3013 + - DL3016 + - DL3018 + - DL3028 + - DL3059 + EOF + + failed=0 + failed_files="" + + for file in ${{ steps.dokcer_files.outputs.found_files }}; do + echo "::group::${file}" + docker run --rm -i \ + -e "NO_COLOR=0" \ + -e "HADOLINT_VERBOSE=1" \ + -v $(pwd)/.hadolint.yaml:/.config/hadolint.yaml \ + hadolint/hadolint < $file || { + failed=1 + failed_files="$failed_files $file" + } + echo "::endgroup::" + done + + if [ $failed -ne 0 ]; then + echo "::error:: hadolint failed for the following files: $failed_files" + exit 1 + fi + + - name: Python - flake8 + if: always() + run: | + python -m flake8 \ + --color=always \ + --verbose + + - name: Python - nbqa flake8 + if: always() + run: | + python -m nbqa flake8 \ + --color=always \ + --verbose \ + . + + - name: Python - nb-clean + if: always() + run: | + output=$(find . -name '*.ipynb' -exec nb-clean check {} \;) + + # fail if there are any issues + if [ -n "$output" ]; then + echo "$output" + exit 1 + fi + + - name: Rust - find Cargo.toml + id: run_cargo + if: always() + run: | + # check if Cargo.toml exists + if [ -f "Cargo.toml" ]; then + echo "found_cargo=true" >> $GITHUB_OUTPUT + else + echo "found_cargo=false" >> $GITHUB_OUTPUT + fi + + - name: Rust - setup toolchain + if: always() && steps.run_cargo.outputs.found_cargo == 'true' + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt + + - name: Rust - cargo fmt + if: always() && steps.run_cargo.outputs.found_cargo == 'true' + run: | + cargo fmt -- --check + + - name: YAML - find files + id: yaml_files + if: always() + run: | + # space separated list of files + FILES=.clang-format + + # empty placeholder + found_files="" + + for FILE in ${FILES}; do + if [ -f "$FILE" ] + then + found_files="$found_files $FILE" + fi + done + + echo "found_files=${found_files}" >> $GITHUB_OUTPUT + + - name: YAML - yamllint + id: yamllint + if: always() + uses: ibiqlik/action-yamllint@v3 + with: + # https://yamllint.readthedocs.io/en/stable/configuration.html#default-configuration + config_data: | + extends: default + rules: + comments: + level: error + document-start: + level: error + line-length: + max: 120 + new-line-at-end-of-file: + level: error + new-lines: + type: unix + truthy: + # GitHub uses "on" for workflow event triggers + # .clang-format file has options of "Yes" "No" that will be caught by this, so changed to "warning" + allowed-values: ['true', 'false', 'on'] + check-keys: true + level: warning + file_or_dir: . ${{ steps.yaml_files.outputs.found_files }} + + - name: YAML - log + if: always() && steps.yamllint.outcome == 'failure' + run: | + cat "${{ steps.yamllint.outputs.logfile }}" >> $GITHUB_STEP_SUMMARY