-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
300 lines (252 loc) · 7.14 KB
/
parse.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"errors"
"log/slog"
"regexp"
"slices"
"sort"
"strconv"
"strings"
"github.com/cli/go-gh/v2/pkg/repository"
)
type reMatch struct {
LineNumber int
Raw string
}
var (
indicatorRE = regexp.MustCompile(`(?i)<!--\s*chainlink(?:\s*| generated from.*)-->`)
headerRE = regexp.MustCompile(`(?im)^ {0,3}#{1,6}\s.*`)
itemRE = regexp.MustCompile(`(?i)^\s{0,4}(- (?P<Checked>\[[ x]])?|(?P<Numbered>\d+)[.] )(:? *)(?P<Message>.*)`)
ErrNotFound = errors.New("no chainlink list found")
)
func Parse(current ChainIssue, content string) (*Chain, error) {
indicators := findRE(content, indicatorRE)
if len(indicators) == 0 {
return nil, ErrNotFound
}
checklists := findChecklistBlocks(content)
for _, ind := range indicators {
indLineNumber := ind.LineNumber
c := sort.Search(len(checklists), func(i int) bool {
return checklists[i].LineNumbers[0] > indLineNumber
})
if c >= len(checklists) {
slog.Warn("no list found for indicator", "lineNumber", indLineNumber)
continue
}
checklistForIndicator := checklists[c]
return &Chain{
Header: closestValidHeaderTo(content, indLineNumber).Raw,
Source: current,
Current: current,
Items: blockToItems(current, checklistForIndicator),
Raw: checklistForIndicator.Raw,
}, nil
}
return nil, ErrNotFound
}
func FindMatchGroups(re *regexp.Regexp, s string) (map[string]string, bool) {
getNamedMatches := func(re *regexp.Regexp, matches []string) map[string]string {
result := make(map[string]string)
for i, name := range re.SubexpNames() {
if i < len(matches) {
result[name] = matches[i]
}
}
return result
}
matches := re.FindStringSubmatch(s)
return getNamedMatches(re, matches), len(matches) > 0
}
func ReplaceChain(body, chain string) string {
indicators := findRE(body, indicatorRE)
if len(indicators) == 0 {
// not found so append the chain to the current body
return body + "\n" + chain
}
checklists := findChecklistBlocks(body)
for _, ind := range indicators {
indLineNumber := ind.LineNumber
c := sort.Search(len(checklists), func(i int) bool {
return checklists[i].LineNumbers[0] > indLineNumber
})
if c >= len(checklists) {
slog.Warn("no list found for indicator", "lineNumber", indLineNumber)
continue
}
start := indLineNumber
// start from header if it was found in body and replacement chain
if closestValidHeaderTo(chain, findRE(chain, indicatorRE)[0].LineNumber).Raw != "" {
header := closestValidHeaderTo(body, indLineNumber)
if header.Raw != "" {
start = header.LineNumber
}
}
// end at bottom of checklist
checklistForIndicator := checklists[c]
checklistEndLineNumber := checklistForIndicator.LineNumbers[len(checklistForIndicator.LineNumbers)-1]
// remove and insert new checklist
body = removeLines(body, start, checklistEndLineNumber)
return insertLinesAt(body, start, chain)
}
return strings.ReplaceAll(body, indicators[0].Raw, chain)
}
func removeLines(s string, start, end int) string {
lines := strings.Split(s, "\n")
lines = append(lines[:start], lines[end:]...)
return strings.Join(lines, "\n")
}
func insertLinesAt(s string, at int, with string) string {
lines := strings.Split(s, "\n")
withLines := strings.Split(with, "\n")
lines = slicesConcat(lines[:at], withLines, lines[at+1:])
return strings.Join(lines, "\n")
}
func blockToItems(current ChainIssue, b block) (items []ChainItem) {
for _, line := range strings.Split(b.Raw, "\n") {
matches, ok := FindMatchGroups(itemRE, line)
if !ok {
continue
}
message := parseMessage(matches)
issue := issueFromMessage(current.Repo, message)
items = append(items,
ChainItem{
ChainIssue: issue,
IsCurrent: issue == current,
Message: message,
ItemState: parseItemState(matches),
Raw: line,
},
)
}
return
}
func parseItemState(s map[string]string) ItemState {
if checked, ok := s["Checked"]; ok && checked != "" {
isChecked := strings.EqualFold(checked, "[x]")
if isChecked {
return Checked
}
return Unchecked
}
if numbered, ok := s["Numbered"]; ok && numbered != "" {
return Numbered
}
return Bulleted
}
func parseMessage(s map[string]string) string {
trimIndicator := func(str string) string {
return strings.TrimSuffix(str, CurrentIndicator)
}
return strings.TrimSpace(trimIndicator(strings.TrimSpace(s["Message"])))
}
func issueFromMessage(currentRepo repository.Repository, s string) ChainIssue {
issue := issueFromString(s)
if issue.Repo == (repository.Repository{}) {
issue.Repo = currentRepo
}
return issue
}
func issueFromString(s string) ChainIssue {
urlRE := regexp.MustCompile(`(?:https?://(?P<host>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)/(issues|pull)/(?P<number>\d+).*)`)
numberRE := regexp.MustCompile(`(?:#(?P<number>\d+))`)
atoi := func(s string) int {
i, _ := strconv.Atoi(s)
return i
}
s = strings.TrimSpace(s)
if urlMatch, matched := FindMatchGroups(urlRE, s); matched {
return ChainIssue{
Repo: repository.Repository{
Host: urlMatch["host"],
Owner: urlMatch["owner"],
Name: urlMatch["repo"],
},
Number: atoi(urlMatch["number"]),
}
}
if numberMatch, matched := FindMatchGroups(numberRE, s); matched {
return ChainIssue{
Number: atoi(numberMatch["number"]),
}
}
return ChainIssue{}
}
func findRE(content string, re *regexp.Regexp) []reMatch {
var matches []reMatch
lines := strings.Split(content, "\n")
for i, line := range lines {
if re.MatchString(line) {
matches = append(matches, reMatch{LineNumber: i, Raw: line})
}
}
sort.SliceStable(matches, func(i, j int) bool { return j > i })
return matches
}
type block struct {
Raw string
LineNumbers []int
}
func findChecklistBlocks(content string) (blocks []block) {
matches := findRE(content, itemRE)
b := block{}
for _, m := range matches {
// block start
if b.Raw == "" {
b.Raw += m.Raw
b.LineNumbers = append(b.LineNumbers, m.LineNumber)
continue
}
// block continuing
last := b.LineNumbers[len(b.LineNumbers)-1]
if last+1 == m.LineNumber {
b.Raw += "\n" + m.Raw
b.LineNumbers = append(b.LineNumbers, m.LineNumber)
continue
}
// block ended
blocks = append(blocks, b)
b = block{
Raw: m.Raw,
LineNumbers: []int{m.LineNumber},
}
}
if b.Raw != "" {
blocks = append(blocks, b)
}
return blocks
}
func closestValidHeaderTo(content string, indLineNumber int) reMatch {
headers := findRE(content, headerRE)
if len(headers) == 0 {
return reMatch{LineNumber: -1}
}
h := sort.Search(len(headers), func(i int) bool {
return headers[i].LineNumber > indLineNumber
})
closest := headers[h-1]
lines := strings.Split(content, "\n")
for i := closest.LineNumber + 1; i < indLineNumber; i++ {
// check all lines between header and indicator are empty
if len(strings.TrimSpace(lines[i])) > 0 {
return reMatch{LineNumber: -1}
}
}
return closest
}
// Concat returns a new slice concatenating the passed in slices.
func slicesConcat[S ~[]E, E any](ss ...S) S {
size := 0
for _, s := range ss {
size += len(s)
if size < 0 {
panic("len out of range")
}
}
newslice := slices.Grow[S](nil, size)
for _, s := range ss {
newslice = append(newslice, s...)
}
return newslice
}