-
Notifications
You must be signed in to change notification settings - Fork 1
/
syncer.go
142 lines (117 loc) · 3.45 KB
/
syncer.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package accountsync
import (
"fmt"
"log"
"strings"
"time"
"github.com/google/go-github/github"
"github.com/travis-ci/encrypted-column"
"golang.org/x/oauth2"
_ "github.com/lib/pq"
)
type tokenSource struct {
token *oauth2.Token
}
func (ts *tokenSource) Token() (*oauth2.Token, error) {
return ts.token, nil
}
type Syncer struct {
db *DB
cfg *Config
}
func NewSyncer(cfg *Config) (*Syncer, error) {
syncer := &Syncer{cfg: cfg}
log.Println("msg=\"creating database connection\"")
db, err := NewDB(syncer.cfg.DatabaseURL, syncer.cfg.SyncCacheSize)
if err != nil {
return nil, err
}
syncer.db = db
return syncer, nil
}
func (syncer *Syncer) Sync() {
log.SetFlags(log.LstdFlags)
if syncer.cfg.EncryptionKey == "" {
log.Fatal("msg=\"missing encryption key\"")
}
userInfoSyncer := NewUserInfoSyncer(syncer.db, syncer.cfg)
orgSyncer := NewOrganizationSyncer(syncer.db, syncer.cfg)
ownerReposSyncer := NewOwnerRepositoriesSyncer(syncer.db, syncer.cfg)
ghTokCol, err := encryptedcolumn.NewEncryptedColumn(syncer.cfg.EncryptionKey, true)
if err != nil {
log.Fatal(err)
}
errMap := map[string][]error{}
for _, githubUsername := range syncer.cfg.GithubUsernames {
if strings.TrimSpace(githubUsername) == "" {
continue
}
user := &User{}
errMap[githubUsername] = []error{}
addErr := func(err error) {
errMap[githubUsername] = append(errMap[githubUsername], err)
}
log.Printf("msg=\"fetching user\" login=%v", githubUsername)
err = syncer.db.Get(user, "SELECT * FROM users WHERE login = $1", githubUsername)
if err != nil {
addErr(err)
continue
}
err = user.Hydrate()
if err != nil {
addErr(err)
continue
}
token, err := ghTokCol.Load(user.GithubOauthToken.String)
if err != nil {
addErr(err)
continue
}
ts := &tokenSource{
token: &oauth2.Token{
AccessToken: token,
},
}
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)
client.UserAgent = fmt.Sprintf("Travis CI Account Sync/%s", VersionString)
fullStarted := time.Now().UTC()
log.Printf("state=started sync=user login=%v", githubUsername)
started := time.Now().UTC()
log.Printf("state=started sync=user_info login=%v", githubUsername)
err = userInfoSyncer.Sync(user, client)
if err != nil {
addErr(err)
log.Printf("state=errored sync=user_info err=%v login=%v", err, githubUsername)
continue
}
log.Printf("state=completed sync=user_info login=%v duration=%v",
githubUsername, time.Now().UTC().Sub(started))
started = time.Now().UTC()
log.Printf("state=started sync=organizations login=%v", githubUsername)
err = orgSyncer.Sync(user, client)
if err != nil {
addErr(err)
log.Printf("state=errored sync=organizations err=%v login=%v", err, githubUsername)
continue
}
log.Printf("state=completed sync=organizations login=%v duration=%v",
githubUsername, time.Now().UTC().Sub(started))
started = time.Now().UTC()
log.Printf("state=started sync=repositories login=%v", githubUsername)
err = ownerReposSyncer.Sync(user, client)
if err != nil {
addErr(err)
continue
}
log.Printf("state=completed sync=repositories login=%v duration=%v",
githubUsername, time.Now().UTC().Sub(started))
log.Printf("state=completed sync=user login=%v duration=%v",
githubUsername, time.Now().UTC().Sub(fullStarted))
}
for githubUsername, errors := range errMap {
for _, err := range errors {
log.Printf("level=error login=%s err=%q", githubUsername, err.Error())
}
}
}