go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/rpc/v0/gerrit.go (about)

     1  // Copyright 2023 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 rpc
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"go.chromium.org/luci/common/sync/parallel"
    22  	apiv0pb "go.chromium.org/luci/cv/api/v0"
    23  	"go.chromium.org/luci/cv/internal/changelist"
    24  	"go.chromium.org/luci/cv/internal/common"
    25  	"go.chromium.org/luci/cv/internal/run"
    26  	"go.chromium.org/luci/gae/service/datastore"
    27  )
    28  
    29  // GerritIntegrationServer implements the v0 API.
    30  type GerritIntegrationServer struct {
    31  	apiv0pb.UnimplementedGerritIntegrationServer
    32  }
    33  
    34  // populateRunInfo converts run.Runs to apiv0pb.GetCLRunInfoResponse_RunInfos for the response.
    35  func populateRunInfo(ctx context.Context, runs []*run.Run) ([]*apiv0pb.GetCLRunInfoResponse_RunInfo, error) {
    36  	respRuns := make([]*apiv0pb.GetCLRunInfoResponse_RunInfo, len(runs))
    37  	errs := parallel.WorkPool(min(len(runs), 16), func(work chan<- func() error) {
    38  		for i, r := range runs {
    39  			i, r := i, r
    40  			work <- func() (err error) {
    41  				respRuns[i], err = populateRunInfoResponse(ctx, r)
    42  				return err
    43  			}
    44  		}
    45  	})
    46  	return respRuns, common.MostSevereError(errs)
    47  }
    48  
    49  // populateRunInfoResponse constructs and populates a
    50  // apiv0pb.GetCLRunInfoResponse_RunInfo to use in a response.
    51  //
    52  // This includes fetching and populating extra information, including CL info
    53  // to fill in details in each apiv0pb.GetCLRunInfoResponse_RunInfo.
    54  func populateRunInfoResponse(ctx context.Context, r *run.Run) (*apiv0pb.GetCLRunInfoResponse_RunInfo, error) {
    55  	var originChange *apiv0pb.GerritChange
    56  	if r.RootCL != 0 {
    57  		// Fetch the origin CL.
    58  		originCL := &changelist.CL{ID: r.RootCL}
    59  		if err := datastore.Get(ctx, originCL); err != nil {
    60  			return nil, err
    61  		}
    62  
    63  		originGerrit := originCL.Snapshot.GetGerrit()
    64  		if originGerrit == nil {
    65  			return nil, fmt.Errorf("root CL %d has non-Gerrit snapshot", r.RootCL)
    66  		}
    67  
    68  		originChange = &apiv0pb.GerritChange{
    69  			Host:     originGerrit.Host,
    70  			Change:   originGerrit.Info.Number,
    71  			Patchset: originCL.Snapshot.Patchset,
    72  		}
    73  	}
    74  
    75  	return &apiv0pb.GetCLRunInfoResponse_RunInfo{
    76  		Id:           r.ID.PublicID(),
    77  		CreateTime:   common.Time2PBNillable(r.CreateTime),
    78  		StartTime:    common.Time2PBNillable(r.StartTime),
    79  		OriginChange: originChange,
    80  		Mode:         string(r.Mode),
    81  	}, nil
    82  }