Repository navigation
feat(ci): automate Go toolchain version updates via govulncheck failures #3267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dmihalcik-virtru
wants to merge
4
commits into
main
Choose a base branch
from
DSPX-2565-golang-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+811
−23
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5da3c4
feat(ci): automate Go toolchain version updates via govulncheck failures
dmihalcik-virtru 0fdc0d5
fix(ci): address zizmor and Copilot review findings
dmihalcik-virtru f37a566
remove cache
dmihalcik-virtru f2dd52b
refactor(ci): simplify Go version management — go.work as single sour…
dmihalcik-virtru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| #!/usr/bin/env bats | ||
| # Unit tests for bump-go-version.sh | ||
| # | ||
| # Requirements: bats-core, jq, go (for go mod edit / go work edit) | ||
|
|
||
| SCRIPT="$(cd "$(dirname "$BATS_TEST_FILENAME")" && pwd)/bump-go-version.sh" | ||
| FIXTURES="$(cd "$(dirname "$BATS_TEST_FILENAME")/fixtures" && pwd)" | ||
|
|
||
| # Local file URL for the mock go.dev API response. | ||
| API_URL="file://${FIXTURES}/go-versions.json" | ||
|
|
||
| # ── Helpers ──────────────────────────────────────────────────────────────────── | ||
|
|
||
| # build_repo ROOT TOOLCHAIN_VER [GO_DIRECTIVE] | ||
| # | ||
| # Populates ROOT with the minimal file set that bump-go-version.sh touches: | ||
| # go.work, one go.mod per MODULE_DIRS entry, and stub workflow YAML files. | ||
| # | ||
| # TOOLCHAIN_VER e.g. "1.25.7" | ||
| # GO_DIRECTIVE e.g. "1.25.0" (defaults to TOOLCHAIN_VER if omitted) | ||
| build_repo() { | ||
| local root="$1" | ||
| local toolchain="$2" | ||
| local go_dir="${3:-${toolchain}}" | ||
|
|
||
| # go.work | ||
| mkdir -p "$root" | ||
| cat > "${root}/go.work" <<EOF | ||
| go ${go_dir} | ||
|
|
||
| toolchain go${toolchain} | ||
|
|
||
| use ( | ||
| ./examples | ||
| ./lib/fixtures | ||
| ./lib/flattening | ||
| ./lib/identifier | ||
| ./lib/ocrypto | ||
| ./protocol/go | ||
| ./sdk | ||
| ./service | ||
| ./tests-bdd | ||
| ) | ||
| EOF | ||
|
|
||
| # One go.mod per module directory | ||
| for mod_dir in \ | ||
| examples sdk service \ | ||
| lib/fixtures lib/flattening lib/identifier lib/ocrypto \ | ||
| protocol/go tests-bdd test/integration; do | ||
| mkdir -p "${root}/${mod_dir}" | ||
| local mod_name | ||
| mod_name="github.com/opentdf/platform/${mod_dir}" | ||
| cat > "${root}/${mod_dir}/go.mod" <<EOF | ||
| module ${mod_name} | ||
|
|
||
| go ${go_dir} | ||
|
|
||
| toolchain go${toolchain} | ||
| EOF | ||
| done | ||
|
|
||
| # Stub CI workflow files | ||
| mkdir -p "${root}/.github/workflows" | ||
| cat > "${root}/.github/workflows/checks.yaml" <<EOF | ||
| name: Checks | ||
| jobs: | ||
| go: | ||
| steps: | ||
| - name: govulncheck | ||
| uses: golang/govulncheck-action@abc123 | ||
| with: | ||
| go-version-input: "1.25.7" | ||
| EOF | ||
|
|
||
| cat > "${root}/.github/workflows/sonarcloud.yml" <<EOF | ||
| name: SonarCloud | ||
| jobs: | ||
| gotest: | ||
| steps: | ||
| - name: Setup Go | ||
| uses: actions/setup-go@abc123 | ||
| with: | ||
| go-version: "1.25.7" | ||
| EOF | ||
| } | ||
|
|
||
| # ── Tests ────────────────────────────────────────────────────────────────────── | ||
|
|
||
| @test "patch version bump updates toolchain in go.work and all go.mod files" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.25.7" "1.25.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| # go.work toolchain | ||
| grep -q "toolchain go1.25.9" "${root}/go.work" | ||
|
|
||
| # Spot-check a few modules | ||
| grep -q "toolchain go1.25.9" "${root}/service/go.mod" | ||
| grep -q "toolchain go1.25.9" "${root}/sdk/go.mod" | ||
| grep -q "toolchain go1.25.9" "${root}/test/integration/go.mod" | ||
| } | ||
|
|
||
| @test "patch version bump does not change the go directive" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.25.7" "1.25.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| # go directive must stay at 1.25.0 | ||
| grep -q "^go 1\.25\.0$" "${root}/go.work" | ||
| grep -q "^go 1\.25\.0$" "${root}/service/go.mod" | ||
| } | ||
|
|
||
| @test "minor version bump updates go directive to X.Y.0" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.24.3" "1.24.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.26.1" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| # go directive must become 1.26.0 | ||
| grep -q "^go 1\.26\.0$" "${root}/go.work" | ||
| grep -q "^go 1\.26\.0$" "${root}/sdk/go.mod" | ||
| grep -q "^go 1\.26\.0$" "${root}/test/integration/go.mod" | ||
|
|
||
| # toolchain must become 1.26.1 | ||
| grep -q "toolchain go1\.26\.1" "${root}/go.work" | ||
| grep -q "toolchain go1\.26\.1" "${root}/service/go.mod" | ||
| } | ||
|
|
||
| @test "already at target version exits with code 2 and does not modify files" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.25.9" "1.25.0" | ||
|
|
||
| # Capture mtime of go.work to verify no write happened | ||
| local before_mtime | ||
| before_mtime="$(stat -c %Y "${root}/go.work" 2>/dev/null || stat -f %m "${root}/go.work")" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" | ||
| [ "$status" -eq 2 ] | ||
|
|
||
| local after_mtime | ||
| after_mtime="$(stat -c %Y "${root}/go.work" 2>/dev/null || stat -f %m "${root}/go.work")" | ||
| [ "$before_mtime" -eq "$after_mtime" ] | ||
| } | ||
|
|
||
| @test "dry-run outputs a unified diff and does not modify files" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.25.7" "1.25.0" | ||
|
|
||
| # Capture original content | ||
| local orig_toolchain | ||
| orig_toolchain="$(grep "^toolchain " "${root}/go.work")" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" --dry-run | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| # Output must contain a diff header | ||
| [[ "$output" == *"--- a/go.work"* ]] | ||
| [[ "$output" == *"+toolchain go1.25.9"* ]] | ||
|
|
||
| # Original file must be untouched | ||
| grep -q "$orig_toolchain" "${root}/go.work" | ||
| } | ||
|
|
||
| @test "dry-run with already-at-latest exits 2 and produces no diff output" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| build_repo "$root" "1.25.9" "1.25.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" --dry-run | ||
| [ "$status" -eq 2 ] | ||
| [ -z "$output" ] || [[ "$output" != *"---"* ]] | ||
| } | ||
|
|
||
| @test "CI YAML files are updated unconditionally even if they lag the toolchain" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| # Repo toolchain is 1.25.8 but YAML files still reference 1.25.7 | ||
| build_repo "$root" "1.25.8" "1.25.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --target "1.25.9" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| grep -q 'go-version-input: "1.25.9"' "${root}/.github/workflows/checks.yaml" | ||
| grep -q 'go-version: "1.25.9"' "${root}/.github/workflows/sonarcloud.yml" | ||
| } | ||
|
|
||
| @test "EOL minor version detection upgrades to latest supported minor" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| # 1.23.x is EOL (not in the two most recent minors per fixtures: 1.25, 1.24) | ||
| build_repo "$root" "1.23.8" "1.23.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --api-url "$API_URL" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| # Should have been bumped to the latest minor (1.25.9 per fixtures) | ||
| grep -q "toolchain go1\.25\." "${root}/go.work" | ||
| grep -q "^go 1\.25\.0$" "${root}/go.work" | ||
| } | ||
|
|
||
| @test "API lookup picks latest patch for current in-support minor" { | ||
| local root="${BATS_TEST_TMPDIR}/repo" | ||
| # 1.25.7 is not the latest patch in fixtures (1.25.9 is) | ||
| build_repo "$root" "1.25.7" "1.25.0" | ||
|
|
||
| run "$SCRIPT" --repo-root "$root" --api-url "$API_URL" | ||
| [ "$status" -eq 0 ] | ||
|
|
||
| grep -q "toolchain go1\.25\.9" "${root}/go.work" | ||
| # go directive must NOT change (still a patch bump) | ||
| grep -q "^go 1\.25\.0$" "${root}/go.work" | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test does not cover pure YAML-drift scenario.
This test verifies YAML files are updated when the toolchain also needs bumping (1.25.8 → 1.25.9). However, it doesn't cover the case where
go.workis already at the target (e.g., 1.25.9) but the YAML files still reference an older version (e.g., 1.25.7). The script's early exit at line 181-184 ofbump-go-version.shwould skip updates in that scenario.Consider adding a test like:
🤖 Prompt for AI Agents