2020-10-09 17:47:59 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-10-09 18:26:43 +03:00
|
|
|
"sort"
|
2020-10-09 17:47:59 +03:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/dlorenc/scorecard/checker"
|
|
|
|
"github.com/dlorenc/scorecard/checks"
|
|
|
|
"github.com/dlorenc/scorecard/roundtripper"
|
|
|
|
"github.com/google/go-github/v32/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
var repo = flag.String("repo", "", "url to the repo")
|
|
|
|
var checksToRun = flag.String("checks", "", "specific checks to run, instead of all")
|
|
|
|
|
2020-10-09 18:26:43 +03:00
|
|
|
type result struct {
|
2020-10-13 18:35:55 +03:00
|
|
|
cr checker.CheckResult
|
2020-10-09 18:26:43 +03:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:47:59 +03:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
split := strings.SplitN(*repo, "/", 3)
|
|
|
|
host, owner, repo := split[0], split[1], split[2]
|
|
|
|
|
|
|
|
switch host {
|
|
|
|
case "github.com":
|
|
|
|
default:
|
|
|
|
log.Fatalf("unsupported host: %s", host)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-10-09 18:26:43 +03:00
|
|
|
// Use our custom roundtripper
|
2020-10-09 17:47:59 +03:00
|
|
|
rt := roundtripper.NewTransport(ctx)
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
}
|
|
|
|
ghClient := github.NewClient(client)
|
|
|
|
|
2020-10-13 18:35:55 +03:00
|
|
|
c := checker.Checker{
|
2020-10-09 17:47:59 +03:00
|
|
|
Ctx: ctx,
|
|
|
|
Client: ghClient,
|
|
|
|
HttpClient: client,
|
|
|
|
Owner: owner,
|
|
|
|
Repo: repo,
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:26:43 +03:00
|
|
|
resultsCh := make(chan result)
|
2020-10-09 17:47:59 +03:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for _, check := range checks.AllChecks {
|
|
|
|
check := check
|
|
|
|
wg.Add(1)
|
2020-10-09 18:26:43 +03:00
|
|
|
log.Printf("Starting [%s]\n", check.Name)
|
2020-10-09 17:47:59 +03:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2020-10-13 18:35:55 +03:00
|
|
|
runner := checker.Runner{Checker: c}
|
|
|
|
r := runner.Run(check.Fn)
|
2020-10-09 18:26:43 +03:00
|
|
|
resultsCh <- result{
|
|
|
|
name: check.Name,
|
|
|
|
cr: r,
|
|
|
|
}
|
2020-10-09 17:47:59 +03:00
|
|
|
}()
|
|
|
|
}
|
2020-10-09 18:26:43 +03:00
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(resultsCh)
|
|
|
|
}()
|
2020-10-09 17:47:59 +03:00
|
|
|
|
2020-10-09 18:26:43 +03:00
|
|
|
// Collect results
|
|
|
|
results := []result{}
|
|
|
|
for result := range resultsCh {
|
|
|
|
log.Printf("Finished [%s]\n", result.name)
|
|
|
|
results = append(results, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort them by name
|
|
|
|
sort.Slice(results, func(i, j int) bool {
|
|
|
|
return results[i].name < results[j].name
|
|
|
|
})
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("RESULTS")
|
|
|
|
fmt.Println("-------")
|
|
|
|
for _, r := range results {
|
|
|
|
fmt.Println(r.name, r.cr.Pass, r.cr.Confidence)
|
|
|
|
}
|
2020-10-09 17:47:59 +03:00
|
|
|
}
|