-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdepsdev.go
More file actions
240 lines (203 loc) · 5.49 KB
/
depsdev.go
File metadata and controls
240 lines (203 loc) · 5.49 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
package enrichment
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/git-pkgs/purl"
)
const defaultHTTPTimeout = 30 * time.Second
// DepsDevClient queries the deps.dev v3 REST API.
type DepsDevClient struct {
baseURL string
httpClient *http.Client
userAgent string
}
// NewDepsDevClient creates a client for the deps.dev API.
func NewDepsDevClient() *DepsDevClient {
return newDepsDevClient(defaultUserAgent)
}
func newDepsDevClient(userAgent string) *DepsDevClient {
return &DepsDevClient{
baseURL: "https://api.deps.dev",
httpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
userAgent: userAgent,
}
}
func (c *DepsDevClient) BulkLookup(ctx context.Context, purls []string) (map[string]*PackageInfo, error) {
result := make(map[string]*PackageInfo, len(purls))
for _, purlStr := range purls {
p, err := purl.Parse(purlStr)
if err != nil {
continue
}
system := purl.PURLTypeToDepsdev(p.Type)
if system == "" {
continue
}
name := p.FullName()
resp, err := c.getPackage(ctx, system, name)
if err != nil {
continue
}
info := &PackageInfo{
Ecosystem: p.Type,
Name: name,
Source: "depsdev",
}
for _, v := range resp.Versions {
if v.IsDefault {
info.LatestVersion = v.VersionKey.Version
break
}
}
// Fetch version details for the latest to get license and links
if info.LatestVersion != "" {
vResp, err := c.getVersion(ctx, system, name, info.LatestVersion)
if err == nil {
if len(vResp.Licenses) > 0 {
info.License = strings.Join(vResp.Licenses, " AND ")
}
for _, link := range vResp.Links {
switch link.Label {
case "HOMEPAGE":
info.Homepage = link.URL
case "SOURCE_REPO":
info.Repository = link.URL
case "ORIGIN":
info.RegistryURL = link.URL
}
}
}
}
result[purlStr] = info
}
return result, nil
}
func (c *DepsDevClient) GetVersions(ctx context.Context, purlStr string) ([]VersionInfo, error) {
p, err := purl.Parse(purlStr)
if err != nil {
return nil, err
}
system := purl.PURLTypeToDepsdev(p.Type)
if system == "" {
return nil, fmt.Errorf("unsupported purl type: %s", p.Type)
}
resp, err := c.getPackage(ctx, system, p.FullName())
if err != nil {
return nil, err
}
result := make([]VersionInfo, 0, len(resp.Versions))
for _, v := range resp.Versions {
info := VersionInfo{
Number: v.VersionKey.Version,
}
if v.PublishedAt != "" {
info.PublishedAt, _ = time.Parse(time.RFC3339, v.PublishedAt)
}
result = append(result, info)
}
return result, nil
}
func (c *DepsDevClient) GetVersion(ctx context.Context, purlStr string) (*VersionInfo, error) {
p, err := purl.Parse(purlStr)
if err != nil {
return nil, err
}
system := purl.PURLTypeToDepsdev(p.Type)
if system == "" {
return nil, fmt.Errorf("unsupported purl type: %s", p.Type)
}
resp, err := c.getVersion(ctx, system, p.FullName(), p.Version)
if err != nil {
return nil, err
}
info := &VersionInfo{
Number: resp.VersionKey.Version,
}
if resp.PublishedAt != "" {
info.PublishedAt, _ = time.Parse(time.RFC3339, resp.PublishedAt)
}
if len(resp.Licenses) > 0 {
info.License = strings.Join(resp.Licenses, " AND ")
}
return info, nil
}
type depsdevPackageResponse struct {
PackageKey struct {
System string `json:"system"`
Name string `json:"name"`
} `json:"packageKey"`
Versions []depsdevVersion `json:"versions"`
}
type depsdevVersion struct {
VersionKey struct {
System string `json:"system"`
Name string `json:"name"`
Version string `json:"version"`
} `json:"versionKey"`
PublishedAt string `json:"publishedAt"`
IsDefault bool `json:"isDefault"`
}
type depsdevVersionResponse struct {
VersionKey struct {
System string `json:"system"`
Name string `json:"name"`
Version string `json:"version"`
} `json:"versionKey"`
PublishedAt string `json:"publishedAt"`
IsDefault bool `json:"isDefault"`
Licenses []string `json:"licenses"`
Links []struct {
Label string `json:"label"`
URL string `json:"url"`
} `json:"links"`
}
func (c *DepsDevClient) getPackage(ctx context.Context, system, name string) (*depsdevPackageResponse, error) {
u := fmt.Sprintf("%s/v3/systems/%s/packages/%s", c.baseURL, system, url.PathEscape(name))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("deps.dev: %s", resp.Status)
}
var result depsdevPackageResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func (c *DepsDevClient) getVersion(ctx context.Context, system, name, version string) (*depsdevVersionResponse, error) {
u := fmt.Sprintf("%s/v3/systems/%s/packages/%s/versions/%s",
c.baseURL, system, url.PathEscape(name), url.PathEscape(version))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", c.userAgent)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("deps.dev: %s", resp.Status)
}
var result depsdevVersionResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}