go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/gerritfake/utils.go (about) 1 // Copyright 2021 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 gerritfake 16 17 import ( 18 "github.com/smarty/assertions" 19 20 gerritpb "go.chromium.org/luci/common/proto/gerrit" 21 ) 22 23 // ResetVotes resets all non-0 votes of the given labels to 0. 24 func ResetVotes(ci *gerritpb.ChangeInfo, labels ...string) { 25 for _, label := range labels { 26 info := ci.Labels[label] 27 if info == nil { 28 continue 29 } 30 for _, vote := range info.All { 31 if vote.Value != 0 { 32 vote.Value = 0 33 vote.Date = nil 34 } 35 } 36 } 37 } 38 39 // NonZeroVotes returns all non-zero votes for the provided label. 40 // 41 // Return nil slice if label doesn't exist. 42 func NonZeroVotes(ci *gerritpb.ChangeInfo, label string) []*gerritpb.ApprovalInfo { 43 l, ok := ci.GetLabels()[label] 44 if !ok { 45 return nil 46 } 47 ret := make([]*gerritpb.ApprovalInfo, 0, len(l.GetAll())) 48 for _, ai := range l.GetAll() { 49 if ai.GetValue() != 0 { 50 ret = append(ret, ai) 51 } 52 } 53 return ret 54 } 55 56 // LastMessage returns the last message from the Gerrit Change. 57 // 58 // Return nil if there are no messages. 59 func LastMessage(ci *gerritpb.ChangeInfo) *gerritpb.ChangeMessageInfo { 60 ms := ci.GetMessages() 61 if l := len(ms); l > 0 { 62 return ms[l-1] 63 } 64 return nil 65 } 66 67 // ShouldLastMessageContain asserts the last posted message on a ChangeInfo 68 // contains the expected substring. 69 func ShouldLastMessageContain(actual any, oneSubstring ...any) string { 70 if len(oneSubstring) != 1 { 71 panic("exactly 1 substring required") 72 } 73 if diff := assertions.ShouldHaveSameTypeAs(oneSubstring[0], string("")); diff != "" { 74 return diff 75 } 76 if diff := assertions.ShouldHaveSameTypeAs(actual, (*gerritpb.ChangeInfo)(nil)); diff != "" { 77 return diff 78 } 79 ci := actual.(*gerritpb.ChangeInfo) 80 expected := oneSubstring[0].(string) 81 82 last := LastMessage(ci) 83 if last == nil { 84 return "ChangeInfo has no messages" 85 } 86 return assertions.ShouldContainSubstring(last.GetMessage(), expected) 87 }