-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathinstallations.go
More file actions
214 lines (173 loc) · 5.05 KB
/
installations.go
File metadata and controls
214 lines (173 loc) · 5.05 KB
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
package github
import (
"context"
"net/http"
"strconv"
"time"
"github.com/src-d/lookout"
"github.com/src-d/lookout/util/cache"
"github.com/src-d/lookout/util/ctxlog"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/github"
"github.com/gregjones/httpcache"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
log "gopkg.in/src-d/go-log.v1"
"gopkg.in/src-d/lookout-sdk.v0/pb"
)
// Installations keeps github installations and allows to sync them
type Installations struct {
appID int
privateKey string
appClient *github.Client
watchMinInterval string
cache *cache.ValidableCache
clientTimeout time.Duration
// [installationID]installationClient
clients map[int64]*Client
Pool *ClientPool
}
// NewInstallations creates a new Installations using the App ID and private key
func NewInstallations(
appID int, privateKey string,
cache *cache.ValidableCache,
watchMinInterval string,
clientTimeout time.Duration,
) (*Installations, error) {
// Use App authorization to list installations
appTr, err := ghinstallation.NewAppsTransportKeyFromFile(
http.DefaultTransport, appID, privateKey)
if err != nil {
return nil, err
}
appClient := github.NewClient(&http.Client{Transport: appTr})
app, _, err := appClient.Apps.Get(context.TODO(), "")
if err != nil {
return nil, err
}
log.Infof("authorized as GitHub application %q, ID %v", app.GetName(), app.GetID())
i := &Installations{
appID: appID,
privateKey: privateKey,
appClient: appClient,
watchMinInterval: watchMinInterval,
cache: cache,
clientTimeout: clientTimeout,
clients: make(map[int64]*Client),
Pool: NewClientPool(),
}
return i, nil
}
// Sync update state from github
func (t *Installations) Sync() error {
log.Debugf("syncing installations with github")
var installations []*github.Installation
opts := &github.ListOptions{
PerPage: 100,
}
for {
installs, resp, err := t.appClient.Apps.ListInstallations(context.TODO(), opts)
if err != nil {
return err
}
installations = append(installations, installs...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
log.Debugf("found %d installations", len(installations))
new := make(map[int64]*github.Installation, len(installations))
for _, installation := range installations {
new[installation.GetID()] = installation
}
// remove revoked installations
for id := range t.clients {
if _, ok := new[id]; !ok {
log.Debugf("remove installation %d, %s", id, new[id].GetAccount().GetLogin())
t.removeInstallation(id)
}
}
// add new installations
for id := range new {
if _, ok := t.clients[id]; !ok {
log.Debugf("add installation %d, %s", id, new[id].GetAccount().GetLogin())
t.addInstallation(id)
}
}
// sync repos for all available installations
for id, c := range t.clients {
repos, err := t.getRepos(c)
if err != nil {
return err
}
log.Debugf("%d repositories found for installation %d, %s",
len(repos), id, new[id].GetAccount().GetLogin())
ghRepos := make([]*repositoryInfo, len(repos))
for i, repo := range repos {
orgIDStr := strconv.FormatInt(new[id].GetAccount().GetID(), 10)
ghRepos[i] = &repositoryInfo{RepositoryInfo: *repo, OrganizationID: orgIDStr}
}
t.Pool.Update(c, ghRepos)
}
return nil
}
func (t *Installations) addInstallation(id int64) error {
c, err := t.createClient(id)
if err != nil {
return err
}
t.clients[id] = c
return nil
}
func (t *Installations) removeInstallation(id int64) {
t.Pool.RemoveClient(t.clients[id])
delete(t.clients, id)
}
func (t *Installations) createClient(installationID int64) (*Client, error) {
cachedT := httpcache.NewTransport(t.cache)
cachedT.MarkCachedResponses = true
itr, err := ghinstallation.NewKeyFromFile(cachedT,
t.appID, int(installationID), t.privateKey)
if err != nil {
return nil, err
}
// Auth must be: https://x-access-token:<token>@github.com/owner/repo.git
// Reference: https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation
gitAuth := func(ctx context.Context) transport.AuthMethod {
token, err := itr.Token()
if err != nil {
ctxlog.Get(ctx).Errorf(err, "failed to get an installation access token")
return nil
}
return &githttp.BasicAuth{
Username: "x-access-token",
Password: token,
}
}
return NewClient(itr, t.cache, t.watchMinInterval, gitAuth, t.clientTimeout), nil
}
func (t *Installations) getRepos(iClient *Client) ([]*lookout.RepositoryInfo, error) {
var repos []*lookout.RepositoryInfo
opts := &github.ListOptions{
PerPage: 100,
}
for {
ghRepos, resp, err := iClient.Apps.ListRepos(context.TODO(), opts)
if err != nil {
return nil, err
}
for _, ghRepo := range ghRepos {
repo, err := pb.ParseRepositoryInfo(*ghRepo.HTMLURL)
if err != nil {
return nil, err
}
repos = append(repos, repo)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return repos, nil
}