forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue-events.go
70 lines (58 loc) · 1.84 KB
/
issue-events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strconv"
"github.com/golang/glog"
"github.com/google/go-github/github"
"github.com/jinzhu/gorm"
"k8s.io/test-infra/velodrome/sql"
)
func findLatestEvent(issueID int, db *gorm.DB, repository string) (*int, error) {
var latestEvent sql.IssueEvent
query := db.
Select("id, event_created_at").
Where(&sql.IssueEvent{IssueID: strconv.Itoa(issueID)}).
Where("repository = ?", repository).
Order("event_created_at desc").
First(&latestEvent)
if query.RecordNotFound() {
return nil, nil
}
if query.Error != nil {
return nil, query.Error
}
id, err := strconv.Atoi(latestEvent.ID)
if err != nil {
return nil, err
}
return &id, nil
}
// UpdateIssueEvents fetches all events until we find the most recent we
// have in db, and saves everything in database
func UpdateIssueEvents(issueID int, db *gorm.DB, client ClientInterface) {
latest, err := findLatestEvent(issueID, db, client.RepositoryName())
if err != nil {
glog.Error("Failed to find last event: ", err)
return
}
c := make(chan *github.IssueEvent, 500)
go client.FetchIssueEvents(issueID, latest, c)
for event := range c {
eventOrm, err := NewIssueEvent(event, issueID, client.RepositoryName())
if err != nil {
glog.Error("Failed to create issue-event", err)
}
db.Create(eventOrm)
}
}