Repository navigation
131 lines (126 loc) · 7.34 KB
/
Copy pathcodeql.yml
File metadata and controls
131 lines (126 loc) · 7.34 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
name: CodeQL
# CodeQL analyzes the code on every pull request, a fork's too once GitHub runs its workflows (the template's
# ADR-0039), so the code-scanning rule that tools/Set-RepositoryBaseline.ps1 adds can be met.
# CodeQL's default setup analyzes no fork's pull request, and GitHub refuses this workflow's results while it is on,
# so the baseline turns it off (the template's ADR-0044).
# The project owns this file, so a language its code needs goes in the detect step, or in an Analyze job of its own
# where it needs a build (Go, Swift, Kotlin).
# Go and Swift need a build, and so does Kotlin: java-kotlin with build-mode none analyzes only Java, so a project
# with Kotlin code adds an Analyze job for it, with build-mode autobuild, or manual and its own build steps.
# In a private repository the Analyze jobs are skipped, since GitHub Free offers code scanning only in a public one (the
# template's ADR-0056).
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened]
# A merge queue's run skips every job: GitHub's code-scanning rule doesn't apply to a merge group, the push run
# analyzes the same merge commit once the queue merges it, and an upload after the queue deletes its ref fails on
# that commit and stops the line. A skipped job passes, so a ruleset that requires a CodeQL check still gets it
# (the template's ADR-0084).
merge_group:
types: [checks_requested]
schedule:
- cron: '41 4 * * 3'
workflow_dispatch:
permissions:
contents: read
# The same groups as ci.yml: a newer push to a pull request or a topic branch cancels the run it supersedes, and a run
# for a default-branch commit or a merge queue entry is never cancelled.
concurrency:
group: codeql-${{ github.ref }}-${{ (github.ref == format('refs/heads/{0}', github.event.repository.default_branch) || github.event_name == 'merge_group') && github.sha || 'latest' }}
cancel-in-progress: ${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) && github.event_name != 'merge_group' }}
jobs:
languages:
name: CodeQL languages
# Skipped in a merge queue, and so are the Analyze jobs that need it, as the comment on merge_group says.
if: github.event_name != 'merge_group'
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
languages: ${{ steps.detect.outputs.languages }}
public: ${{ steps.visibility.outputs.public }}
steps:
# Code scanning on a private repository needs GitHub Code Security, which GitHub Free doesn't offer, so the upload
# fails there. The visibility is read from the REST API because a scheduled run's event payload may carry no
# repository; a failed read fails the job, so a public repository never loses CodeQL silently (the template's
# ADR-0056).
# The same step is in scorecard.yml's visibility job, dependency-review.yml and tag-release.yml's policy job;
# keep the four the same (the template's tests/Test-Template.ps1 checks it). This file is the project's, so it
# also lists the copies the project adds in workflows of its own, which the template's files can't name: add each
# copy here when the project adds it, and keep them the same too. The project's own copies: none.
- name: Read whether the repository is public
id: visibility
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
run: |
$private = gh api "repos/$env:GITHUB_REPOSITORY" --jq .private
if ($LASTEXITCODE -or $private -notin 'true', 'false') { throw "Couldn't read whether $env:GITHUB_REPOSITORY is private." }
"public=$($private -eq 'false')".ToLowerInvariant() | Add-Content -LiteralPath $env:GITHUB_OUTPUT
"Public: $($private -eq 'false')"
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Detect the languages to analyze
id: detect
shell: pwsh
run: |
# actions analyzes the workflows, which every project has; each other language needs a tracked file in it.
# Each language here analyzes without a build (build-mode: none). Go and Swift need a build, and so does
# Kotlin: java-kotlin with build-mode none analyzes only Java, so a project with Kotlin code adds an Analyze
# job for it, with build-mode autobuild, or manual and its own build steps.
$files = @(git ls-files)
$languages = [Collections.Generic.List[string]]::new()
$languages.Add('actions')
if (@($files -match '\.(c|cc|cpp|cxx)$').Count) { $languages.Add('c-cpp') }
if (@($files -match '\.(cs|csx|cshtml|razor)$').Count) { $languages.Add('csharp') }
if (@($files -match '\.java$').Count) { $languages.Add('java-kotlin') }
# CodeQL's JavaScript extractor skips every folder whose name starts with a dot, so a script only there, such
# as .template/check-markdown.mjs, would fail the analysis with no code to process: count only scripts outside
# such folders. A dot-file outside them, such as src/.eslintrc.js, still counts.
$scripts = @($files -match '\.(js|jsx|mjs|cjs|ts|tsx|mts|cts)$' -notmatch '(^|/)\.[^/]+/')
if ($scripts.Count) { $languages.Add('javascript-typescript') }
if (@($files -match '\.py$').Count) { $languages.Add('python') }
if (@($files -match '\.rb$').Count) { $languages.Add('ruby') }
if (@($files -match '\.rs$').Count) { $languages.Add('rust') }
"languages=$(ConvertTo-Json -Compress -InputObject @($languages))" | Add-Content -LiteralPath $env:GITHUB_OUTPUT
"CodeQL analyzes: $($languages -join ', ')"
analyze:
name: Analyze (${{ matrix.language }})
needs: languages
# In a private repository the job is skipped, and a skipped job passes, so the check stays green (the template's
# ADR-0056). In a merge queue it is skipped with the languages job it needs (the template's ADR-0084).
if: needs.languages.outputs.public == 'true'
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: read
# Reads the workflow run's details in a private repository.
actions: read
# Uploads the results to code scanning.
security-events: write
strategy:
fail-fast: false
matrix:
language: ${{ fromJSON(needs.languages.outputs.languages) }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
# build-mode none builds nothing, and restores the C# projects' packages with the SDK global.json selects, which a
# .NET project installs with its install-dotnet action.
- name: Install .NET
if: matrix.language == 'csharp' && hashFiles('.github/actions/install-dotnet/action.yml') != ''
uses: ./.github/actions/install-dotnet
- name: Initialize CodeQL
uses: github/codeql-action/init@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
with:
languages: ${{ matrix.language }}
build-mode: none
# The extended suite the baseline gave CodeQL's default setup (the template's ADR-0030).
queries: security-extended
- name: Analyze
uses: github/codeql-action/analyze@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
with:
category: /language:${{ matrix.language }}