-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate-release-version.sh
More file actions
executable file
·415 lines (371 loc) · 13.6 KB
/
update-release-version.sh
File metadata and controls
executable file
·415 lines (371 loc) · 13.6 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env bash
set -euo pipefail
## update-release-version.sh
## Deterministically updates the release version across all version-bearing files
## in the codeql-development-mcp-server repository.
##
## Version-bearing files:
## .codeql-version (vX.Y.Z format)
## package.json (X.Y.Z format)
## client/package.json (X.Y.Z format)
## server/package.json (X.Y.Z format)
## server/src/codeql-development-mcp-server.ts (X.Y.Z format)
## server/ql/*/tools/src/codeql-pack.yml (X.Y.Z format)
## server/ql/*/tools/test/codeql-pack.yml (X.Y.Z format)
##
## The base version (X.Y.Z without any prerelease suffix) must correspond to
## an actual CodeQL CLI release. The script validates this by checking against
## the installed `codeql` CLI or the `.codeql-version` file.
##
## Usage:
## ./server/scripts/update-release-version.sh <new-version>
## ./server/scripts/update-release-version.sh --check [<expected-version>]
##
## Examples:
## ./server/scripts/update-release-version.sh 2.24.1
## ./server/scripts/update-release-version.sh 2.24.1-beta
## ./server/scripts/update-release-version.sh v2.24.1-beta
## ./server/scripts/update-release-version.sh --check
## ./server/scripts/update-release-version.sh --check 2.24.1-beta
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
## Supported languages for ql-mcp-* packs
LANGUAGES=("actions" "cpp" "csharp" "go" "java" "javascript" "python" "ruby" "swift")
usage() {
cat <<EOF
Usage: $0 <new-version>
$0 --check [<expected-version>]
Deterministically updates the release version across all version-bearing files.
The base version (X.Y.Z, without prerelease suffixes) must correspond to an
actual CodeQL CLI release.
ARGUMENTS:
<new-version> The new version to set (e.g., 2.24.1 or 2.24.1-beta).
The 'v' prefix is optional and will be normalized.
The base version (X.Y.Z) is validated against the
installed CodeQL CLI or .codeql-version file.
OPTIONS:
--check [<version>] Check version consistency across all files.
If <version> is provided, also validates that all files
match the expected version.
--dry-run Show what would be changed without modifying files.
--skip-cli-validation Skip CodeQL CLI version validation (not recommended).
-h, --help Show this help message.
EXAMPLES:
$0 2.24.1 Update all files to version 2.24.1
$0 2.24.1-beta Update all files to version 2.24.1-beta
$0 v2.24.1-beta Same as above (v prefix is stripped automatically)
$0 --check Verify all version-bearing files are consistent
$0 --check 2.24.1 Verify all files contain version 2.24.1
$0 --dry-run 2.24.1 Preview changes without writing files
EOF
}
## Collect all version-bearing files and their current versions
collect_versions() {
local versions=()
## .codeql-version (stores vX.Y.Z)
local codeql_version_file="${REPO_ROOT}/.codeql-version"
if [[ -f "${codeql_version_file}" ]]; then
local raw_version
raw_version=$(tr -d '[:space:]' < "${codeql_version_file}")
versions+=(".codeql-version|${raw_version#v}")
else
echo "WARNING: .codeql-version not found" >&2
fi
## package.json files
local pkg_files=("package.json" "client/package.json" "server/package.json")
for pkg_file in "${pkg_files[@]}"; do
local full_path="${REPO_ROOT}/${pkg_file}"
if [[ -f "${full_path}" ]]; then
local pkg_version
pkg_version=$(grep -m1 '"version"' "${full_path}" | sed 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
versions+=("${pkg_file}|${pkg_version}")
else
echo "WARNING: ${pkg_file} not found" >&2
fi
done
## server runtime version constant
local server_ts="${REPO_ROOT}/server/src/codeql-development-mcp-server.ts"
if [[ -f "${server_ts}" ]]; then
local runtime_version
runtime_version=$(grep -m1 "^const VERSION = " "${server_ts}" | sed "s/.*VERSION = '\([^']*\)'.*/\1/")
versions+=("server/src/codeql-development-mcp-server.ts|${runtime_version}")
else
echo "WARNING: server/src/codeql-development-mcp-server.ts not found" >&2
fi
## codeql-pack.yml files (src and test packs for each language)
for lang in "${LANGUAGES[@]}"; do
for pack_type in "src" "test"; do
local pack_file="server/ql/${lang}/tools/${pack_type}/codeql-pack.yml"
local full_path="${REPO_ROOT}/${pack_file}"
if [[ -f "${full_path}" ]]; then
local pack_version
pack_version=$(grep -m1 "^version:" "${full_path}" | awk '{print $2}')
versions+=("${pack_file}|${pack_version}")
fi
done
done
printf '%s\n' "${versions[@]}"
}
## Check version consistency
check_versions() {
local expected_version="${1:-}"
local all_consistent=true
local first_version=""
local file_count=0
echo "=== Version Consistency Check ==="
echo ""
while IFS='|' read -r file version; do
file_count=$((file_count + 1))
if [[ -z "${first_version}" ]]; then
first_version="${version}"
fi
if [[ -n "${expected_version}" ]]; then
if [[ "${version}" == "${expected_version}" ]]; then
echo " ✅ ${file}: ${version}"
else
echo " ❌ ${file}: ${version} (expected ${expected_version})"
all_consistent=false
fi
else
if [[ "${version}" == "${first_version}" ]]; then
echo " ✅ ${file}: ${version}"
else
echo " ❌ ${file}: ${version} (differs from ${first_version})"
all_consistent=false
fi
fi
done < <(collect_versions)
echo ""
echo "Checked ${file_count} version-bearing files."
if [[ "${all_consistent}" == true ]]; then
if [[ -n "${expected_version}" ]]; then
echo "✅ All files match expected version: ${expected_version}"
else
echo "✅ All files are consistent at version: ${first_version}"
fi
return 0
else
echo "❌ Version inconsistency detected!"
return 1
fi
}
## Validate version format (X.Y.Z or X.Y.Z-suffix)
validate_version() {
local version="$1"
if [[ ! "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9._-]+)?$ ]]; then
echo "ERROR: Invalid version format '${version}'" >&2
echo "Expected format: X.Y.Z or X.Y.Z-suffix (e.g., 2.24.1 or 2.24.1-beta)" >&2
return 1
fi
}
## Extract the base version (X.Y.Z) from a potentially suffixed version (X.Y.Z-beta)
extract_base_version() {
local version="$1"
echo "${version%%-*}"
}
## Validate that the base version corresponds to an actual CodeQL CLI version.
## Checks in order:
## 1. If 'codeql' CLI is on PATH, compare its version
## 2. Fall back to the .codeql-version file in the repo
validate_cli_version() {
local base_version="$1"
echo "=== CodeQL CLI Version Validation ==="
echo " Required base version: ${base_version}"
## Try the installed CLI first
if command -v codeql >/dev/null 2>&1; then
local installed_version
installed_version=$(codeql version --format=terse 2>/dev/null || echo "unknown")
echo " Installed CLI version: ${installed_version}"
if [[ "${installed_version}" == "${base_version}" ]]; then
echo " ✅ Base version ${base_version} matches installed CodeQL CLI"
return 0
else
echo " ❌ Base version ${base_version} does not match installed CodeQL CLI (${installed_version})" >&2
echo "" >&2
echo " The base version (X.Y.Z, without prerelease suffix) must match an" >&2
echo " available CodeQL CLI release. Either:" >&2
echo " - Install the correct CLI: gh codeql set-version ${base_version}" >&2
echo " - Use a version matching the installed CLI: ${installed_version}" >&2
echo " - Skip validation with --skip-cli-validation (not recommended)" >&2
return 1
fi
fi
## Fall back to .codeql-version file
local codeql_version_file="${REPO_ROOT}/.codeql-version"
if [[ -f "${codeql_version_file}" ]]; then
local file_version
file_version=$(tr -d '[:space:]' < "${codeql_version_file}")
file_version="${file_version#v}" ## Strip v prefix
echo " .codeql-version file: ${file_version}"
if [[ "${file_version}" == "${base_version}" ]]; then
echo " ✅ Base version ${base_version} matches .codeql-version"
return 0
else
echo " ❌ Base version ${base_version} does not match .codeql-version (${file_version})" >&2
echo "" >&2
echo " The base version (X.Y.Z, without prerelease suffix) must correspond" >&2
echo " to the CodeQL CLI version in .codeql-version." >&2
echo " Current .codeql-version: v${file_version}" >&2
echo " Skip validation with --skip-cli-validation (not recommended)" >&2
return 1
fi
fi
echo " WARNING: Cannot validate base version — no CodeQL CLI on PATH and no .codeql-version file" >&2
return 1
}
## Update a JSON file's "version" field using sed
update_json_version() {
local file="$1"
local new_version="$2"
## Match the "version": "..." line and replace the version value
sed -i.bak "s/\"version\"[[:space:]]*:[[:space:]]*\"[^\"]*\"/\"version\": \"${new_version}\"/" "${file}"
rm -f "${file}.bak"
}
## Update a codeql-pack.yml file's version field using sed
update_pack_version() {
local file="$1"
local new_version="$2"
## Match the version: line at the start and replace the version value
sed -i.bak "s/^version:[[:space:]]*.*/version: ${new_version}/" "${file}"
rm -f "${file}.bak"
}
## Update all version-bearing files
update_versions() {
local new_version="$1"
local dry_run="${2:-false}"
local updated_count=0
echo "=== Updating Release Version to ${new_version} ==="
echo ""
## 1. Update .codeql-version (uses v prefix)
local codeql_version_file="${REPO_ROOT}/.codeql-version"
if [[ -f "${codeql_version_file}" ]]; then
local old_version
old_version=$(tr -d '[:space:]' < "${codeql_version_file}")
if [[ "${dry_run}" == true ]]; then
echo " [DRY RUN] .codeql-version: ${old_version} -> v${new_version}"
else
printf "v%s\n" "${new_version}" > "${codeql_version_file}"
echo " ✅ .codeql-version: ${old_version} -> v${new_version}"
fi
updated_count=$((updated_count + 1))
fi
## 2. Update package.json files
local pkg_files=("package.json" "client/package.json" "server/package.json")
for pkg_file in "${pkg_files[@]}"; do
local full_path="${REPO_ROOT}/${pkg_file}"
if [[ -f "${full_path}" ]]; then
local old_version
old_version=$(grep -m1 '"version"' "${full_path}" | sed 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
if [[ "${dry_run}" == true ]]; then
echo " [DRY RUN] ${pkg_file}: ${old_version} -> ${new_version}"
else
update_json_version "${full_path}" "${new_version}"
echo " ✅ ${pkg_file}: ${old_version} -> ${new_version}"
fi
updated_count=$((updated_count + 1))
fi
done
## 2.5. Update server runtime version constant
local server_ts="${REPO_ROOT}/server/src/codeql-development-mcp-server.ts"
if [[ -f "${server_ts}" ]]; then
local old_version
old_version=$(grep -m1 "^const VERSION = " "${server_ts}" | sed "s/.*VERSION = '\([^']*\)'.*/\1/")
if [[ "${dry_run}" == true ]]; then
echo " [DRY RUN] server/src/codeql-development-mcp-server.ts: ${old_version} -> ${new_version}"
else
sed -i.bak "s/^const VERSION = '.*';/const VERSION = '${new_version}';/" "${server_ts}"
rm -f "${server_ts}.bak"
echo " ✅ server/src/codeql-development-mcp-server.ts: ${old_version} -> ${new_version}"
fi
updated_count=$((updated_count + 1))
fi
## 3. Update codeql-pack.yml files (src and test packs for each language)
for lang in "${LANGUAGES[@]}"; do
for pack_type in "src" "test"; do
local pack_file="server/ql/${lang}/tools/${pack_type}/codeql-pack.yml"
local full_path="${REPO_ROOT}/${pack_file}"
if [[ -f "${full_path}" ]]; then
local old_version
old_version=$(grep -m1 "^version:" "${full_path}" | awk '{print $2}')
if [[ "${dry_run}" == true ]]; then
echo " [DRY RUN] ${pack_file}: ${old_version} -> ${new_version}"
else
update_pack_version "${full_path}" "${new_version}"
echo " ✅ ${pack_file}: ${old_version} -> ${new_version}"
fi
updated_count=$((updated_count + 1))
fi
done
done
echo ""
if [[ "${dry_run}" == true ]]; then
echo "Would update ${updated_count} files. (Dry run — no files modified)"
else
echo "Updated ${updated_count} files to version ${new_version}."
echo ""
echo "Next steps:"
echo " 1. Run 'npm install' to regenerate package-lock.json"
echo " 2. Run 'npm run build-and-test' to validate the changes"
echo " 3. Commit the changes and tag with 'v${new_version}'"
fi
}
## Parse arguments
CHECK_MODE=false
DRY_RUN=false
SKIP_CLI_VALIDATION=false
NEW_VERSION=""
while [[ $# -gt 0 ]]; do
case $1 in
--check)
CHECK_MODE=true
shift
## Optional expected version argument
if [[ $# -gt 0 && ! "$1" =~ ^-- ]]; then
NEW_VERSION="${1#v}"
shift
fi
;;
--dry-run)
DRY_RUN=true
shift
;;
--skip-cli-validation)
SKIP_CLI_VALIDATION=true
shift
;;
-h|--help)
usage
exit 0
;;
-*)
echo "Error: Unknown option $1" >&2
usage >&2
exit 1
;;
*)
NEW_VERSION="${1#v}" ## Strip optional v prefix
shift
;;
esac
done
if [[ "${CHECK_MODE}" == true ]]; then
check_versions "${NEW_VERSION}"
exit $?
fi
if [[ -z "${NEW_VERSION}" ]]; then
echo "Error: No version specified" >&2
echo "" >&2
usage >&2
exit 1
fi
validate_version "${NEW_VERSION}"
## Validate that the base version matches an actual CodeQL CLI release
if [[ "${SKIP_CLI_VALIDATION}" == false ]]; then
BASE_VERSION=$(extract_base_version "${NEW_VERSION}")
validate_cli_version "${BASE_VERSION}"
echo ""
else
echo "⚠️ CLI version validation skipped (--skip-cli-validation)"
echo ""
fi
update_versions "${NEW_VERSION}" "${DRY_RUN}"