-
Notifications
You must be signed in to change notification settings - Fork 1
/
owner_repositories_syncer.go
111 lines (89 loc) · 1.99 KB
/
owner_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
package accountsync
import (
"fmt"
"strings"
"github.com/google/go-github/github"
)
type OwnerRepositoriesSyncer struct {
db *DB
cfg *Config
}
type ownerRepoSyncContext struct {
user *User
client *github.Client
}
type errOrgSync struct {
errMap *map[string][]error
}
func (eos *errOrgSync) Error() string {
if eos.errMap == nil {
return ""
}
s := []string{}
for org, errors := range *eos.errMap {
for _, err := range errors {
s = append(s, fmt.Sprintf("%v:%v", org, err))
}
}
return strings.Join(s, "; ")
}
func NewOwnerRepositoriesSyncer(db *DB, cfg *Config) *OwnerRepositoriesSyncer {
return &OwnerRepositoriesSyncer{db: db, cfg: cfg}
}
func (ors *OwnerRepositoriesSyncer) Sync(user *User, client *github.Client) error {
ctx := &ownerRepoSyncContext{
user: user,
client: client,
}
err := user.HydrateOrganizations(ors.db)
if err != nil {
return err
}
owners := []*Owner{
&Owner{
Type: "user",
User: user,
},
}
for _, org := range user.Organizations {
owners = append(owners,
&Owner{
Type: "organization",
Organization: org,
})
}
hadRepoSyncErr := false
githubRepoIDs := []*int{}
orgSyncErrors := map[string][]error{}
for _, owner := range owners {
addErr := func(err error) {
hadRepoSyncErr = true
key := owner.Key()
if _, ok := orgSyncErrors[key]; !ok {
orgSyncErrors[key] = []error{}
}
orgSyncErrors[key] = append(orgSyncErrors[key], err)
}
rs := NewRepositoriesSyncer(ors.db, ors.cfg)
repoIDs, err := rs.Sync(owner, user, client)
if err != nil {
addErr(err)
continue
}
if repoIDs != nil {
githubRepoIDs = append(githubRepoIDs, repoIDs...)
}
}
err = ors.cleanupRepos(githubRepoIDs, ctx)
if err != nil {
return err
}
if hadRepoSyncErr {
return &errOrgSync{errMap: &orgSyncErrors}
}
return nil
}
func (ors *OwnerRepositoriesSyncer) cleanupRepos(githubRepoIDs []*int, ctx *ownerRepoSyncContext) error {
// TODO: find old repos and revoke all permissions for the user
return nil
}