github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/fake_open_wrapper.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package plugins 18 19 import ( 20 "container/heap" 21 22 "k8s.io/test-infra/velodrome/sql" 23 ) 24 25 // EventTimeHeap is a min-heap on Event creation time 26 type EventTimeHeap []sql.IssueEvent 27 28 var _ heap.Interface = &EventTimeHeap{} 29 30 func (t EventTimeHeap) Len() int { return len(t) } 31 func (t EventTimeHeap) Swap(i, j int) { t[i], t[j] = t[j], t[i] } 32 func (t EventTimeHeap) Less(i, j int) bool { return t[i].EventCreatedAt.Before(t[j].EventCreatedAt) } 33 34 // Push adds event to the heap 35 func (t *EventTimeHeap) Push(x interface{}) { 36 *t = append(*t, x.(sql.IssueEvent)) 37 } 38 39 // Pop retrieves the last added event 40 func (t *EventTimeHeap) Pop() interface{} { 41 old := *t 42 n := len(old) 43 x := old[n-1] 44 *t = old[0 : n-1] 45 return x 46 } 47 48 // FakeOpenPluginWrapper sends new "opened" event to ReceiveEvent 49 type FakeOpenPluginWrapper struct { 50 // Min-heap of "opened" events to inject 51 openEvents EventTimeHeap 52 alreadyOpen map[string]bool 53 // Actual plugin 54 plugin Plugin 55 } 56 57 var _ Plugin = &FakeOpenPluginWrapper{} 58 59 // NewFakeOpenPluginWrapper is the constructor for FakeOpenPluginWrapper 60 func NewFakeOpenPluginWrapper(plugin Plugin) *FakeOpenPluginWrapper { 61 return &FakeOpenPluginWrapper{ 62 plugin: plugin, 63 alreadyOpen: map[string]bool{}, 64 } 65 } 66 67 // ReceiveIssue creates a fake "opened" event 68 func (o *FakeOpenPluginWrapper) ReceiveIssue(issue sql.Issue) []Point { 69 if _, ok := o.alreadyOpen[issue.ID]; !ok { 70 // Create/Add fake "opened" events 71 heap.Push(&o.openEvents, sql.IssueEvent{ 72 Event: "opened", 73 IssueID: issue.ID, 74 Actor: &issue.User, 75 EventCreatedAt: issue.IssueCreatedAt, 76 }) 77 o.alreadyOpen[issue.ID] = true 78 } 79 80 return o.plugin.ReceiveIssue(issue) 81 } 82 83 // ReceiveIssueEvent injects an extra "opened" event before calling plugin.ReceiveIssueEvent() 84 func (o *FakeOpenPluginWrapper) ReceiveIssueEvent(event sql.IssueEvent) []Point { 85 points := []Point{} 86 87 // Inject extra "opened" events. Inject "opened" before other events at the same time. 88 for o.openEvents.Len() > 0 && !o.openEvents[0].EventCreatedAt.After(event.EventCreatedAt) { 89 points = append(points, o.plugin.ReceiveIssueEvent(heap.Pop(&o.openEvents).(sql.IssueEvent))...) 90 } 91 92 return append(points, o.plugin.ReceiveIssueEvent(event)...) 93 } 94 95 // ReceiveComment is a wrapper on plugin.ReceiveComment() 96 func (o *FakeOpenPluginWrapper) ReceiveComment(comment sql.Comment) []Point { 97 return o.plugin.ReceiveComment(comment) 98 }