github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/github/status_change.go (about) 1 /* 2 Copyright 2016 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 github 18 19 import ( 20 "sync" 21 22 "k8s.io/kubernetes/pkg/util/sets" 23 ) 24 25 // StatusChange keeps track of issue/commit for status changes 26 type StatusChange struct { 27 heads map[int]string // Pull-Request ID -> head-sha 28 pullRequests map[string]sets.Int // head-sha -> Pull-Request IDs 29 changed sets.String // SHA of commits whose status changed 30 mutex sync.Mutex 31 } 32 33 // NewStatusChange creates a new status change tracker 34 func NewStatusChange() *StatusChange { 35 return &StatusChange{ 36 heads: map[int]string{}, 37 pullRequests: map[string]sets.Int{}, 38 changed: sets.NewString(), 39 } 40 } 41 42 // UpdatePullRequestHead updates the head commit for a pull-request 43 func (s *StatusChange) UpdatePullRequestHead(pullRequestID int, newHead string) { 44 s.mutex.Lock() 45 defer s.mutex.Unlock() 46 47 if oldHead, has := s.heads[pullRequestID]; has { 48 delete(s.pullRequests, oldHead) 49 } 50 s.heads[pullRequestID] = newHead 51 if _, has := s.pullRequests[newHead]; !has { 52 s.pullRequests[newHead] = sets.NewInt() 53 } 54 s.pullRequests[newHead].Insert(pullRequestID) 55 } 56 57 // CommitStatusChanged must be called when the status for this commit has changed 58 func (s *StatusChange) CommitStatusChanged(commit string) { 59 s.mutex.Lock() 60 defer s.mutex.Unlock() 61 62 s.changed.Insert(commit) 63 } 64 65 // PopChangedPullRequests returns the list of issues changed since last call 66 func (s *StatusChange) PopChangedPullRequests() []int { 67 s.mutex.Lock() 68 defer s.mutex.Unlock() 69 70 changedPullRequests := sets.NewInt() 71 for _, commit := range s.changed.List() { 72 if pullRequests, has := s.pullRequests[commit]; has { 73 changedPullRequests = changedPullRequests.Union(pullRequests) 74 } 75 } 76 s.changed = sets.NewString() 77 78 return changedPullRequests.List() 79 }