Repository navigation
Expand file tree
/
Copy pathforge.go
More file actions
589 lines (527 loc) · 18 KB
/
Copy pathforge.go
File metadata and controls
589 lines (527 loc) · 18 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package forges
import (
"context"
"errors"
"fmt"
"maps"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"github.com/git-pkgs/purl"
)
// ErrNotFound is returned when the requested resource does not exist.
var ErrNotFound = errors.New("not found")
// ErrOwnerNotFound is returned when the requested owner (org or user) does not exist.
var ErrOwnerNotFound = errors.New("owner not found")
// ErrNotSupported is returned when a forge does not support an operation.
var ErrNotSupported = errors.New("not supported by this forge")
// ErrLabelExists is returned when creating a label that already exists.
var ErrLabelExists = errors.New("label already exists")
// ErrInvalidWebhookSignature is returned when a webhook delivery's signature
// or token does not match the expected secret.
var ErrInvalidWebhookSignature = errors.New("invalid webhook signature")
// ErrUnauthorized is returned when a forge rejects the request's credentials or
// denies it permission (HTTP 401 or 403).
var ErrUnauthorized = errors.New("unauthorized")
// ErrRateLimited is returned when a forge rate limits the request, including
// GitHub's rate limit responses that use HTTP 403.
var ErrRateLimited = errors.New("rate limited")
// ErrServerError is returned when a forge fails to handle the request (HTTP 5xx).
var ErrServerError = errors.New("server error")
// statusSentinel returns the sentinel error for an HTTP status, or nil if the
// status has none.
func statusSentinel(status int) error {
switch {
case status == http.StatusUnauthorized || status == http.StatusForbidden:
return ErrUnauthorized
case status == http.StatusTooManyRequests:
return ErrRateLimited
case status >= http.StatusInternalServerError && status <= 599:
return ErrServerError
}
return nil
}
// classifiedError makes err match a sentinel with errors.Is. Its message and
// the types it wraps are those of err.
type classifiedError struct {
sentinel error
err error
}
func (e *classifiedError) Error() string { return e.err.Error() }
func (e *classifiedError) Unwrap() []error { return []error{e.sentinel, e.err} }
// ClassifyStatus returns err extended to match ErrUnauthorized, ErrRateLimited
// or ErrServerError according to the HTTP status, keeping its message and
// wrapped errors. A status with no matching sentinel returns err unchanged.
// Backends use this for errors produced by their native SDKs.
func ClassifyStatus(status int, err error) error {
sentinel := statusSentinel(status)
if err == nil || sentinel == nil {
return err
}
return &classifiedError{sentinel: sentinel, err: err}
}
// ErrReadOnly is returned when a client created by ReadOnlyHTTPClient refuses
// a request that could modify forge state.
var ErrReadOnly = errors.New("write refused in read-only mode")
// ResourceType identifies the kind of resource a URL points to.
type ResourceType string
const (
ResourceTypePR ResourceType = "pr"
ResourceTypeIssue ResourceType = "issue"
)
// ResourceRef identifies a resource (PR, issue, etc.) within a repository.
type ResourceRef struct {
Owner string
Repo string
Type ResourceType
Number int
}
// HTTPError represents a non-OK HTTP response from a forge API.
type HTTPError struct {
StatusCode int
URL string
Body string
}
func (e *HTTPError) Error() string {
return fmt.Sprintf("forge: HTTP %d from %s", e.StatusCode, e.URL)
}
// Is reports whether target is the sentinel error for e's status.
func (e *HTTPError) Is(target error) bool {
sentinel := statusSentinel(e.StatusCode)
return sentinel != nil && sentinel == target
}
// APIBaseURLProvider is implemented by forge backends that can expose their
// raw API root URL for arbitrary endpoint requests.
type APIBaseURLProvider interface {
APIBaseURL() string
}
// Forge is the interface each forge backend implements.
type Forge interface {
Repos() RepoService
Issues() IssueService
PullRequests() PullRequestService
Labels() LabelService
Milestones() MilestoneService
Releases() ReleaseService
CI() CIService
Branches() BranchService
DeployKeys() DeployKeyService
Webhooks() WebhookService
Secrets() SecretService
Notifications() NotificationService
Reviews() ReviewService
Files() FileService
Collaborators() CollaboratorService
CommitStatuses() CommitStatusService
CheckRuns() CheckRunService
Commits() CommitService
Attestations() AttestationService
GetRateLimit(ctx context.Context) (*RateLimit, error)
// GetAuthenticatedUser returns the profile of the user the client's
// token belongs to.
GetAuthenticatedUser(ctx context.Context) (*User, error)
// ParsePath parses URL path segments into a resource reference.
ParsePath(pathParts []string) (*ResourceRef, error)
}
// Client routes requests to the appropriate Forge based on the URL domain.
type Client struct {
mu sync.RWMutex // guards forges and tokens once the Client is in use
forges map[string]Forge
tokens map[string]string
httpClient *http.Client
detection *ForgeBuilders
aliases map[string]string
}
// Option configures a Client.
type Option func(*Client)
// WithToken sets the API token for the given domain. Domains are matched
// case-insensitively.
func WithToken(domain, token string) Option {
return func(c *Client) {
c.tokens[strings.ToLower(domain)] = token
}
}
// WithHTTPClient overrides the default HTTP client used by forge backends.
func WithHTTPClient(hc *http.Client) Option {
return func(c *Client) {
c.httpClient = hc
}
}
// WithForge registers a Forge implementation for the given domain. Domains are
// matched case-insensitively.
func WithForge(domain string, f Forge) Option {
return func(c *Client) {
c.forges[strings.ToLower(domain)] = f
}
}
// WithHostAlias makes repository URLs on host use the forge for domain, for
// forges that serve git from a different host than their API. A Forge
// registered for host itself takes precedence. Hosts are matched
// case-insensitively.
func WithHostAlias(host, domain string) Option {
return func(c *Client) {
c.aliases[strings.ToLower(host)] = strings.ToLower(domain)
}
}
// WithDetection makes the Client detect and register domains it hasn't seen,
// using the given builders, the first time a repository URL on that domain is
// used. Detection always uses https, whatever the scheme of the URL, so a
// plain-HTTP instance has to be registered explicitly with RegisterDomain.
// Detection sends unauthenticated requests to the domain, so callers handling
// untrusted URLs should validate them first. Detection failures are not cached.
func WithDetection(builders ForgeBuilders) Option {
return func(c *Client) {
c.detection = &builders
}
}
// NewClient creates a Client and applies the given options.
func NewClient(opts ...Option) *Client {
c := &Client{
forges: make(map[string]Forge),
tokens: make(map[string]string),
aliases: make(map[string]string),
}
for _, opt := range opts {
opt(c)
}
return c
}
// Tokens returns a copy of the token map so callers can read tokens set via
// WithToken. The map is keyed by lowercased domain.
func (c *Client) Tokens() map[string]string {
c.mu.RLock()
defer c.mu.RUnlock()
return maps.Clone(c.tokens)
}
// HTTPClient returns the HTTP client configured on this Client, or nil.
func (c *Client) HTTPClient() *http.Client {
return c.httpClient
}
// normalizeBaseURL accepts either a bare host[:port] or a full http(s) URL and
// returns (baseURL, host). A bare host is assumed https. The host is lowercased
// in both return values.
func normalizeBaseURL(s string) (baseURL, host string, err error) {
if !strings.Contains(s, "://") {
s = "https://" + s
}
u, err := url.Parse(s)
if err != nil {
return "", "", fmt.Errorf("invalid forge URL %q: %w", s, err)
}
u.Scheme = strings.ToLower(u.Scheme)
if u.Scheme != "http" && u.Scheme != "https" {
return "", "", fmt.Errorf("invalid forge URL scheme %q: must be http or https", u.Scheme)
}
if u.Host == "" {
return "", "", fmt.Errorf("invalid forge URL %q: host is required", s)
}
if u.User != nil {
return "", "", fmt.Errorf("invalid forge URL %q: user information is not supported", s)
}
if u.ForceQuery || u.RawQuery != "" || u.Fragment != "" {
return "", "", fmt.Errorf("invalid forge URL %q: query and fragment are not supported", s)
}
u.Host = strings.ToLower(u.Host)
u.Path = strings.TrimRight(u.Path, "/")
u.RawPath = strings.TrimRight(u.RawPath, "/")
return u.String(), u.Host, nil
}
// RegisterDomain detects the forge type for a domain and registers the
// appropriate Forge using the provided builder functions. The domain may
// include an http:// or https:// prefix; without one, https is assumed.
// The lowercased bare host[:port] is used as the registry key.
func (c *Client) RegisterDomain(ctx context.Context, domain, token string, builders ForgeBuilders) error {
return c.registerDomain(ctx, domain, token, builders, false)
}
// registerDomain implements RegisterDomain. With keepExisting set, a domain
// that was registered while detection was running is left as it is.
func (c *Client) registerDomain(ctx context.Context, domain, token string, builders ForgeBuilders, keepExisting bool) error {
baseURL, domain, err := normalizeBaseURL(domain)
if err != nil {
return err
}
ft, err := DetectForgeType(ctx, baseURL, c.httpClient)
if err != nil {
return fmt.Errorf("detecting forge type for %s: %w", domain, err)
}
var build func(baseURL, token string, hc *http.Client) Forge
switch ft {
case GitHub:
build = builders.GitHub
case GitLab:
build = builders.GitLab
case Gitea, Forgejo:
build = builders.Gitea
case Gerrit:
build = builders.Gerrit
case Tangled:
build = builders.Tangled
case Radicle:
build = builders.Radicle
default:
return fmt.Errorf("unsupported forge type %q for %s", ft, domain)
}
if build == nil {
return fmt.Errorf("no builder registered for forge type %q at %s", ft, domain)
}
// Build before locking so a builder can call back into the Client.
f := build(baseURL, token, c.httpClient)
c.mu.Lock()
defer c.mu.Unlock()
if _, ok := c.forges[domain]; ok && keepExisting {
return nil
}
c.tokens[domain] = token
c.forges[domain] = f
return nil
}
// ForgeBuilders holds constructor functions for each forge type.
// Used by RegisterDomain to create the right forge after detection.
type ForgeBuilders struct {
GitHub func(baseURL, token string, hc *http.Client) Forge
GitLab func(baseURL, token string, hc *http.Client) Forge
Gitea func(baseURL, token string, hc *http.Client) Forge
Gerrit func(baseURL, token string, hc *http.Client) Forge
Tangled func(baseURL, token string, hc *http.Client) Forge
Radicle func(baseURL, token string, hc *http.Client) Forge
}
func (c *Client) forgeFor(domain string) (Forge, error) {
c.mu.RLock()
defer c.mu.RUnlock()
f, ok := c.forges[strings.ToLower(domain)]
if !ok {
return nil, fmt.Errorf("no forge registered for domain %q", domain)
}
return f, nil
}
// forgeOrDetect returns the Forge registered for domain. If there is none and
// detection is enabled, it probes the domain over https and registers what it
// finds.
func (c *Client) forgeOrDetect(ctx context.Context, domain string) (Forge, error) {
domain = strings.ToLower(domain)
if !strings.Contains(domain, "://") {
// Use the key registerDomain writes, so a trailing slash or similar
// doesn't miss a registered host.
if _, host, err := normalizeBaseURL(domain); err == nil {
domain = host
}
if alias, ok := c.aliases[domain]; ok {
if _, err := c.forgeFor(domain); err != nil {
domain = alias
}
}
}
f, err := c.forgeFor(domain)
if err == nil || c.detection == nil || strings.Contains(domain, "://") {
return f, err
}
c.mu.RLock()
token := c.tokens[domain]
c.mu.RUnlock()
if err := c.registerDomain(ctx, "https://"+domain, token, *c.detection, true); err != nil {
return nil, err
}
return c.forgeFor(domain)
}
// ForgeFor returns the Forge implementation registered for the given domain.
// Domains are matched case-insensitively.
func (c *Client) ForgeFor(domain string) (Forge, error) {
return c.forgeFor(domain)
}
// ForgeForURL returns the Forge for a repository URL along with the owner and
// repo parsed from it. Without WithDetection, a domain that has not been
// registered is an error. With it, the domain is detected and registered on
// first use over https, even if the URL itself is http://.
func (c *Client) ForgeForURL(ctx context.Context, repoURL string) (f Forge, owner, repo string, err error) {
domain, owner, repo, err := ParseRepoURL(repoURL)
if err != nil {
return nil, "", "", err
}
f, err = c.forgeOrDetect(ctx, domain)
if err != nil {
return nil, "", "", err
}
return f, owner, repo, nil
}
// FetchRepository fetches normalized repository metadata from a URL string.
func (c *Client) FetchRepository(ctx context.Context, repoURL string) (*Repository, error) {
f, owner, repo, err := c.ForgeForURL(ctx, repoURL)
if err != nil {
return nil, err
}
return f.Repos().Get(ctx, owner, repo)
}
// FetchRepositoryFromPURL fetches repository metadata using a PURL's
// repository_url qualifier.
func (c *Client) FetchRepositoryFromPURL(ctx context.Context, p *purl.PURL) (*Repository, error) {
repoURL := p.RepositoryURL()
if repoURL == "" {
return nil, fmt.Errorf("PURL has no repository_url qualifier")
}
return c.FetchRepository(ctx, repoURL)
}
// FetchTags fetches git tags from a URL string.
func (c *Client) FetchTags(ctx context.Context, repoURL string) ([]Tag, error) {
f, owner, repo, err := c.ForgeForURL(ctx, repoURL)
if err != nil {
return nil, err
}
return f.Repos().ListTags(ctx, owner, repo)
}
// ListRepositories lists all repositories for an owner on the given domain.
func (c *Client) ListRepositories(ctx context.Context, domain, owner string, opts ListRepoOpts) ([]Repository, error) {
f, err := c.forgeOrDetect(ctx, domain)
if err != nil {
return nil, err
}
return f.Repos().List(ctx, owner, opts)
}
// RepoMatchesFilters reports whether a repository satisfies the archived and
// fork filters requested in opts. Backends can use this to decide, while
// paginating, whether a repository counts toward opts.Limit.
func RepoMatchesFilters(r Repository, opts ListRepoOpts) bool {
switch opts.Archived {
case ArchivedExclude:
if r.Archived {
return false
}
case ArchivedOnly:
if !r.Archived {
return false
}
}
switch opts.Forks {
case ForkExclude:
if r.Fork {
return false
}
case ForkOnly:
if !r.Fork {
return false
}
}
return true
}
// FilterRepos applies archived and fork filters to a slice of repositories.
func FilterRepos(repos []Repository, opts ListRepoOpts) []Repository {
n := 0
for _, r := range repos {
if !RepoMatchesFilters(r, opts) {
continue
}
repos[n] = r
n++
}
return repos[:n]
}
// CapRepos truncates repos to at most opts.Limit entries. A Limit of 0 or
// less means unlimited and the slice is returned unchanged.
func CapRepos(repos []Repository, opts ListRepoOpts) []Repository {
if opts.Limit > 0 && len(repos) > opts.Limit {
return repos[:opts.Limit]
}
return repos
}
// FetchTagsFromPURL fetches git tags using a PURL's repository_url qualifier.
func (c *Client) FetchTagsFromPURL(ctx context.Context, p *purl.PURL) ([]Tag, error) {
repoURL := p.RepositoryURL()
if repoURL == "" {
return nil, fmt.Errorf("PURL has no repository_url qualifier")
}
return c.FetchTags(ctx, repoURL)
}
// ParseRepoURL extracts the domain, owner, and repo from a repository URL.
// It handles https://, schemeless, and scp-like SSH URLs (sshuser@host:owner/repo
// or host:owner/repo), and strips .git suffixes and extra path segments. The
// domain is lowercased, so it can be used as a Client registry key.
func ParseRepoURL(rawURL string) (domain, owner, repo string, err error) {
rawURL = strings.TrimSpace(rawURL)
if rawURL == "" {
return "", "", "", fmt.Errorf("empty URL")
}
// Handle scp-like syntax: user@host:owner/repo (e.g. git@github.com:user/repo).
if !strings.Contains(rawURL, "://") {
if domain, path, ok := splitSCPLike(rawURL); ok {
return splitOwnerRepo(strings.ToLower(domain), path)
}
rawURL = "https://" + rawURL
}
u, err := url.Parse(rawURL)
if err != nil {
return "", "", "", fmt.Errorf("invalid URL: %w", err)
}
domain = u.Hostname()
if scheme := strings.ToLower(u.Scheme); scheme == "http" || scheme == "https" {
domain = u.Host
}
return splitOwnerRepo(strings.ToLower(domain), u.Path)
}
// splitSCPLike recognizes git's scp-like remote syntax host:path, optionally
// with userinfo (sshuser@host:path). It returns the host with any userinfo
// stripped, and the path. ok is false when rawURL isn't scp-like, for example
// when the colon is a numeric port (host:2222/path).
func splitSCPLike(rawURL string) (domain, path string, ok bool) {
colonIdx := -1
inBrackets := false
findSeparator:
for i := 0; i < len(rawURL); i++ {
switch rawURL[i] {
case '[':
inBrackets = true
case ']':
inBrackets = false
case '/':
if !inBrackets {
return "", "", false
}
case ':':
if !inBrackets {
colonIdx = i
break findSeparator
}
}
}
if colonIdx < 0 {
return "", "", false
}
slashIdx := len(rawURL)
if slashOffset := strings.Index(rawURL[colonIdx+1:], "/"); slashOffset >= 0 {
slashIdx = colonIdx + 1 + slashOffset
}
host := rawURL[:colonIdx]
at := strings.LastIndex(host, "@")
// Without explicit userinfo, a colon immediately followed by digits is a port.
if port := rawURL[colonIdx+1 : slashIdx]; at < 0 && port != "" {
if _, err := strconv.Atoi(port); err == nil {
return "", "", false
}
}
path = rawURL[colonIdx+1:]
if path == "" {
return "", "", false
}
if at >= 0 {
host = host[at+1:]
}
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = host[1 : len(host)-1]
}
if host == "" {
return "", "", false
}
return host, path, true
}
const minOwnerRepoParts = 2
func splitOwnerRepo(domain, path string) (string, string, string, error) {
path = strings.TrimSuffix(path, ".git")
path = strings.Trim(path, "/")
parts := strings.Split(path, "/")
if len(parts) < minOwnerRepoParts {
return "", "", "", fmt.Errorf("URL path must contain owner/repo, got %q", path)
}
owner := strings.Join(parts[:len(parts)-1], "/")
repo := parts[len(parts)-1]
return domain, owner, repo, nil
}