github.com/abayer/test-infra@v0.0.5/velodrome/fetcher/issue-events.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 main 18 19 import ( 20 "strconv" 21 22 "github.com/golang/glog" 23 "github.com/google/go-github/github" 24 "github.com/jinzhu/gorm" 25 "k8s.io/test-infra/velodrome/sql" 26 ) 27 28 func findLatestEvent(issueID int, db *gorm.DB, repository string) (*int, error) { 29 var latestEvent sql.IssueEvent 30 31 query := db. 32 Select("id, event_created_at"). 33 Where(&sql.IssueEvent{IssueID: strconv.Itoa(issueID)}). 34 Where("repository = ?", repository). 35 Order("event_created_at desc"). 36 First(&latestEvent) 37 if query.RecordNotFound() { 38 return nil, nil 39 } 40 if query.Error != nil { 41 return nil, query.Error 42 } 43 44 id, err := strconv.Atoi(latestEvent.ID) 45 if err != nil { 46 return nil, err 47 } 48 49 return &id, nil 50 } 51 52 // UpdateIssueEvents fetches all events until we find the most recent we 53 // have in db, and saves everything in database 54 func UpdateIssueEvents(issueID int, db *gorm.DB, client ClientInterface) { 55 latest, err := findLatestEvent(issueID, db, client.RepositoryName()) 56 if err != nil { 57 glog.Error("Failed to find last event: ", err) 58 return 59 } 60 c := make(chan *github.IssueEvent, 500) 61 62 go client.FetchIssueEvents(issueID, latest, c) 63 for event := range c { 64 eventOrm, err := NewIssueEvent(event, issueID, client.RepositoryName()) 65 if err != nil { 66 glog.Error("Failed to create issue-event", err) 67 } 68 db.Create(eventOrm) 69 } 70 }