go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/artifacts/common.go (about)

     1  // Copyright 2019 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"log"
    11  	"strconv"
    12  
    13  	"google.golang.org/genproto/protobuf/field_mask"
    14  	"google.golang.org/grpc"
    15  
    16  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    17  )
    18  
    19  // The name of the recipe property passed to Fuchsia builders, which communicates which
    20  // Cloud storage bucket to upload artifacts to.
    21  const bucketPropertyName = "artifact_gcs_bucket"
    22  
    23  // The name of the storage bucket that stores build artifacts that don't expire. If a
    24  // Buildbucket build has expired, check this bucket to see if the artifacts still exist.
    25  const defaultStorageBucket = "fuchsia-artifacts-release"
    26  
    27  // buildsClient sends RPCs to a BuildBucket server.
    28  type buildsClient interface {
    29  	GetBuild(context.Context, *buildbucketpb.GetBuildRequest, ...grpc.CallOption) (*buildbucketpb.Build, error)
    30  }
    31  
    32  func getStorageBucket(ctx context.Context, client buildsClient, build string) (string, error) {
    33  	buildID, err := strconv.ParseInt(build, 10, 64)
    34  	if err != nil {
    35  		return "", err
    36  	}
    37  
    38  	response, err := client.GetBuild(ctx, &buildbucketpb.GetBuildRequest{
    39  		Id: buildID,
    40  		Fields: &field_mask.FieldMask{
    41  			Paths: []string{"output"},
    42  		},
    43  	})
    44  	if err != nil || response == nil {
    45  		// If the build can't be found because it expired, return the
    46  		// default bucket to check if the artifacts still exist.
    47  		log.Printf("failed to find build with response: %v, err: %s\n", response, err)
    48  		log.Printf("using default bucket %s\n", defaultStorageBucket)
    49  		return defaultStorageBucket, nil
    50  	}
    51  
    52  	prop, ok := response.Output.Properties.Fields[bucketPropertyName]
    53  	if !ok {
    54  		return "", fmt.Errorf("no output property %q found for build %q", bucketPropertyName, build)
    55  	}
    56  
    57  	return prop.GetStringValue(), nil
    58  }