/
parser.go
80 lines (72 loc) · 2.15 KB
/
parser.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
package analyzer
import (
"fmt"
"regexp"
"strings"
. "github.com/sebbalex/issue-opener/model"
log "github.com/sirupsen/logrus"
)
// ParseGHComments ...
func ParseGHComments(event *Event, comments []Comment, issueID int) error {
// var messages []Message
for _, c := range comments {
log.Tracef("parsing comment body: %v", c.Body)
m, err := parseBodyComment(c.Body)
if err != nil {
log.Errorf("error parsing comment: %s", err)
return err
}
// messages = append(messages, m)
m.IssueID = issueID
event.Message = append(event.Message, m)
log.Debugf("done appending message: %v", m)
}
return nil
}
func parseValidationErrors(valErrors []string) (Message, error) {
var m Message
var validHeader = regexp.MustCompile(`^###\ [a-zA-Z]+`)
var validMessage = regexp.MustCompile(`^-\ [a-zA-Z]+`)
var validFooter = regexp.MustCompile(`^##\ [a-zA-Z]+`)
for _, line := range valErrors {
switch {
case validHeader.MatchString(line):
m.Header = line
case validMessage.MatchString(line):
m.Message = append(m.Message, line)
case validFooter.MatchString(line):
m.Footer = line
default:
log.Debug("It doesn't match")
}
}
log.Debugf("Message: %v count %d", m, len(m.Message))
return messageToValidationErrors(&m), nil
}
func parseBodyComment(body string) (Message, error) {
// parsing body and extract validation errors
return parseValidationErrors(strings.Split(strings.TrimSuffix(body, "\r\n"), "\r\n"))
}
func joinKeyValueValidationErrors(errors *[]Error) []string {
var out []string
for _, value := range *errors {
out = append(out, fmt.Sprintf("%s %s", value.Key, value.Reason))
}
return out
}
func messageToValidationErrors(m *Message) Message {
var validKey = regexp.MustCompile(`^-\ [a-zA-Z]+\ `)
for _, mess := range m.Message {
var e Error
e.Key = validKey.FindString(mess)
e.Reason = strings.Trim(strings.Replace(mess, e.Key, "", 1), " ")
// normalizing
e.Key = strings.Trim(strings.Replace(e.Key, "- ", "", 1), " ")
m.ValidationErrors = append(m.ValidationErrors, e)
}
log.Debugf("Message: %s count %d", m.ValidationErrors, len(m.Message))
return *m
}
func mergeMessages(messages []Message) error {
return nil
}