-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
main.go
1223 lines (1106 loc) · 38.7 KB
/
main.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
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// update-urls updates GitHub URL docs for each service endpoint.
//
// It is meant to be used periodically by go-github repo maintainers
// to update stale GitHub Developer v3 API documentation URLs.
//
// Usage (from go-github directory):
// go run ./update-urls/main.go
// go generate ./...
// go test ./...
// go vet ./...
//
// When confronted with "PLEASE CHECK MANUALLY AND FIX", the problematic
// URL needs to be debugged. To debug a specific file, run like this:
// go run ./update-urls/main.go -v -d enterprise_actions_runners.go
package main
import (
"errors"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"strings"
)
const (
codeLegacySplitString = `<code>`
codeSplitString = `<code class="hljs language-javascript"><span class="hljs-keyword">await</span> octokit.request(<span class="hljs-string">'`
fragmentIDString = `<h3 id="`
skipPrefix = "gen-"
// enterpriseURL = "docs.github.com"
stdURL = "docs.github.com"
enterpriseRefFmt = "// GitHub Enterprise API docs: %v"
stdRefFmt = "// GitHub API docs: %v"
)
var (
verbose = flag.Bool("v", false, "Print verbose log messages")
debugFile = flag.String("d", "", "Debug named file only")
// skipMethods holds methods which are skipped because they do not have GitHub v3
// API URLs or are otherwise problematic in parsing, discovering, and/or fixing.
skipMethods = map[string]bool{
"ActionsService.DownloadArtifact": true,
"AdminService.CreateOrg": true,
"AdminService.CreateUser": true,
"AdminService.CreateUserImpersonation": true,
"AdminService.DeleteUserImpersonation": true,
"AdminService.GetAdminStats": true,
"AdminService.RenameOrg": true,
"AdminService.RenameOrgByName": true,
"AdminService.UpdateTeamLDAPMapping": true,
"AdminService.UpdateUserLDAPMapping": true,
"AppsService.FindRepositoryInstallationByID": true,
"AuthorizationsService.CreateImpersonation": true,
"AuthorizationsService.DeleteImpersonation": true,
"IssueImportService.CheckStatus": true,
"IssueImportService.CheckStatusSince": true,
"IssueImportService.Create": true,
"MarketplaceService.marketplaceURI": true,
"OrganizationsService.GetByID": true,
"RepositoriesService.DeletePreReceiveHook": true,
"RepositoriesService.DownloadContents": true,
"RepositoriesService.DownloadContentsWithMeta": true,
"RepositoriesService.GetArchiveLink": true,
"RepositoriesService.GetByID": true,
"RepositoriesService.GetPreReceiveHook": true,
"RepositoriesService.ListPreReceiveHooks": true,
"RepositoriesService.UpdatePreReceiveHook": true,
"SearchService.search": true,
"TeamsService.ListTeamMembersByID": true,
"UsersService.DemoteSiteAdmin": true,
"UsersService.GetByID": true,
"UsersService.PromoteSiteAdmin": true,
"UsersService.Suspend": true,
"UsersService.Unsuspend": true,
}
helperOverrides = map[string]overrideFunc{
"s.search": func(arg string) (httpMethod, url string) {
return "GET", fmt.Sprintf("search/%v", arg)
},
}
// methodOverrides contains overrides for troublesome endpoints.
methodOverrides = map[string]string{
"OrganizationsService.EditOrgMembership: method orgs/%v/memberships/%v": "PUT",
"OrganizationsService.EditOrgMembership: PUT user/memberships/orgs/%v": "PATCH",
}
paramLegacyRE = regexp.MustCompile(`:[a-z_]+`)
paramRE = regexp.MustCompile(`{[a-z_]+}`)
)
type overrideFunc func(arg string) (httpMethod, url string)
func logf(fmt string, args ...interface{}) {
if *verbose {
log.Printf(fmt, args...)
}
}
type servicesMap map[string]*Service
type endpointsMap map[string]*Endpoint
func main() {
flag.Parse()
fset := token.NewFileSet()
sourceFilter := func(fi os.FileInfo) bool {
return !strings.HasSuffix(fi.Name(), "_test.go") && !strings.HasPrefix(fi.Name(), skipPrefix)
}
if err := os.Chdir("./github"); err != nil {
if err := os.Chdir("../github"); err != nil {
log.Fatalf("Please run this from the go-github directory.")
}
}
pkgs, err := parser.ParseDir(fset, ".", sourceFilter, parser.ParseComments)
if err != nil {
log.Fatal(err)
}
// Step 1 - get a map of all services.
services := findAllServices(pkgs)
// Step 2 - find all the API service endpoints.
iter := &realAstFileIterator{fset: fset, pkgs: pkgs}
endpoints, err := findAllServiceEndpoints(iter, services)
if err != nil {
log.Fatalf("\n%v", err)
}
// Step 3 - resolve all missing httpMethods from helperMethods.
// Additionally, use existing URLs as hints to pre-cache all apiDocs.
docCache := &documentCache{}
usedHelpers, endpointsByFilename := resolveHelpersAndCacheDocs(endpoints, docCache)
// Step 4 - validate and rewrite all URLs, skipping used helper methods.
frw := &liveFileRewriter{fset: fset}
validateRewriteURLs(usedHelpers, endpointsByFilename, docCache, frw)
logf("Done.")
}
type usedHelpersMap map[string]bool
type endpointsByFilenameMap map[string][]*Endpoint
// FileRewriter read/writes files and converts AST token positions.
type FileRewriter interface {
Position(token.Pos) token.Position
ReadFile(filename string) ([]byte, error)
WriteFile(filename string, buf []byte, mode os.FileMode) error
}
// liveFileRewriter implements FileRewriter.
type liveFileRewriter struct {
fset *token.FileSet
}
func (lfr *liveFileRewriter) Position(pos token.Pos) token.Position { return lfr.fset.Position(pos) }
func (lfr *liveFileRewriter) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func (lfr *liveFileRewriter) WriteFile(filename string, buf []byte, mode os.FileMode) error {
return ioutil.WriteFile(filename, buf, mode)
}
func validateRewriteURLs(usedHelpers usedHelpersMap, endpointsByFilename endpointsByFilenameMap, docCache documentCacheReader, fileRewriter FileRewriter) {
for filename, slc := range endpointsByFilename {
logf("Step 4 - Processing %v methods in %v ...", len(slc), filename)
var fileEdits []*FileEdit
for _, endpoint := range slc {
fullName := fmt.Sprintf("%v.%v", endpoint.serviceName, endpoint.endpointName)
if usedHelpers[fullName] {
logf("Step 4 - skipping used helper method %q", fullName)
continue
}
// First, find the correct GitHub v3 API URL by httpMethod and urlFormat.
for _, path := range endpoint.urlFormats {
path = strings.ReplaceAll(path, "%d", "%v")
path = strings.ReplaceAll(path, "%s", "%v")
// Check the overrides.
endpoint.checkHTTPMethodOverride(path)
methodAndPath := fmt.Sprintf("%v %v", endpoint.httpMethod, path)
url, ok := docCache.URLByMethodAndPath(methodAndPath)
if !ok {
if i := len(endpoint.endpointComments); i > 0 {
pos := fileRewriter.Position(endpoint.endpointComments[i-1].Pos())
fmt.Printf("%v:%v:%v: WARNING: unable to find online docs for %q: (%v)\nPLEASE CHECK MANUALLY AND FIX.\n", pos.Filename, pos.Line, pos.Column, fullName, methodAndPath)
} else {
fmt.Printf("%v: WARNING: unable to find online docs for %q: (%v)\nPLEASE CHECK MANUALLY AND FIX.\n", filename, fullName, methodAndPath)
}
continue
}
logf("found %q for: %q (%v)", url, fullName, methodAndPath)
// Make sure URL is up-to-date.
switch {
case len(endpoint.enterpriseRefLines) > 1:
log.Printf("WARNING: multiple Enterprise GitHub URLs found - skipping:")
for i, refLine := range endpoint.enterpriseRefLines {
log.Printf("line %v: %#v", i, refLine)
}
case len(endpoint.enterpriseRefLines) > 0:
line := fmt.Sprintf(enterpriseRefFmt, url)
cmt := endpoint.enterpriseRefLines[0]
if cmt.Text != line {
pos := fileRewriter.Position(cmt.Pos())
logf("At byte offset %v:\nFOUND %q\nWANT: %q", pos.Offset, cmt.Text, line)
fileEdits = append(fileEdits, &FileEdit{
pos: pos,
fromText: cmt.Text,
toText: line,
})
}
case len(endpoint.stdRefLines) > 1:
var foundMatch bool
line := fmt.Sprintf(stdRefFmt, url)
for i, stdRefLine := range endpoint.stdRefLines {
if stdRefLine.Text == line {
foundMatch = true
logf("found match with %v, not editing and removing from list", line)
// Remove matching line
endpoint.stdRefLines = append(endpoint.stdRefLines[:i], endpoint.stdRefLines[i+1:]...)
break
}
}
if !foundMatch { // Edit last stdRefLine, then remove it.
cmt := endpoint.stdRefLines[len(endpoint.stdRefLines)-1]
pos := fileRewriter.Position(cmt.Pos())
logf("stdRefLines=%v: At byte offset %v:\nFOUND %q\nWANT: %q", len(endpoint.stdRefLines), pos.Offset, cmt.Text, line)
fileEdits = append(fileEdits, &FileEdit{
pos: pos,
fromText: cmt.Text,
toText: line,
})
endpoint.stdRefLines = endpoint.stdRefLines[:len(endpoint.stdRefLines)-1]
}
case len(endpoint.stdRefLines) > 0:
line := fmt.Sprintf(stdRefFmt, url)
cmt := endpoint.stdRefLines[0]
if cmt.Text != line {
pos := fileRewriter.Position(cmt.Pos())
logf("stdRefLines=1: At byte offset %v:\nFOUND %q\nWANT: %q", pos.Offset, cmt.Text, line)
fileEdits = append(fileEdits, &FileEdit{
pos: pos,
fromText: cmt.Text,
toText: line,
})
}
endpoint.stdRefLines = nil
case len(endpoint.endpointComments) > 0:
lastCmt := endpoint.endpointComments[len(endpoint.endpointComments)-1]
// logf("lastCmt.Text=%q (len=%v)", lastCmt.Text, len(lastCmt.Text))
pos := fileRewriter.Position(lastCmt.Pos())
pos.Offset += len(lastCmt.Text)
line := "\n" + fmt.Sprintf(stdRefFmt, url)
if lastCmt.Text != "//" {
line = "\n//" + line // Add blank comment line before URL.
}
// logf("line=%q (len=%v)", line, len(line))
// logf("At byte offset %v: adding missing documentation:\n%q", pos.Offset, line)
fileEdits = append(fileEdits, &FileEdit{
pos: pos,
fromText: "",
toText: line,
})
default: // Missing documentation - add it.
log.Printf("WARNING: file %v has no godoc comment string for method %v", fullName, methodAndPath)
}
}
}
if len(fileEdits) > 0 {
b, err := fileRewriter.ReadFile(filename)
if err != nil {
log.Fatalf("ReadFile: %v", err)
}
log.Printf("Performing %v edits on file %v", len(fileEdits), filename)
b = performBufferEdits(b, fileEdits)
if err := fileRewriter.WriteFile(filename, b, 0644); err != nil {
log.Fatalf("WriteFile: %v", err)
}
}
}
}
func performBufferEdits(b []byte, fileEdits []*FileEdit) []byte {
fileEdits = sortAndMergeFileEdits(fileEdits)
for _, edit := range fileEdits {
prelude := b[0:edit.pos.Offset]
postlude := b[edit.pos.Offset+len(edit.fromText):]
logf("At byte offset %v, replacing %v bytes with %v bytes\nBEFORE: %v\nAFTER : %v", edit.pos.Offset, len(edit.fromText), len(edit.toText), edit.fromText, edit.toText)
b = []byte(fmt.Sprintf("%s%v%s", prelude, edit.toText, postlude))
}
return b
}
func sortAndMergeFileEdits(fileEdits []*FileEdit) []*FileEdit {
// Sort edits from last to first in the file.
// If the offsets are identical, sort the comment "toText" strings, ascending.
var foundDups bool
sort.Slice(fileEdits, func(a, b int) bool {
if fileEdits[a].pos.Offset == fileEdits[b].pos.Offset {
foundDups = true
return fileEdits[a].toText < fileEdits[b].toText
}
return fileEdits[a].pos.Offset > fileEdits[b].pos.Offset
})
if !foundDups {
return fileEdits
}
// Merge the duplicate edits.
var mergedEdits []*FileEdit
var dupOffsets []*FileEdit
mergeFunc := func() {
if len(dupOffsets) > 1 {
isInsert := dupOffsets[0].fromText == ""
var hasBlankCommentLine bool
// Merge dups
var lines []string
for _, dup := range dupOffsets {
if isInsert && strings.HasPrefix(dup.toText, "\n//\n//") {
lines = append(lines, strings.TrimPrefix(dup.toText, "\n//"))
hasBlankCommentLine = true
} else {
lines = append(lines, dup.toText)
}
}
sort.Strings(lines)
var joinStr string
// if insert, no extra newlines
if !isInsert { // if replacement - add newlines
joinStr = "\n"
}
toText := strings.Join(lines, joinStr)
if hasBlankCommentLine { // Add back in
toText = "\n//" + toText
}
mergedEdits = append(mergedEdits, &FileEdit{
pos: dupOffsets[0].pos,
fromText: dupOffsets[0].fromText,
toText: toText,
})
} else if len(dupOffsets) > 0 {
// Move non-dup to final output
mergedEdits = append(mergedEdits, dupOffsets[0])
}
dupOffsets = nil
}
lastOffset := -1
for _, fileEdit := range fileEdits {
if fileEdit.pos.Offset != lastOffset {
mergeFunc()
}
dupOffsets = append(dupOffsets, fileEdit)
lastOffset = fileEdit.pos.Offset
}
mergeFunc()
return mergedEdits
}
// astFileIterator iterates over all files in an ast.Package.
type astFileIterator interface {
// Finds the position of a token.
Position(token.Pos) token.Position
// Reset resets the iterator.
Reset()
// Next returns the next filenameAstFilePair pair or nil if done.
Next() *filenameAstFilePair
}
type filenameAstFilePair struct {
filename string
astFile *ast.File
}
// realAstFileIterator implements astFileIterator.
type realAstFileIterator struct {
fset *token.FileSet
pkgs map[string]*ast.Package
ch chan *filenameAstFilePair
closed bool
}
func (rafi *realAstFileIterator) Position(pos token.Pos) token.Position {
return rafi.fset.Position(pos)
}
func (rafi *realAstFileIterator) Reset() {
if !rafi.closed && rafi.ch != nil {
logf("Closing old channel on Reset")
close(rafi.ch)
}
rafi.ch = make(chan *filenameAstFilePair, 10)
rafi.closed = false
go func() {
var count int
for _, pkg := range rafi.pkgs {
for filename, f := range pkg.Files {
// logf("Sending file #%v: %v to channel", count, filename)
rafi.ch <- &filenameAstFilePair{filename: filename, astFile: f}
count++
}
}
rafi.closed = true
close(rafi.ch)
logf("Closed channel after sending %v files", count)
if count == 0 {
log.Fatalf("Processed no files. Did you run this from the go-github directory?")
}
}()
}
func (rafi *realAstFileIterator) Next() *filenameAstFilePair {
for pair := range rafi.ch {
// logf("Next: returning file %v", pair.filename)
return pair
}
return nil
}
func findAllServices(pkgs map[string]*ast.Package) servicesMap {
services := servicesMap{}
for _, pkg := range pkgs {
for filename, f := range pkg.Files {
if filename != "github.go" {
continue
}
logf("Step 1 - Processing %v ...", filename)
if err := findClientServices(filename, f, services); err != nil {
log.Fatal(err)
}
}
}
return services
}
func findAllServiceEndpoints(iter astFileIterator, services servicesMap) (endpointsMap, error) {
endpoints := endpointsMap{}
iter.Reset()
var errs []string // Collect all the errors and return in a big batch.
for next := iter.Next(); next != nil; next = iter.Next() {
filename, f := next.filename, next.astFile
if filename == "github.go" {
continue
}
if *debugFile != "" && !strings.Contains(filename, *debugFile) {
continue
}
logf("Step 2 - Processing %v ...", filename)
if err := processAST(filename, f, services, endpoints, iter); err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return nil, errors.New(strings.Join(errs, "\n"))
}
return endpoints, nil
}
func resolveHelpersAndCacheDocs(endpoints endpointsMap, docCache documentCacheWriter) (usedHelpers usedHelpersMap, endpointsByFilename endpointsByFilenameMap) {
usedHelpers = usedHelpersMap{}
endpointsByFilename = endpointsByFilenameMap{}
for k, v := range endpoints {
if _, ok := endpointsByFilename[v.filename]; !ok {
endpointsByFilename[v.filename] = []*Endpoint{}
}
endpointsByFilename[v.filename] = append(endpointsByFilename[v.filename], v)
for _, cmt := range v.enterpriseRefLines {
docCache.CacheDocFromInternet(cmt.Text, v.filename)
}
for _, cmt := range v.stdRefLines {
docCache.CacheDocFromInternet(cmt.Text, v.filename)
}
if v.httpMethod == "" && v.helperMethod != "" {
fullName := fmt.Sprintf("%v.%v", v.serviceName, v.helperMethod)
hm, ok := endpoints[fullName]
if !ok {
log.Fatalf("Unable to find helper method %q for %q", fullName, k)
}
if hm.httpMethod == "" {
log.Fatalf("Helper method %q for %q has empty httpMethod: %#v", fullName, k, hm)
}
v.httpMethod = hm.httpMethod
usedHelpers[fullName] = true
}
}
return usedHelpers, endpointsByFilename
}
type documentCacheReader interface {
URLByMethodAndPath(string) (string, bool)
}
type documentCacheWriter interface {
CacheDocFromInternet(urlWithFragmentID, filename string)
}
// documentCache implements documentCacheReader and documentCachWriter.
type documentCache struct {
apiDocs map[string]map[string][]*Endpoint // cached by URL, then mapped by web fragment identifier.
urlByMethodAndPath map[string]string
}
func (dc *documentCache) URLByMethodAndPath(methodAndPath string) (string, bool) {
url, ok := dc.urlByMethodAndPath[methodAndPath]
return url, ok
}
func (dc *documentCache) CacheDocFromInternet(urlWithID, filename string) {
if dc.apiDocs == nil {
dc.apiDocs = map[string]map[string][]*Endpoint{} // cached by URL, then mapped by web fragment identifier.
dc.urlByMethodAndPath = map[string]string{}
}
url := getURL(urlWithID)
if _, ok := dc.apiDocs[url]; ok {
return // already cached
}
logf("GET %q ...", url)
resp, err := http.Get(url)
check("Unable to get URL: %v: %v", url, err)
if resp.StatusCode != http.StatusOK {
log.Fatalf("filename: %v - url %v - StatusCode=%v", filename, url, resp.StatusCode)
}
finalURL := resp.Request.URL.String()
url = getURL(finalURL)
logf("The final URL is: %v; url=%v\n", finalURL, url)
b, err := ioutil.ReadAll(resp.Body)
check("Unable to read body of URL: %v, %v", url, err)
check("Unable to close body of URL: %v, %v", url, resp.Body.Close())
dc.apiDocs[url] = parseWebPageEndpoints(string(b))
logf("Found %v web page fragment identifiers.", len(dc.apiDocs[url]))
if len(dc.apiDocs[url]) == 0 {
logf("webage text: %s", b)
}
// Now reverse-map the methods+paths to URLs.
for fragID, v := range dc.apiDocs[url] {
logf("For fragID=%q, found %v endpoints.", fragID, len(v))
for _, endpoint := range v {
logf("For fragID=%q, endpoint=%q, found %v paths.", fragID, endpoint, len(endpoint.urlFormats))
for _, path := range endpoint.urlFormats {
methodAndPath := fmt.Sprintf("%v %v", endpoint.httpMethod, path)
dc.urlByMethodAndPath[methodAndPath] = fmt.Sprintf("%v#%v", url, fragID)
logf("urlByMethodAndPath[%q] = %q", methodAndPath, dc.urlByMethodAndPath[methodAndPath])
}
}
}
}
// FileEdit represents an edit that needs to be performed on a file.
type FileEdit struct {
pos token.Position
fromText string
toText string
}
func getURL(s string) string {
i := strings.Index(s, "http")
if i < 0 {
return ""
}
j := strings.Index(s, "#")
if j < i {
s = s[i:]
} else {
s = s[i:j]
}
if !strings.HasSuffix(s, "/") { // Prevent unnecessary redirects if possible.
s += "/"
}
return s
}
// Service represents a go-github service.
type Service struct {
serviceName string
}
// Endpoint represents an API endpoint in this repo.
type Endpoint struct {
endpointName string
filename string
serviceName string
urlFormats []string
httpMethod string
helperMethod string // If populated, httpMethod lives in helperMethod.
enterpriseRefLines []*ast.Comment
stdRefLines []*ast.Comment
endpointComments []*ast.Comment
}
// String helps with debugging by providing an easy-to-read summary of the endpoint.
func (e *Endpoint) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(" filename: %v\n", e.filename))
b.WriteString(fmt.Sprintf(" serviceName: %v\n", e.serviceName))
b.WriteString(fmt.Sprintf(" endpointName: %v\n", e.endpointName))
b.WriteString(fmt.Sprintf(" httpMethod: %v\n", e.httpMethod))
if e.helperMethod != "" {
b.WriteString(fmt.Sprintf(" helperMethod: %v\n", e.helperMethod))
}
for i := 0; i < len(e.urlFormats); i++ {
b.WriteString(fmt.Sprintf(" urlFormats[%v]: %v\n", i, e.urlFormats[i]))
}
for i := 0; i < len(e.enterpriseRefLines); i++ {
b.WriteString(fmt.Sprintf(" enterpriseRefLines[%v]: comment: %v\n", i, e.enterpriseRefLines[i].Text))
}
for i := 0; i < len(e.stdRefLines); i++ {
b.WriteString(fmt.Sprintf(" stdRefLines[%v]: comment: %v\n", i, e.stdRefLines[i].Text))
}
return b.String()
}
func (e *Endpoint) checkHTTPMethodOverride(path string) {
lookupOverride := fmt.Sprintf("%v.%v: %v %v", e.serviceName, e.endpointName, e.httpMethod, path)
logf("Looking up override for %q", lookupOverride)
if v, ok := methodOverrides[lookupOverride]; ok {
logf("overriding method for %v to %q", lookupOverride, v)
e.httpMethod = v
return
}
}
func processAST(filename string, f *ast.File, services servicesMap, endpoints endpointsMap, iter astFileIterator) error {
var errs []string
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl: // Doc, Recv, Name, Type, Body
if decl.Recv == nil || len(decl.Recv.List) != 1 || decl.Name == nil || decl.Body == nil {
continue
}
recv := decl.Recv.List[0]
se, ok := recv.Type.(*ast.StarExpr) // Star, X
if !ok || se.X == nil || len(recv.Names) != 1 {
if decl.Name.Name != "String" && decl.Name.Name != "Equal" && decl.Name.Name != "IsPullRequest" {
pos := iter.Position(recv.Pos())
if id, ok := recv.Type.(*ast.Ident); ok {
pos = iter.Position(id.Pos())
}
errs = append(errs, fmt.Sprintf("%v:%v:%v: method %v does not use a pointer receiver and needs fixing!", pos.Filename, pos.Line, pos.Column, decl.Name))
}
continue
}
recvType, ok := se.X.(*ast.Ident) // NamePos, Name, Obj
if !ok {
return fmt.Errorf("unhandled se.X = %T", se.X)
}
serviceName := recvType.Name
if _, ok := services[serviceName]; !ok {
continue
}
endpointName := decl.Name.Name
fullName := fmt.Sprintf("%v.%v", serviceName, endpointName)
if skipMethods[fullName] {
logf("skipping %v", fullName)
continue
}
receiverName := recv.Names[0].Name
logf("\n\nast.FuncDecl: %#v", *decl) // Doc, Recv, Name, Type, Body
logf("ast.FuncDecl.Name: %#v", *decl.Name) // NamePos, Name, Obj(nil)
// logf("ast.FuncDecl.Recv: %#v", *decl.Recv) // Opening, List, Closing
logf("ast.FuncDecl.Recv.List[0]: %#v", *recv) // Doc, Names, Type, Tag, Comment
// for i, name := range decl.Recv.List[0].Names {
// logf("recv.name[%v] = %v", i, name.Name)
// }
logf("recvType = %#v", recvType)
var enterpriseRefLines []*ast.Comment
var stdRefLines []*ast.Comment
var endpointComments []*ast.Comment
if decl.Doc != nil {
endpointComments = decl.Doc.List
for i, comment := range decl.Doc.List {
logf("doc.comment[%v] = %#v", i, *comment)
// if strings.Contains(comment.Text, enterpriseURL) {
// enterpriseRefLines = append(enterpriseRefLines, comment)
// } else
if strings.Contains(comment.Text, stdURL) {
stdRefLines = append(stdRefLines, comment)
}
}
logf("%v comment lines, %v enterprise URLs, %v standard URLs", len(decl.Doc.List), len(enterpriseRefLines), len(stdRefLines))
}
bd := &bodyData{receiverName: receiverName}
if err := bd.parseBody(decl.Body); err != nil { // Lbrace, List, Rbrace
return fmt.Errorf("parseBody: %v", err)
}
ep := &Endpoint{
endpointName: endpointName,
filename: filename,
serviceName: serviceName,
urlFormats: bd.urlFormats,
httpMethod: bd.httpMethod,
helperMethod: bd.helperMethod,
enterpriseRefLines: enterpriseRefLines,
stdRefLines: stdRefLines,
endpointComments: endpointComments,
}
// ep.checkHTTPMethodOverride("")
endpoints[fullName] = ep
logf("endpoints[%q] = %#v", fullName, endpoints[fullName])
if ep.httpMethod == "" && (ep.helperMethod == "" || len(ep.urlFormats) == 0) {
return fmt.Errorf("could not find body info: %#v", *ep)
}
case *ast.GenDecl:
default:
return fmt.Errorf("unhandled decl type: %T", decl)
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
return nil
}
// bodyData contains information found in a BlockStmt.
type bodyData struct {
receiverName string // receiver name of method to help identify helper methods.
httpMethod string
urlVarName string
urlFormats []string
assignments []lhsrhs
helperMethod string // If populated, httpMethod lives in helperMethod.
}
func (b *bodyData) parseBody(body *ast.BlockStmt) error {
logf("body=%#v", *body)
// Find the variable used for the format string, its one-or-more values,
// and the httpMethod used for the NewRequest.
for _, stmt := range body.List {
switch stmt := stmt.(type) {
case *ast.AssignStmt:
hm, uvn, hlp, asgn := processAssignStmt(b.receiverName, stmt)
if b.httpMethod != "" && hm != "" && b.httpMethod != hm {
return fmt.Errorf("found two httpMethod values: %q and %q", b.httpMethod, hm)
}
if hm != "" {
b.httpMethod = hm
// logf("parseBody: httpMethod=%v", b.httpMethod)
}
if hlp != "" {
b.helperMethod = hlp
}
b.assignments = append(b.assignments, asgn...)
// logf("assignments=%#v", b.assignments)
if b.urlVarName == "" && uvn != "" {
b.urlVarName = uvn
// logf("parseBody: urlVarName=%v", b.urlVarName)
// By the time the urlVarName is found, all assignments should
// have already taken place so that we can find the correct
// ones and determine the urlFormats.
for _, lr := range b.assignments {
if lr.lhs == b.urlVarName {
b.urlFormats = append(b.urlFormats, lr.rhs)
logf("found urlFormat: %v", lr.rhs)
}
}
}
case *ast.DeclStmt:
logf("*ast.DeclStmt: %#v", *stmt)
case *ast.DeferStmt:
logf("*ast.DeferStmt: %#v", *stmt)
case *ast.ExprStmt:
logf("*ast.ExprStmt: %#v", *stmt)
case *ast.IfStmt:
if err := b.parseIf(stmt); err != nil {
return err
}
case *ast.RangeStmt:
logf("*ast.RangeStmt: %#v", *stmt)
case *ast.ReturnStmt: // Return Results
logf("*ast.ReturnStmt: %#v", *stmt)
if len(stmt.Results) > 0 {
ce, ok := stmt.Results[0].(*ast.CallExpr)
if ok {
recv, funcName, args := processCallExpr(ce)
logf("return CallExpr: recv=%q, funcName=%q, args=%#v", recv, funcName, args)
// If the httpMethod has not been found at this point, but
// this method is calling a helper function, then see if
// any of its arguments match a previous assignment, then
// record the urlFormat and remember the helper method.
if b.httpMethod == "" && len(args) > 1 && recv == b.receiverName {
if args[0] != "ctx" {
return fmt.Errorf("expected helper function to get ctx as first arg: %#v, %#v", args, *b)
}
if len(b.assignments) == 0 && len(b.urlFormats) == 0 {
b.urlFormats = append(b.urlFormats, strings.Trim(args[1], `"`))
b.helperMethod = funcName
logf("found urlFormat: %v and helper method: %v", b.urlFormats[0], b.helperMethod)
} else {
for _, lr := range b.assignments {
if lr.lhs == args[1] { // Multiple matches are possible. Loop over all assignments.
b.urlVarName = args[1]
b.urlFormats = append(b.urlFormats, lr.rhs)
b.helperMethod = funcName
logf("found urlFormat: %v and helper method: %v", lr.rhs, b.helperMethod)
}
}
}
}
}
}
case *ast.SwitchStmt:
logf("*ast.SwitchStmt: %#v", *stmt)
default:
return fmt.Errorf("unhandled stmt type: %T", stmt)
}
}
logf("parseBody: assignments=%#v", b.assignments)
return nil
}
func (b *bodyData) parseIf(stmt *ast.IfStmt) error {
logf("*ast.IfStmt: %#v", *stmt)
if err := b.parseBody(stmt.Body); err != nil {
return err
}
logf("if body: b=%#v", *b)
if stmt.Else != nil {
switch els := stmt.Else.(type) {
case *ast.BlockStmt:
if err := b.parseBody(els); err != nil {
return err
}
logf("if else: b=%#v", *b)
case *ast.IfStmt:
if err := b.parseIf(els); err != nil {
return err
}
default:
return fmt.Errorf("unhandled else stmt type %T", els)
}
}
return nil
}
// lhsrhs represents an assignment with a variable name on the left
// and a string on the right - used to find the URL format string.
type lhsrhs struct {
lhs string
rhs string
}
func processAssignStmt(receiverName string, stmt *ast.AssignStmt) (httpMethod, urlVarName, helperMethod string, assignments []lhsrhs) {
logf("*ast.AssignStmt: %#v", *stmt) // Lhs, TokPos, Tok, Rhs
var lhs []string
for _, expr := range stmt.Lhs {
switch expr := expr.(type) {
case *ast.Ident: // NamePos, Name, Obj
logf("processAssignStmt: *ast.Ident: %#v", expr)
lhs = append(lhs, expr.Name)
case *ast.SelectorExpr: // X, Sel
logf("processAssignStmt: *ast.SelectorExpr: %#v", expr)
default:
log.Fatalf("unhandled AssignStmt Lhs type: %T", expr)
}
}
for i, expr := range stmt.Rhs {
switch expr := expr.(type) {
case *ast.BasicLit: // ValuePos, Kind, Value
v := strings.Trim(expr.Value, `"`)
if !strings.HasPrefix(v, "?") { // Hack to remove "?recursive=1"
assignments = append(assignments, lhsrhs{lhs: lhs[i], rhs: v})
}
case *ast.BinaryExpr:
logf("processAssignStmt: *ast.BinaryExpr: %#v", *expr)
case *ast.CallExpr: // Fun, Lparen, Args, Ellipsis, Rparen
recv, funcName, args := processCallExpr(expr)
logf("processAssignStmt: CallExpr: recv=%q, funcName=%q, args=%#v", recv, funcName, args)
switch funcName {
case "addOptions":
if v := strings.Trim(args[0], `"`); v != args[0] {
assignments = append(assignments, lhsrhs{lhs: lhs[i], rhs: v})
urlVarName = lhs[i]
} else {
urlVarName = args[0]
}
case "Sprintf":
assignments = append(assignments, lhsrhs{lhs: lhs[i], rhs: strings.Trim(args[0], `"`)})
case "NewRequest":
httpMethod = strings.Trim(args[0], `"`)
urlVarName = args[1]
case "NewUploadRequest":
httpMethod = "POST"
urlVarName = args[0]
}
if recv == receiverName && len(args) > 1 && args[0] == "ctx" { // This might be a helper method.
fullName := fmt.Sprintf("%v.%v", recv, funcName)
logf("checking for override: fullName=%v", fullName)
if fn, ok := helperOverrides[fullName]; ok {
logf("found helperOverride for %v", fullName)
hm, url := fn(strings.Trim(args[1], `"`))
httpMethod = hm
urlVarName = "u" // arbitrary
assignments = []lhsrhs{{lhs: urlVarName, rhs: url}}
} else {
urlVarName = args[1] // For this to work correctly, the URL must be the second arg to the helper method!
helperMethod = funcName
logf("found possible helper method: funcName=%v, urlVarName=%v", funcName, urlVarName)
}
}
case *ast.CompositeLit: // Type, Lbrace, Elts, Rbrace, Incomplete
logf("processAssignStmt: *ast.CompositeLit: %#v", *expr)
case *ast.FuncLit:
logf("processAssignStmt: *ast.FuncLit: %#v", *expr)
case *ast.SelectorExpr:
logf("processAssignStmt: *ast.SelectorExpr: %#v", *expr)
case *ast.UnaryExpr: // OpPos, Op, X
logf("processAssignStmt: *ast.UnaryExpr: %#v", *expr)
case *ast.TypeAssertExpr: // X, Lparen, Type, Rparen
logf("processAssignStmt: *ast.TypeAssertExpr: %#v", *expr)
case *ast.Ident: // NamePos, Name, Obj
logf("processAssignStmt: *ast.Ident: %#v", *expr)
default:
log.Fatalf("unhandled AssignStmt Rhs type: %T", expr)
}
}
logf("urlVarName=%v, assignments=%#v", urlVarName, assignments)
return httpMethod, urlVarName, helperMethod, assignments
}
func processCallExpr(expr *ast.CallExpr) (recv, funcName string, args []string) {
logf("*ast.CallExpr: %#v", *expr)
for _, arg := range expr.Args {
switch arg := arg.(type) {
case *ast.ArrayType:
logf("processCallExpr: *ast.ArrayType: %#v", arg)
case *ast.BasicLit: // ValuePos, Kind, Value
args = append(args, arg.Value) // Do not trim quotes here so as to identify it later as a string literal.
case *ast.CallExpr: // Fun, Lparen, Args, Ellipsis, Rparen
logf("processCallExpr: *ast.CallExpr: %#v", arg)
r, fn, as := processCallExpr(arg)
if r == "fmt" && fn == "Sprintf" && len(as) > 0 { // Special case - return format string.
args = append(args, as[0])
}
case *ast.CompositeLit:
logf("processCallExpr: *ast.CompositeLit: %#v", arg) // Type, Lbrace, Elts, Rbrace, Incomplete
case *ast.Ident: // NamePos, Name, Obj
args = append(args, arg.Name)
case *ast.MapType:
logf("processCallExpr: *ast.MapType: %#v", arg)