go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/buildbucket/facade/testdata/main.go (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 "flag" 20 "fmt" 21 "net/http" 22 "os" 23 24 "google.golang.org/protobuf/encoding/protojson" 25 26 "go.chromium.org/luci/auth" 27 bbpb "go.chromium.org/luci/buildbucket/proto" 28 "go.chromium.org/luci/common/logging" 29 "go.chromium.org/luci/grpc/prpc" 30 31 "go.chromium.org/luci/cv/internal/buildbucket" 32 ) 33 34 var downloadBuildFlag = flag.Int64("download-build", 0, "Fetches & stores test data for a given build ID.\n\nFor internal builds, run inside luci-auth context, but beware of confidential data.") 35 36 func downloadFixture(ctx context.Context, buildID int64) error { 37 opts := auth.Options{} 38 opts.PopulateDefaults() 39 rt, err := auth.NewAuthenticator(ctx, auth.OptionalLogin, opts).Transport() 40 if err != nil { 41 return err 42 } 43 client := bbpb.NewBuildsPRPCClient(&prpc.Client{ 44 C: &http.Client{Transport: rt}, 45 Host: "cr-buildbucket.appspot.com", 46 }) 47 build, err := client.GetBuild(ctx, &bbpb.GetBuildRequest{Id: buildID, Mask: buildbucket.TryjobBuildMask}) 48 if err != nil { 49 return err 50 } 51 52 buildJSON, err := protojson.MarshalOptions{Indent: " "}.Marshal(build) 53 if err != nil { 54 return err 55 } 56 57 fName := fmt.Sprintf("%d.json", buildID) 58 if err := os.WriteFile(fName, buildJSON, 0644); err != nil { 59 return err 60 } 61 62 fmt.Printf("Downloaded build %d to %q\n", buildID, fName) 63 return nil 64 } 65 66 func main() { 67 ctx := context.Background() 68 flag.Parse() 69 if *downloadBuildFlag > 0 { 70 if err := downloadFixture(ctx, *downloadBuildFlag); err != nil { 71 logging.Errorf(ctx, "downloading %d %s", downloadBuildFlag, err) 72 os.Exit(1) 73 } 74 } else { 75 fmt.Fprintln(os.Stderr, "-download-build is required") 76 } 77 os.Exit(0) 78 }