-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgh-codeql
More file actions
executable file
·310 lines (273 loc) · 11.4 KB
/
gh-codeql
File metadata and controls
executable file
·310 lines (273 loc) · 11.4 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
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env bash
set -e
debug="$(gh config get extensions.codeql.debug 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
if [ "$debug" = "true" ] ; then
set -x
fi
error() {
echo "ERROR: $*" 1>&2
exit 1
}
warning() {
echo "WARNING: $*" 1>&2
}
rootdir="$(dirname "$0")"
channel="$(gh config get extensions.codeql.channel 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
version="$(gh config get extensions.codeql.version 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
# Handle local-version command first to prevent local version warning when enabling local version support if a local version is specified..
if [ "$1" = "local-version" ] ; then
if [ "$2" = "on" ] ; then
warning "Local version support is disabled by default to remove the opportunity from untrusted git repositories to
abuse older CodeQL CLIs. To disable local version support again run the command 'gh codeql local-version off'."
gh config set extensions.codeql.local-version true 2> /dev/null # Ignore a warning about unrecognized custom keys
elif [ "$2" = "off" ] ; then
gh config set extensions.codeql.local-version false 2> /dev/null # Ignore a warning about unrecognized custom keys
else
error "Invalid local-version command: '$2'. Supported values are 'on' or 'off'."
fi
exit 0
fi
local_version="$(gh config get extensions.codeql.local-version 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
if [ -e .codeql-version ]; then
if [ "$local_version" = "true" ]; then
version=$(<.codeql-version)
else
warning "Ignoring local version because local version support is off. Call the command 'gh codeql local-version on' to enable local version support. "
fi
fi
# The environment variable GH_CODEQL_VERSION overrides all specified versions.
version=${GH_CODEQL_VERSION:-$version}
if [ -z "$1" ]; then
cat <<EOF
GitHub command-line wrapper for the CodeQL CLI.
Usage:
gh codeql set-channel [release|nightly] # default: release
gh codeql set-version [version] # default: latest
gh codeql local-version [on|off] # enable/disable local version support
gh codeql set-local-version [version] # set the version for the current working directory, default: latest
gh codeql unset-local-version # switch back to the global version
gh codeql list-versions # list all available versions for current channel
gh codeql list-installed # list installed versions for current channel
gh codeql cleanup <version> # delete a specific downloaded version
gh codeql cleanup-all # delete all installed versions for all channels
gh codeql download [version] # download a specific version (default: latest)
gh codeql debug [on|off] # enable/disable debug output for gh extension
gh codeql install-stub [dir] # install an executable script named 'codeql' that invokes 'gh codeql' with the passed arguments (default: /usr/local/bin/)
gh codeql <anything else> # pass arguments to CodeQL CLI
Current channel: ${channel:-not specified}.
Current version: ${version:-not specified}.
EOF
exit 0
fi
if [ -z "$channel" ] || [ "$channel" = "release" ] ; then
channel="release"
repo=github/codeql-cli-binaries
elif [ "$channel" = "nightly" ] ; then
repo=dsp-testing/codeql-cli-nightlies
else
error "Channel must be 'release' or 'nightly'."
fi
# determine platform using OSTYPE
platform="$(gh config get extensions.codeql.platform 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
if [[ -z "$platform" ]] ; then
if [[ "$OSTYPE" == "darwin"* ]] ; then
platform=osx64
elif [[ "$OSTYPE" == "linux"* ]] ; then
platform=linux64
elif [[ "$OSTYPE" == "win"* ]] || [[ $OSTYPE == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] ; then
platform=win64
else
error "Couldn't determine platform from OSTYPE='$OSTYPE'. Consider setting extensions.codeql.platform."
fi
fi
# Handle debug command.
if [ "$1" = "debug" ] ; then
if [ "$2" = "on" ] ; then
gh config set extensions.codeql.debug true 2> /dev/null # Ignore a warning about unrecognized custom keys
elif [ "$2" = "off" ] ; then
gh config set extensions.codeql.debug false 2> /dev/null # Ignore a warning about unrecognized custom keys
else
error "Invalid debug command: '$2'."
fi
exit 0
fi
# Handle list-versions command.
if [ "$1" = "list-versions" ]; then
gh api "repos/$repo/releases" --paginate --jq ".[].tag_name"
exit 0
fi
# Handle set-channel command.
if [ "$1" = "set-channel" ]; then
if [ "$2" != "release" ] && [ "$2" != "nightly" ]; then
error "Invalid channel: '$2'."
fi
old_channel="$(gh config get extensions.codeql.channel 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
if [ "${old_channel:-release}" != "$2" ] ; then
gh config set extensions.codeql.channel "$2" 2> /dev/null # Ignore a warning about unrecognized custom keys
gh config set extensions.codeql.version "" 2> /dev/null # Ignore a warning about unrecognized custom keys
echo "Switched to $2 channel. Any pinned version has been unset."
else
echo "Channel already set to $2."
fi
exit 0
fi
function get_latest() {
if [ "$channel" = "release" ]; then
# For the release channel, we sort by semantic version order since the tags correspond to release versions.
# We ignore draft releases and pre-release versions in this case since we want to give the user a stable version.
gh api "repos/$repo/releases" --paginate --jq ".[] | select(.draft == false and .prerelease == false) | .tag_name" | sort -V | tail -1
else
# For the nightly channel, we just take the latest non-draft release since the tags there are not semantic versions.
gh api "repos/$repo/releases" --jq ".[] | select(.draft == false) | .tag_name" | head -1
fi
}
function download() {
local version="$1"
if [ -z "$version" ] || [ "$version" = "latest" ]; then
version="$(get_latest)"
fi
if [ -x "$rootdir/dist/$channel/$version/codeql" ] ; then
# Already downloaded.
return 0
fi
id=$(gh api "repos/$repo/releases" --paginate --jq ".[] | select(.tag_name == \"$version\") | .id" | head -n1)
if [ -z "$id" ]; then
error "Version '$1' not found."
fi
mkdir -p "$rootdir/dist/$channel"
tempdir="$(mktemp -d "$rootdir/dist/$channel/temp_$version.XXXXXXXX")"
trap 'rm -rf "$tempdir"' EXIT
echo "Downloading CodeQL CLI version $version..."
curl -L -o "$tempdir/codeql-$platform.zip" "https://github.com/$repo/releases/download/$version/codeql-$platform.zip"
# The below is neater but cannot be made to display a progress bar at the moment:
# gh release download -R "$repo" "$version" --pattern "codeql-$platform.zip" --dir "$tempdir"
echo "Unpacking CodeQL CLI version $version..."
unzip -oq "$tempdir/codeql-$platform.zip" -d "$tempdir"
mv "$tempdir/codeql" "$rootdir/dist/$channel/$version"
rm -rf "$tempdir"
}
function validate_version() {
local version="$1"
if [ -z "$version" ]; then
error "Version must be specified. Use 'latest' to automatically determine the latest version."
elif [ "$version" = "latest" ]; then
version="$(get_latest)"
else
# Versions in github/codeql-cli-binaries have tags prefixed with v, which the user might have omitted.
# If necessary, we add it in here.
repoName=${repo#*/}
repoOwner=${repo%/*}
version="$(gh api graphql -f query='{
repository(owner: "'"$repoOwner"'", name: "'"$repoName"'") {
asEntered: release(tagName: "'"$version"'") {
tagName
}
vPrefixed: release(tagName: "v'"$version"'") {
tagName
}
}
}' --jq '[.data.repository.[] | values][0].tagName')"
if [ -z "$version" ] ; then
error "Unknown version: '$1'."
fi
fi
echo "$version"
}
function set_version() {
local version=$(validate_version "$1")
if [ -z "$version" ]; then
exit 1
fi
download "$version"
gh config set extensions.codeql.version "$version" 2> /dev/null # Ignore a warning about unrecognized custom keys
}
function set_local_version() {
local version=$(validate_version "$1")
download "$version"
echo "$version" > .codeql-version
}
function install_stub() {
local bindir="$1"
if [ -z "$bindir" ]; then
bindir="/usr/local/bin"
fi
if [ ! -e "$bindir" ]; then
error "The directory $bindir doesn't exist, please provide a different directory like 'gh codeql install-stub [dir]' to install a stub."
fi
if [ -w "$bindir" ]; then
cat << "_codeql_stub" > "$bindir/codeql"
#!/usr/bin/env bash
set -e
exec -a codeql gh codeql "$@"
_codeql_stub
chmod +x "$bindir/codeql"
else
error "Missing write permission on $bindir. Please provide a directory with write permissions to install a stub."
fi
}
# Handle the download command.
if [ "$1" = "download" ]; then
download "$2"
exit 0
fi
# Handle the set-version command.
if [ "$1" = "set-version" ]; then
set_version "$2"
version="$(gh config get extensions.codeql.version 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
exec "$rootdir/dist/$channel/$version/codeql" version
fi
# Handle the set-local-version command
if [ "$1" = "set-local-version" ]; then
if [ "$local_version" = "true" ]; then
set_local_version "$2"
version="$(<.codeql-version)" || : # Supress an error and return empty if the file doesn't exist
exec "$rootdir/dist/$channel/$version/codeql" version
else
error "$(cat << _message
Local version support is disabled by default to remove the opportunity from untrusted git repositories to
abuse older CodeQL CLIs. Enable local version support with the command 'gh codeql local-version on'.
_message
)"
fi
fi
# Handle the unset-local-version command
if [ "$1" = "unset-local-version" ]; then
if [ -e .codeql-version ]; then
rm -f .codeql-version
else
warning "No local version specified."
fi
exit 0
fi
# Handle the list-installed command.
if [ "$1" = "list-installed" ]; then
( cd "$rootdir/dist/$channel" ; find . -mindepth 1 -maxdepth 1 -type d | cut -c3- ; )
exit 0
fi
# Handle the cleanup command.
if [ "$1" = "cleanup" ]; then
if [ -z "$2" ]; then
error "Version must be specified."
elif [ ! -d "$rootdir/dist/$channel/$2" ]; then
error "Version '$2' not found."
fi
rm -rf "$rootdir/dist/$channel/$2"
exit 0
fi
# Handle the cleanup-all command
if [ "$1" = "cleanup-all" ]; then
rm -rf "$rootdir/dist"
exit 0
fi
# Handle the install-stub command
if [ "$1" = "install-stub" ]; then
install_stub "$2"
exit 0
fi
if [ -z "$version" ]; then
set_version latest
version="$(gh config get extensions.codeql.version 2> /dev/null)" || : # Suppress an error and return empty if the field doesn't exist
fi
download "$version"
export CODEQL_DIST="$rootdir/dist/$channel/$version"
exec "$rootdir/dist/$channel/$version/codeql" "$@"