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

     1  // Copyright 2022 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 gerrit
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"google.golang.org/grpc"
    22  	"google.golang.org/grpc/codes"
    23  	"google.golang.org/grpc/status"
    24  	"google.golang.org/protobuf/proto"
    25  	emptypb "google.golang.org/protobuf/types/known/emptypb"
    26  
    27  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    28  )
    29  
    30  // fakeClientFactory produces FakeClients.
    31  type fakeClientFactory struct {
    32  	clsByHost map[string][]*gerritpb.ChangeInfo
    33  }
    34  
    35  // FakeClient is a fake implementation of gerritpb.GerritClient for testing.
    36  type FakeClient struct {
    37  	Changelists []*gerritpb.ChangeInfo
    38  }
    39  
    40  // UseFakeClient installs a fake gerrit client into the context,
    41  // with the given gerrit change data.
    42  func UseFakeClient(ctx context.Context, clsByHost map[string][]*gerritpb.ChangeInfo) context.Context {
    43  	clientFactory := &fakeClientFactory{clsByHost: clsByHost}
    44  	return context.WithValue(ctx, &testingGerritClientKey, clientFactory)
    45  }
    46  
    47  func (f *fakeClientFactory) WithHost(host string) gerritpb.GerritClient {
    48  	cls, ok := f.clsByHost[host]
    49  	if !ok {
    50  		panic(fmt.Sprintf("Caller asked for invalid gerrit host %s", host))
    51  	}
    52  	return &FakeClient{Changelists: cls}
    53  }
    54  
    55  func (f *FakeClient) GetChange(ctx context.Context, in *gerritpb.GetChangeRequest, opts ...grpc.CallOption) (*gerritpb.ChangeInfo, error) {
    56  	var result *gerritpb.ChangeInfo
    57  	for _, cl := range f.Changelists {
    58  		if cl.Number != in.Number {
    59  			continue
    60  		}
    61  		if in.Project != "" && cl.Project != in.Project {
    62  			return nil, status.Error(codes.InvalidArgument, "mismatch between CL project in gerrit and supplied project")
    63  		}
    64  		result = cl
    65  	}
    66  	if result == nil {
    67  		return nil, status.Error(codes.NotFound, "CL not found")
    68  	}
    69  	// Copy the result to avoid the caller getting an alias to our internal
    70  	// copy.
    71  	result = proto.Clone(result).(*gerritpb.ChangeInfo)
    72  
    73  	detailedAccounts := false
    74  	for _, opt := range in.Options {
    75  		if opt == gerritpb.QueryOption_DETAILED_ACCOUNTS {
    76  			detailedAccounts = true
    77  		}
    78  	}
    79  	if result.Owner != nil && !detailedAccounts {
    80  		result.Owner.Email = ""
    81  		result.Owner.Name = ""
    82  		result.Owner.SecondaryEmails = nil
    83  		result.Owner.Username = ""
    84  	}
    85  	return result, nil
    86  }
    87  
    88  func (f *FakeClient) ListProjects(ctx context.Context, in *gerritpb.ListProjectsRequest, opts ...grpc.CallOption) (*gerritpb.ListProjectsResponse, error) {
    89  	panic("not implemented")
    90  }
    91  
    92  func (f *FakeClient) GetRefInfo(ctx context.Context, in *gerritpb.RefInfoRequest, opts ...grpc.CallOption) (*gerritpb.RefInfo, error) {
    93  	panic("not implemented")
    94  }
    95  
    96  func (f *FakeClient) ListFileOwners(ctx context.Context, in *gerritpb.ListFileOwnersRequest, opts ...grpc.CallOption) (*gerritpb.ListOwnersResponse, error) {
    97  	panic("not implemented")
    98  }
    99  
   100  func (f *FakeClient) ListChanges(ctx context.Context, in *gerritpb.ListChangesRequest, opts ...grpc.CallOption) (*gerritpb.ListChangesResponse, error) {
   101  	panic("not implemented")
   102  }
   103  
   104  func (f *FakeClient) GetMergeable(ctx context.Context, in *gerritpb.GetMergeableRequest, opts ...grpc.CallOption) (*gerritpb.MergeableInfo, error) {
   105  	panic("not implemented")
   106  }
   107  
   108  func (f *FakeClient) ListFiles(ctx context.Context, in *gerritpb.ListFilesRequest, opts ...grpc.CallOption) (*gerritpb.ListFilesResponse, error) {
   109  	panic("not implemented")
   110  }
   111  
   112  func (f *FakeClient) GetRelatedChanges(ctx context.Context, in *gerritpb.GetRelatedChangesRequest, opts ...grpc.CallOption) (*gerritpb.GetRelatedChangesResponse, error) {
   113  	panic("not implemented")
   114  }
   115  
   116  func (f *FakeClient) GetPureRevert(ctx context.Context, in *gerritpb.GetPureRevertRequest, opts ...grpc.CallOption) (*gerritpb.PureRevertInfo, error) {
   117  	panic("not implemented")
   118  }
   119  
   120  func (f *FakeClient) GetMetaDiff(ctx context.Context, in *gerritpb.GetMetaDiffRequest, opts ...grpc.CallOption) (*gerritpb.MetaDiff, error) {
   121  	panic("not implemented")
   122  }
   123  
   124  func (f *FakeClient) CreateChange(ctx context.Context, in *gerritpb.CreateChangeRequest, opts ...grpc.CallOption) (*gerritpb.ChangeInfo, error) {
   125  	panic("not implemented")
   126  }
   127  
   128  func (f *FakeClient) ChangeEditFileContent(ctx context.Context, in *gerritpb.ChangeEditFileContentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
   129  	panic("not implemented")
   130  }
   131  
   132  func (f *FakeClient) DeleteEditFileContent(ctx context.Context, in *gerritpb.DeleteEditFileContentRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
   133  	panic("not implemented")
   134  }
   135  
   136  func (f *FakeClient) ChangeEditPublish(ctx context.Context, in *gerritpb.ChangeEditPublishRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
   137  	panic("not implemented")
   138  }
   139  
   140  func (f *FakeClient) AddReviewer(ctx context.Context, in *gerritpb.AddReviewerRequest, opts ...grpc.CallOption) (*gerritpb.AddReviewerResult, error) {
   141  	panic("not implemented")
   142  }
   143  
   144  func (f *FakeClient) DeleteReviewer(ctx context.Context, in *gerritpb.DeleteReviewerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
   145  	panic("not implemented")
   146  }
   147  
   148  func (f *FakeClient) SetReview(ctx context.Context, in *gerritpb.SetReviewRequest, opts ...grpc.CallOption) (*gerritpb.ReviewResult, error) {
   149  	panic("not implemented")
   150  }
   151  
   152  func (f *FakeClient) AddToAttentionSet(ctx context.Context, in *gerritpb.AttentionSetRequest, opts ...grpc.CallOption) (*gerritpb.AccountInfo, error) {
   153  	panic("not implemented")
   154  }
   155  
   156  func (f *FakeClient) SubmitChange(ctx context.Context, in *gerritpb.SubmitChangeRequest, opts ...grpc.CallOption) (*gerritpb.ChangeInfo, error) {
   157  	panic("not implemented")
   158  }
   159  
   160  func (f *FakeClient) RevertChange(ctx context.Context, in *gerritpb.RevertChangeRequest, opts ...grpc.CallOption) (*gerritpb.ChangeInfo, error) {
   161  	panic("not implemented")
   162  }
   163  
   164  func (f *FakeClient) AbandonChange(ctx context.Context, in *gerritpb.AbandonChangeRequest, opts ...grpc.CallOption) (*gerritpb.ChangeInfo, error) {
   165  	panic("not implemented")
   166  }
   167  
   168  func (f *FakeClient) SubmitRevision(ctx context.Context, in *gerritpb.SubmitRevisionRequest, opts ...grpc.CallOption) (*gerritpb.SubmitInfo, error) {
   169  	panic("not implemented")
   170  }