-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathproject.go
254 lines (210 loc) · 7.42 KB
/
project.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
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
244
245
246
247
248
249
250
251
252
253
254
package project
import (
"math/rand"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/wakatime/wakatime-cli/pkg/regex"
)
// nolint: gochecknoglobals
var driveLetterRegex = regexp.MustCompile(`^[a-zA-Z]:\\$`)
// maxRecursiveIteration limits the number of a func will be called recursively.
const maxRecursiveIteration = 500
// Detecter is a common interface for project.
type Detecter interface {
Detect() (Result, bool, error)
String() string
}
// Result contains the result of Detect().
type Result struct {
Project string
Branch string
Folder string
}
// Config contains project detection configurations.
type Config struct {
// Patterns contains the overridden project name per path.
MapPatterns []MapPattern
// SubmodulePatterns contains the paths to validate for submodules.
SubmodulePatterns []regex.Regex
// ShouldObfuscateProject determines if the project name should be obfuscated according some rules.
ShouldObfuscateProject bool
}
// MapPattern contains [projectmap] data.
type MapPattern struct {
// Name is the project name.
Name string
// Regex is the regular expression for a specific path.
Regex regex.Regex
}
// WithDetection finds the current project and branch.
// First looks for a .wakatime-project file. Second, uses the --project arg.
// Third, uses the folder name from a revision control repository. Last, uses
// the --alternate-project arg.
func WithDetection(c Config) heartbeat.HandleOption {
return func(next heartbeat.Handle) heartbeat.Handle {
return func(hh []heartbeat.Heartbeat) ([]heartbeat.Result, error) {
log.Debugln("execute project detection")
for n, h := range hh {
if h.EntityType != heartbeat.FileType {
project := firstNonEmptyString(h.ProjectOverride, h.ProjectAlternate)
hh[n].Project = &project
continue
}
var result Result
result.Project, result.Branch = Detect(h.Entity, c.MapPatterns)
if result.Project == "" {
result.Project = h.ProjectOverride
}
if result.Project == "" || result.Branch == "" {
revControlResult := DetectWithRevControl(h.Entity, c.SubmodulePatterns, c.ShouldObfuscateProject)
result.Project = firstNonEmptyString(result.Project, revControlResult.Project)
result.Branch = firstNonEmptyString(result.Branch, revControlResult.Branch)
if result.Project == "" {
result.Project = setProjectName(h.ProjectAlternate, c.ShouldObfuscateProject, revControlResult.Folder)
}
}
hh[n].Project = &result.Project
hh[n].Branch = &result.Branch
}
return next(hh)
}
}
}
// Detect finds the current project and branch from config plugins.
func Detect(entity string, patterns []MapPattern) (project, branch string) {
var configPlugins []Detecter = []Detecter{
File{
Filepath: entity,
},
Map{
Filepath: entity,
Patterns: patterns,
},
}
for _, p := range configPlugins {
result, detected, err := p.Detect()
if err != nil {
log.Errorf("unexpected error occurred at %q: %s", p.String(), err)
continue
} else if detected {
return result.Project, result.Branch
}
}
return "", ""
}
// DetectWithRevControl finds the current project and branch from rev control.
func DetectWithRevControl(entity string, submodulePatterns []regex.Regex, shouldObfuscate bool) Result {
var revControlPlugins []Detecter = []Detecter{
Git{
Filepath: entity,
SubmodulePatterns: submodulePatterns,
},
Mercurial{
Filepath: entity,
},
Subversion{
Filepath: entity,
},
Tfvc{
Filepath: entity,
},
}
for _, p := range revControlPlugins {
result, detected, err := p.Detect()
if err != nil {
log.Errorf("unexpected error occurred at %q: %s", p.String(), err)
continue
}
if detected {
result := Result{
Project: result.Project,
Branch: result.Branch,
Folder: result.Folder,
}
if shouldObfuscate {
result.Project = ""
}
return result
}
}
return Result{}
}
func setProjectName(alternate string, shouldObfuscateProject bool, folder string) string {
if !shouldObfuscateProject {
return alternate
}
project := generateProjectName()
err := Write(folder, project)
if err != nil {
log.Warnf("failed to write: %s", err)
}
return project
}
func generateProjectName() string {
adjectives := []string{
"aged", "ancient", "autumn", "billowing", "bitter", "black", "blue", "bold",
"broad", "broken", "calm", "cold", "cool", "crimson", "curly", "damp",
"dark", "dawn", "delicate", "divine", "dry", "empty", "falling", "fancy",
"flat", "floral", "fragrant", "frosty", "gentle", "green", "hidden", "holy",
"icy", "jolly", "late", "lingering", "little", "lively", "long", "lucky",
"misty", "morning", "muddy", "mute", "nameless", "noisy", "odd", "old",
"orange", "patient", "plain", "polished", "proud", "purple", "quiet", "rapid",
"raspy", "red", "restless", "rough", "round", "royal", "shiny", "shrill",
"shy", "silent", "small", "snowy", "soft", "solitary", "sparkling", "spring",
"square", "steep", "still", "summer", "super", "sweet", "throbbing", "tight",
"tiny", "twilight", "wandering", "weathered", "white", "wild", "winter", "wispy",
"withered", "yellow", "young"}
nouns := []string{
"art", "band", "bar", "base", "bird", "block", "boat", "bonus",
"bread", "breeze", "brook", "bush", "butterfly", "cake", "cell", "cherry",
"cloud", "credit", "darkness", "dawn", "dew", "disk", "dream", "dust",
"feather", "field", "fire", "firefly", "flower", "fog", "forest", "frog",
"frost", "glade", "glitter", "grass", "hall", "hat", "haze", "heart",
"hill", "king", "lab", "lake", "leaf", "limit", "math", "meadow",
"mode", "moon", "morning", "mountain", "mouse", "mud", "night", "paper",
"pine", "poetry", "pond", "queen", "rain", "recipe", "resonance", "rice",
"river", "salad", "scene", "sea", "shadow", "shape", "silence", "sky",
"smoke", "snow", "snowflake", "sound", "star", "sun", "sun", "sunset",
"surf", "term", "thunder", "tooth", "tree", "truth", "union", "unit",
"violet", "voice", "water", "waterfall", "wave", "wildflower", "wind", "wood"}
str := []string{}
rand.Seed(time.Now().UnixNano())
str = append(str, strings.Title(adjectives[rand.Intn(len(adjectives))])) //nolint:gosec
rand.Seed(time.Now().UnixNano())
str = append(str, strings.Title(nouns[rand.Intn(len(nouns))])) //nolint:gosec
rand.Seed(time.Now().UnixNano())
str = append(str, strconv.Itoa(rand.Intn(100))) //nolint:gosec
return strings.Join(str, " ")
}
// findFileOrDirectory searches for a file or directory with name `filename`.
// Search starts in `startDir` and will traverse through all parent directories until the file is found,
// root directory is reached or `maxRecursiveIteration` is exceeded.
func findFileOrDirectory(startDir, fileDir, filename string) (string, bool) {
i := 0
for i < maxRecursiveIteration {
if fileExists(filepath.Join(startDir, fileDir, filename)) {
return filepath.Join(startDir, fileDir, filename), true
}
startDir = filepath.Clean(filepath.Join(startDir, ".."))
if startDir == "." || startDir == "/" || driveLetterRegex.MatchString(startDir) {
return "", false
}
i++
}
log.Warnf("didn't find %s after %d iterations", filename, maxRecursiveIteration)
return "", false
}
// firstNonEmptyString accepts multiple values and return the first non empty string value.
func firstNonEmptyString(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}