@@ -21,80 +21,79 @@ concurrency:
2121 cancel-in-progress : ${{ github.ref != 'refs/heads/main' }}
2222
2323jobs :
24- # First job to discover all packages dynamically
25- discover-packages :
26- name : Discover Packages
24+ # Discover packages with changes for targeted scanning
25+ discover-changed- packages :
26+ name : Discover Changed Packages
2727 runs-on : ubuntu-latest
2828 outputs :
2929 matrix : ${{ steps.set-matrix.outputs.matrix }}
30+ has-changes : ${{ steps.set-matrix.outputs.has-changes }}
3031 steps :
3132 - if : github.actor == 'dependabot[bot]' || github.event_name == 'merge_group'
3233 run : exit 0
3334
3435 - uses : actions/checkout@v4
3536 with :
3637 ref : ${{ github.event.pull_request.head.sha || github.ref }}
38+ fetch-depth : 0
3739
38- - name : Discover packages
40+ - name : Discover packages with changes
3941 id : set-matrix
4042 run : |
41- packages=$(find packages -maxdepth 1 -type d -name "auth0_*" | sed 's|^packages/||' | jq -R -s -c 'split("\n")[:-1]')
43+ # For push events or scheduled runs, scan all packages
44+ if [[ "${{ github.event_name }}" == "push" || "${{ github.event_name }}" == "schedule" || "${{ github.event_name }}" == "workflow_dispatch" ]]; then
45+ packages=$(find packages -maxdepth 1 -type d -name "auth0_*" | sed 's|^packages/||' | jq -R -s -c 'split("\n")[:-1]')
46+ echo "Scanning all packages for ${{ github.event_name }} event"
47+ else
48+ # For PRs, only scan packages with changes
49+ changed_files=$(git diff --name-only origin/main...HEAD)
50+ changed_packages=$(echo "$changed_files" | grep '^packages/auth0_' | cut -d'/' -f2 | sort -u | jq -R -s -c 'split("\n")[:-1] | map(select(length > 0))')
51+ packages="$changed_packages"
52+ echo "Changed files: $changed_files"
53+ echo "Scanning changed packages for PR: $packages"
54+ fi
55+
4256 echo "matrix={\"package\":$packages}" >> $GITHUB_OUTPUT
43- echo "Found packages: $packages"
57+ if [ "$packages" = "[]" ]; then
58+ echo "has-changes=false" >> $GITHUB_OUTPUT
59+ else
60+ echo "has-changes=true" >> $GITHUB_OUTPUT
61+ fi
62+ echo "Final packages to scan: $packages"
4463
45- # Main security scanning job for each package
64+ # Security scanning for packages with changes
4665 security-scan :
4766 name : Security Scan (${{ matrix.package }})
4867 runs-on : ubuntu-latest
49- needs : discover-packages
50- if : needs.discover-packages.outputs.matrix != '{"package":[]} '
68+ needs : discover-changed- packages
69+ if : needs.discover-changed- packages.outputs.has-changes == 'true '
5170 strategy :
5271 fail-fast : false
53- matrix : ${{ fromJson(needs.discover-packages.outputs.matrix) }}
72+ matrix : ${{ fromJson(needs.discover-changed- packages.outputs.matrix) }}
5473
5574 steps :
5675 - if : github.actor == 'dependabot[bot]' || github.event_name == 'merge_group'
57- run : exit 0 # Skip unnecessary test runs for dependabot and merge queues
76+ run : exit 0
5877
5978 - uses : actions/checkout@v4
6079 with :
6180 ref : ${{ github.event.pull_request.head.sha || github.ref }}
6281
63- - name : Set up Python
64- uses : actions/setup-python@v5
65- with :
66- python-version : ' 3.11'
67-
68- - name : Prepare dependencies for Snyk scan
82+ - name : Check for requirements.txt
6983 working-directory : packages/${{ matrix.package }}
7084 run : |
71- # Check if requirements.txt exists, if not, generate from Poetry
72- if [ -f "requirements.txt" ]; then
73- echo "Using existing requirements.txt for ${{ matrix.package }}"
74- cp requirements.txt snyk-requirements.txt
75- elif [ -f "pyproject.toml" ]; then
76- echo "Generating requirements.txt from pyproject.toml for ${{ matrix.package }}"
77- pip install poetry
78- poetry export --format requirements.txt --output snyk-requirements.txt --without-hashes
79- else
80- echo "No dependency file found for ${{ matrix.package }}"
85+ if [ ! -f "requirements.txt" ]; then
86+ echo "❌ requirements.txt not found for ${{ matrix.package }}"
87+ echo "Please ensure requirements.txt exists in the package directory"
8188 exit 1
8289 fi
83-
84- # Show what we're scanning
90+ echo "✅ Found requirements.txt for ${{ matrix.package }}"
8591 echo "Dependencies to scan:"
86- head -10 snyk- requirements.txt
92+ head -5 requirements.txt
8793
8894 - name : Run Snyk security scan
8995 uses : snyk/actions/python@b98d498629f1c368650224d6d212bf7dfa89e4bf # pin@0.4.0
9096 env :
9197 SNYK_TOKEN : ${{ secrets.SNYK_TOKEN }}
9298 with :
93- args : --file=packages/${{ matrix.package }}/snyk-requirements.txt --package-manager=pip
94-
95- - name : Upload Snyk results to GitHub Code Scanning
96- uses : github/codeql-action/upload-sarif@v3
97- if : always()
98- with :
99- sarif_file : snyk.sarif
100- category : snyk-${{ matrix.package }}
99+ args : --file=packages/${{ matrix.package }}/requirements.txt --package-manager=pip
0 commit comments