Repository navigation
168 lines (159 loc) · 6.91 KB
/
Copy pathcodeql.yml
File metadata and controls
168 lines (159 loc) · 6.91 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
name: CodeQL
on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: "41 4 * * 0"
workflow_dispatch:
concurrency:
group: codeql-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
security-events: write
jobs:
change-scope:
name: Trusted change scope + documentation validation
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: read
outputs:
scope: ${{ steps.classify.outputs.scope }}
docs_validated: ${{ steps.classify.outputs.docs_validated }}
tested_sha: ${{ steps.classify.outputs.tested_sha }}
classifier_blob: ${{ steps.classify.outputs.classifier_blob }}
steps:
- name: Checkout immutable tested source
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
- name: Set up pinned Python
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.15.2"
- name: Classify with the immutable trusted base
id: classify
shell: bash
run: |
python -I -B - <<'PY'
import json
import os
from pathlib import Path
import re
import subprocess
import sys
repo = Path(os.environ["GITHUB_WORKSPACE"])
event = json.loads(Path(os.environ["GITHUB_EVENT_PATH"]).read_text(encoding="utf-8"))
name = os.environ["GITHUB_EVENT_NAME"]
base = ""
if name == "push":
base = event.get("before", "")
elif name == "pull_request":
base = event.get("pull_request", {}).get("base", {}).get("sha", "")
control = Path(os.environ["RUNNER_TEMP"]) / "emulebb-ci-scope"
control.mkdir(parents=True, exist_ok=True)
receipt = control / "classification.json"
blob = None
if name in {"push", "pull_request"} and isinstance(base, str) and re.fullmatch(r"[0-9a-f]{40}", base) and set(base) != {"0"}:
result = subprocess.run(["git", "-C", str(repo), "ls-tree", base, "--", "tools/ci_change_scope.py"], capture_output=True, timeout=30)
match = re.fullmatch(rb"100644 blob ([0-9a-f]{40})\ttools/ci_change_scope.py\n", result.stdout) if result.returncode == 0 else None
if match:
blob = match.group(1).decode("ascii")
if blob is None:
# WHY: first deployment and unknown events require the complete matrix.
# Never fall back to executing the classifier from the proposed head.
value = {"schema": "emulebb.ci-change-scope/1", "repository": os.environ["GITHUB_REPOSITORY"],
"event": name, "baseSha": base, "testedSha": os.environ["GITHUB_SHA"],
"headSha": event.get("pull_request", {}).get("head", {}).get("sha") if name == "pull_request" else os.environ["GITHUB_SHA"],
"classifierBlob": None, "scope": "full", "docsValidated": False,
"reason": "trusted-base-unavailable-or-forced-full-event", "changes": []}
receipt.write_text(json.dumps(value, indent=2) + "\n", encoding="utf-8")
with Path(os.environ["GITHUB_OUTPUT"]).open("a", encoding="utf-8") as output:
output.write("scope=full\ndocs_validated=false\nclassifier_blob=\n")
output.write("tested_sha=" + os.environ["GITHUB_SHA"] + "\n")
else:
source = subprocess.run(["git", "-C", str(repo), "cat-file", "blob", blob], check=True,
capture_output=True, timeout=30).stdout
trusted = control / "trusted-classifier.py"
trusted.write_bytes(source)
subprocess.run([sys.executable, "-I", "-B", str(trusted), "--repo", str(repo),
"--event-path", os.environ["GITHUB_EVENT_PATH"], "--event-name", name,
"--repository", os.environ["GITHUB_REPOSITORY"], "--tested-sha", os.environ["GITHUB_SHA"],
"--base-sha", base, "--github-output", os.environ["GITHUB_OUTPUT"],
"--receipt", str(receipt)], check=True, timeout=120)
PY
- name: Retain source-bound scope receipt
if: always()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: change-scope-${{ github.sha }}-${{ github.run_attempt }}
path: ${{ runner.temp }}/emulebb-ci-scope/classification.json
if-no-files-found: error
retention-days: 14
analyze:
needs: change-scope
if: ${{ needs.change-scope.outputs.scope == 'full' }}
name: CodeQL (${{ matrix.language }})
runs-on: ubuntu-24.04
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
include:
- language: actions
build-mode: none
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
- language: rust
build-mode: none
steps:
- name: Checkout emulebb-rust
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Initialize CodeQL
uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-extended
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4
with:
category: /language:${{ matrix.language }}
required-codeql:
name: Required CodeQL
if: ${{ always() }}
needs: [change-scope, analyze]
runs-on: ubuntu-24.04
timeout-minutes: 5
steps:
- name: Require every CodeQL language
env:
SCOPE_RESULT: ${{ needs.change-scope.result }}
SCOPE: ${{ needs.change-scope.outputs.scope }}
DOCS_VALIDATED: ${{ needs.change-scope.outputs.docs_validated }}
CLASSIFIED_TESTED_SHA: ${{ needs.change-scope.outputs.tested_sha }}
CLASSIFIER_BLOB: ${{ needs.change-scope.outputs.classifier_blob }}
EXPECTED_TESTED_SHA: ${{ github.sha }}
ANALYZE_RESULT: ${{ needs.analyze.result }}
shell: bash
run: |
set -euo pipefail
test "$SCOPE_RESULT" = success
test -n "$EXPECTED_TESTED_SHA"
test "$CLASSIFIED_TESTED_SHA" = "$EXPECTED_TESTED_SHA"
case "$SCOPE" in
full) test "$DOCS_VALIDATED" = false; expected=success ;;
docs)
test "$DOCS_VALIDATED" = true
[[ "$CLASSIFIER_BLOB" =~ ^[0-9a-f]{40}$ ]]
expected=skipped
;;
*) exit 1 ;;
esac
test "$ANALYZE_RESULT" = "$expected"