-
Notifications
You must be signed in to change notification settings - Fork 63
/
github.go
286 lines (238 loc) · 6.77 KB
/
github.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package manifest
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gobwas/glob"
"github.com/google/go-github/v32/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"kraftkit.sh/internal/ghrepo"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/unikraft"
)
type GitHubProvider struct {
path string
repo ghrepo.Interface
mopts *ManifestOptions
client *github.Client
branch string
ctx context.Context
}
// NewGitHubProvider attempts to parse the input path as a location provided on
// GitHub. Additional context for authentication is necessary to use this
// provider if the location is held within a private repository. Otherwise we
// can both probe GitHub's API as well as exploit the fact that it is a Git
// repository to retrieve information and deliver information as a Manifest
// format.
func NewGitHubProvider(ctx context.Context, path string, opts ...ManifestOption) (Provider, error) {
var branch string
repo, err := ghrepo.NewFromURL(path)
if err != nil {
return nil, err
}
provider := GitHubProvider{
path: path,
repo: repo,
branch: branch,
ctx: ctx,
mopts: NewManifestOptions(opts...),
}
provider.client = github.NewClient(nil)
if ghauth, ok := provider.mopts.auths[repo.RepoHost()]; ok {
if !ghauth.VerifySSL {
insecureClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
ctx = context.WithValue(
ctx,
oauth2.HTTPClient,
insecureClient,
)
}
oauth2Client := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: ghauth.Token,
},
))
// Is this a GitHub enterprise host?
if repo.RepoHost() != "github.com" {
endpoint, err := url.Parse(repo.RepoHost())
if err != nil {
return nil, fmt.Errorf("failed to parse v3 endpoint: %s", err)
}
provider.client, err = github.NewEnterpriseClient(
endpoint.String(),
endpoint.String(),
oauth2Client,
)
if err != nil {
return nil, err
}
} else {
provider.client = github.NewClient(oauth2Client)
}
}
return provider, nil
}
func (ghp GitHubProvider) Manifests() ([]*Manifest, error) {
// Is this a wildcard? E.g. lib-*?
if strings.HasSuffix(ghp.repo.RepoName(), "*") {
return ghp.manifestsFromWildcard()
}
// Ultimately, since this is Git, we can use the GitProvider, and update the
// path to the resource with a known location
repo := ghp.path
if len(ghp.branch) > 0 {
repo += "@" + ghp.branch
}
manifest, err := gitProviderFromGitHub(ghp.ctx, repo, ghp.mopts.opts...)
if err != nil {
return nil, err
}
manifest.Provider = ghp
return []*Manifest{manifest}, nil
}
func (ghp GitHubProvider) PullManifest(ctx context.Context, manifest *Manifest, popts ...pack.PullOption) error {
if useGit {
return pullGit(ctx, manifest, popts...)
}
manifest.mopts = ghp.mopts
if err := pullArchive(ctx, manifest, popts...); err != nil {
log.G(ctx).Trace(err)
return pullGit(ctx, manifest, popts...)
}
return nil
}
func (ghp GitHubProvider) DeleteManifest(context.Context) error {
return fmt.Errorf("not implemented: manifest.GitHubProvider.DeleteManifest")
}
func (ghp GitHubProvider) String() string {
return "github"
}
// manifestsFromWildcard is an internal method which is called by Manifests to
// parse a GitHub source with a wildcard repository name, e.g. lib-*
func (ghp GitHubProvider) manifestsFromWildcard() ([]*Manifest, error) {
if !strings.HasSuffix(ghp.repo.RepoName(), "*") {
return nil, fmt.Errorf("not a wildcard")
}
var repos []*github.Repository
opts := github.ListOptions{}
g := glob.MustCompile(ghp.repo.RepoName())
for {
log.G(ghp.ctx).WithFields(logrus.Fields{
"page": opts.Page,
}).Trace("querying GitHub API for repositories")
more, resp, err := ghp.client.Repositories.ListByOrg(
ghp.ctx,
ghp.repo.RepoOwner(),
&github.RepositoryListByOrgOptions{
ListOptions: opts,
},
)
if err != nil {
return nil, err
}
for _, repo := range more {
if !g.Match(*repo.Name) {
continue
}
log.G(ghp.ctx).Infof("found via wildcard %s", *repo.CloneURL)
repos = append(repos, repo)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return ghp.appendRepositoriesToManifestInParallel(repos)
}
func (ghp GitHubProvider) appendRepositoriesToManifestInParallel(repos []*github.Repository) ([]*Manifest, error) {
var manifests []*Manifest
var errs []error
var wg sync.WaitGroup
var mu sync.RWMutex
wg.Add(len(repos))
for _, repo := range repos {
go func(repo *github.Repository) {
defer wg.Done()
manifest, err := gitProviderFromGitHub(ghp.ctx, *repo.CloneURL, ghp.mopts.opts...)
if err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
return
}
// Populate with GitHub API-centric information
if repo.Description != nil {
manifest.Description = *repo.Description
}
t, _, _, err := unikraft.GuessTypeNameVersion(*repo.Name)
if err != nil {
mu.Lock()
errs = append(errs, err)
mu.Unlock()
return
}
manifest.Type = t
mu.Lock()
manifests = append(manifests, manifest)
mu.Unlock()
}(repo)
}
wg.Wait()
if len(errs) > 0 {
return nil, errors.Join(errs...)
}
return manifests, nil
}
// gitProviderFromGitHub is a cheap internal hack since we know that GitProvider
// simply returns a slice containing exactly 1 Manifest so as to be complicit
// with the Provider interface. We can exploit this knowledge by accepting an
// input GitHub repository and making the necessary known adjustments to the
// channel and version Resource attribute.
func gitProviderFromGitHub(ctx context.Context, repo string, opts ...ManifestOption) (*Manifest, error) {
// Ultimately, since this is Git, we can use the GitProvider, and update the
// path to the resource with a known location
provider, err := NewGitProvider(ctx, repo, opts...)
if err != nil {
return nil, err
}
manifests, err := provider.Manifests()
if err != nil {
return nil, err
}
// Here's the "hack"
manifest := manifests[0]
ghr, err := ghrepo.NewFromURL(repo)
if err != nil {
return nil, err
}
for j, channel := range manifest.Channels {
channel.Resource = ghrepo.BranchArchive(ghr, channel.Name)
manifest.Channels[j] = channel
}
for j, version := range manifest.Versions {
if version.Type == ManifestVersionGitSha {
version.Resource = ghrepo.SHAArchive(ghr, version.Version)
} else {
version.Resource = ghrepo.TagArchive(ghr, version.Version)
}
manifest.Versions[j] = version
}
return manifest, nil
}