github.com/abayer/test-infra@v0.0.5/velodrome/fetcher/comments.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 "time" 22 23 "k8s.io/test-infra/velodrome/sql" 24 25 "github.com/golang/glog" 26 "github.com/google/go-github/github" 27 "github.com/jinzhu/gorm" 28 ) 29 30 func findLatestCommentUpdate(issueID int, db *gorm.DB, repository string) time.Time { 31 var comment sql.Comment 32 comment.CommentUpdatedAt = time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC) 33 34 db.Select("comment_updated_at"). 35 Where(&sql.Comment{IssueID: strconv.Itoa(issueID)}). 36 Where("repository = ?", repository). 37 Order("comment_updated_at desc"). 38 First(&comment) 39 40 return comment.CommentUpdatedAt 41 } 42 43 func updateIssueComments(issueID int, latest time.Time, db *gorm.DB, client ClientInterface) { 44 c := make(chan *github.IssueComment, 200) 45 46 go client.FetchIssueComments(issueID, latest, c) 47 48 for comment := range c { 49 commentOrm, err := NewIssueComment(issueID, comment, client.RepositoryName()) 50 if err != nil { 51 glog.Error("Failed to create IssueComment: ", err) 52 continue 53 } 54 if db.Create(commentOrm).Error != nil { 55 // If we can't create, let's try update 56 db.Save(commentOrm) 57 } 58 } 59 } 60 61 func updatePullComments(issueID int, latest time.Time, db *gorm.DB, client ClientInterface) { 62 c := make(chan *github.PullRequestComment, 200) 63 64 go client.FetchPullComments(issueID, latest, c) 65 66 for comment := range c { 67 commentOrm, err := NewPullComment(issueID, comment, client.RepositoryName()) 68 if err != nil { 69 glog.Error("Failed to create PullComment: ", err) 70 continue 71 } 72 if db.Create(commentOrm).Error != nil { 73 // If we can't create, let's try update 74 db.Save(commentOrm) 75 } 76 } 77 } 78 79 // UpdateComments downloads issue and pull-request comments and save in DB 80 func UpdateComments(issueID int, pullRequest bool, db *gorm.DB, client ClientInterface) { 81 latest := findLatestCommentUpdate(issueID, db, client.RepositoryName()) 82 83 updateIssueComments(issueID, latest, db, client) 84 if pullRequest { 85 updatePullComments(issueID, latest, db, client) 86 } 87 }