github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  func (t *EventTimeHeap) Push(x interface{}) {
    35  	*t = append(*t, x.(sql.IssueEvent))
    36  }
    37  
    38  func (t *EventTimeHeap) Pop() interface{} {
    39  	old := *t
    40  	n := len(old)
    41  	x := old[n-1]
    42  	*t = old[0 : n-1]
    43  	return x
    44  }
    45  
    46  // FakeOpenPluginWrapper sends new "opened" event to ReceiveEvent
    47  type FakeOpenPluginWrapper struct {
    48  	// Min-heap of "opened" events to inject
    49  	openEvents  EventTimeHeap
    50  	alreadyOpen map[string]bool
    51  	// Actual plugin
    52  	plugin Plugin
    53  }
    54  
    55  var _ Plugin = &FakeOpenPluginWrapper{}
    56  
    57  func NewFakeOpenPluginWrapper(plugin Plugin) *FakeOpenPluginWrapper {
    58  	return &FakeOpenPluginWrapper{
    59  		plugin:      plugin,
    60  		alreadyOpen: map[string]bool{},
    61  	}
    62  }
    63  
    64  func (o *FakeOpenPluginWrapper) ReceiveIssue(issue sql.Issue) []Point {
    65  	if _, ok := o.alreadyOpen[issue.ID]; !ok {
    66  		// Create/Add fake "opened" events
    67  		heap.Push(&o.openEvents, sql.IssueEvent{
    68  			Event:          "opened",
    69  			IssueId:        issue.ID,
    70  			Actor:          &issue.User,
    71  			EventCreatedAt: issue.IssueCreatedAt,
    72  		})
    73  		o.alreadyOpen[issue.ID] = true
    74  	}
    75  
    76  	return o.plugin.ReceiveIssue(issue)
    77  }
    78  
    79  func (o *FakeOpenPluginWrapper) ReceiveIssueEvent(event sql.IssueEvent) []Point {
    80  	points := []Point{}
    81  
    82  	// Inject extra "opened" events. Inject "opened" before other events at the same time.
    83  	for o.openEvents.Len() > 0 && !o.openEvents[0].EventCreatedAt.After(event.EventCreatedAt) {
    84  		points = append(points, o.plugin.ReceiveIssueEvent(heap.Pop(&o.openEvents).(sql.IssueEvent))...)
    85  	}
    86  
    87  	return append(points, o.plugin.ReceiveIssueEvent(event)...)
    88  }
    89  
    90  func (o *FakeOpenPluginWrapper) ReceiveComment(comment sql.Comment) []Point {
    91  	return o.plugin.ReceiveComment(comment)
    92  }