forked from tsoding/snitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.go
More file actions
191 lines (158 loc) · 4.32 KB
/
Copy pathtodo.go
File metadata and controls
191 lines (158 loc) · 4.32 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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
// Todo contains information about a TODO in the repo
type Todo struct {
Prefix string
Suffix string
Keyword string
ID *string
Filename string
Line int
Title string
Body []string
}
// LogString formats TODO for compilation logging. Format is
// compatible with Emacs compilation mode, so you can easily jump
// between the todos.
func (todo Todo) LogString() string {
if todo.ID == nil {
return fmt.Sprintf("%s:%d: %s%s: %s",
todo.Filename, todo.Line,
todo.Prefix, todo.Keyword,
todo.Suffix)
}
return fmt.Sprintf("%s:%d: %s%s(%s): %s",
todo.Filename, todo.Line,
todo.Prefix, todo.Keyword,
*todo.ID, todo.Suffix)
}
func (todo Todo) String() string {
if todo.ID == nil {
return fmt.Sprintf("%s%s: %s",
todo.Prefix, todo.Keyword, todo.Suffix)
}
return fmt.Sprintf("%s%s(%s): %s",
todo.Prefix, todo.Keyword, *todo.ID,
todo.Suffix)
}
// ParseBodyLine strips off the prefix of a body line of the TODO
func (todo Todo) ParseBodyLine(line string) *string {
if strings.HasPrefix(line, todo.Prefix) {
bodyLine := strings.TrimPrefix(line, todo.Prefix)
return &bodyLine
}
return nil
}
func (todo Todo) updateToFile(outputFilename string, lineCallback func(int, string) (string, bool)) error {
inputFile, err := os.Open(todo.Filename)
if err != nil {
return err
}
defer inputFile.Close()
outputFile, err := os.Create(outputFilename)
if err != nil {
return err
}
defer func() {
cerr := outputFile.Close()
if cerr != nil {
err = cerr
}
}()
scanner := bufio.NewScanner(inputFile)
lineNumber := 1
for scanner.Scan() {
line := scanner.Text()
replace, remove := lineCallback(lineNumber, line)
if !remove {
fmt.Fprintln(outputFile, replace)
}
lineNumber = lineNumber + 1
}
return err
}
func (todo Todo) updateInPlace(lineCallback func(int, string) (string, bool)) error {
outputFilename := todo.Filename + ".snitch"
err := todo.updateToFile(outputFilename, lineCallback)
if err != nil {
return err
}
err = os.Rename(outputFilename, todo.Filename)
if err != nil {
return err
}
return err
}
// Update updates the file where the Todo is located in-place.
func (todo Todo) Update() error {
return todo.updateInPlace(func(lineNumber int, line string) (string, bool) {
if lineNumber == todo.Line {
return todo.String(), false
}
return line, false
})
}
// Remove removes the Todo from the file where it is located in-place.
func (todo Todo) Remove() error {
// FIXME(#124): Todo.Remove does not remove the body of the TODO
// It should remove both:
// - The TODO itself
// - The body of the TODO
return todo.updateInPlace(func(lineNumber int, line string) (string, bool) {
if todo.Line <= lineNumber && lineNumber <= todo.Line+len(todo.Body) {
return "", true
}
return line, false
})
}
// GitCommit commits the Todo location to the git repo
func (todo Todo) GitCommit(prefix string) error {
// FIXME(#96): there is no way to check that Todo is unreported at compile time
if todo.ID == nil {
panic(fmt.Sprintf("Trying to commit an unreported TODO! %v", todo))
}
if err := LogCommand(exec.Command("git", "add", todo.Filename)).Run(); err != nil {
return err
}
if err := LogCommand(exec.Command("git", "commit", "-m", fmt.Sprintf("%s %s(%s)", prefix, todo.Keyword, *todo.ID))).Run(); err != nil {
return err
}
return nil
}
// RetrieveGithubStatus retrieves the current status of TODOs issue
// from GitHub
func (todo Todo) RetrieveGithubStatus(creds GithubCredentials, repo string) (string, error) {
json, err := creds.QueryGithubAPI(
"GET",
// FIXME(#59): possible GitHub API injection attack
"https://api.github.com/repos/"+repo+"/issues/"+(*todo.ID)[1:],
nil)
if err != nil {
return "", err
}
return json["state"].(string), nil
}
// ReportTodo reports the todo as a Github Issue, updates the file
// where the todo is located and commits the changes to the git repo.
func (todo Todo) ReportTodo(creds GithubCredentials, repo string, body string) (Todo, error) {
json, err := creds.QueryGithubAPI(
"POST",
"https://api.github.com/repos/"+repo+"/issues",
map[string]interface{}{
"title": todo.Title,
"body": body,
})
if err != nil {
return todo, err
}
id := "#" + strconv.Itoa(int(json["number"].(float64)))
todo.ID = &id
return todo, err
}