-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect.go
More file actions
118 lines (98 loc) · 2.87 KB
/
detect.go
File metadata and controls
118 lines (98 loc) · 2.87 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
package forges
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// DetectForgeType probes a domain to identify which forge software it runs.
// It checks HTTP response headers first, then falls back to API endpoints.
// If hc is nil, http.DefaultClient is used.
func DetectForgeType(ctx context.Context, domain string, hc ...*http.Client) (ForgeType, error) {
client := http.DefaultClient
if len(hc) > 0 && hc[0] != nil {
client = hc[0]
}
baseURL := "https://" + domain
ft, err := detectFromHeaders(ctx, client, baseURL)
if err == nil && ft != Unknown {
return ft, nil
}
return detectFromAPI(ctx, client, baseURL)
}
func detectFromHeaders(ctx context.Context, client *http.Client, baseURL string) (ForgeType, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL, nil)
if err != nil {
return Unknown, err
}
resp, err := client.Do(req)
if err != nil {
return Unknown, err
}
defer func() { _ = resp.Body.Close() }()
if resp.Header.Get("X-Forgejo-Version") != "" {
return Forgejo, nil
}
if resp.Header.Get("X-Gitea-Version") != "" {
return Gitea, nil
}
if resp.Header.Get("X-Gitlab-Meta") != "" {
return GitLab, nil
}
if resp.Header.Get("X-GitHub-Request-Id") != "" {
return GitHub, nil
}
return Unknown, nil
}
func detectFromAPI(ctx context.Context, client *http.Client, baseURL string) (ForgeType, error) {
// Try Gitea/Forgejo /api/v1/version
if ft, err := probeGiteaAPI(ctx, client, baseURL); err == nil {
return ft, nil
}
// Try GitLab /api/v4/version
if ok, err := probeURL(ctx, client, baseURL+"/api/v4/version"); err == nil && ok {
return GitLab, nil
}
// Try GitHub Enterprise /api/v3/meta
if ok, err := probeURL(ctx, client, baseURL+"/api/v3/meta"); err == nil && ok {
return GitHub, nil
}
return Unknown, fmt.Errorf("could not detect forge type for %s", baseURL)
}
func probeGiteaAPI(ctx context.Context, client *http.Client, baseURL string) (ForgeType, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/v1/version", nil)
if err != nil {
return Unknown, err
}
resp, err := client.Do(req)
if err != nil {
return Unknown, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return Unknown, fmt.Errorf("status %d", resp.StatusCode)
}
var v struct {
Version string `json:"version"`
}
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return Unknown, err
}
if strings.Contains(strings.ToLower(v.Version), "forgejo") {
return Forgejo, nil
}
return Gitea, nil
}
func probeURL(ctx context.Context, client *http.Client, url string) (bool, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false, err
}
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer func() { _ = resp.Body.Close() }()
return resp.StatusCode == http.StatusOK, nil
}