forked from tsoding/snitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
243 lines (200 loc) · 5.21 KB
/
Copy pathproject.go
File metadata and controls
243 lines (200 loc) · 5.21 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io"
"os"
"os/exec"
"regexp"
)
// TransformRule defines a title transformation rule
type TransformRule struct {
Match string
Replace string
}
// Transform applies a title transformation rule
func (transformRule *TransformRule) Transform(title string) string {
matchRegexp := regexp.MustCompile(transformRule.Match)
return string(matchRegexp.ReplaceAll(
[]byte(title), []byte(transformRule.Replace)))
}
// TitleConfig contains project level configuration related to issue titles
type TitleConfig struct {
Transforms []*TransformRule
}
// Transform transforms the suffix into the title
func (titleConfig *TitleConfig) Transform(title string) string {
for _, rule := range titleConfig.Transforms {
title = rule.Transform(title)
}
return title
}
// Project contains the project level configuration
type Project struct {
Title *TitleConfig
Keywords []string
}
func unreportedTodoRegexp(keyword string) string {
return "^(.*)" + regexp.QuoteMeta(keyword) + ": (.*)$"
}
func reportedTodoRegexp(keyword string) string {
return "^(.*)" + regexp.QuoteMeta(keyword) + "\\((.*)\\): (.*)$"
}
func (project Project) lineAsUnreportedTodo(line string) *Todo {
for _, keyword := range project.Keywords {
unreportedTodo := regexp.MustCompile(
unreportedTodoRegexp(keyword))
groups := unreportedTodo.FindStringSubmatch(line)
if groups != nil {
prefix := groups[1]
suffix := groups[2]
title := project.Title.Transform(suffix)
return &Todo{
Prefix: prefix,
Suffix: suffix,
Keyword: keyword,
ID: nil,
Filename: "",
Line: 0,
Title: title,
}
}
}
return nil
}
func (project Project) lineAsReportedTodo(line string) *Todo {
for _, keyword := range project.Keywords {
unreportedTodo := regexp.MustCompile(reportedTodoRegexp(keyword))
groups := unreportedTodo.FindStringSubmatch(line)
if groups != nil {
prefix := groups[1]
suffix := groups[3]
id := groups[2]
title := project.Title.Transform(suffix)
return &Todo{
Prefix: prefix,
Suffix: suffix,
Keyword: keyword,
ID: &id,
Filename: "",
Line: 0,
Title: title,
}
}
}
return nil
}
// LineAsTodo constructs a Todo from a string
func (project Project) LineAsTodo(line string) *Todo {
if todo := project.lineAsUnreportedTodo(line); todo != nil {
return todo
}
if todo := project.lineAsReportedTodo(line); todo != nil {
return todo
}
return nil
}
// WalkTodosOfFile visits all of the TODOs in a particular file
func (project Project) WalkTodosOfFile(path string, visit func(Todo) error) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
reader := bufio.NewReader(file)
var todo *Todo
text, _, err := reader.ReadLine()
for line := 1; err == nil; line = line + 1 {
if todo == nil { // LookingForTodo
todo = project.LineAsTodo(string(text))
if todo != nil { // Switch to CollectingBody
todo.Filename = path
todo.Line = line
}
} else { // CollectingBody
if possibleTodo := project.LineAsTodo(string(text)); possibleTodo != nil {
if err := visit(*todo); err != nil {
return err
}
todo = possibleTodo // Remain in CollectingBody but for the next todo
todo.Filename = path
todo.Line = line
} else if bodyLine := todo.ParseBodyLine(string(text)); bodyLine != nil {
todo.Body = append(todo.Body, *bodyLine)
} else {
if err := visit(*todo); err != nil {
return err
}
todo = nil // Switch to LookingForTodo
}
}
text, _, err = reader.ReadLine()
}
if todo != nil {
if err := visit(*todo); err != nil {
return err
}
todo = nil // Switch to LookingForTodo
}
if err != io.EOF {
return err
}
return nil
}
// WalkTodosOfDir visits all of the TODOs in a particular directory
func (project Project) WalkTodosOfDir(dirpath string, visit func(todo Todo) error) error {
cmd := exec.Command("git", "ls-files", dirpath)
var outb bytes.Buffer
cmd.Stdout = &outb
err := cmd.Run()
if err != nil {
return err
}
scanner := bufio.NewScanner(&outb)
for scanner.Scan() {
filepath := scanner.Text()
stat, err := os.Stat(filepath)
if err != nil {
return err
}
if !stat.IsDir() {
err = project.WalkTodosOfFile(filepath, visit)
if err != nil {
return err
}
} else {
// FIXME(#145): snitch should go inside of git submodules recursively
fmt.Printf("[WARN] `%s` is probably a submodule. Skipping it for now...\n", filepath)
}
}
return err
}
// NewProject constructs the Project from a YAML file
func NewProject(filePath string) (*Project, error) {
project := &Project{
Title: &TitleConfig{
Transforms: []*TransformRule{},
},
Keywords: []string{},
}
// FIXME(#149): snitch does not work without .snitch.yaml
if stat, err := os.Stat(filePath); !os.IsNotExist(err) && !stat.IsDir() {
configFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer configFile.Close()
yamlDecoder := yaml.NewDecoder(configFile)
err = yamlDecoder.Decode(&project)
if err != nil {
return nil, errors.Wrap(err, filePath)
}
}
if len(project.Keywords) == 0 {
project.Keywords = []string{"TODO"}
}
return project, nil
}