Repository navigation
Expand file tree
/
Copy pathcode-security.sh
More file actions
executable file
·61 lines (55 loc) · 2.3 KB
/
Copy pathcode-security.sh
File metadata and controls
executable file
·61 lines (55 loc) · 2.3 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
#!/usr/bin/env bash
# Reports each repository that no code security configuration covers, and
# with --apply, attaches one. A repository that runs CodeQL from a workflow
# of its own, as one whose code must be built while CodeQL watches does,
# gets "Github recommended with custom code scanning", which leaves code
# scanning to that workflow. Every other repository gets "GitHub
# recommended". A repository already covered keeps its configuration, so
# one attached by hand stays as it is.
#
# ./code-security.sh [--apply]
#
# It reads Terraform's repositories output, so it needs the state of an
# apply, and gh signed in with organization Administration write.
set -euo pipefail
cd "$(dirname "$0")"
org=liken-sh
apply=false
[ "${1:-}" = --apply ] && apply=true
configuration() {
gh api "orgs/$org/code-security/configurations" --jq ".[] | select(.name == \"$1\") | .id"
}
# Whether a workflow of the repository's own runs CodeQL's analysis.
scans_itself() {
local paths path
# A repository with no workflows answers 404, and so has none of its own.
paths=$(gh api "repos/$org/$1/contents/.github/workflows" --jq '.[].path' 2>/dev/null) || return 1
for path in $paths; do
if gh api "repos/$org/$1/contents/$path" -H "Accept: application/vnd.github.raw" |
grep -q 'github/codeql-action/analyze'; then
return 0
fi
done
return 1
}
uncovered=0
for repository in $(terraform output -json repositories | jq -r '.[]'); do
# A repository with no configuration answers 204 with no body.
attached=$(gh api "repos/$org/$repository/code-security-configuration" --jq '.configuration.name // empty')
[ -n "$attached" ] && continue
uncovered=$((uncovered + 1))
if ! $apply; then
echo "$repository: no code security configuration"
continue
fi
if scans_itself "$repository"; then
name="Github recommended with custom code scanning"
else
name="GitHub recommended"
fi
gh api --silent -X POST "orgs/$org/code-security/configurations/$(configuration "$name")/attach" \
-f scope=selected -F "selected_repository_ids[]=$(gh api "repos/$org/$repository" --jq .id)"
echo "$repository: attached $name"
done
# A report that finds a repository uncovered fails, so a scheduled run says so.
$apply || [ "$uncovered" -eq 0 ]