go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/buildbucket/facade/facade.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 bbfacade 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/codes" 21 "google.golang.org/grpc/status" 22 "google.golang.org/protobuf/types/known/fieldmaskpb" 23 24 bbpb "go.chromium.org/luci/buildbucket/proto" 25 "go.chromium.org/luci/common/errors" 26 "go.chromium.org/luci/common/proto/structmask" 27 "go.chromium.org/luci/common/retry/transient" 28 "go.chromium.org/luci/grpc/grpcutil" 29 30 "go.chromium.org/luci/cv/internal/buildbucket" 31 "go.chromium.org/luci/cv/internal/tryjob" 32 ) 33 34 // Facade provides APIs that LUCI CV can use to interact with buildbucket 35 // tryjobs. 36 type Facade struct { 37 ClientFactory buildbucket.ClientFactory 38 } 39 40 func (f *Facade) Kind() string { 41 return "buildbucket" 42 } 43 44 var defaultMask *bbpb.BuildMask 45 46 func init() { 47 defaultMask = &bbpb.BuildMask{ 48 Fields: &fieldmaskpb.FieldMask{}, 49 } 50 if err := defaultMask.Fields.Append((*bbpb.Build)(nil), fieldsToParse...); err != nil { 51 panic(err) 52 } 53 for _, key := range outputPropKeys { 54 defaultMask.OutputProperties = append(defaultMask.OutputProperties, &structmask.StructMask{ 55 Path: []string{key}, 56 }) 57 } 58 } 59 60 // Fetch retrieves the Buildbucket build for the given external ID and returns 61 // its current status and result. 62 func (f *Facade) Fetch(ctx context.Context, luciProject string, eid tryjob.ExternalID) (tryjob.Status, *tryjob.Result, error) { 63 host, buildID, err := eid.ParseBuildbucketID() 64 if err != nil { 65 return 0, nil, err 66 } 67 68 bbClient, err := f.ClientFactory.MakeClient(ctx, host, luciProject) 69 if err != nil { 70 return 0, nil, err 71 } 72 73 build, err := bbClient.GetBuild(ctx, &bbpb.GetBuildRequest{Id: buildID, Mask: defaultMask}) 74 switch code := status.Code(err); { 75 case code == codes.OK: 76 return parseStatusAndResult(ctx, build) 77 case grpcutil.IsTransientCode(code) || code == codes.DeadlineExceeded: 78 return 0, nil, transient.Tag.Apply(err) 79 default: 80 return 0, nil, err 81 } 82 } 83 84 // Parse parses tryjob status and result from the buildbucket build. 85 // 86 // Returns error if the given data is not a buildbucket build. 87 func (f *Facade) Parse(ctx context.Context, data any) (tryjob.Status, *tryjob.Result, error) { 88 build, ok := data.(*bbpb.Build) 89 if !ok { 90 return tryjob.Status_STATUS_UNSPECIFIED, nil, errors.Reason("expected data to be *bbpb.Build, got %T", data).Err() 91 } 92 return parseStatusAndResult(ctx, build) 93 } 94 95 // CancelTryjob asks buildbucket to cancel a running tryjob. 96 // 97 // It returns nil error if the buildbucket build is ended. 98 func (f *Facade) CancelTryjob(ctx context.Context, tj *tryjob.Tryjob, reason string) error { 99 host, buildID, err := tj.ExternalID.ParseBuildbucketID() 100 if err != nil { 101 return err 102 } 103 104 bbClient, err := f.ClientFactory.MakeClient(ctx, host, tj.LUCIProject()) 105 if err != nil { 106 return err 107 } 108 109 _, err = bbClient.CancelBuild(ctx, &bbpb.CancelBuildRequest{ 110 Id: buildID, 111 SummaryMarkdown: reason, 112 }) 113 return err 114 }