generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_commit.go
71 lines (56 loc) · 1.86 KB
/
parse_commit.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
package quoad
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
referenceFormatRegex = regexp.MustCompile(`Refs:?[^\r\n]*`)
referenceIDFormatRegex = regexp.MustCompile(`\#([0-9]+)`)
expectedFormatRegex = regexp.MustCompile(`(?s)^(?P<category>\w+?)?(?P<scope>\([^\)]+\))?(?P<breaking>!?)?: (?P<heading>[^\n\r]+)?([\n\r]{2}(?P<body>.*))?`)
)
// GetIssueNumbers converts the matches from the reference regular expression to integers
func GetIssueNumbers(matches []string) []int {
var issueNumbers []int
for _, match := range matches {
for _, refID := range referenceIDFormatRegex.FindAllStringSubmatch(match, -1) {
issueNumber, err := strconv.Atoi(refID[1])
if err != nil {
fmt.Println("couldn't convert reference ID to number")
continue
}
issueNumbers = append(issueNumbers, issueNumber)
}
}
return issueNumbers
}
// ParseCommitMessage creates a slice of Commits that contain information about category and scope parsed from commit message
func ParseCommitMessage(commitMessage string) Commit {
references := referenceFormatRegex.FindAllString(commitMessage, -1)
commitMessage = referenceFormatRegex.ReplaceAllString(commitMessage, "")
match := expectedFormatRegex.FindStringSubmatch(commitMessage)
if len(match) > 0 {
result := make(map[string]string)
for i, name := range expectedFormatRegex.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
category := result["category"]
scope := result["scope"]
heading := result["heading"]
body := result["body"]
scope = strings.Replace(scope, "(", "", 1)
scope = strings.Replace(scope, ")", "", 1)
return Commit{
Category: category,
Heading: heading,
Scope: scope,
Breaking: result["breaking"] == "!",
Body: strings.TrimRight(body, "\r\n\t "),
Issues: GetIssueNumbers(references),
}
}
return Commit{Heading: commitMessage}
}