go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/bugs/monorail/testdata.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 monorail
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	mpb "go.chromium.org/luci/analysis/internal/bugs/monorail/api_proto"
    23  )
    24  
    25  // IssueData is a representation of all data stored for an issue, used by
    26  // FakeIssuesClient.
    27  type IssueData struct {
    28  	Issue    *mpb.Issue
    29  	Comments []*mpb.Comment
    30  	// NotifyCount is the number of times a notification has been generated
    31  	// for the issue.
    32  	NotifyCount int
    33  	// The error that should be returned if an attempt is made to modify the
    34  	// issue, if any.
    35  	UpdateError error
    36  }
    37  
    38  // FakeIssuesSystem stores the state of bugs for a fake implementation of monorail.
    39  type FakeIssuesStore struct {
    40  	Issues []*IssueData
    41  	// Resource names of valid components.
    42  	// E.g. projects/chromium/componentDefs/Blink>Workers.
    43  	ComponentNames    []string
    44  	NextID            int
    45  	PriorityFieldName string
    46  
    47  	// The error to return on an attempt to update any issue.
    48  	// This is an alternative Issues[i].UpdateError for
    49  	// the case where the issue is not known.
    50  	UpdateError error
    51  }
    52  
    53  // NewIssueData creates new monorail issue data for testing.
    54  func NewIssueData(uniqifier int) *IssueData {
    55  	result := &IssueData{}
    56  	result.Issue = NewIssue(uniqifier)
    57  	result.Comments = []*mpb.Comment{
    58  		NewComment(result.Issue.Name, 1),
    59  	}
    60  	result.NotifyCount = 0
    61  	return result
    62  }
    63  
    64  // NewIssue returns a new monorail issue proto for testing.
    65  func NewIssue(uniqifier int) *mpb.Issue {
    66  	return &mpb.Issue{
    67  		Name:    fmt.Sprintf("projects/monorailproject/issues/%v", uniqifier),
    68  		Summary: fmt.Sprintf("This is the summary of bug %v.", uniqifier),
    69  		State:   mpb.IssueContentState_ACTIVE,
    70  		Status: &mpb.Issue_StatusValue{
    71  			Status: UntriagedStatus,
    72  		},
    73  		Reporter: "user@chromium.org",
    74  		FieldValues: []*mpb.FieldValue{
    75  			{
    76  				Field: ChromiumTestTypeField,
    77  				Value: "Bug",
    78  			},
    79  			{
    80  				Field: ChromiumTestPriorityField,
    81  				Value: "1",
    82  			},
    83  		},
    84  	}
    85  }
    86  
    87  // NewComment returns a new monorail comment proto for testing.
    88  func NewComment(issueName string, number int) *mpb.Comment {
    89  	return &mpb.Comment{
    90  		Name:      fmt.Sprintf("%s/comment/%v", issueName, number),
    91  		State:     mpb.IssueContentState_ACTIVE,
    92  		Type:      mpb.Comment_DESCRIPTION,
    93  		Content:   "Issue Description.",
    94  		Commenter: "user@chromium.org",
    95  	}
    96  }
    97  
    98  // CopyIssuesStore performs a deep copy of the given FakeIssuesStore.
    99  func CopyIssuesStore(s *FakeIssuesStore) *FakeIssuesStore {
   100  	var issues []*IssueData
   101  	for _, iss := range s.Issues {
   102  		issues = append(issues, CopyIssueData(iss))
   103  	}
   104  	return &FakeIssuesStore{
   105  		Issues:            issues,
   106  		ComponentNames:    append([]string{}, s.ComponentNames...),
   107  		NextID:            s.NextID,
   108  		PriorityFieldName: s.PriorityFieldName,
   109  	}
   110  }
   111  
   112  // CopyIssuesStore performs a deep copy of the given IssueData.
   113  func CopyIssueData(d *IssueData) *IssueData {
   114  	return &IssueData{
   115  		Issue:       CopyIssue(d.Issue),
   116  		Comments:    CopyComments(d.Comments),
   117  		NotifyCount: d.NotifyCount,
   118  	}
   119  }
   120  
   121  // CopyIssue performs a deep copy of the given Issue.
   122  func CopyIssue(issue *mpb.Issue) *mpb.Issue {
   123  	result := &mpb.Issue{}
   124  	proto.Merge(result, issue)
   125  	return result
   126  }
   127  
   128  // CopyComments performs a deep copy of the given Comment.
   129  func CopyComments(comments []*mpb.Comment) []*mpb.Comment {
   130  	var result []*mpb.Comment
   131  	for _, c := range comments {
   132  		copy := &mpb.Comment{}
   133  		proto.Merge(copy, c)
   134  		result = append(result, copy)
   135  	}
   136  	return result
   137  }