-
Notifications
You must be signed in to change notification settings - Fork 1
/
repositories_syncer.go
375 lines (323 loc) · 8.95 KB
/
repositories_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package accountsync
import (
"database/sql"
"fmt"
"log"
"time"
"github.com/google/go-github/github"
)
type repoSyncContext struct {
owner *Owner
user *User
client *github.Client
}
type RepositoriesSyncer struct {
db *DB
cfg *Config
}
func NewRepositoriesSyncer(db *DB, cfg *Config) *RepositoriesSyncer {
return &RepositoriesSyncer{
db: db,
cfg: cfg,
}
}
func (rs *RepositoriesSyncer) Sync(owner *Owner, user *User, client *github.Client) ([]*int, error) {
ctx := &repoSyncContext{
owner: owner,
user: user,
client: client,
}
githubRepoIDs := []*int{}
for _, syncType := range rs.cfg.SyncTypes {
syncTypeGithubIDs, err := rs.syncReposOfType(syncType, ctx)
if err != nil {
return githubRepoIDs, err
}
githubRepoIDs = append(githubRepoIDs, syncTypeGithubIDs...)
}
return githubRepoIDs, nil
}
func (rs *RepositoriesSyncer) syncReposOfType(syncType string, ctx *repoSyncContext) ([]*int, error) {
curPage := rs.cfg.RepositoriesStartPage
for {
opts := &github.RepositoryListOptions{
Type: syncType,
ListOptions: github.ListOptions{
PerPage: 100,
Page: curPage,
},
}
log.Printf("sync=repositories page=%v owner=%v login=%v",
curPage, ctx.owner, ctx.user.Login.String)
var (
repos []github.Repository
response *github.Response
err error
)
switch ctx.owner.Type {
case "user":
repos, response, err = rs.getUserRepositories(opts, ctx)
case "organization":
repos, response, err = rs.getOrganizationRepositories(opts, ctx)
default:
panic(fmt.Errorf("invalid owner type %q", ctx.owner.Type))
}
if err != nil {
log.Printf("level=error sync=repositories page=%v owner=%v login=%v err=%v",
curPage, ctx.owner, ctx.user.Login.String, err)
continue
}
for _, repo := range repos {
err = rs.syncRepo(&repo, ctx)
if err != nil {
log.Printf("level=error sync=repository repo_id=%v login=%v repo=%v err=%v",
*repo.ID, ctx.user.Login.String, *repo.FullName, err)
}
}
if response.NextPage == 0 {
break
}
curPage += 1
}
return nil, nil
}
func (rs *RepositoriesSyncer) getUserRepositories(opts *github.RepositoryListOptions, ctx *repoSyncContext) ([]github.Repository, *github.Response, error) {
return ctx.client.Repositories.List("", opts)
}
func (rs *RepositoriesSyncer) getOrganizationRepositories(opts *github.RepositoryListOptions, ctx *repoSyncContext) ([]github.Repository, *github.Response, error) {
repos := []github.Repository{}
reqURL := fmt.Sprintf("/organizations/%v/repos?page=%v&per_page=%v&type=%s",
ctx.owner.Organization.GithubID, opts.ListOptions.Page, opts.ListOptions.PerPage, opts.Type)
req, err := ctx.client.NewRequest("GET", reqURL, nil)
if err != nil {
return repos, nil, err
}
response, err := ctx.client.Do(req, &repos)
if err != nil {
return repos, response, err
}
return repos, response, err
}
func (rs *RepositoriesSyncer) shouldSync(repo *github.Repository) bool {
t := "public"
if *repo.Private {
t = "private"
}
return sliceContains(rs.cfg.SyncTypes, t)
}
func (rs *RepositoriesSyncer) syncRepo(ghRepo *github.Repository, ctx *repoSyncContext) error {
log.Printf("sync=repository repo_id=%v login=%v repo=%v\n",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName)
if !rs.shouldSync(ghRepo) {
log.Printf("msg=\"skipping\" sync=repository repo_id=%v login=%v repo=%v\n",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName)
return nil
}
started := time.Now().UTC()
log.Printf("state=started sync=repository repo_id=%v login=%v repo=%v",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName)
owner, err := rs.findRepoOwner(ghRepo, ctx)
if err != nil {
return err
}
if owner == nil {
owner, err = rs.createRepoOwner(ghRepo, ctx)
}
repo, err := rs.findRepoByGithubID(*ghRepo.ID, ctx)
if err != nil {
return err
}
if repo == nil {
log.Printf("action=creating sync=repository repo_id=%v login=%v repo=%v",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName)
repo, err = rs.createRepo(ghRepo, ctx)
if err != nil {
return err
}
} else {
log.Printf("action=updating sync=repository repo_id=%v login=%v repo=%v",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName)
repo.UpdateFromGithubRepository(ghRepo)
repo, err = rs.updateRepo(repo, ctx)
if err != nil {
return err
}
}
// TODO: sync permissions if present
// TODO: permit if permittable
if err != nil {
return err
}
log.Printf("state=completed sync=repository repo_id=%v login=%v repo=%v duration=%v",
*ghRepo.ID, ctx.user.Login.String, *ghRepo.FullName, time.Now().UTC().Sub(started))
return nil
}
func (rs *RepositoriesSyncer) findRepoOwner(ghRepo *github.Repository, ctx *repoSyncContext) (*Owner, error) {
owner := &Owner{}
log.Printf("level=debug sync=repository msg=\"finding user\" github_id=%v", *ghRepo.Owner.ID)
user, err := rs.db.FindUserByGithubID(*ghRepo.Owner.ID)
if err != nil {
return nil, err
}
if user != nil {
owner.Type = "user"
owner.User = user
return owner, nil
}
log.Printf("level=debug sync=repository msg=\"finding org\" github_id=%v", *ghRepo.Owner.ID)
org, err := rs.db.FindOrgByGithubID(*ghRepo.Owner.ID)
if err != nil {
return nil, err
}
if org != nil {
owner.Type = "organization"
owner.Organization = org
return owner, nil
}
return nil, nil
}
func (rs *RepositoriesSyncer) findRepoByGithubID(ghRepoID int, ctx *repoSyncContext) (*Repository, error) {
repo := &Repository{}
err := rs.db.Get(repo, `SELECT * FROM repositories WHERE github_id = $1`, ghRepoID)
if err == sql.ErrNoRows {
repo = nil
err = nil
}
return repo, err
}
func (rs *RepositoriesSyncer) createRepo(ghRepo *github.Repository, ctx *repoSyncContext) (*Repository, error) {
now := time.Now().UTC()
repo := &Repository{
CreatedAt: &now,
UpdatedAt: &now,
}
repo.UpdateFromGithubRepository(ghRepo)
res, err := rs.db.NamedExec(`
INSERT INTO repositories (
created_at,
default_branch,
description,
github_id,
github_language,
name,
owner_id,
owner_name,
owner_type,
private,
url,
updated_at
) VALUES (
:created_at,
:default_branch,
:description,
:github_id,
:github_language,
:name,
:owner_id,
:owner_name,
:owner_type,
:private,
:url,
:updated_at
) RETURNING id
`, repo)
if err != nil {
return nil, err
}
id, err := res.LastInsertId()
if err != nil {
return nil, err
}
repo.ID = sql.NullInt64{Int64: int64(id), Valid: true}
return repo, nil
}
func (rs *RepositoriesSyncer) updateRepo(repo *Repository, ctx *repoSyncContext) (*Repository, error) {
now := time.Now().UTC()
repo.UpdatedAt = &now
_, err := rs.db.NamedExec(`
UPDATE repositories
SET
default_branch = :default_branch,
description = :description,
github_id = :github_id,
github_language = :github_language,
name = :name,
owner_id = :owner_id,
owner_name = :owner_name,
owner_type = :owner_type,
private = :private,
url = :url,
updated_at = :updated_at
WHERE id = :id
`, repo)
return repo, err
}
func (rs *RepositoriesSyncer) createRepoOwner(repo *github.Repository, ctx *repoSyncContext) (*Owner, error) {
switch *repo.Owner.Type {
case "User":
ghUser, err := rs.getGithubUserByID(*repo.Owner.ID, ctx)
if err != nil {
return nil, err
}
user, err := rs.createUserFromGithubUser(ghUser, ctx)
if err != nil {
return nil, err
}
owner := &Owner{
Type: "user",
User: user,
}
log.Printf("level=warn login=%v id=%v sync=repository slug=%v status=created_user reason=owner_not_found",
user.Login, user.ID, *repo.FullName)
return owner, nil
case "Organization":
ghOrg, err := rs.getGithubOrgByID(*repo.Owner.ID, ctx)
if err != nil {
return nil, err
}
org, err := rs.createOrgFromGithubOrg(ghOrg, ctx)
if err != nil {
return nil, err
}
owner := &Owner{
Type: "organization",
Organization: org,
}
log.Printf("level=warn login=%v id=%v sync=repository slug=%v status=created_org reason=owner_not_found",
org.Login, org.ID, *repo.FullName)
return owner, nil
}
return nil, nil
}
func (rs *RepositoriesSyncer) getGithubUserByID(userID int, ctx *repoSyncContext) (*github.User, error) {
reqURL := fmt.Sprintf("/user/%v", userID)
req, err := ctx.client.NewRequest("GET", reqURL, nil)
if err != nil {
return nil, err
}
user := &github.User{}
_, err = ctx.client.Do(req, user)
if err != nil {
return user, err
}
return user, err
}
func (rs *RepositoriesSyncer) getGithubOrgByID(orgID int, ctx *repoSyncContext) (*github.Organization, error) {
reqURL := fmt.Sprintf("/organizations/%v", orgID)
req, err := ctx.client.NewRequest("GET", reqURL, nil)
if err != nil {
return nil, err
}
org := &github.Organization{}
_, err = ctx.client.Do(req, org)
if err != nil {
return org, err
}
return org, err
}
func (rs *RepositoriesSyncer) createUserFromGithubUser(ghUser *github.User, ctx *repoSyncContext) (*User, error) {
return nil, nil
}
func (rs *RepositoriesSyncer) createOrgFromGithubOrg(ghOrg *github.Organization, ctx *repoSyncContext) (*Organization, error) {
return nil, nil
}