-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.go
More file actions
75 lines (69 loc) · 2.16 KB
/
Copy pathfilter.go
File metadata and controls
75 lines (69 loc) · 2.16 KB
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
package dependents
import "time"
const (
ReasonFork = "fork"
ReasonArchived = "archived"
ReasonMirror = "mirror"
ReasonStale = "stale"
ReasonNotAnalyzed = "not analyzed"
ReasonNoTests = "no tests"
ReasonNoImports = "no upstream references"
)
// FilterOptions lets each consumer choose its repository eligibility policy.
// A zero MaxAge does not filter stale repositories. Missing push dates are
// retained.
type FilterOptions struct {
ExcludeForks bool
ExcludeArchived bool
ExcludeMirrors bool
MaxAge time.Duration
Now time.Time
RequireAnalyzed bool
RequireTests bool
RequireImports bool
}
// Rejection records a candidate excluded by Filter and the first matching
// reason.
type Rejection struct {
Candidate Candidate
Reason string
}
// Filter applies repository health policy without changing candidates.
func Filter(candidates []Candidate, opts FilterOptions) ([]Candidate, []Rejection) {
now := opts.Now
if now.IsZero() {
now = time.Now()
}
kept := make([]Candidate, 0, len(candidates))
rejected := make([]Rejection, 0)
for _, candidate := range candidates {
reason := rejectionReason(candidate, opts, now)
if reason == "" {
kept = append(kept, candidate)
continue
}
rejected = append(rejected, Rejection{Candidate: candidate, Reason: reason})
}
return kept, rejected
}
func rejectionReason(candidate Candidate, opts FilterOptions, now time.Time) string {
metadata := candidate.RepositoryMetadata
switch {
case opts.ExcludeForks && metadata.Fork:
return ReasonFork
case opts.ExcludeArchived && metadata.Archived:
return ReasonArchived
case opts.ExcludeMirrors && (metadata.MirrorURL != "" || metadata.SourceName != ""):
return ReasonMirror
case opts.MaxAge > 0 && !metadata.PushedAt.IsZero() && now.Sub(metadata.PushedAt) > opts.MaxAge:
return ReasonStale
case (opts.RequireAnalyzed || opts.RequireTests || opts.RequireImports) && !candidate.Analyzed:
return ReasonNotAnalyzed
case opts.RequireTests && candidate.Analysis.TestFiles == 0:
return ReasonNoTests
case opts.RequireImports && candidate.Analysis.ImportFiles == 0:
return ReasonNoImports
default:
return ""
}
}