-
Notifications
You must be signed in to change notification settings - Fork 2
299 lines (264 loc) · 10.5 KB
/
__call-codeql.yml
File metadata and controls
299 lines (264 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
---
# This workflow will analyze all supported languages in the repository using CodeQL Analysis.
name: CodeQL (called)
permissions: {}
on:
push:
branches:
- master
pull_request:
workflow_call:
inputs:
runner:
required: false
type: string
default: '["ubuntu-latest"]'
jobs:
languages:
name: Get language matrix
permissions:
contents: read
outputs:
matrix: ${{ steps.lang.outputs.result }}
continue: ${{ steps.continue.outputs.result }}
runs-on: ${{ (inputs && inputs.runner && fromJson(inputs.runner)) || 'ubuntu-latest' }}
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Get repo languages
id: lang
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RUNNER_INPUT: ${{ inputs.runner }}
with:
script: |
// CodeQL supports the following:
// actions
// c
// cpp
// csharp
// go
// java
// javascript
// kotlin
// python
// ruby
// rust
// swift
// Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
const supported_languages = [
'cpp',
'csharp',
'go',
'java',
'javascript',
'python',
'ruby',
'rust',
'swift',
]
const remap_languages = {
'c': 'cpp',
'c++': 'cpp',
'c#': 'csharp',
'kotlin': 'java',
'typescript': 'javascript',
}
// Get custom runner input if provided
const runnerInput = process.env.RUNNER_INPUT || '["ubuntu-latest"]'
let customRunners = []
try {
customRunners = JSON.parse(runnerInput)
console.log(`Custom runners provided: ${JSON.stringify(customRunners)}`)
} catch (e) {
console.log(`Failed to parse runner input, using default behavior: ${e}`)
}
const useCustomRunners = customRunners.length > 0 && runnerInput !== '["ubuntu-latest"]'
const repo = context.repo
const response = await github.rest.repos.listLanguages(repo)
let matrix = {
"include": []
}
// Track languages we've already added to avoid duplicates
const addedLanguages = new Set()
// Check if workflow files exist to determine if we should add actions language
const fs = require('fs');
const hasYmlFiles = fs.existsSync('.github/workflows') &&
fs.readdirSync('.github/workflows').some(file => file.endsWith('.yml') || file.endsWith('.yaml'));
// Add actions language if workflow files exist
if (hasYmlFiles) {
console.log('Found GitHub Actions workflow files. Adding actions to the matrix.');
matrix['include'].push({
"category": "/language:actions",
"language": "actions",
"name": "actions",
"os": "ubuntu-latest",
"runner": useCustomRunners ? customRunners : "ubuntu-latest",
"build-mode": "none",
});
}
for (let [key, value] of Object.entries(response.data)) {
// remap language
if (remap_languages[key.toLowerCase()]) {
console.log(`Remapping language: ${key} to ${remap_languages[key.toLowerCase()]}`)
key = remap_languages[key.toLowerCase()]
}
const normalizedKey = key.toLowerCase()
if (supported_languages.includes(normalizedKey) && !addedLanguages.has(normalizedKey)) {
// Mark this language as added
addedLanguages.add(normalizedKey)
console.log(`Found supported language: ${normalizedKey}`)
if (useCustomRunners) {
// Use custom runners as a group/pool
let category = `/language:${normalizedKey}`
let build_mode = 'none';
// Set build mode based on language
switch (normalizedKey) {
case 'csharp':
build_mode = 'autobuild'
break
case 'go':
build_mode = 'autobuild'
break
case 'java':
build_mode = 'autobuild'
break
default:
build_mode = 'none'
}
// Determine OS based on language (for display purposes)
let os = 'ubuntu-latest'
if (normalizedKey === 'swift') {
os = 'macos-latest'
}
// add to matrix with runner group
matrix['include'].push({
"category": category,
"language": normalizedKey,
"name": normalizedKey,
"os": os,
"runner": customRunners,
"build-mode": build_mode,
})
} else {
// Use default OS-based behavior
let osList = ['ubuntu-latest'];
if (normalizedKey === 'swift') {
osList = ['macos-latest'];
}
for (let os of osList) {
// set name for matrix
let name = osList.length === 1 ? normalizedKey : `${normalizedKey}, ${os}`
// set category for matrix
let category = `/language:${normalizedKey}`
let build_mode = 'none';
// Set build mode based on language
switch (normalizedKey) {
case 'csharp':
build_mode = 'autobuild'
break
case 'go':
build_mode = 'autobuild'
break
case 'java':
build_mode = 'autobuild'
break
default:
build_mode = 'none'
}
// add to matrix
matrix['include'].push({
"category": category,
"language": normalizedKey,
"name": name,
"os": os,
"runner": os,
"build-mode": build_mode,
})
}
}
}
}
// print languages
console.log(`matrix: ${JSON.stringify(matrix)}`)
return matrix
- name: Continue
id: continue
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
// if matrix['include'] is an empty list return false, otherwise true
const matrix = ${{ steps.lang.outputs.result }} // this is already json encoded
if (matrix['include'].length == 0) {
return false
} else {
return true
}
analyze:
name: Analyze (${{ matrix.name }})
if: needs.languages.outputs.continue == 'true'
env:
GITHUB_CODEQL_BUILD: true
needs: languages
permissions:
actions: read
contents: read
security-events: write
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.languages.outputs.matrix) }}
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 60 }}
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
submodules: recursive
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# yamllint disable-line rule:line-length
# 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
# queries: security-extended,security-and-quality
config: |
paths-ignore:
- build
- node_modules
- third-party
build-mode: ${{ matrix.build-mode || 'none' }}
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
- name: Autobuild
if: matrix.build-mode == 'autobuild'
uses: github/codeql-action/autobuild@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
with:
category: "${{ matrix.category }}"
output: sarif-results
upload: failure-only
- name: filter-sarif
uses: advanced-security/filter-sarif@2da736ff05ef065cb2894ac6892e47b5eac2c3c0 # v1.1.0.1.1
with:
input: sarif-results/${{ matrix.language }}.sarif
output: sarif-results/${{ matrix.language }}.sarif
patterns: |
-build/**
-node_modules/**
-third\-party/**
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
with:
category: "${{ matrix.category }}"
sarif_file: sarif-results/${{ matrix.language }}.sarif
- name: Upload loc as a Build Artifact
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: sarif-results-${{ matrix.language }}-${{ runner.os }}
path: sarif-results
if-no-files-found: error
retention-days: 1