Skip to content

ci: Enable initial unit tests and remove auto-formatting in PRs #9

ci: Enable initial unit tests and remove auto-formatting in PRs

ci: Enable initial unit tests and remove auto-formatting in PRs #9

Workflow file for this run

# Copyright (c) 2025, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: PyLint and flake8 linting
on:
pull_request:
types: [opened, synchronize, reopened, labeled, unlabeled]
workflow_call:
jobs:
linting:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: |
pip install -r requirements/requirements_test.txt
- name: Get changed files
id: changed-files
uses: step-security/changed-files@v45.0.1
with:
files: |
**/*.py
- name: Run PyLint
id: pylint
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
SKIP_DOCS: ${{ contains(github.event.pull_request.labels.*.name, 'skip-docs') }}
SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }}
run: |
if [[ -z "$CHANGED_FILES" ]]; then
echo Nothing to lint.
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
exit 0
fi
if [[ $SKIP_DOCS == true ]]; then
ADDITIONAL_PYLINT_ARGS="--disable=C0115,C0116"
else
ADDITIONAL_PYLINT_ARGS=""
fi
if [[ $SKIP_LINTING == true ]]; then
ADDITIONAL_PYLINT_ARGS="--exit-zero"
fi
set +e
pylint $ADDITIONAL_PYLINT_ARGS --output "pylintrc.txt" --rcfile ".pylintrc" ${CHANGED_FILES[@]}
echo "exit-code=$?" | tee -a "$GITHUB_OUTPUT"
- name: Run flake8
id: flake8
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }}
run: |
if [[ -z "$CHANGED_FILES" ]]; then
echo Nothing to lint.
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
exit 0
fi
if [[ $SKIP_LINTING == true ]]; then
ADDITIONAL_FLAKE8_ARGS="--exit-zero"
else
ADDITIONAL_FLAKE8_ARGS=""
fi
set +e
flake8 $ADDITIONAL_FLAKE8_ARGS --output "flake8.txt" --config ".flake8" ${CHANGED_FILES[@]}
echo "exit-code=$?" | tee -a "$GITHUB_OUTPUT"
- name: Run isort
id: isort
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }}
run: |
if [[ -z "$CHANGED_FILES" ]]; then
echo Nothing to lint.
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
exit 0
fi
set +e
isort --check-only --diff ${CHANGED_FILES[@]} > isort.txt
ISORT_EXIT_CODE=$?
if [[ $SKIP_LINTING == true ]]; then
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
else
echo "exit-code=$ISORT_EXIT_CODE" | tee -a "$GITHUB_OUTPUT"
fi
- name: Run black
id: black
env:
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }}
run: |
if [[ -z "$CHANGED_FILES" ]]; then
echo Nothing to lint.
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
exit 0
fi
set +e
black --check --diff ${CHANGED_FILES[@]} > black.txt
BLACK_EXIT_CODE=$?
if [[ $SKIP_LINTING == true ]]; then
echo "exit-code=0" | tee -a "$GITHUB_OUTPUT"
else
echo "exit-code=$BLACK_EXIT_CODE" | tee -a "$GITHUB_OUTPUT"
fi
- name: Summary
env:
PYLINT: ${{ steps.pylint.outputs.exit-code == 0 }}
FLAKE8: ${{ steps.flake8.outputs.exit-code == 0 }}
ISORT: ${{ steps.isort.outputs.exit-code == 0 }}
BLACK: ${{ steps.black.outputs.exit-code == 0 }}
SKIP_LINTING: ${{ contains(github.event.pull_request.labels.*.name, 'skip-linting') }}
run: |
if [[ "$PYLINT" != "true" ]]; then
echo "Pylint output:" | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
cat pylintrc.txt | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
fi
if [[ "$FLAKE8" != "true" ]]; then
echo "Flake8 output:" | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
cat flake8.txt | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
fi
if [[ "$ISORT" != "true" ]]; then
echo "isort output:" | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
cat isort.txt | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
fi
if [[ "$BLACK" != "true" ]]; then
echo "black output:" | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
cat black.txt | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
fi
if [[ "$PYLINT" != "true" || "$FLAKE8" != "true" || "$ISORT" != "true" || "$BLACK" != "true" ]]; then
echo "The following directories got scanned:" | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
echo ${{ steps.filter.outputs.main }} | tee -a $GITHUB_STEP_SUMMARY
echo '```' | tee -a $GITHUB_STEP_SUMMARY
exit 1
fi
Nemo_Linting_Test:
needs: linting
runs-on: ubuntu-latest
if: always()
steps:
- name: Main
env:
RESULTS: ${{ toJson(needs.linting) }}
run: |
RESULT=$(echo "$RESULTS" | jq -r '.result')
if [[ "$RESULT" == "success" ]]; then
echo "All passed."
exit 0
else
echo "Some linting domains failed."
exit 1
fi