forked from bblfsh/sdk
/
discovery.go
308 lines (275 loc) · 7.58 KB
/
discovery.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
// Package discovery package implements helpers for clients to discover language drivers supported by Babelfish.
package discovery
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"strings"
"sync"
"github.com/google/go-github/github"
"gopkg.in/bblfsh/sdk.v2/driver/manifest"
)
const (
GithubOrg = "bblfsh"
)
// topics that each driver repository on Github should be annotated with
var topics = []string{
"babelfish", "driver",
}
// Driver is an object describing language driver and it's repository-related information.
type Driver struct {
manifest.Manifest
repo *github.Repository
}
type byStatusAndName []Driver
func (arr byStatusAndName) Len() int {
return len(arr)
}
func (arr byStatusAndName) Less(i, j int) bool {
a, b := arr[i], arr[j]
// sort by status, features count, name
if s1, s2 := a.Status.Rank(), b.Status.Rank(); s1 > s2 {
return true
} else if s1 < s2 {
return false
}
if n1, n2 := len(a.Features), len(b.Features); n1 > n2 {
return true
} else if n1 < n2 {
return false
}
return a.Language < b.Language
}
func (arr byStatusAndName) Swap(i, j int) {
arr[i], arr[j] = arr[j], arr[i]
}
// Maintainer is an information about project maintainer.
//
// Deprecated: use manifest.Maintainer
type Maintainer = manifest.Maintainer
// RepositoryURL returns Github repository URL for browsers (not git).
func (d Driver) RepositoryURL() string {
return d.repo.GetHTMLURL()
}
// repositoryFileURL returns an URL of file in the driver's repository.
func (d Driver) repositoryFileURL(path string) string {
path = strings.TrimPrefix(path, "/")
return fmt.Sprintf("https://raw.githubusercontent.com/%s/master/%s", d.repo.GetFullName(), path)
}
// newReq constructs a GET request with context.
func newReq(ctx context.Context, url string) *http.Request {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
// if it fails, it's a programmer's error
panic(err)
}
return req.WithContext(ctx)
}
// loadManifest reads manifest file from repository and decodes it into object.
func (d *Driver) loadManifest(ctx context.Context) error {
req := newReq(ctx, d.repositoryFileURL(manifest.Filename))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
// outdated driver
d.Name = d.Language
d.Status = manifest.Inactive
return nil
} else if resp.StatusCode/100 != 2 { // 2xx
return fmt.Errorf("status: %v", resp.Status)
}
lang := d.Language
if err := d.Manifest.Decode(resp.Body); err != nil {
return err
}
// override language ID from manifest (prevents copy-paste of manifests)
d.Language = lang
if d.Name == "" {
d.Name = d.Language
}
return nil
}
// fetchFromGithub returns a manifest.OpenFunc that is bound to context and fetches files directly from Github.
func (d Driver) fetchFromGithub(ctx context.Context) manifest.OpenFunc {
return func(path string) (io.ReadCloser, error) {
req := newReq(ctx, d.repositoryFileURL(path))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
return nil, nil
} else if resp.StatusCode/100 != 2 {
resp.Body.Close()
return nil, fmt.Errorf("status: %v", resp.Status)
}
return resp.Body, nil
}
}
// loadMaintainers reads MAINTAINERS file from repository and decodes it into object.
func (d *Driver) loadMaintainers(ctx context.Context) error {
list, err := manifest.Maintainers(d.fetchFromGithub(ctx))
if err != nil {
return err
}
d.Maintainers = list
return nil
}
// loadSDKVersion reads SDK version from repository and decodes it into object.
func (d *Driver) loadSDKVersion(ctx context.Context) error {
vers, err := manifest.SDKVersion(d.fetchFromGithub(ctx))
if err != nil {
return err
}
d.SDKVersion = vers
return nil
}
// Options controls how drivers are being discovered and what information is fetched for them.
type Options struct {
Organization string // Github organization name
NamesOnly bool // driver manifest will only have Language field populated
NoMaintainers bool // do not load maintainers list
NoSDKVersion bool // do not check SDK version
NoStatic bool // do not use a static manifest - discover drivers
}
// isRateLimit checks if error is due to rate limiting.
func isRateLimit(err error) bool {
_, ok := err.(*github.RateLimitError)
return ok
}
// getDriversForOrg lists all repositories for an organization and filters ones that contains topics of the driver.
func getDriversForOrg(ctx context.Context, org string) ([]Driver, error) {
cli := github.NewClient(nil)
var out []Driver
// list all repositories in organization
for page := 1; ; page++ {
repos, _, err := cli.Repositories.ListByOrg(ctx, org, &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{
Page: page, PerPage: 100,
},
Type: "public",
})
if err != nil {
return out, err
} else if len(repos) == 0 {
break
}
for _, r := range repos {
// filter repos by topics to find babelfish drivers
if containsTopics(r.Topics, topics...) {
out = append(out, Driver{
Manifest: manifest.Manifest{
Language: strings.TrimSuffix(r.GetName(), "-driver"),
},
repo: r,
})
}
}
}
return out, nil
}
const staticDriversURL = `https://raw.githubusercontent.com/` + GithubOrg + `/documentation/master/languages.json`
// getStaticDrivers downloads a static drivers list hosted by Babelfish org.
func getStaticDrivers(ctx context.Context) ([]Driver, error) {
req, err := http.NewRequest("GET", staticDriversURL, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("cannot download static driver list: status: %v", resp.Status)
}
var drivers []Driver
err = json.NewDecoder(resp.Body).Decode(&drivers)
if err != nil {
return nil, fmt.Errorf("cannot decode static driver list: %v", err)
}
return drivers, nil
}
// OfficialDrivers lists all available language drivers for Babelfish.
func OfficialDrivers(ctx context.Context, opt *Options) ([]Driver, error) {
if opt == nil {
opt = &Options{}
}
if opt.Organization == "" {
opt.Organization = GithubOrg
}
out, err := getDriversForOrg(ctx, opt.Organization)
if isRateLimit(err) && opt.Organization == GithubOrg && !opt.NoStatic {
return getStaticDrivers(ctx)
} else if err != nil {
return out, err
}
if opt.NamesOnly {
sort.Sort(byStatusAndName(out))
return out, nil
}
// load manifest and maintainers file from repositories
var (
wg sync.WaitGroup
// limits the number of concurrent requests
tokens = make(chan struct{}, 3)
mu sync.Mutex
last error
)
setErr := func(err error) {
mu.Lock()
last = err
mu.Unlock()
}
for i := range out {
wg.Add(1)
go func(d *Driver) {
defer wg.Done()
tokens <- struct{}{}
defer func() {
<-tokens
}()
if err := d.loadManifest(ctx); err != nil {
setErr(err)
}
if !opt.NoSDKVersion {
if err := d.loadSDKVersion(ctx); err != nil {
setErr(err)
}
}
if !opt.NoMaintainers {
if err := d.loadMaintainers(ctx); err != nil {
setErr(err)
}
}
}(&out[i])
}
wg.Wait()
sort.Sort(byStatusAndName(out))
return out, last
}
// containsTopics returns true if all inc topics are present in the list.
func containsTopics(topics []string, inc ...string) bool {
n := 0
for _, t := range topics {
ok := false
for _, t2 := range inc {
if t == t2 {
ok = true
break
}
}
if ok {
n++
}
}
return n == len(inc)
}