1
1
package main
2
2
3
3
import (
4
- "encoding/json "
4
+ "context "
5
5
"fmt"
6
6
"net/http"
7
7
"regexp"
8
8
"strings"
9
- "time"
10
9
11
10
"golang.org/x/net/html"
12
11
)
13
12
14
- type licensesReply struct {
15
- Key string `json:"key"`
16
- Name string `json:"name"`
17
- URL string `json:"url"`
18
- Featured bool `json:"featured"`
19
- }
20
-
21
- type ownerReply struct {
22
- URL string `json:"url"`
23
- }
24
-
25
- type repositoryReply struct {
26
- License licensesReply `json:"license"`
27
- CreatedAt string `json:"created_at"`
28
- Description string `json:"description"`
29
- Owner ownerReply `json:"owner"`
30
- }
31
-
32
- type licenseReply struct {
33
- Body string `json:"body"`
34
- }
35
-
36
- type usersReply struct {
37
- Name string `json:"name"`
38
- }
39
-
40
13
// To update, use:
41
- // curl -s -H 'Accept: application/vnd.github.drax-preview+json' https://api.github.com/licenses | jq '.[].key'
14
+ // curl -s https://api.github.com/licenses | jq '.[].key'
42
15
// then compare with https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/#license-specification
43
16
var githubLicenseToDebianLicense = map [string ]string {
44
17
//"agpl-3.0" (not in debian?)
@@ -85,7 +58,7 @@ var debianLicenseText = map[string]string{
85
58
86
59
var githubRegexp = regexp .MustCompile (`github\.com/([^/]+/[^/]+)` )
87
60
88
- func findGitHubRepo (gopkg string ) (string , error ) {
61
+ func findGitHubOwnerRepo (gopkg string ) (string , error ) {
89
62
if strings .HasPrefix (gopkg , "github.com/" ) {
90
63
return strings .TrimPrefix (gopkg , "github.com/" ), nil
91
64
}
@@ -136,27 +109,30 @@ func findGitHubRepo(gopkg string) (string, error) {
136
109
}
137
110
}
138
111
139
- func getLicenseForGopkg (gopkg string ) (string , string , error ) {
140
- repo , err := findGitHubRepo (gopkg )
112
+ func findGitHubRepo (gopkg string ) (owner string , repo string , _ error ) {
113
+ ownerrepo , err := findGitHubOwnerRepo (gopkg )
141
114
if err != nil {
142
115
return "" , "" , err
143
116
}
144
- // TODO: cache this reply
145
- req , err := http .NewRequest ("GET" , "https://api.github.com/repos/" + repo , nil )
146
- if err != nil {
147
- return "" , "" , err
117
+ parts := strings .Split (ownerrepo , "/" )
118
+ if got , want := len (parts ), 2 ; got != want {
119
+ return "" , "" , fmt .Errorf ("invalid GitHub repo: %q does not follow owner/repo" , repo )
148
120
}
149
- req .Header .Set ("Accept" , "application/vnd.github.drax-preview+json" )
150
- resp , err := http .DefaultClient .Do (req )
121
+ return parts [0 ], parts [1 ], nil
122
+ }
123
+
124
+ func getLicenseForGopkg (gopkg string ) (string , string , error ) {
125
+ owner , repo , err := findGitHubRepo (gopkg )
151
126
if err != nil {
152
127
return "" , "" , err
153
128
}
154
- defer resp . Body . Close ()
155
- var r repositoryReply
156
- if err := json . NewDecoder ( resp . Body ). Decode ( & r ); err != nil {
129
+
130
+ rl , _ , err := gitHub . Repositories . License ( context . TODO (), owner , repo )
131
+ if err != nil {
157
132
return "" , "" , err
158
133
}
159
- if deblicense , ok := githubLicenseToDebianLicense [r .License .Key ]; ok {
134
+
135
+ if deblicense , ok := githubLicenseToDebianLicense [rl .GetLicense ().GetKey ()]; ok {
160
136
fulltext := debianLicenseText [deblicense ]
161
137
if fulltext == "" {
162
138
fulltext = "TODO"
@@ -168,74 +144,53 @@ func getLicenseForGopkg(gopkg string) (string, string, error) {
168
144
}
169
145
170
146
func getAuthorAndCopyrightForGopkg (gopkg string ) (string , string , error ) {
171
- repo , err := findGitHubRepo (gopkg )
172
- if err != nil {
173
- return "" , "" , err
174
- }
175
- resp , err := http .Get ("https://api.github.com/repos/" + repo )
147
+ owner , repo , err := findGitHubRepo (gopkg )
176
148
if err != nil {
177
149
return "" , "" , err
178
150
}
179
- defer resp .Body .Close ()
180
-
181
- var rr repositoryReply
182
- if err := json .NewDecoder (resp .Body ).Decode (& rr ); err != nil {
183
- return "" , "" , err
184
- }
185
151
186
- creation , err := time . Parse ( "2006-01-02T15:04:05Z" , rr . CreatedAt )
152
+ rr , _ , err := gitHub . Repositories . Get ( context . TODO (), owner , repo )
187
153
if err != nil {
188
154
return "" , "" , err
189
155
}
190
156
191
- if strings .TrimSpace (rr .Owner . URL ) == "" {
157
+ if strings .TrimSpace (rr .GetOwner (). GetURL () ) == "" {
192
158
return "" , "" , fmt .Errorf ("Repository owner URL not present in API response" )
193
159
}
194
160
195
- resp , err = http . Get (rr .Owner . URL )
161
+ ur , _ , err := gitHub . Users . Get (context . TODO (), rr .GetOwner (). GetLogin () )
196
162
if err != nil {
197
163
return "" , "" , err
198
164
}
199
- defer resp .Body .Close ()
200
165
201
- var ur usersReply
202
- if err := json .NewDecoder (resp .Body ).Decode (& ur ); err != nil {
203
- return "" , "" , err
204
- }
205
-
206
- copyright := creation .Format ("2006" ) + " " + ur .Name
166
+ copyright := rr .CreatedAt .Format ("2006" ) + " " + ur .GetName ()
207
167
if strings .HasPrefix (repo , "google/" ) {
208
168
// As per https://opensource.google.com/docs/creating/, Google retains
209
169
// the copyright for repositories underneath github.com/google/.
210
- copyright = creation .Format ("2006" ) + " Google Inc."
170
+ copyright = rr . CreatedAt .Format ("2006" ) + " Google Inc."
211
171
}
212
172
213
- return ur .Name , copyright , nil
173
+ return ur .GetName () , copyright , nil
214
174
}
215
175
216
176
func getDescriptionForGopkg (gopkg string ) (string , error ) {
217
- repo , err := findGitHubRepo (gopkg )
177
+ owner , repo , err := findGitHubRepo (gopkg )
218
178
if err != nil {
219
179
return "" , err
220
180
}
221
- resp , err := http .Get ("https://api.github.com/repos/" + repo )
222
- if err != nil {
223
- return "" , err
224
- }
225
- defer resp .Body .Close ()
226
181
227
- var rr repositoryReply
228
- if err := json . NewDecoder ( resp . Body ). Decode ( & rr ); err != nil {
182
+ rr , _ , err := gitHub . Repositories . Get ( context . TODO (), owner , repo )
183
+ if err != nil {
229
184
return "" , err
230
185
}
231
186
232
- return strings .TrimSpace (rr .Description ), nil
187
+ return strings .TrimSpace (rr .GetDescription () ), nil
233
188
}
234
189
235
190
func getHomepageForGopkg (gopkg string ) string {
236
- repo , err := findGitHubRepo (gopkg )
191
+ owner , repo , err := findGitHubRepo (gopkg )
237
192
if err != nil {
238
193
return "TODO"
239
194
}
240
- return "https://github.com/" + repo
195
+ return "https://github.com/" + owner + "/" + repo
241
196
}
0 commit comments