-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_issues.go
103 lines (82 loc) · 2.15 KB
/
todo_issues.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
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/da4nik/todo_issues/config"
"github.com/da4nik/todo_issues/integrations"
)
var validFileName = regexp.MustCompile(`\.(go|txt)$`)
var todoText = regexp.MustCompile(`(#|\/\/)\s*TODO:\s*(.+)$`)
var issueExists = regexp.MustCompile(`(#|\/\/)\s*TODO:\s*#\S+\s(.+)$`)
func visit(path string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() ||
!validFileName.MatchString(fi.Name()) ||
strings.HasPrefix(path, "vendor/") {
return nil
}
read, err := ioutil.ReadFile(path)
if err != nil {
return err
}
scanner := bufio.NewScanner(bytes.NewReader(read))
contents := make([]string, 0)
lineNumber := 0
for scanner.Scan() {
lineNumber++
// Leaving existing issues as is
if issueExists.MatchString(scanner.Text()) {
contents = append(contents, scanner.Text())
continue
}
matches := todoText.FindStringSubmatch(scanner.Text())
if len(matches) < 3 {
contents = append(contents, scanner.Text())
continue
}
comment := string(matches[1])
title := string(matches[2])
if title == "" {
contents = append(contents, scanner.Text())
continue
}
fmt.Printf("Creating issue: %s\n", title)
issue := integrations.CreateIssue(title, path, lineNumber)
if issue.ID == "" {
return nil
}
lineWithID := fmt.Sprintf("%s TODO: #%s %s", comment, issue.ID, title)
additionText := fmt.Sprintf("%s TODO: #%s %s", comment, issue.ID, issue.IssueLink)
updatedLine := todoText.ReplaceAllString(scanner.Text(), lineWithID)
contents = append(contents, updatedLine)
if config.Additions && issue.IssueLink != "" {
addition := todoText.ReplaceAllString(scanner.Text(), additionText)
contents = append(contents, addition)
}
}
if scannerErr := scanner.Err(); scannerErr != nil {
return scannerErr
}
// Add new line to the EOF
contents = append(contents, "")
err = ioutil.WriteFile(path, []byte(strings.Join(contents, "\n")), fi.Mode().Perm())
if err != nil {
return err
}
return nil
}
func main() {
config.Load()
err := filepath.Walk(".", visit)
if err != nil {
panic(err)
}
}