|
| 1 | +# ==================================================================================================================== # |
| 2 | +# Authors: # |
| 3 | +# Patrick Lehmann # |
| 4 | +# # |
| 5 | +# ==================================================================================================================== # |
| 6 | +# Copyright 2025-2025 The pyTooling Authors # |
| 7 | +# # |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); # |
| 9 | +# you may not use this file except in compliance with the License. # |
| 10 | +# You may obtain a copy of the License at # |
| 11 | +# # |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 # |
| 13 | +# # |
| 14 | +# Unless required by applicable law or agreed to in writing, software # |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, # |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # |
| 17 | +# See the License for the specific language governing permissions and # |
| 18 | +# limitations under the License. # |
| 19 | +# # |
| 20 | +# SPDX-License-Identifier: Apache-2.0 # |
| 21 | +# ==================================================================================================================== # |
| 22 | +name: Code Quality Checking |
| 23 | + |
| 24 | +on: |
| 25 | + workflow_call: |
| 26 | + inputs: |
| 27 | + ubuntu_image_version: |
| 28 | + description: 'Ubuntu image version.' |
| 29 | + required: false |
| 30 | + default: '24.04' |
| 31 | + type: string |
| 32 | + python_version: |
| 33 | + description: 'Python version.' |
| 34 | + required: false |
| 35 | + default: '3.13' |
| 36 | + type: string |
| 37 | + package_directory: |
| 38 | + description: 'The package''s directory' |
| 39 | + required: true |
| 40 | + type: string |
| 41 | + requirements: |
| 42 | + description: 'Python dependencies to be installed through pip.' |
| 43 | + required: false |
| 44 | + default: '-r requirements.txt' |
| 45 | + type: string |
| 46 | + bandit: |
| 47 | + description: 'Run bandit checks.' |
| 48 | + required: false |
| 49 | + default: 'true' |
| 50 | + type: string |
| 51 | + radon: |
| 52 | + description: 'Run radon checks.' |
| 53 | + required: false |
| 54 | + default: 'true' |
| 55 | + type: string |
| 56 | + pylint: |
| 57 | + description: 'Run pylint checks.' |
| 58 | + required: false |
| 59 | + default: 'true' |
| 60 | + type: string |
| 61 | + artifact: |
| 62 | + description: 'Name of the package artifact.' |
| 63 | + required: true |
| 64 | + type: string |
| 65 | + |
| 66 | +jobs: |
| 67 | + Bandit: |
| 68 | + name: 🚨 Security Scanning (Bandit) |
| 69 | + runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}" |
| 70 | + if: inputs.bandit == 'true' |
| 71 | + |
| 72 | + steps: |
| 73 | + - name: ⏬ Checkout repository |
| 74 | + uses: actions/checkout@v5 |
| 75 | + with: |
| 76 | + lfs: true |
| 77 | + submodules: true |
| 78 | + |
| 79 | + - name: 🐍 Setup Python ${{ inputs.python_version }} |
| 80 | + uses: actions/setup-python@v6 |
| 81 | + with: |
| 82 | + python-version: ${{ inputs.python_version }} |
| 83 | + |
| 84 | + - name: ⚙ Install dependencies for running bandit |
| 85 | + run: python -m pip install --disable-pip-version-check bandit |
| 86 | + |
| 87 | + - name: 👮 Bandit |
| 88 | + id: bandit |
| 89 | + if: inputs.artifact != '' |
| 90 | + run: | |
| 91 | + set +e |
| 92 | +
|
| 93 | + ANSI_LIGHT_RED=$'\x1b[91m' |
| 94 | + ANSI_LIGHT_GREEN=$'\x1b[92m' |
| 95 | + ANSI_LIGHT_BLUE=$'\x1b[94m' |
| 96 | + ANSI_NOCOLOR=$'\x1b[0m' |
| 97 | +
|
| 98 | + bandit_directory=report/bandit |
| 99 | + bandit_fullpath=report/bandit/report.xml |
| 100 | +
|
| 101 | + tee "${GITHUB_OUTPUT}" <<EOF |
| 102 | + bandit_directory=${bandit_directory} |
| 103 | + bandit_fullpath=${bandit_fullpath} |
| 104 | + EOF |
| 105 | +
|
| 106 | + mkdir -p ${bandit_directory} |
| 107 | + printf "\nRun bandit ...\n" |
| 108 | + bandit -c pyproject.toml -r ${{ inputs.package_directory }} -f xml -o ${bandit_fullpath} |
| 109 | + if [[ $? -eq 0 ]]; then |
| 110 | + printf "Bandit result: ${ANSI_LIGHT_GREEN}[PASSED]${ANSI_NOCOLOR}\n" |
| 111 | +
|
| 112 | + printf "bandit_passed=true\n" >> "${GITHUB_OUTPUT}" |
| 113 | + else |
| 114 | + faults=$(grep -Poh '(?<=<testsuite\sname="bandit"\stests=")(\d+)(?=">)' ${bandit_fullpath}) |
| 115 | +
|
| 116 | + printf "Bandit result: ${ANSI_LIGHT_RED}[FAILED]${ANSI_NOCOLOR}\n" |
| 117 | + printf " ${ANSI_LIGHT_RED}Bandit found %s issues.${ANSI_NOCOLOR}\n" "${faults}" |
| 118 | + printf "::error title=%s::%s\n" "🚨 Security Scanning (Bandit)" "Bandi found ${faults} issues." |
| 119 | +
|
| 120 | + printf "bandit_passed=false\n" >> "${GITHUB_OUTPUT}" |
| 121 | +
|
| 122 | + printf "::group::${ANSI_LIGHT_BLUE}JUnit XML report created by Bandit ...${ANSI_NOCOLOR}\n" |
| 123 | + cat ${bandit_fullpath} |
| 124 | + printf "\n::endgroup::\n" |
| 125 | + fi |
| 126 | +
|
| 127 | + - name: 📊 Publish Bandit Results |
| 128 | + uses: dorny/test-reporter@v2 |
| 129 | + if: steps.bandit.outputs.bandit_passed == 'false' |
| 130 | + continue-on-error: true |
| 131 | + with: |
| 132 | + name: 'Bandit Results' |
| 133 | + path: ${{ steps.bandit.outputs.bandit_fullpath }} |
| 134 | + reporter: java-junit |
| 135 | + |
| 136 | + Radon: |
| 137 | + name: ☢️ Metrics and Complexity |
| 138 | + runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}" |
| 139 | + if: inputs.radon == 'true' |
| 140 | + |
| 141 | + steps: |
| 142 | + - name: ⏬ Checkout repository |
| 143 | + uses: actions/checkout@v5 |
| 144 | + with: |
| 145 | + lfs: true |
| 146 | + submodules: true |
| 147 | + |
| 148 | + - name: 🐍 Setup Python ${{ inputs.python_version }} |
| 149 | + uses: actions/setup-python@v6 |
| 150 | + with: |
| 151 | + python-version: ${{ inputs.python_version }} |
| 152 | + |
| 153 | + - name: ⚙ Install dependencies for running radon |
| 154 | + run: python -m pip install --disable-pip-version-check radon |
| 155 | + |
| 156 | + - name: Code Metrics |
| 157 | +# if: inputs.artifact != '' |
| 158 | + run: | |
| 159 | + radon raw ${{ inputs.package_directory }} -s |
| 160 | +
|
| 161 | + - name: Code Complexity |
| 162 | +# if: inputs.artifact != '' |
| 163 | + run: | |
| 164 | + radon cc ${{ inputs.package_directory }} --total-average |
| 165 | +
|
| 166 | + - name: Halstead Complexity Metrics |
| 167 | +# if: inputs.artifact != '' |
| 168 | + run: | |
| 169 | + radon hal ${{ inputs.package_directory }} |
| 170 | +
|
| 171 | + - name: Maintainability Index |
| 172 | +# if: inputs.artifact != '' |
| 173 | + run: | |
| 174 | + radon mi ${{ inputs.package_directory }} -s |
| 175 | +
|
| 176 | + PyLint: |
| 177 | + name: 🩺 Linting |
| 178 | + runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}" |
| 179 | + if: inputs.pylint == 'true' |
| 180 | + |
| 181 | + steps: |
| 182 | + - name: ⏬ Checkout repository |
| 183 | + uses: actions/checkout@v5 |
| 184 | + with: |
| 185 | + lfs: true |
| 186 | + submodules: true |
| 187 | + |
| 188 | + - name: 🐍 Setup Python ${{ inputs.python_version }} |
| 189 | + uses: actions/setup-python@v6 |
| 190 | + with: |
| 191 | + python-version: ${{ inputs.python_version }} |
| 192 | + |
| 193 | + - name: ⚙ Install dependencies for running PyLint |
| 194 | + run: | |
| 195 | + python -m pip install --disable-pip-version-check pylint |
| 196 | + python -m pip install --disable-pip-version-check ${{ inputs.requirements }} |
| 197 | +
|
| 198 | + - name: 🩺 PyLint |
| 199 | +# if: inputs.artifact != '' |
| 200 | + run: | |
| 201 | + pylint ${{ inputs.package_directory }} |
0 commit comments