go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/services/recorder/create_test_result.go (about)

     1  // Copyright 2019 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 recorder
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/clock"
    22  	"go.chromium.org/luci/common/errors"
    23  	"go.chromium.org/luci/grpc/appstatus"
    24  
    25  	"go.chromium.org/luci/resultdb/pbutil"
    26  	pb "go.chromium.org/luci/resultdb/proto/v1"
    27  )
    28  
    29  func validateCreateTestResultRequest(msg *pb.CreateTestResultRequest, now time.Time) error {
    30  	if err := pbutil.ValidateInvocationName(msg.Invocation); err != nil {
    31  		return errors.Annotate(err, "invocation").Err()
    32  	}
    33  	if err := pbutil.ValidateTestResult(now, msg.TestResult); err != nil {
    34  		return errors.Annotate(err, "test_result").Err()
    35  	}
    36  	if err := pbutil.ValidateRequestID(msg.RequestId); err != nil {
    37  		return errors.Annotate(err, "request_id").Err()
    38  	}
    39  	return nil
    40  }
    41  
    42  // CreateTestResult implements pb.RecorderServer.
    43  func (s *recorderServer) CreateTestResult(ctx context.Context, in *pb.CreateTestResultRequest) (*pb.TestResult, error) {
    44  	now := clock.Now(ctx).UTC()
    45  	if err := validateCreateTestResultRequest(in, now); err != nil {
    46  		return nil, appstatus.BadRequest(err)
    47  	}
    48  
    49  	// Piggy back on BatchCreateTestResults.
    50  	res, err := s.BatchCreateTestResults(ctx, &pb.BatchCreateTestResultsRequest{
    51  		Invocation: in.Invocation,
    52  		Requests:   []*pb.CreateTestResultRequest{in},
    53  		RequestId:  in.RequestId,
    54  	})
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return res.TestResults[0], nil
    59  }