Skip to content

Commit a3c81e2

Browse files
committed
fix(codeql): python code analysis
- add dedicated CodeQL config file - configure Python-specific analysis with proper PYTHONPATH - add module import script to ensure complete code coverage - move the Python code into different directory and set up proper Python project structure for analysis AI-Generated: true AI-Model: claude-3.7-sonnet Signed-off-by: AtomicFS <vojtech.vesely@9elements.com>
1 parent c36b347 commit a3c81e2

File tree

3 files changed

+86
-24
lines changed

3 files changed

+86
-24
lines changed

.cspell.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
"KERNELVERSION",
5151
"Kortumstraße",
5252
"NOTSET",
53+
"PYTHONPATH",
5354
"REPOPATH",
55+
"STDLIB",
5456
"TARGETARCH",
5557
"TOOLSDIR",
5658
"Taskfile",
@@ -68,6 +70,7 @@
6870
"cmds",
6971
"cocogitto",
7072
"commitlint",
73+
"compileall",
7174
"complgen",
7275
"composefile",
7376
"coreboot",
@@ -92,6 +95,7 @@
9295
"elif",
9396
"emeraldlake",
9497
"endgroup",
98+
"endswith",
9599
"exitcode",
96100
"filenamify",
97101
"githubaction",
@@ -143,16 +147,19 @@
143147
"pytest",
144148
"rdparty",
145149
"readarray",
150+
"relpath",
146151
"rootdir",
147152
"runslow",
148153
"rustup",
149154
"savedefconfig",
150155
"seabios",
151156
"setenv",
152157
"sethvargo",
158+
"setuptools",
153159
"shellcheck",
154160
"skipframes",
155161
"sloglint",
162+
"splitext",
156163
"startswith",
157164
"staticcheck",
158165
"stmsg",

.github/codeql/codeql-config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
paths:
2+
- .dagger-ci
3+
paths-ignore:
4+
- '**/node_modules/**'
5+
- '**/vendor/**'
6+
- '**/tests/**'

.github/workflows/codeql.yml

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -77,45 +77,94 @@ jobs:
7777
run: |
7878
pip install -r ./.dagger-ci/daggerci/requirements.txt
7979
80+
# Copy Python files to a standard location
81+
- name: Copy Python files to standard location
82+
if: ${{ matrix.language == 'python' }}
83+
run: |
84+
# Create a standard Python project structure
85+
mkdir -p /tmp/python-project/src
86+
87+
# Copy all Python files from .dagger-ci to the standard location
88+
cp -r ./.dagger-ci/* /tmp/python-project/src/
89+
90+
# Create a setup.py file to make it look like a standard Python project
91+
cat > /tmp/python-project/setup.py << 'EOF'
92+
from setuptools import setup, find_packages
93+
94+
setup(
95+
name="daggerci",
96+
version="0.1",
97+
packages=find_packages(where="src"),
98+
package_dir={"": "src"}
99+
)
100+
EOF
101+
102+
# List all files in the standard location to verify
103+
find /tmp/python-project -type f | sort
104+
80105
# Initializes the CodeQL tools for scanning.
81106
- name: Initialize CodeQL for not-Python
82107
if: ${{ matrix.language != 'python' }}
83108
uses: github/codeql-action/init@v3
84109
with:
85110
languages: ${{ matrix.language }}
86111
build-mode: ${{ matrix.build-mode }}
112+
config-file: .github/codeql/codeql-config.yml
87113

88114
- name: Initialize CodeQL for Python
89115
if: ${{ matrix.language == 'python' }}
90116
uses: github/codeql-action/init@v3
91117
with:
92118
languages: ${{ matrix.language }}
93119
build-mode: ${{ matrix.build-mode }}
94-
source-root: ./.dagger-ci
95-
96-
# If you wish to specify custom queries, you can do so here or in a config file.
97-
# By default, queries listed here will override any specified in a config file.
98-
# Prefix the list here with "+" to use these queries and those in the config file.
99-
100-
# For more details on CodeQL's query packs, refer to:
101-
# https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
102-
# queries: security-extended,security-and-quality
103-
104-
# If the analyze step fails for one of the languages you are analyzing with
105-
# "We were unable to automatically build your code", modify the matrix above
106-
# to set the build mode to "manual" for that language. Then modify this step
107-
# to build your code.
108-
# Command-line programs to run using the OS shell.
109-
# See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
110-
- if: matrix.build-mode == 'manual'
111-
shell: bash
120+
config-file: .github/codeql/codeql-config.yml
121+
env:
122+
# Extract the standard library to help with imports
123+
CODEQL_EXTRACTOR_PYTHON_EXTRACT_STDLIB: true
124+
# Set the Python path to include our repository
125+
PYTHONPATH: ${{ github.workspace }}/.dagger-ci:${{ github.workspace }}
126+
127+
# Run a Python script that imports all modules to ensure they're analyzed
128+
- name: Run Python imports for CodeQL
129+
if: ${{ matrix.language == 'python' }}
112130
run: |
113-
echo 'If you are using a "manual" build mode for one or more of the' \
114-
'languages you are analyzing, replace this with the commands to build' \
115-
'your code, for example:'
116-
echo ' make bootstrap'
117-
echo ' make release'
118-
exit 1
131+
# Create a script that imports all Python modules
132+
cat > /tmp/import_all.py << 'EOF'
133+
import os
134+
import sys
135+
import importlib
136+
137+
# Add the repository root to the Python path
138+
repo_root = os.environ.get('GITHUB_WORKSPACE', '/home/runner/work/firmware-action/firmware-action')
139+
sys.path.insert(0, repo_root)
140+
141+
# Add the .dagger-ci directory to the Python path
142+
dagger_ci_path = os.path.join(repo_root, '.dagger-ci')
143+
sys.path.insert(0, dagger_ci_path)
144+
145+
# Find all Python files in the .dagger-ci directory
146+
for root, dirs, files in os.walk(dagger_ci_path):
147+
for file in files:
148+
if file.endswith('.py'):
149+
# Convert file path to module name
150+
rel_path = os.path.relpath(os.path.join(root, file), dagger_ci_path)
151+
module_name = os.path.splitext(rel_path)[0].replace(os.path.sep, '.')
152+
153+
# Skip __init__.py files
154+
if module_name.endswith('__init__'):
155+
module_name = module_name[:-9]
156+
157+
# Try to import the module
158+
print(f"Trying to import: {module_name}")
159+
try:
160+
importlib.import_module(module_name)
161+
print(f"Successfully imported: {module_name}")
162+
except Exception as e:
163+
print(f"Failed to import {module_name}: {e}")
164+
EOF
165+
166+
# Run the import script
167+
PYTHONPATH=${{ github.workspace }}/.dagger-ci:${{ github.workspace }} python /tmp/import_all.py
119168
120169
- name: Perform CodeQL Analysis
121170
uses: github/codeql-action/analyze@v3

0 commit comments

Comments
 (0)