This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
forked from gojp/goreportcard
-
Notifications
You must be signed in to change notification settings - Fork 2
/
checks.go
178 lines (144 loc) · 4.27 KB
/
checks.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
package handlers
import (
"encoding/json"
"fmt"
"log"
"time"
"github.com/dgraph-io/badger"
"github.com/dustin/go-humanize"
"github.com/gojp/goreportcard/check"
"github.com/gojp/goreportcard/download"
)
type notFoundError struct {
repo string
}
func (n notFoundError) Error() string {
return fmt.Sprintf("%q not found in cache", n.repo)
}
func dirName(repo string) string {
return fmt.Sprintf("_repos/src/%s", repo)
}
func getFromCache(db *badger.DB, repo string) (checksResp, error) {
// try and fetch from badger
resp := checksResp{}
err := db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(RepoPrefix + repo))
if err != nil && err != badger.ErrKeyNotFound {
return err
}
if item != nil {
log.Printf("repo %q found in badger cache", repo)
err = item.Value(func(val []byte) error {
err = json.Unmarshal(val, &resp)
if err != nil {
return fmt.Errorf("failed to parse JSON for %q in cache", repo)
}
return nil
})
}
if item == nil {
log.Println("Repo not found in badger")
return notFoundError{repo}
}
return nil
})
if err != nil {
return resp, err
}
resp.LastRefresh = resp.LastRefresh.UTC()
resp.LastRefreshFormatted = resp.LastRefresh.Format(time.UnixDate)
resp.LastRefreshHumanized = humanize.Time(resp.LastRefresh.UTC())
return resp, nil
}
type checksResp struct {
Checks []check.Score `json:"checks"`
Average float64 `json:"average"`
Grade check.Grade `json:"grade"`
Files int `json:"files"`
Issues int `json:"issues"`
Repo string `json:"repo"`
ResolvedRepo string `json:"resolvedRepo"`
LastRefresh time.Time `json:"last_refresh"`
LastRefreshFormatted string `json:"formatted_last_refresh"`
LastRefreshHumanized string `json:"humanized_last_refresh"`
}
func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, error) {
if !forceRefresh {
resp, err := getFromCache(db, repo)
if err != nil {
// just log the error and continue
log.Println(err)
} else {
resp.Grade = check.GradeFromPercentage(resp.Average * 100) // grade is not stored for some repos, yet
return resp, nil
}
}
// fetch the repo and grade it
repoRoot, err := download.Download(repo, "_repos/src")
if err != nil {
return checksResp{}, fmt.Errorf("could not clone repo: %v", err)
}
repo = repoRoot.Root
checkResult, err := check.Run(dirName(repo))
if err != nil {
return checksResp{}, err
}
t := time.Now().UTC()
resp := checksResp{
Checks: checkResult.Checks,
Average: checkResult.Average,
Grade: checkResult.Grade,
Files: checkResult.Files,
Issues: checkResult.Issues,
Repo: repo,
ResolvedRepo: repoRoot.Repo,
LastRefresh: t,
LastRefreshFormatted: t.Format(time.UnixDate),
LastRefreshHumanized: humanize.Time(t),
}
badgeCache.Store(repo, checkResult.Grade)
respBytes, err := json.Marshal(resp)
if err != nil {
return checksResp{}, fmt.Errorf("could not marshal json: %v", err)
}
// is this a new repo? if so, increase the count in the high scores bucket later
isNewRepo := false
var oldRepoBytes []byte
err = db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(RepoPrefix + repo))
if err != nil {
return err
}
err = item.Value(func(val []byte) error {
oldRepoBytes = val
return nil
})
return err
})
if err != nil && err != badger.ErrKeyNotFound {
log.Println("ERROR getting repo badger:", err)
}
isNewRepo = oldRepoBytes == nil
// if this is a new repo, or the user force-refreshed, update the cache
if isNewRepo || forceRefresh {
err = db.Update(func(txn *badger.Txn) error {
log.Printf("Saving repo %q to cache...", repo)
// save repo to cache
err = txn.Set([]byte(RepoPrefix+repo), respBytes)
if err != nil {
return err
}
return updateMetadata(txn, resp, repo, isNewRepo)
})
if err != nil {
log.Println("Badger writing error:", err)
}
}
err = db.Update(func(txn *badger.Txn) error {
return updateRecentlyViewed(txn, repo)
})
if err != nil {
log.Printf("ERROR: could not update recently viewed: %v", err)
}
return resp, nil
}