|
| 1 | +# This workflow determines which sub projects of a monorepo are affected by a PR, and then runs CodeQL analysis on those projects. |
| 2 | +# |
| 3 | +# It uses Actions from `advanced-security/monorepo-code-scanning-action` |
| 4 | +# |
| 5 | +# The specific language and paths affected are passed to the CodeQL analysis, along with a custom analysis workflow if one is provided. |
| 6 | +# |
| 7 | +# For TypeScript/JavaScript, Python, and Ruby, and when using 'build-mode: none' for Java and C#, you can let the CodeQL Action handle the "build" step, |
| 8 | +# and only target the project that is being changed by the PR. |
| 9 | +# |
| 10 | +# For Kotlin, Swift and C/C++, or when not using 'build-mode: none' for Java and C#, you will need to manually build the project, |
| 11 | +# in a way that you can define in the optional custom analysis workflow. |
| 12 | +# |
| 13 | +# If you want to specifiy custom queries, you can do so in the custom analysis workflow. |
| 14 | +# |
| 15 | +# You can find an example of what that looks like in this repository at .github/workflows/custom-codeql-analysis.yml |
| 16 | + |
| 17 | +name: "CodeQL monorepo" |
| 18 | + |
| 19 | +on: |
| 20 | + pull_request: |
| 21 | + branches: ["main"] |
| 22 | + types: |
| 23 | + - opened |
| 24 | + - reopened |
| 25 | + - synchronize |
| 26 | + - closed |
| 27 | + |
| 28 | +jobs: |
| 29 | + changes: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + permissions: |
| 32 | + pull-requests: read |
| 33 | + contents: read |
| 34 | + outputs: |
| 35 | + projects: ${{ steps.changes.outputs.projects }} |
| 36 | + steps: |
| 37 | + # EX: |
| 38 | + # { |
| 39 | + # "javascript": { |
| 40 | + # "babel-cli": [ |
| 41 | + # "packages/babel-cli" |
| 42 | + # ], |
| 43 | + # "babel-code-frame": [ |
| 44 | + # "packages/babel-code-frame" |
| 45 | + # ], |
| 46 | + # "babel-compat-data": [ |
| 47 | + # "packages/babel-compat-data" |
| 48 | + # ], |
| 49 | + # ............ (for each directory under projects ) |
| 50 | + - name: Build language based projects JSON for each package in the monorepo |
| 51 | + run: | |
| 52 | + #!/bin/bash |
| 53 | + json="{ \"javascript\": {" |
| 54 | + for dir in packages/*/ |
| 55 | + do |
| 56 | + folder_name=$(basename "$dir") |
| 57 | + json+="\"$folder_name\": [ \"packages/$folder_name\" ]," |
| 58 | + done |
| 59 | + json="${json%,}}}" |
| 60 | + echo -e "$json" > output.json |
| 61 | + cat dynamic-projects.json |
| 62 | +
|
| 63 | + - name: Spot changes to projects |
| 64 | + id: changes |
| 65 | + uses: advanced-security/monorepo-code-scanning-action/changes@main |
| 66 | + with: |
| 67 | + projects-json: dynamic-projects.json |
| 68 | + |
| 69 | + scan: |
| 70 | + if: needs.changes.outputs.scan-required == true |
| 71 | + runs-on: ubuntu-latest |
| 72 | + permissions: |
| 73 | + contents: read |
| 74 | + actions: read |
| 75 | + security-events: write |
| 76 | + needs: changes |
| 77 | + strategy: |
| 78 | + matrix: |
| 79 | + project: ${{ fromJson(needs.changes.outputs.projects).projects }} |
| 80 | + steps: |
| 81 | + - name: Analyze code |
| 82 | + uses: advanced-security/monorepo-code-scanning-action/scan@main |
| 83 | + # custom-analysis: true |
| 84 | + |
| 85 | + republish: |
| 86 | + runs-on: ubuntu-latest |
| 87 | + permissions: |
| 88 | + contents: read |
| 89 | + security-events: write |
| 90 | + actions: read |
| 91 | + needs: changes |
| 92 | + steps: |
| 93 | + - name: Republish results on merge |
| 94 | + uses: advanced-security/monorepo-code-scanning-action/republish-sarif@main |
| 95 | + with: |
| 96 | + projects: ${{ needs.changes.outputs.projects }} |
| 97 | + merged-only: true |
0 commit comments