| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "log" |
| "strconv" |
| |
| "google.golang.org/genproto/protobuf/field_mask" |
| "google.golang.org/grpc" |
| |
| buildbucketpb "go.chromium.org/luci/buildbucket/proto" |
| ) |
| |
| // The name of the recipe property passed to Fuchsia builders, which communicates which |
| // Cloud storage bucket to upload artifacts to. |
| const bucketPropertyName = "artifact_gcs_bucket" |
| |
| // The name of the storage bucket that stores build artifacts that don't expire. If a |
| // Buildbucket build has expired, check this bucket to see if the artifacts still exist. |
| const defaultStorageBucket = "fuchsia-artifacts-release" |
| |
| // buildsClient sends RPCs to a BuildBucket server. |
| type buildsClient interface { |
| GetBuild(context.Context, *buildbucketpb.GetBuildRequest, ...grpc.CallOption) (*buildbucketpb.Build, error) |
| } |
| |
| func getStorageBucket(ctx context.Context, client buildsClient, build string) (string, error) { |
| buildID, err := strconv.ParseInt(build, 10, 64) |
| if err != nil { |
| return "", err |
| } |
| |
| response, err := client.GetBuild(ctx, &buildbucketpb.GetBuildRequest{ |
| Id: buildID, |
| Fields: &field_mask.FieldMask{ |
| Paths: []string{"output"}, |
| }, |
| }) |
| if err != nil || response == nil { |
| // If the build can't be found because it expired, return the |
| // default bucket to check if the artifacts still exist. |
| log.Printf("failed to find build with response: %v, err: %s\n", response, err) |
| log.Printf("using default bucket %s\n", defaultStorageBucket) |
| return defaultStorageBucket, nil |
| } |
| |
| prop, ok := response.Output.Properties.Fields[bucketPropertyName] |
| if !ok { |
| return "", fmt.Errorf("no output property %q found for build %q", bucketPropertyName, build) |
| } |
| |
| return prop.GetStringValue(), nil |
| } |