Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c76ae1

Browse files
committed
Initial implementation
0 parents  commit 0c76ae1

2 files changed

Lines changed: 217 additions & 0 deletions

File tree

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# CodeQL extension for the [GitHub CLI](https://cli.github.com/)
2+
3+
This CLI extension exposes the [CodeQL CLI](https://codeql.github.com/docs/codeql-cli/) as a subcommand of the GitHub CLI, with some additional niceties such as version management.
4+
5+
## Installation
6+
7+
Once you have installed the GitHub CLI (version 2.0 or higher), run `gh extensions install github/gh-codeql`.
8+
9+
## Usage
10+
11+
```bash
12+
$ gh codeql
13+
GitHub command-line wrapper for the CodeQL CLI.
14+
15+
Usage:
16+
gh codeql set-channel [release|nightly] # default: release
17+
gh codeql set-version [version] # default: latest
18+
gh codeql list-versions # list all available versions for current channel
19+
gh codeql list-installed # list installed versions for current channel
20+
gh codeql cleanup <version> # delete a specific downloaded version
21+
gh codeql cleanup-all # delete all installed versions for all channels
22+
gh codeql download [version] # download a specific version (default: latest)
23+
gh codeql debug [on|off] # enable/disable debug output for gh extension
24+
gh codeql <anything else> # pass arguments to CodeQL CLI
25+
26+
Current channel: release.
27+
Current version: not specified.
28+
```
29+
30+
You should be able to prefix any `codeql` command you run with `gh` to automatically download the selected version (by default: the latest release version at the time you first run it) and delegate to it.
31+
32+
### Channels
33+
34+
There are two channels: "release" and "nightly". You are on the release channel by default, and switching channels unpins the selected version (meaning that, unless you run `gh codeql set-version`, the latest version of the current channel will be selected the next time you run a command).
35+
36+
You can list the versions available on the current channel with `gh codeql list-versions`.
37+
38+
You can list the installed versions from the current channel with `gh codeql list-installed`, and reclaim disk space with `gh codeql cleanup <version>`. There is no automatic cleanup.
39+
40+
### Versions
41+
42+
The `gh codeql` command always works relative to a pinned version on the current channel. You can manually specify the pinned version using `gh codeql set-version`.
43+
44+
You can download additional versions without pinning them (perhaps to prepare for local comparisons) using `gh codeql download`.
45+
46+
To upgrade, run `gh codeql set-version latest`, which will pin you to the current latest version.

gh-codeql

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ "$(gh config get extensions.codeql.debug)" = "true" ] ; then
5+
set -x
6+
fi
7+
8+
error() {
9+
echo "ERROR: $*" 1>&2
10+
exit 1
11+
}
12+
13+
rootdir="$(dirname "$0")"
14+
channel="$(gh config get extensions.codeql.channel)"
15+
16+
if [ -z "$1" ]; then
17+
cat <<EOF
18+
GitHub command-line wrapper for the CodeQL CLI.
19+
20+
Usage:
21+
gh codeql set-channel [release|nightly] # default: release
22+
gh codeql set-version [version] # default: latest
23+
gh codeql list-versions # list all available versions for current channel
24+
gh codeql list-installed # list installed versions for current channel
25+
gh codeql cleanup <version> # delete a specific downloaded version
26+
gh codeql cleanup-all # delete all installed versions for all channels
27+
gh codeql download [version] # download a specific version (default: latest)
28+
gh codeql debug [on|off] # enable/disable debug output for gh extension
29+
gh codeql <anything else> # pass arguments to CodeQL CLI
30+
31+
Current channel: ${channel:-not specified}.
32+
Current version: ${version:-not specified}.
33+
EOF
34+
exit 0
35+
fi
36+
37+
if [ -z "$channel" ] || [ "$channel" = "release" ] ; then
38+
channel="release"
39+
repo=github/codeql-cli-binaries
40+
elif [ "$channel" = "nightly" ] ; then
41+
repo=dsp-testing/codeql-cli-nightlies
42+
else
43+
error "Channel must be 'release' or 'nightly'."
44+
fi
45+
46+
# determine platform using OSTYPE
47+
platform="$(gh config get extensions.codeql.platform)"
48+
if [[ -z "$platform" ]] ; then
49+
if [[ "$OSTYPE" == "darwin"* ]] ; then
50+
platform=osx64
51+
elif [[ "$OSTYPE" == "linux"* ]] ; then
52+
platform=linux64
53+
elif [[ "$OSTYPE" == "win"* ]] || [[ $OSTYPE == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]] ; then
54+
platform=win64
55+
else
56+
error "Couldn't determine platform from OSTYPE='$OSTYPE'. Consider setting extensions.codeql.platform."
57+
fi
58+
fi
59+
60+
# Handle debug command.
61+
if [ "$1" = "debug" ] ; then
62+
if [ "$2" = "on" ] ; then
63+
gh config set extensions.codeql.debug true
64+
elif [ "$2" = "off" ] ; then
65+
gh config set extensions.codeql.debug false
66+
else
67+
error "Invalid debug command: '$2'."
68+
fi
69+
exit 0
70+
fi
71+
72+
# Handle list-versions command.
73+
if [ "$1" = "list-versions" ]; then
74+
gh api "repos/$repo/releases" --paginate --jq ".[].tag_name"
75+
exit 0
76+
fi
77+
78+
# Handle set-channel command.
79+
if [ "$1" = "set-channel" ]; then
80+
if [ "$2" != "release" ] && [ "$2" != "nightly" ]; then
81+
error "Invalid channel: '$2'."
82+
fi
83+
old_channel="$(gh config get extensions.codeql.channel)"
84+
if [ "${old_channel:-release}" != "$2" ] ; then
85+
gh config set extensions.codeql.channel "$2"
86+
gh config set extensions.codeql.version ""
87+
echo "Switched to $2 channel. Any pinned version has been unset."
88+
else
89+
echo "Channel already set to $2."
90+
fi
91+
exit 0
92+
fi
93+
94+
function download() {
95+
local version="$1"
96+
if [ -z "$version" ] || [ "$version" = "latest" ]; then
97+
version=$(gh api "repos/$repo/releases/latest" --jq '.tag_name')
98+
fi
99+
if [ -x "$rootdir/dist/$channel/$version/codeql" ] ; then
100+
# Already downloaded.
101+
return 0
102+
fi
103+
id=$(gh api "repos/$repo/releases" --paginate --jq ".[] | select(.tag_name == \"$version\") | .id" | head -n1)
104+
if [ -z "$id" ]; then
105+
error "Version '$1' not found."
106+
fi
107+
mkdir -p "$rootdir/dist/$channel"
108+
tempdir="$(mktemp -d "$rootdir/dist/$channel/temp_$version.XXXXXXXX")"
109+
trap 'rm -rf "$tempdir"' EXIT
110+
echo "Downloading CodeQL CLI version $version..."
111+
gh release download -R "$repo" "$version" --pattern "codeql-$platform.zip" --dir "$tempdir"
112+
echo "Unpacking CodeQL CLI version $version..."
113+
unzip -oq "$tempdir/codeql-$platform.zip" -d "$tempdir"
114+
mv "$tempdir/codeql" "$rootdir/dist/$channel/$version"
115+
rm -rf "$tempdir"
116+
}
117+
118+
function set_version() {
119+
local version="$1"
120+
if [ -z "$version" ]; then
121+
error "Version must be specified. Use 'latest' to automatically determine the latest version."
122+
elif [ "$version" = "latest" ]; then
123+
version="$(gh api "repos/$repo/releases/latest" --jq ".tag_name")"
124+
fi
125+
download "$version"
126+
gh config set extensions.codeql.version "$version"
127+
}
128+
129+
# Handle the download command.
130+
if [ "$1" = "download" ]; then
131+
download "$2"
132+
exit 0
133+
fi
134+
135+
# Handle the set-version command.
136+
if [ "$1" = "set-version" ]; then
137+
set_version "$2"
138+
version="$(gh config get extensions.codeql.version)"
139+
exec "$rootdir/dist/$channel/$version/codeql" version
140+
fi
141+
142+
# Handle the list-installed command.
143+
if [ "$1" = "list-installed" ]; then
144+
( cd "$rootdir/dist/$channel" ; find . -depth 1 -type d | cut -c3- ; )
145+
exit 0
146+
fi
147+
148+
# Handle the cleanup command.
149+
if [ "$1" = "cleanup" ]; then
150+
if [ -z "$2" ]; then
151+
error "Version must be specified."
152+
elif [ ! -d "$rootdir/dist/$channel/$2" ]; then
153+
error "Version '$2' not found."
154+
fi
155+
rm -rf "$rootdir/dist/$channel/$2"
156+
exit 0
157+
fi
158+
159+
# Handle the cleanup-all command
160+
if [ "$1" = "cleanup-all" ]; then
161+
rm -rf "$rootdir/dist"
162+
exit 0
163+
fi
164+
165+
version="$(gh config get extensions.codeql.version)"
166+
if [ -z "$version" ]; then
167+
set_version latest
168+
version="$(gh config get extensions.codeql.version)"
169+
fi
170+
download "$version"
171+
exec "$rootdir/dist/$channel/$version/codeql" "$@"

0 commit comments

Comments
 (0)