From 7455924850b0aeee63881d32c41cfe1fc7ef1bb8 Mon Sep 17 00:00:00 2001 From: Shawn Smith Date: Sat, 27 Nov 2021 16:36:45 +0900 Subject: [PATCH] use module version in github URLs --- check/check_test.go | 2 +- .../{testrepo => testrepo@v0.1.0}/a.go | 0 check/utils.go | 107 +++++++----------- check/utils_test.go | 19 +++- download/proxy.go | 16 +-- handlers/checks.go | 2 +- 6 files changed, 67 insertions(+), 79 deletions(-) rename check/testdata/{testrepo => testrepo@v0.1.0}/a.go (100%) diff --git a/check/check_test.go b/check/check_test.go index d910e28..b82a9b9 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -5,7 +5,7 @@ import ( ) func TestRun(t *testing.T) { - cr, err := Run("testdata/testrepo") + cr, err := Run("testdata/testrepo@v0.1.0") if err != nil { t.Fatal(err) } diff --git a/check/testdata/testrepo/a.go b/check/testdata/testrepo@v0.1.0/a.go similarity index 100% rename from check/testdata/testrepo/a.go rename to check/testdata/testrepo@v0.1.0/a.go diff --git a/check/utils.go b/check/utils.go index 7f251f7..1c8408f 100644 --- a/check/utils.go +++ b/check/utils.go @@ -101,6 +101,7 @@ func lineCount(filepath string) (int, error) { if err != nil { return 0, err } + // wc output is like: 999 filename.go count, err := strconv.Atoi(strings.Split(strings.TrimSpace(string(out)), " ")[0]) if err != nil { @@ -168,6 +169,17 @@ func (fs *FileSummary) AddError(out string) error { return nil } +func displayFilename(filename string) string { + sp := strings.Split(filename, "@") + if len(sp) < 2 { + return filename + } + + fsp := strings.Split(sp[1], "/") + + return filepath.Join(fsp[1:len(fsp)]...) +} + // borrowed from github.com/client9/gosupplychain // MIT LICENSE: https://github.com/client9/gosupplychain/blob/master/LICENSE func goPkgInToGitHub(name string) string { @@ -212,23 +224,38 @@ func goPkgInToGitHub(name string) string { } func fileURL(dir, filename string) string { + f := displayFilename(filename) + + dirSp := strings.Split(dir, "@") + repo := dirSp[0] + verSp := strings.Split(dirSp[1], "/") + ver := verSp[0] + var fileURL string - base := strings.TrimPrefix(dir, "_repos/src/") + base := strings.TrimPrefix(repo, "_repos/src/") switch { case strings.HasPrefix(base, "golang.org/x/"): var pkg string if len(strings.Split(base, "/")) >= 3 { pkg = strings.Split(base, "/")[2] } + return fmt.Sprintf("https://github.com/golang/%s/blob/master%s", pkg, strings.TrimPrefix(filename, "/"+base)) case strings.HasPrefix(base, "github.com/"): if len(strings.Split(base, "/")) == 4 { base = strings.Join(strings.Split(base, "/")[0:3], "/") } + + if ver != "" { + if strings.Contains(ver, "-") { + ver = strings.Split(ver, "-")[2] + } + + return fmt.Sprintf("https://%s/blob/%s/%s", base, ver, f) + } + return fmt.Sprintf("https://%s/blob/master%s", base, strings.TrimPrefix(filename, "/"+base)) case strings.HasPrefix(base, "gopkg.in/"): - fmt.Println(goPkgInToGitHub(base)) - fmt.Println(strings.TrimPrefix(filename, "/"+base)) return goPkgInToGitHub(base) + strings.TrimPrefix(filename, "/"+base) } @@ -260,35 +287,39 @@ func getFileSummaryMap(out *bufio.Scanner, dir string) (map[string]FileSummary, outer: for out.Scan() { filename := strings.Split(out.Text(), ":")[0] + for _, skip := range skipSuffixes { if strings.HasSuffix(filename, skip) { continue outer } } + if autoGenerated(filename) { continue outer } filename = strings.TrimPrefix(filename, "_repos/src") + dfn := displayFilename(filename) fu := fileURL(dir, filename) - fs := fsMap[filename] + fs := fsMap[dfn] if fs.Filename == "" { - fs.Filename = makeFilename(filename) + // fs.Filename = makeFilename(filename) + fs.Filename = dfn fs.FileURL = fu } err := fs.AddError(out.Text()) if err != nil { return nil, err } - fsMap[filename] = fs + fsMap[dfn] = fs } + return fsMap, nil } // GoTool runs a given go command (for example gofmt, go tool vet) // on a directory func GoTool(dir string, filenames, command []string) (float64, []FileSummary, error) { - // started := time.Now() // temporary disabling of misspell as it's the slowest // command right now if strings.Contains(command[len(command)-1], "misspell") && len(filenames) > 300 { @@ -327,6 +358,7 @@ func GoTool(dir string, filenames, command []string) (float64, []FileSummary, er if err != nil { return 0, []FileSummary{}, err } + if err := out.Err(); err != nil { return 0, []FileSummary{}, err } @@ -362,66 +394,5 @@ func GoTool(dir string, filenames, command []string) (float64, []FileSummary, er return float64(lc-errors) / float64(lc), failed, nil } - // log.Println("END: ", command, time.Now().Sub(started)) return float64(len(filenames)-len(failed)) / float64(len(filenames)), failed, nil } - -/* -// GoFmtNative runs gofmt via golang's stdlib format pkg -func GoFmtNative(dir string, filenames []string) (float64, []FileSummary, error) { - fsChan := make(chan FileSummary) - errChan := make(chan error) - stopChan := make(chan bool) - go func(stopChan chan bool) { - for _, f := range filenames { - for _, skip := range skipSuffixes { - if strings.HasSuffix(f, skip) { - continue - } - } - - if autoGenerated(f) { - continue - } - - go func(c chan FileSummary, errChan chan error, f string) { - b, err := ioutil.ReadFile(f) - if err != nil { - errChan <- err - } - g, err := format.Source(b) - if err != nil { - errChan <- err - } - if !bytes.Equal(b, g) { - filename := strings.TrimPrefix(f, "_repos/src") - fs := FileSummary{} - fs.Filename = makeFilename(filename) - fu := fileURL(dir, strings.TrimPrefix(f, "_repos/src")) - fs.FileURL = fu - fs.Errors = append(fs.Errors, Error{1, "file is not gofmted"}) - - fsChan <- fs - } - }(fsChan, errChan, f) - } - stopChan <- true - }(stopChan) - - var ( - failed = []FileSummary{} - f FileSummary - err error - ) - for { - select { - case fsChan <- f: - failed = append(failed, f) - case errChan <- err: - return 0, []FileSummary{}, err - case <-stopChan: - return float64(len(filenames)-len(failed)) / float64(len(filenames)), failed, nil - } - } -} -*/ diff --git a/check/utils_test.go b/check/utils_test.go index 2fa4c5e..57ceacc 100644 --- a/check/utils_test.go +++ b/check/utils_test.go @@ -69,7 +69,8 @@ func TestFileURL(t *testing.T) { fn string want string }{ - {"_repos/src/github.com/foo/bar/baz.go", "/github.com/foo/bar/baz.go", "https://github.com/foo/bar/blob/master/baz.go"}, + {"_repos/src/github.com/foo/testrepo@v0.1.0/bar/baz.go", "/github.com/foo/testrepo@v0.1.0/bar/baz.go", "https://github.com/foo/testrepo/blob/v0.1.0/bar/baz.go"}, + {"_repos/src/github.com/foo/testrepo@v0.0.0-20211126063219-a5e10ccf946a/bar/baz.go", "/github.com/foo/testrepo@v0.0.0-20211126063219-a5e10ccf946a/bar/baz.go", "https://github.com/foo/testrepo/blob/a5e10ccf946a/bar/baz.go"}, } for _, tt := range cases { @@ -79,3 +80,19 @@ func TestFileURL(t *testing.T) { } } + +func TestDisplayFilename(t *testing.T) { + cases := []struct { + fn string + want string + }{ + {"foo@v0.1.0/bar/baz.go", "bar/baz.go"}, + {"foo@v0.1.0/a/b/c/d/baz.go", "a/b/c/d/baz.go"}, + } + + for _, tt := range cases { + if got := displayFilename(tt.fn); got != tt.want { + t.Errorf("displayFilename(%q) = %q, want %q", tt.fn, got, tt.want) + } + } +} diff --git a/download/proxy.go b/download/proxy.go index f39c535..76f1024 100644 --- a/download/proxy.go +++ b/download/proxy.go @@ -138,10 +138,10 @@ func (c *ProxyClient) ProxyDownload(path string) (string, error) { return "", err } - err = os.RemoveAll(filepath.Join(reposDir, path)) - if err != nil { - return "", err - } + // err = os.RemoveAll(filepath.Join(reposDir, path, "@"+ver)) + // if err != nil { + // return "", err + // } cmd := exec.Command("unzip", "-o", zipPath, "-d", reposDir) @@ -155,10 +155,10 @@ func (c *ProxyClient) ProxyDownload(path string) (string, error) { return "", err } - err = os.Rename(filepath.Join(reposDir, lowerPath+"@"+ver), filepath.Join(reposDir, lowerPath)) - if err != nil { - return "", err - } + // err = os.Rename(filepath.Join(reposDir, lowerPath+"@"+ver), filepath.Join(reposDir, lowerPath)) + // if err != nil { + // return "", err + // } return ver, nil } diff --git a/handlers/checks.go b/handlers/checks.go index df9cb99..ecae24b 100644 --- a/handlers/checks.go +++ b/handlers/checks.go @@ -99,7 +99,7 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e return checksResp{}, fmt.Errorf("could not download repo: %v", err) } - checkResult, err := check.Run(dirName(repo)) + checkResult, err := check.Run(dirName(repo + "@" + ver)) if err != nil { return checksResp{}, err }