-
Notifications
You must be signed in to change notification settings - Fork 1
/
repo.go
204 lines (169 loc) · 5.43 KB
/
repo.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
package modcheck
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
jsoniter "github.com/json-iterator/go"
"golang.org/x/mod/semver"
)
// Project is a struct that contains information about a project.
type Project struct {
ProjectKey struct {
ID string `json:"id"`
} `json:"projectKey"`
OpenIssuesCount string `json:"openIssuesCount"`
StarsCount string `json:"starsCount"`
ForksCount string `json:"forksCount"`
License string `json:"license"`
Description string `json:"description"`
Homepage string `json:"homepage"`
Scorecard struct {
Date string `json:"date"`
Repository struct {
Name string `json:"name"`
Commit string `json:"commit"`
} `json:"repository"`
Scorecard struct {
Version string `json:"version"`
Commit string `json:"commit"`
} `json:"scorecard"`
Checks []struct {
Name string `json:"name"`
Documentation struct {
ShortDescription string `json:"shortDescription"`
URL string `json:"url"`
} `json:"documentation"`
Score string `json:"score"`
Reason string `json:"reason"`
Details []string `json:"details"`
} `json:"checks"`
OverallScore float32 `json:"overallScore"`
Metadata []string `json:"metadata"`
} `json:"scorecard"`
}
// PackageKey is a struct that contains ssystem and name for a package.
type PackageKey struct {
System string `json:"system"`
Name string `json:"name"`
}
// Package Data is a struct that contains PackageKey and Versions for a package.
type PackageData struct {
PackageKey PackageKey `json:"packageKey"`
Versions []Version `json:"versions"`
}
// Repo is a struct that contains links, versions abd other data about a repository.
type Repo struct {
Link string
AlternateLink string
ModVersion string
OSIData Project
VersionData PackageData
}
// GetOSIData GET call to the OSI API "projects" endpoint.
// See documentation for more detail: https://docs.deps.dev/api/v3alpha/#getproject
func (p *Repo) GetOSIData() error {
repoName := p.Link
if len(p.AlternateLink) == 0 && !strings.Contains(p.Link, "github.com") {
p.AlternateLink = FindGitHub(p.Link)
repoName = p.AlternateLink
}
endpoint := "https://api.deps.dev/v3alpha/projects/" + url.QueryEscape(repoName)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return fmt.Errorf("error creating request for OSI data for %s: %w", repoName, err)
}
req = req.WithContext(context.Background())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error getting OSI data for %s: %w", repoName, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading OSI data for %s: %w", repoName, err)
}
err = jsoniter.Unmarshal(body, &p.OSIData)
if err != nil {
return fmt.Errorf("error unmarshalling OSI data for %s: %w", repoName, err)
}
return nil
}
// GetPackageData GET call to the OSI API "packages" endpoint.
func (p *Repo) GetPackageData() error {
endpoint := "https://api.deps.dev/v3alpha/systems/go/packages/" + url.QueryEscape(p.Link)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return fmt.Errorf("error creating request for package data for %s: %w", p.Link, err)
}
req = req.WithContext(context.Background())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error getting package data for %s: %w", p.Link, err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading package data for %s: %w", p.Link, err)
}
err = jsoniter.Unmarshal(body, &p.VersionData)
if err != nil {
return fmt.Errorf("error unmarshalling package data for %s: %w", p.Link, err)
}
// TODO(unassigned): ensure that the slice of versions are sorted
return nil
}
// IsLatestVersion returns 1 if the package is the latest version, 0 if it is not, and -1 if there is no OSI data.
func (p *Repo) IsLatestVersion() int {
if len(p.VersionData.Versions) == 0 {
return 0
}
latestVersion := p.VersionData.Versions[len(p.VersionData.Versions)-1].VersionKey.Version
return semver.Compare(p.ModVersion, latestVersion)
}
// LatestVersion returns the latest version of the package.
func (p *Repo) LatestVersion() string {
if len(p.VersionData.Versions) == 0 {
return "N/A"
}
return p.VersionData.Versions[len(p.VersionData.Versions)-1].VersionKey.Version
}
// ExtractRepoInfo takes in a filepath to a go.mod file and returns a slice of Repo structs.
func ExtractRepoInfo(filepath string) []Repo {
gomod, err := os.ReadFile(filepath)
if err != nil {
panic(err)
}
return LinksToRepos(gomod)
}
// LinksToRepos takes in a go.mod file as a byte slice and returns a slice of Repo structs.
func LinksToRepos(gomod []byte) []Repo {
var links []Repo
lines := bytes.Split(gomod, []byte("\n"))
for _, line := range lines {
if bytes.HasPrefix(line, []byte("\t")) && !bytes.Contains(line, []byte("indirect")) {
strLine := string(line)
links = append(links, Repo{
Link: strings.Split(strLine, " ")[0][1:],
ModVersion: strings.Split(strLine, " ")[1],
OSIData: Project{},
})
}
}
return links
}
// UpdateAllRepos takes in a slice of Repo structs and updates the OSI data for each one.
func UpdateAllRepos(repos []Repo) {
for i := range repos {
err := repos[i].GetOSIData()
if err != nil {
repos[i].OSIData = Project{}
}
}
}