From a8b651a7ccbb5faa999066c9d1f757da0a665a28 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 14 Apr 2022 10:53:10 +0000 Subject: [PATCH] tools: Add resultsdb package Wraps the luci package to make it a bit friendlier. Bug: dawn:1342 Change-Id: I8e0b5c27696d23aaf704fedcbdf71b26070f5d34 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/86528 Reviewed-by: Corentin Wallez Kokoro: Kokoro Commit-Queue: Ben Clayton --- tools/src/resultsdb/resultsdb.go | 97 ++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tools/src/resultsdb/resultsdb.go diff --git a/tools/src/resultsdb/resultsdb.go b/tools/src/resultsdb/resultsdb.go new file mode 100644 index 0000000000..3a8f0d6472 --- /dev/null +++ b/tools/src/resultsdb/resultsdb.go @@ -0,0 +1,97 @@ +// Copyright 2022 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package resultsdb provides helpers for interfacing with resultsdb +package resultsdb + +import ( + "context" + "fmt" + + "dawn.googlesource.com/dawn/tools/src/buildbucket" + "go.chromium.org/luci/auth" + "go.chromium.org/luci/grpc/prpc" + "go.chromium.org/luci/hardcoded/chromeinfra" + rdbpb "go.chromium.org/luci/resultdb/proto/v1" + "google.golang.org/protobuf/types/known/fieldmaskpb" +) + +// ResultsDB is the client to communicate with ResultDB. +type ResultsDB struct { + client rdbpb.ResultDBClient +} + +// New creates a client to communicate with ResultDB. +func New(ctx context.Context, credentials auth.Options) (*ResultsDB, error) { + http, err := auth.NewAuthenticator(ctx, auth.InteractiveLogin, credentials).Client() + if err != nil { + return nil, err + } + client, err := rdbpb.NewResultDBPRPCClient( + &prpc.Client{ + C: http, + Host: chromeinfra.ResultDBHost, + Options: prpc.DefaultOptions(), + }), nil + if err != nil { + return nil, err + } + + return &ResultsDB{client}, nil +} + +// QueryTestResults fetches the test results for the given builds. +// f is called once per page of test variants. +func (r *ResultsDB) QueryTestResults( + ctx context.Context, + builds []buildbucket.BuildID, + filterRegex string, + f func(*rdbpb.TestResult) error) error { + + invocationNames := make([]string, len(builds)) + for i, id := range builds { + invocationNames[i] = fmt.Sprintf("invocations/build-%v", id) + } + + pageToken := "" + for { + rsp, err := r.client.QueryTestResults(ctx, &rdbpb.QueryTestResultsRequest{ + Invocations: invocationNames, + Predicate: &rdbpb.TestResultPredicate{ + TestIdRegexp: filterRegex, + }, + ReadMask: &fieldmaskpb.FieldMask{Paths: []string{ + "status", "tags", "failure_reason", + }}, + PageSize: 1000, // Maximum page size. + PageToken: pageToken, + }) + if err != nil { + return err + } + + for _, res := range rsp.TestResults { + if err := f(res); err != nil { + return err + } + } + + pageToken = rsp.GetNextPageToken() + if pageToken == "" { + break + } + } + + return nil +}