|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Pins every `codeql/<name>: '*'` dependency in this repo's qlpack.yml files to |
| 3 | +# the exact library version shipped in the official CodeQL Bundle for a given |
| 4 | +# CLI release. |
| 5 | +# |
| 6 | +# Why this exists: `codeql pack upgrade` resolves an unconstrained `'*'` |
| 7 | +# dependency to the *latest-ever-published* version in the configured |
| 8 | +# registry (GHCR) - completely independent of whatever CodeQL CLI version is |
| 9 | +# pinned in `.codeqlversion`. That mismatch can jump `codeql/<lang>-all` |
| 10 | +# several major versions ahead of what the pinned CLI actually ships/tests |
| 11 | +# against, which can silently break analyses (see CONTRIBUTING.md's |
| 12 | +# "Updating the pinned CodeQL CLI/library version" section for a worked |
| 13 | +# example). Pinning these `codeql/*` deps to the exact bundle-paired version |
| 14 | +# before running `codeql pack upgrade` makes that resolution deterministic |
| 15 | +# and keeps every pack's declared library dependency in lockstep with the |
| 16 | +# CLI version this repo says it supports. |
| 17 | +# |
| 18 | +# Usage: pin-codeql-library-versions.sh <cli-version> |
| 19 | +# e.g. pin-codeql-library-versions.sh 2.21.4 |
| 20 | +# |
| 21 | +# Requires: gh (authenticated), tar, sed, find. Run from the repo root. |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +if [[ $# -ne 1 ]]; then |
| 25 | + echo "Usage: $0 <cli-version>" >&2 |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +VERSION="$1" |
| 30 | +BUNDLE_TAG="codeql-bundle-v${VERSION}" |
| 31 | +WORKDIR="$(mktemp -d)" |
| 32 | +trap 'rm -rf "$WORKDIR"' EXIT |
| 33 | + |
| 34 | +echo "Determining codeql/* library versions bundled with CodeQL CLI ${VERSION}..." |
| 35 | +echo "(source of truth: github/codeql-action release '${BUNDLE_TAG}', asset codeql-bundle-linux64.tar.gz)" |
| 36 | + |
| 37 | +if ! gh release download "$BUNDLE_TAG" --repo github/codeql-action \ |
| 38 | + --pattern "codeql-bundle-linux64.tar.gz" --dir "$WORKDIR" --clobber; then |
| 39 | + echo "::error::Could not download the CodeQL Bundle for tag '${BUNDLE_TAG}' from github/codeql-action releases." >&2 |
| 40 | + echo "Every CLI release published to github/codeql-cli-binaries should have a matching 'codeql-bundle-v<version>' release in github/codeql-action - check that the tag exists." >&2 |
| 41 | + exit 1 |
| 42 | +fi |
| 43 | + |
| 44 | +# List every bundled `codeql/<name>` qlpack and its exact version WITHOUT |
| 45 | +# extracting any file contents - the bundle is large (~500MB) and we only |
| 46 | +# need the directory listing (name + version are encoded in the path). |
| 47 | +VERSIONS_FILE="$WORKDIR/versions.txt" |
| 48 | +tar tzf "$WORKDIR/codeql-bundle-linux64.tar.gz" \ |
| 49 | + | grep -E '^codeql/qlpacks/codeql/[^/]+/[^/]+/$' \ |
| 50 | + | sed -E 's#^codeql/qlpacks/codeql/([^/]+)/([^/]+)/$#\1 \2#' \ |
| 51 | + | sort -u > "$VERSIONS_FILE" |
| 52 | + |
| 53 | +echo "Discovered $(wc -l < "$VERSIONS_FILE") bundled codeql/* packages for CLI ${VERSION}." |
| 54 | + |
| 55 | +# Every real qlpack.yml in the repo, excluding gitignored local build |
| 56 | +# artifacts (.codeql/ pack caches, the /codeql cloned-repo checkout dir). |
| 57 | +mapfile -t QLPACK_FILES < <(find . -name qlpack.yml -not -path "*/.codeql/*" -not -path "./codeql/*") |
| 58 | + |
| 59 | +declare -A PINNED_COUNT=() |
| 60 | +while read -r pkg ver; do |
| 61 | + [[ -z "$pkg" ]] && continue |
| 62 | + count=0 |
| 63 | + for file in "${QLPACK_FILES[@]}"; do |
| 64 | + # Only touch lines declaring an unconstrained `codeql/<pkg>: '*'` (or |
| 65 | + # unquoted / double-quoted `*`) dependency; any trailing comment is left |
| 66 | + # untouched since the substitution only replaces the matched quote-star-quote. |
| 67 | + if grep -qE "^[[:space:]]*codeql/${pkg}:[[:space:]]*[\"']?\\*[\"']?[[:space:]]*(#.*)?\$" "$file"; then |
| 68 | + sed -i -E "s#^([[:space:]]*codeql/${pkg}:[[:space:]]*)([\"']?)\\*\\2#\\1\\2${ver}\\2#" "$file" |
| 69 | + count=$((count + 1)) |
| 70 | + fi |
| 71 | + done |
| 72 | + if [[ $count -gt 0 ]]; then |
| 73 | + PINNED_COUNT["$pkg@$ver"]=$count |
| 74 | + fi |
| 75 | +done < "$VERSIONS_FILE" |
| 76 | + |
| 77 | +echo |
| 78 | +echo "Pinned codeql/* dependencies:" |
| 79 | +for key in "${!PINNED_COUNT[@]}"; do |
| 80 | + echo " - ${key} (${PINNED_COUNT[$key]} file(s))" |
| 81 | +done | sort |
| 82 | + |
| 83 | +# Surface any remaining unconstrained codeql/* dependency that this script |
| 84 | +# did NOT pin (e.g. it isn't one of the standard per-language bundle |
| 85 | +# packages) so it doesn't silently keep resolving to registry-latest. |
| 86 | +echo |
| 87 | +echo "codeql/* dependencies left unpinned (not found in the CodeQL Bundle):" |
| 88 | +REMAINING=0 |
| 89 | +for file in "${QLPACK_FILES[@]}"; do |
| 90 | + if grep -qE "^[[:space:]]*codeql/[A-Za-z0-9_.-]+:[[:space:]]*[\"']?\*[\"']?[[:space:]]*(#.*)?\$" "$file"; then |
| 91 | + grep -nE "^[[:space:]]*codeql/[A-Za-z0-9_.-]+:[[:space:]]*[\"']?\*[\"']?[[:space:]]*(#.*)?\$" "$file" | sed "s#^# ${file}:#" |
| 92 | + REMAINING=1 |
| 93 | + fi |
| 94 | +done |
| 95 | +if [[ "$REMAINING" -eq 0 ]]; then |
| 96 | + echo " (none)" |
| 97 | +fi |
0 commit comments