-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
119 lines (94 loc) · 2.74 KB
/
processor.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
package gomodstarguard
import (
"fmt"
"go/parser"
"go/token"
"os"
"regexp"
"strings"
)
type Configuration struct {
Warn int `yaml:"warn"`
Error int `yaml:"error"`
Exeptions []struct {
Repository string `yaml:"repository"`
Reason string `yaml:"reason"`
} `yaml:"exeptions"`
}
type Processor struct {
Config *Configuration
Stargazer *Stargazer
}
func NewProcessor(config *Configuration, stargazer *Stargazer) (*Processor, error) {
return &Processor{
Config: config,
Stargazer: stargazer,
}, nil
}
func (p *Processor) ProcessFiles(filenames []string) (issues []Issue) {
for _, filename := range filenames {
data, err := os.ReadFile(filename)
if err != nil {
issues = append(issues, Issue{
FileName: filename,
LineNumber: 0,
Reason: fmt.Sprintf("unable to read file, file cannot be linted (%s)", err.Error()),
})
continue
}
issues = append(issues, p.process(filename, data)...)
}
return issues
}
func (p *Processor) process(filename string, data []byte) (issues []Issue) {
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, filename, data, parser.ParseComments)
if err != nil {
issues = append(issues, Issue{
FileName: filename,
LineNumber: 0,
Reason: fmt.Sprintf("invalid syntax, file cannot be linted (%s)", err.Error()),
})
return issues
}
imports := file.Imports
for n := range imports {
importedPkg := strings.TrimSpace(strings.Trim(imports[n].Path.Value, "\""))
// To check if importedPkg is a valid URL:
if p.isValidURL(importedPkg) {
re := regexp.MustCompile(`^github.com/[^/]+/[^/]+`)
match := re.FindStringSubmatch(importedPkg)
repoBaseURL := match[0]
url := "https://" + repoBaseURL
stars, err := p.Stargazer.GetStars(url)
if err != nil {
issues = append(issues, p.addError(fileSet, imports[n].Pos(), err.Error()))
continue
}
// logger.Println("stars: ", stars)
if stars < p.Config.Error {
reason := fmt.Sprintf("github stars of %s is less than the error threshold (%d)", importedPkg, p.Config.Error)
issues = append(issues, p.addError(fileSet, imports[n].Pos(), reason))
continue
}
if stars < p.Config.Warn {
reason := fmt.Sprintf("github stars of %s is less than the warn threshold (%d)", importedPkg, p.Config.Warn)
issues = append(issues, p.addError(fileSet, imports[n].Pos(), reason))
continue
}
}
}
return issues
}
func (p *Processor) addError(fileset *token.FileSet, pos token.Pos, reason string) Issue {
position := fileset.Position(pos)
return Issue{
FileName: position.Filename,
LineNumber: position.Line,
Position: position,
Reason: reason,
}
}
func (p *Processor) isValidURL(input string) bool {
return strings.HasPrefix(input, "github.com")
}