Skip to content

Commit

Permalink
fix(releaser): add MODULE.bazel boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-french committed Jan 15, 2025
1 parent 203aa70 commit 18e43af
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 8 deletions.
6 changes: 5 additions & 1 deletion go/tools/releaser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ go_library(

go_test(
name = "releaser_test",
srcs = ["upgradedep_test.go"],
srcs = [
"boilerplate_test.go",
"upgradedep_test.go",
],
data = glob(["testdata/**"]),
embed = [":releaser_lib"],
deps = ["@com_github_bazelbuild_buildtools//build:go_default_library"],
)
27 changes: 22 additions & 5 deletions go/tools/releaser/boilerplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,44 @@ import (
"io"
"net/http"
"sort"
"strings"

"golang.org/x/mod/semver"
)

func genBoilerplate(version, shasum, goVersion string) string {
return fmt.Sprintf(`load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
func genBoilerplate(version, shasum, goVersion, rnotesData string) string {
trimmedVersion := strings.TrimPrefix(version, "v")
return fmt.Sprintf(`%[4]s
## `+"`MODULE.bazel`"+` code
`+"```"+`
bazel_dep(name = "rules_go", version = "%[1]s")
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "%[3]s")
`+"```"+`
## `+"`WORKSPACE`"+` code
`+"```"+`
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "%[2]s",
urls = [
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/%[1]s/rules_go-%[1]s.zip",
"https://github.com/bazel-contrib/rules_go/releases/download/%[1]s/rules_go-%[1]s.zip",
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v%[1]s/rules_go-v%[1]s.zip",
"https://github.com/bazel-contrib/rules_go/releases/download/v%[1]s/rules_go-v%[1]s.zip",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "%[3]s")`, version, shasum, goVersion)
go_register_toolchains(version = "%[3]s")
`+"```\n", trimmedVersion, shasum, goVersion, rnotesData)
}

func findLatestGoVersion() (v string, err error) {
Expand Down
29 changes: 29 additions & 0 deletions go/tools/releaser/boilerplate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestGenBoilerplate(t *testing.T) {
version := "v1.2.3"
shasum := "abcd1234"
goVersion := "1.23"
rnotesData := "Release notes"

actual := genBoilerplate(version, shasum, goVersion, rnotesData)

expectedPath := filepath.Join(".", "testdata", "boilerplate.md")
expectedBytes, err := os.ReadFile(expectedPath)
if err != nil {
t.Fatalf("failed to read expected boilerplate: %v", err)
}
expected := strings.TrimSpace(string(expectedBytes))
actual = strings.TrimSpace(actual)

if actual != expected {
t.Errorf("generated boilerplate does not match expected\nExpected:\n%s\nActual:\n%s", expected, actual)
}
}
3 changes: 1 addition & 2 deletions go/tools/releaser/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func runPrepare(ctx context.Context, stderr io.Writer, args []string) error {
if err != nil {
return err
}
boilerplate := genBoilerplate(version, arcSum, goVersion)
rnotesStr := string(rnotesData) + "\n\n## `WORKSPACE` code\n\n```\n" + boilerplate + "\n```\n"
rnotesStr := genBoilerplate(version, arcSum, goVersion, string(rnotesData))

// Push the release branch.
fmt.Fprintf(stderr, "pushing branch %s to origin...\n", branchName)
Expand Down
31 changes: 31 additions & 0 deletions go/tools/releaser/testdata/boilerplate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Release notes

## `MODULE.bazel` code

```
bazel_dep(name = "rules_go", version = "1.2.3")
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(version = "1.23")
```

## `WORKSPACE` code

```
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "abcd1234",
urls = [
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v1.2.3/rules_go-v1.2.3.zip",
"https://github.com/bazel-contrib/rules_go/releases/download/v1.2.3/rules_go-v1.2.3.zip",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "1.23")
```

0 comments on commit 18e43af

Please sign in to comment.