github.com/abayer/test-infra@v0.0.5/velodrome/fetcher/issues.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 "time" 21 22 "k8s.io/test-infra/velodrome/sql" 23 24 "github.com/golang/glog" 25 "github.com/google/go-github/github" 26 "github.com/jinzhu/gorm" 27 ) 28 29 func findLatestIssueUpdate(db *gorm.DB, repository string) (time.Time, error) { 30 var issue sql.Issue 31 issue.IssueUpdatedAt = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC) 32 33 query := db. 34 Select("issue_updated_at"). 35 Where("repository = ?", repository). 36 Order("issue_updated_at desc"). 37 First(&issue) 38 if !query.RecordNotFound() && query.Error != nil { 39 return time.Time{}, query.Error 40 } 41 42 return issue.IssueUpdatedAt, nil 43 } 44 45 // UpdateIssues downloads new issues and saves in database 46 func UpdateIssues(db *gorm.DB, client ClientInterface) { 47 latest, err := findLatestIssueUpdate(db, client.RepositoryName()) 48 if err != nil { 49 glog.Error("Failed to find last issue update: ", err) 50 return 51 } 52 c := make(chan *github.Issue, 200) 53 54 go client.FetchIssues(latest, c) 55 for issue := range c { 56 issueOrm, err := NewIssue(issue, client.RepositoryName()) 57 if err != nil { 58 glog.Error("Can't create issue:", err) 59 continue 60 } 61 if db.Create(issueOrm).Error != nil { 62 // We assume record already exists. Let's 63 // update. First we need to delete labels and 64 // assignees, as they are just concatenated 65 // otherwise. 66 db.Delete(sql.Label{}, 67 "issue_id = ? AND repository = ?", 68 issueOrm.ID, client.RepositoryName()) 69 db.Delete(sql.Assignee{}, 70 "issue_id = ? AND repository = ?", 71 issueOrm.ID, client.RepositoryName()) 72 73 if err := db.Save(issueOrm).Error; err != nil { 74 glog.Error("Failed to update database issue: ", err) 75 } 76 } 77 78 // Issue is updated, find if we have new comments 79 UpdateComments(*issue.Number, issueOrm.IsPR, db, client) 80 // and find if we have new events 81 UpdateIssueEvents(*issue.Number, db, client) 82 } 83 }