mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-29 06:49:05 +08:00
fix number of go get-related bugs
This commit is contained in:
@@ -57,8 +57,8 @@ func badgeURL(grade Grade) string {
|
||||
}
|
||||
|
||||
// BadgeHandler handles fetching the badge images
|
||||
func BadgeHandler(w http.ResponseWriter, r *http.Request, org, repo string) {
|
||||
name := fmt.Sprintf("%s/%s", org, repo)
|
||||
func BadgeHandler(w http.ResponseWriter, r *http.Request, repo string) {
|
||||
name := fmt.Sprintf("%s", repo)
|
||||
resp, err := newChecksResp(name, false)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: fetching badge for %s: %v", name, err)
|
||||
|
||||
@@ -30,6 +30,16 @@ func CheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
repo := r.FormValue("repo")
|
||||
log.Printf("Checking repo %s...", repo)
|
||||
|
||||
// repoRoot, err := vcs.RepoRootForImportPath(repo, true)
|
||||
// if err != nil {
|
||||
// log.Println("ERROR:", err)
|
||||
// http.Error(w, err.Error(), 404)
|
||||
// return
|
||||
// }
|
||||
// repo = repoRoot.Root
|
||||
// log.Println("RepoRootForImportPath:", repo)
|
||||
|
||||
if strings.ToLower(repo) == "golang/go" {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("We've decided to omit results for the Go repository because it has lots of test files that (purposely) don't pass our checks. Go gets an A+ in our books though!"))
|
||||
@@ -38,7 +48,7 @@ func CheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||
forceRefresh := r.Method != "GET" // if this is a GET request, try to fetch from cached version in boltdb first
|
||||
resp, err := newChecksResp(repo, forceRefresh)
|
||||
if err != nil {
|
||||
log.Println("ERROR: ", err)
|
||||
log.Println("ERROR: from newChecksResp: ", err)
|
||||
b, _ := json.Marshal(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write(b)
|
||||
|
||||
@@ -8,13 +8,16 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/gojp/goreportcard/check"
|
||||
)
|
||||
|
||||
func dirName(repo string) string {
|
||||
return fmt.Sprintf("repos/src/%s", repo)
|
||||
}
|
||||
|
||||
func getFromCache(repo string) (checksResp, error) {
|
||||
// try and fetch from boltdb
|
||||
db, err := bolt.Open(DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
|
||||
@@ -67,24 +70,11 @@ type checksResp struct {
|
||||
LastRefresh time.Time `json:"last_refresh"`
|
||||
}
|
||||
|
||||
func orgRepoNames(url string) (string, string) {
|
||||
dir := strings.TrimSuffix(url, ".git")
|
||||
split := strings.Split(dir, "/")
|
||||
org := split[len(split)-2]
|
||||
repoName := split[len(split)-1]
|
||||
|
||||
return org, repoName
|
||||
}
|
||||
|
||||
func dirName(url string) string {
|
||||
org, repoName := orgRepoNames(url)
|
||||
|
||||
return fmt.Sprintf("repos/src/github.com/%s/%s", org, repoName)
|
||||
}
|
||||
|
||||
func clone(url string) error {
|
||||
org, _ := orgRepoNames(url)
|
||||
if err := os.Mkdir(fmt.Sprintf("repos/src/github.com/%s", org), 0755); err != nil && !os.IsExist(err) {
|
||||
func goGet(repo string) error {
|
||||
log.Println("Go getting", repo, "...")
|
||||
dir := dirName(repo)
|
||||
log.Println("dirName is", dir)
|
||||
if err := os.Mkdir("repos", 0755); err != nil && !os.IsExist(err) {
|
||||
return fmt.Errorf("could not create dir: %v", err)
|
||||
}
|
||||
d, err := filepath.Abs("repos")
|
||||
@@ -92,40 +82,24 @@ func clone(url string) error {
|
||||
return fmt.Errorf("could not fetch absolute path: %v", err)
|
||||
}
|
||||
os.Setenv("GOPATH", d)
|
||||
dir := dirName(url)
|
||||
_, err = os.Stat(dir)
|
||||
if os.IsNotExist(err) {
|
||||
cmd := exec.Command("go", "get", "-u", url)
|
||||
cmd := exec.Command("go", "get", "-d", repo)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("could not run go get: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not stat dir: %v", err)
|
||||
}
|
||||
|
||||
// cmd := exec.Command("git", "-C", dir, "fetch", "origin", "master")
|
||||
// if err := cmd.Run(); err != nil {
|
||||
// return fmt.Errorf("could not fetch master branch: %v", err)
|
||||
// }
|
||||
// cmd = exec.Command("git", "-C", dir, "reset", "--hard", "origin/master")
|
||||
// if err = cmd.Run(); err != nil {
|
||||
// return fmt.Errorf("could not reset origin/master: %v", err)
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newChecksResp(repo string, forceRefresh bool) (checksResp, error) {
|
||||
url := repo
|
||||
if strings.Count(repo, ".") == 0 {
|
||||
url = "github.com/" + url
|
||||
}
|
||||
|
||||
if !forceRefresh {
|
||||
resp, err := getFromCache(repo)
|
||||
if err != nil {
|
||||
@@ -138,12 +112,12 @@ func newChecksResp(repo string, forceRefresh bool) (checksResp, error) {
|
||||
}
|
||||
|
||||
// fetch the repo and grade it
|
||||
err := clone(url)
|
||||
err := goGet(repo)
|
||||
if err != nil {
|
||||
return checksResp{}, fmt.Errorf("could not clone repo: %v", err)
|
||||
}
|
||||
|
||||
dir := dirName(url)
|
||||
dir := dirName(repo)
|
||||
filenames, err := check.GoFiles(dir)
|
||||
if err != nil {
|
||||
return checksResp{}, fmt.Errorf("could not get filenames: %v", err)
|
||||
|
||||
@@ -3,6 +3,6 @@ package handlers
|
||||
import "net/http"
|
||||
|
||||
// ReportHandler handles the report page
|
||||
func ReportHandler(w http.ResponseWriter, r *http.Request, org, repo string) {
|
||||
func ReportHandler(w http.ResponseWriter, r *http.Request, repo string) {
|
||||
http.ServeFile(w, r, "templates/home.html")
|
||||
}
|
||||
|
||||
12
main.go
12
main.go
@@ -12,9 +12,9 @@ import (
|
||||
"github.com/gojp/goreportcard/handlers"
|
||||
)
|
||||
|
||||
func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string, string)) http.HandlerFunc {
|
||||
func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
validPath := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)$`, name))
|
||||
validPath := regexp.MustCompile(fmt.Sprintf(`^/%s/([a-zA-Z0-9\-_\/\.]+)$`, name))
|
||||
|
||||
m := validPath.FindStringSubmatch(r.URL.Path)
|
||||
|
||||
@@ -23,13 +23,7 @@ func makeHandler(name string, fn func(http.ResponseWriter, *http.Request, string
|
||||
return
|
||||
}
|
||||
|
||||
// catch the special period cases that github does not allow for repos
|
||||
if m[2] == "." || m[2] == ".." {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
fn(w, r, m[1], m[2])
|
||||
fn(w, r, m[1])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@
|
||||
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
|
||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
|
||||
<script id="template-alert" type="text/x-handlebars-template">
|
||||
<div class="notification is-primary">
|
||||
<div class="notification is-warning">
|
||||
<button class="delete"></button>
|
||||
{{{message}}}
|
||||
</div>
|
||||
@@ -219,9 +219,10 @@
|
||||
</script>
|
||||
<script id="template-grade" type="text/x-handlebars-template">
|
||||
<div class="column">
|
||||
<h1 class="title">Report for <strong>{{repo}}</strong></h1>
|
||||
<p><span class="huge">{{grade}}</span> {{gradeMessage grade}}    Found <strong>{{issues}}</strong> issues across <strong>{{files}}</strong> files</p>
|
||||
</div>
|
||||
<div class="column badge-col"><img src="http://goreportcard.com/badge/{{repo}}"/></div>
|
||||
<div class="column is-quarter badge-col"><img src="http://goreportcard.com/badge/{{repo}}"/></div>
|
||||
</script>
|
||||
<script id="template-check" type="text/x-handlebars-template">
|
||||
<a class="menu-block" href="#{{{name}}}">
|
||||
@@ -309,7 +310,6 @@
|
||||
var $resultsText = $(".results-text");
|
||||
var $resultsDetails = $('.results-details');
|
||||
$resultsText.html($(templates.grade(data)));
|
||||
|
||||
var $table = $(".results");
|
||||
$table.html('<p class="menu-heading">Results</p>');
|
||||
for (var i = 0; i < checks.length; i++) {
|
||||
|
||||
Reference in New Issue
Block a user