@@ -19,7 +19,6 @@ import (
19
19
"archive/zip"
20
20
"bytes"
21
21
"compress/gzip"
22
- "encoding/json"
23
22
"errors"
24
23
"fmt"
25
24
"io"
@@ -35,7 +34,9 @@ import (
35
34
"strings"
36
35
"time"
37
36
37
+ "github.com/cockroachdb/cockroach-go/v2/testserver/version"
38
38
"github.com/gofrs/flock"
39
+ "gopkg.in/yaml.v3"
39
40
)
40
41
41
42
const (
@@ -45,15 +46,17 @@ const (
45
46
)
46
47
47
48
const (
48
- linuxUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.linux-%s.tgz"
49
- macUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.darwin-%s-%s.tgz"
50
- winUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.windows-6.2-amd64.zip"
51
- sourceUrlPat = "https://binaries.cockroachdb.com/cockroach-v%s.src.tgz)"
49
+ linuxUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.linux-%s.tgz"
50
+ macUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.darwin-%s-%s.tgz"
51
+ winUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.windows-6.2-amd64.zip"
52
52
)
53
53
54
- // updatesUrl is used to get the info of the latest stable version of CRDB.
55
- // Note that it may return a withdrawn version, but the risk is low for local tests here.
56
- const updatesUrl = "https://register.cockroachdb.com/api/updates"
54
+ // releaseDataURL is the location of the YAML file maintained by the
55
+ // docs team where release information is encoded. This data is used
56
+ // to render the public CockroachDB releases page. We leverage the
57
+ // data in structured format to generate release information used
58
+ // for testing purposes.
59
+ const releaseDataURL = "https://raw.githubusercontent.com/cockroachdb/docs/main/src/current/_data/releases.yml"
57
60
58
61
var muslRE = regexp .MustCompile (`(?i)\bmusl\b` )
59
62
@@ -249,37 +252,77 @@ func DownloadBinary(tc *TestConfig, desiredVersion string, nonStable bool) (stri
249
252
}
250
253
251
254
// GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
252
- func GetDownloadFilename (
253
- desiredVersion string ,
254
- ) (string , error ) {
255
+ func GetDownloadFilename (desiredVersion string ) (string , error ) {
255
256
filename := fmt .Sprintf ("cockroach-%s" , desiredVersion )
256
257
if runtime .GOOS == "windows" {
257
258
filename += ".exe"
258
259
}
259
260
return filename , nil
260
261
}
261
262
263
+ // Release contains the information we extract from the YAML file in
264
+ // `releaseDataURL`.
265
+ type Release struct {
266
+ Name string `yaml:"release_name"`
267
+ Withdrawn bool `yaml:"withdrawn"`
268
+ CloudOnly bool `yaml:"cloud_only"`
269
+ }
270
+
262
271
// getLatestStableVersionInfo returns the latest stable CRDB's download URL,
263
272
// and the formatted corresponding version number. The download URL is based
264
273
// on the runtime OS.
265
274
// Note that it may return a withdrawn version, but the risk is low for local tests here.
266
275
func getLatestStableVersionInfo () (string , string , error ) {
267
- resp , err := http .Get (updatesUrl )
276
+ resp , err := http .Get (releaseDataURL )
268
277
if err != nil {
269
- return "" , "" , err
278
+ return "" , "" , fmt . Errorf ( "could not download release data: %w" , err )
270
279
}
271
- var respJson map [string ]string
272
- if err := json .NewDecoder (resp .Body ).Decode (& respJson ); err != nil {
273
- return "" , "" , err
280
+ defer resp .Body .Close ()
281
+
282
+ var blob bytes.Buffer
283
+ if _ , err := io .Copy (& blob , resp .Body ); err != nil {
284
+ return "" , "" , fmt .Errorf ("error reading response body: %w" , err )
274
285
}
275
- latestStableVersion , ok := respJson ["version" ]
276
- if ! ok {
277
- return "" , "" , fmt .Errorf ("api/updates response is of wrong format" )
286
+
287
+ var data []Release
288
+ if err := yaml .Unmarshal (blob .Bytes (), & data ); err != nil { //nolint:yaml
289
+ return "" , "" , fmt .Errorf ("failed to YAML parse release data: %w" , err )
290
+ }
291
+
292
+ latestStableVersion := version .MustParse ("v0.0.0" )
293
+
294
+ for _ , r := range data {
295
+ // We ignore versions that cannot be parsed; this should
296
+ // correspond to really old beta releases.
297
+ v , err := version .Parse (r .Name )
298
+ if err != nil {
299
+ continue
300
+ }
301
+
302
+ // Skip cloud-only releases, since they cannot be downloaded from
303
+ // binaries.cockroachdb.com.
304
+ if r .CloudOnly {
305
+ continue
306
+ }
307
+
308
+ // Ignore any withdrawn releases, since they are known to be broken.
309
+ if r .Withdrawn {
310
+ continue
311
+ }
312
+
313
+ // Ignore alphas, betas, and RCs.
314
+ if v .PreRelease () != "" {
315
+ continue
316
+ }
317
+
318
+ if v .Compare (latestStableVersion ) > 0 {
319
+ latestStableVersion = v
320
+ }
278
321
}
279
322
280
- downloadUrl := getDownloadUrlForVersion (latestStableVersion )
323
+ downloadUrl := getDownloadUrlForVersion (latestStableVersion . String () )
281
324
282
- latestStableVerFormatted := strings .ReplaceAll (latestStableVersion , "." , "-" )
325
+ latestStableVerFormatted := strings .ReplaceAll (latestStableVersion . String () , "." , "-" )
283
326
return downloadUrl , latestStableVerFormatted , nil
284
327
}
285
328
0 commit comments