|
| 1 | +--- |
| 2 | +# This action is centrally managed in https://github.com/<organization>/.github/ |
| 3 | +# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in |
| 4 | +# the above-mentioned repo. |
| 5 | + |
| 6 | +# This workflow will analyze all supported languages in the repository using CodeQL Analysis. |
| 7 | + |
| 8 | +name: "CodeQL" |
| 9 | + |
| 10 | +on: |
| 11 | + push: |
| 12 | + branches: ["master"] |
| 13 | + pull_request: |
| 14 | + branches: ["master"] |
| 15 | + schedule: |
| 16 | + - cron: '00 12 * * 0' # every Sunday at 12:00 UTC |
| 17 | + |
| 18 | +concurrency: |
| 19 | + group: "${{ github.workflow }}-${{ github.ref }}" |
| 20 | + cancel-in-progress: true |
| 21 | + |
| 22 | +jobs: |
| 23 | + languages: |
| 24 | + name: Get language matrix |
| 25 | + runs-on: ubuntu-latest |
| 26 | + outputs: |
| 27 | + matrix: ${{ steps.lang.outputs.result }} |
| 28 | + continue: ${{ steps.continue.outputs.result }} |
| 29 | + steps: |
| 30 | + - name: Get repo languages |
| 31 | + uses: actions/github-script@v7 |
| 32 | + id: lang |
| 33 | + with: |
| 34 | + script: | |
| 35 | + // CodeQL supports ['cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift'] |
| 36 | + // Use only 'java' to analyze code written in Java, Kotlin or both |
| 37 | + // Use only 'javascript' to analyze code written in JavaScript, TypeScript or both |
| 38 | + // Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support |
| 39 | + const supported_languages = ['cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift'] |
| 40 | +
|
| 41 | + const remap_languages = { |
| 42 | + 'c++': 'cpp', |
| 43 | + 'c#': 'csharp', |
| 44 | + 'kotlin': 'java', |
| 45 | + 'typescript': 'javascript', |
| 46 | + } |
| 47 | +
|
| 48 | + const repo = context.repo |
| 49 | + const response = await github.rest.repos.listLanguages(repo) |
| 50 | + let matrix = { |
| 51 | + "include": [] |
| 52 | + } |
| 53 | +
|
| 54 | + for (let [key, value] of Object.entries(response.data)) { |
| 55 | + // remap language |
| 56 | + if (remap_languages[key.toLowerCase()]) { |
| 57 | + console.log(`Remapping language: ${key} to ${remap_languages[key.toLowerCase()]}`) |
| 58 | + key = remap_languages[key.toLowerCase()] |
| 59 | + } |
| 60 | + if (supported_languages.includes(key.toLowerCase())) { |
| 61 | + console.log(`Found supported language: ${key}`) |
| 62 | + let osList = ['ubuntu-latest']; |
| 63 | + if (key.toLowerCase() === 'swift') { |
| 64 | + osList = ['macos-latest']; |
| 65 | + } else if (key.toLowerCase() === 'cpp') { |
| 66 | + // TODO: update macos to latest after the below issue is resolved |
| 67 | + // https://github.com/github/codeql-action/issues/2266 |
| 68 | + osList = ['macos-13', 'ubuntu-latest', 'windows-latest']; |
| 69 | + } |
| 70 | + for (let os of osList) { |
| 71 | + // set name for matrix |
| 72 | + if (osList.length == 1) { |
| 73 | + name = key.toLowerCase() |
| 74 | + } else { |
| 75 | + name = `${key.toLowerCase()}, ${os}` |
| 76 | + } |
| 77 | +
|
| 78 | + // add to matrix |
| 79 | + matrix['include'].push({"language": key.toLowerCase(), "os": os, "name": name}) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + // print languages |
| 85 | + console.log(`matrix: ${JSON.stringify(matrix)}`) |
| 86 | +
|
| 87 | + return matrix |
| 88 | +
|
| 89 | + - name: Continue |
| 90 | + uses: actions/github-script@v7 |
| 91 | + id: continue |
| 92 | + with: |
| 93 | + script: | |
| 94 | + // if matrix['include'] is an empty list return false, otherwise true |
| 95 | + const matrix = ${{ steps.lang.outputs.result }} // this is already json encoded |
| 96 | +
|
| 97 | + if (matrix['include'].length == 0) { |
| 98 | + return false |
| 99 | + } else { |
| 100 | + return true |
| 101 | + } |
| 102 | +
|
| 103 | + analyze: |
| 104 | + name: Analyze (${{ matrix.name }}) |
| 105 | + if: ${{ needs.languages.outputs.continue == 'true' }} |
| 106 | + defaults: |
| 107 | + run: |
| 108 | + shell: ${{ matrix.os == 'windows-latest' && 'msys2 {0}' || 'bash' }} |
| 109 | + env: |
| 110 | + GITHUB_CODEQL_BUILD: true |
| 111 | + needs: [languages] |
| 112 | + runs-on: ${{ matrix.os || 'ubuntu-latest' }} |
| 113 | + timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }} |
| 114 | + permissions: |
| 115 | + actions: read |
| 116 | + contents: read |
| 117 | + security-events: write |
| 118 | + |
| 119 | + strategy: |
| 120 | + fail-fast: false |
| 121 | + matrix: ${{ fromJson(needs.languages.outputs.matrix) }} |
| 122 | + |
| 123 | + steps: |
| 124 | + - name: Maximize build space |
| 125 | + if: >- |
| 126 | + runner.os == 'Linux' && |
| 127 | + matrix.language == 'cpp' |
| 128 | + uses: easimon/maximize-build-space@v10 |
| 129 | + with: |
| 130 | + root-reserve-mb: 30720 |
| 131 | + remove-dotnet: ${{ (matrix.language == 'csharp' && 'false') || 'true' }} |
| 132 | + remove-android: 'true' |
| 133 | + remove-haskell: 'true' |
| 134 | + remove-codeql: 'false' |
| 135 | + remove-docker-images: 'true' |
| 136 | + |
| 137 | + - name: Checkout repository |
| 138 | + uses: actions/checkout@v4 |
| 139 | + with: |
| 140 | + submodules: recursive |
| 141 | + |
| 142 | + - name: Setup msys2 |
| 143 | + if: >- |
| 144 | + runner.os == 'Windows' && |
| 145 | + matrix.language == 'cpp' |
| 146 | + uses: msys2/setup-msys2@v2 |
| 147 | + with: |
| 148 | + msystem: ucrt64 |
| 149 | + update: true |
| 150 | + |
| 151 | + # Initializes the CodeQL tools for scanning. |
| 152 | + - name: Initialize CodeQL |
| 153 | + uses: github/codeql-action/init@v3 |
| 154 | + with: |
| 155 | + languages: ${{ matrix.language }} |
| 156 | + # If you wish to specify custom queries, you can do so here or in a config file. |
| 157 | + # By default, queries listed here will override any specified in a config file. |
| 158 | + # Prefix the list here with "+" to use these queries and those in the config file. |
| 159 | + |
| 160 | + # yamllint disable-line rule:line-length |
| 161 | + # For more details on CodeQL's query packs, refer to: 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 |
| 162 | + # queries: security-extended,security-and-quality |
| 163 | + config: | |
| 164 | + paths-ignore: |
| 165 | + - build |
| 166 | + - node_modules |
| 167 | + - third-party |
| 168 | +
|
| 169 | + # Pre autobuild |
| 170 | + # create a file named .codeql-prebuild-${{ matrix.language }}.sh in the root of your repository |
| 171 | + # create a file named .codeql-build-${{ matrix.language }}.sh in the root of your repository |
| 172 | + - name: Prebuild |
| 173 | + id: prebuild |
| 174 | + run: | |
| 175 | + # check if prebuild script exists |
| 176 | + filename=".codeql-prebuild-${{ matrix.language }}-${{ runner.os }}.sh" |
| 177 | + if [ -f "./${filename}" ]; then |
| 178 | + echo "Running prebuild script: ${filename}" |
| 179 | + ./${filename} |
| 180 | + fi |
| 181 | +
|
| 182 | + # Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift). |
| 183 | + - name: Autobuild |
| 184 | + if: steps.prebuild.outputs.skip_autobuild != 'true' |
| 185 | + uses: github/codeql-action/autobuild@v3 |
| 186 | + |
| 187 | + - name: Perform CodeQL Analysis |
| 188 | + uses: github/codeql-action/analyze@v3 |
| 189 | + with: |
| 190 | + category: "/language:${{matrix.language}}" |
| 191 | + output: sarif-results |
| 192 | + upload: failure-only |
| 193 | + |
| 194 | + - name: filter-sarif |
| 195 | + uses: advanced-security/filter-sarif@v1 |
| 196 | + with: |
| 197 | + input: sarif-results/${{ matrix.language }}.sarif |
| 198 | + output: sarif-results/${{ matrix.language }}.sarif |
| 199 | + patterns: | |
| 200 | + -build/** |
| 201 | + -node_modules/** |
| 202 | + -third\-party/** |
| 203 | +
|
| 204 | + - name: Upload SARIF |
| 205 | + uses: github/codeql-action/upload-sarif@v3 |
| 206 | + with: |
| 207 | + sarif_file: sarif-results/${{ matrix.language }}.sarif |
| 208 | + |
| 209 | + - name: Upload loc as a Build Artifact |
| 210 | + uses: actions/upload-artifact@v4 |
| 211 | + with: |
| 212 | + name: sarif-results-${{ matrix.language }}-${{ runner.os }} |
| 213 | + path: sarif-results |
| 214 | + retention-days: 1 |
0 commit comments