2020-08-28 01:57:55 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
2020-09-01 01:28:54 +00:00
|
|
|
"bufio"
|
2020-08-28 01:57:55 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-02-14 04:04:32 +00:00
|
|
|
const (
|
2021-02-15 03:05:43 +00:00
|
|
|
breakingChangeFooterKey = "BREAKING CHANGE"
|
|
|
|
breakingChangeMetadataKey = "breaking-change"
|
|
|
|
issueMetadataKey = "issue"
|
2021-02-14 04:04:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CommitMessage is a message using conventional commits.
|
|
|
|
type CommitMessage struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
Scope string `json:"scope,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
IsBreakingChange bool `json:"isBreakingChange,omitempty"`
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// NewCommitMessage commit message constructor.
|
2021-02-14 04:04:32 +00:00
|
|
|
func NewCommitMessage(ctype, scope, description, body, issue, breakingChanges string) CommitMessage {
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
if issue != "" {
|
2021-02-15 03:05:43 +00:00
|
|
|
metadata[issueMetadataKey] = issue
|
2021-02-14 04:04:32 +00:00
|
|
|
}
|
|
|
|
if breakingChanges != "" {
|
2021-02-15 03:05:43 +00:00
|
|
|
metadata[breakingChangeMetadataKey] = breakingChanges
|
2021-02-14 04:04:32 +00:00
|
|
|
}
|
|
|
|
return CommitMessage{Type: ctype, Scope: scope, Description: description, Body: body, IsBreakingChange: breakingChanges != "", Metadata: metadata}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue return issue from metadata.
|
|
|
|
func (m CommitMessage) Issue() string {
|
2021-02-15 03:05:43 +00:00
|
|
|
return m.Metadata[issueMetadataKey]
|
2021-02-14 04:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BreakingMessage return breaking change message from metadata.
|
|
|
|
func (m CommitMessage) BreakingMessage() string {
|
2021-02-15 03:05:43 +00:00
|
|
|
return m.Metadata[breakingChangeMetadataKey]
|
2021-02-14 04:04:32 +00:00
|
|
|
}
|
2020-12-02 02:15:51 +00:00
|
|
|
|
2020-12-02 02:52:15 +00:00
|
|
|
// MessageProcessor interface.
|
|
|
|
type MessageProcessor interface {
|
2021-03-04 03:42:51 +00:00
|
|
|
SkipBranch(branch string, detached bool) bool
|
2020-08-28 01:57:55 +00:00
|
|
|
Validate(message string) error
|
2021-07-31 03:47:58 +00:00
|
|
|
ValidateType(ctype string) error
|
|
|
|
ValidateScope(scope string) error
|
|
|
|
ValidateDescription(description string) error
|
2020-08-28 01:57:55 +00:00
|
|
|
Enhance(branch string, message string) (string, error)
|
2020-12-02 02:15:51 +00:00
|
|
|
IssueID(branch string) (string, error)
|
2021-02-14 04:04:32 +00:00
|
|
|
Format(msg CommitMessage) (string, string, string)
|
|
|
|
Parse(subject, body string) CommitMessage
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// NewMessageProcessor MessageProcessorImpl constructor.
|
2021-02-14 04:48:11 +00:00
|
|
|
func NewMessageProcessor(mcfg CommitMessageConfig, bcfg BranchesConfig) *MessageProcessorImpl {
|
2020-12-02 02:52:15 +00:00
|
|
|
return &MessageProcessorImpl{
|
2021-02-14 04:48:11 +00:00
|
|
|
messageCfg: mcfg,
|
|
|
|
branchesCfg: bcfg,
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 02:52:15 +00:00
|
|
|
// MessageProcessorImpl process validate message hook.
|
|
|
|
type MessageProcessorImpl struct {
|
2021-02-14 04:48:11 +00:00
|
|
|
messageCfg CommitMessageConfig
|
|
|
|
branchesCfg BranchesConfig
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SkipBranch check if branch should be ignored.
|
2021-03-04 03:42:51 +00:00
|
|
|
func (p MessageProcessorImpl) SkipBranch(branch string, detached bool) bool {
|
|
|
|
return contains(branch, p.branchesCfg.Skip) || (p.branchesCfg.SkipDetached != nil && *p.branchesCfg.SkipDetached && detached)
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate commit message.
|
2020-12-02 02:52:15 +00:00
|
|
|
func (p MessageProcessorImpl) Validate(message string) error {
|
2021-02-15 02:17:04 +00:00
|
|
|
subject, body := splitCommitMessageContent(message)
|
|
|
|
msg := p.Parse(subject, body)
|
|
|
|
|
|
|
|
if !regexp.MustCompile("^[a-z+]+(\\(.+\\))?!?: .+$").MatchString(subject) {
|
2021-02-26 01:40:07 +00:00
|
|
|
return fmt.Errorf("subject [%s] should be valid according with conventional commits", subject)
|
2021-02-15 02:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 03:47:58 +00:00
|
|
|
if err := p.ValidateType(msg.Type); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.ValidateScope(msg.Scope); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.ValidateDescription(msg.Description); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p MessageProcessorImpl) ValidateType(ctype string) error {
|
|
|
|
if ctype == "" || !contains(ctype, p.messageCfg.Types) {
|
2021-02-15 02:17:04 +00:00
|
|
|
return fmt.Errorf("message type should be one of [%v]", strings.Join(p.messageCfg.Types, ", "))
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
2021-07-31 03:47:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-15 02:17:04 +00:00
|
|
|
|
2021-07-31 03:47:58 +00:00
|
|
|
func (p MessageProcessorImpl) ValidateScope(scope string) error {
|
|
|
|
if len(p.messageCfg.Scope.Values) > 0 && !contains(scope, p.messageCfg.Scope.Values) {
|
2021-02-15 02:17:04 +00:00
|
|
|
return fmt.Errorf("message scope should one of [%v]", strings.Join(p.messageCfg.Scope.Values, ", "))
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
2021-07-31 03:47:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-02-15 02:17:04 +00:00
|
|
|
|
2021-07-31 03:47:58 +00:00
|
|
|
func (p MessageProcessorImpl) ValidateDescription(description string) error {
|
|
|
|
if !regexp.MustCompile("^[a-z]+.*$").MatchString(description) {
|
|
|
|
return fmt.Errorf("description [%s] should begins with lowercase letter", description)
|
|
|
|
}
|
2020-08-28 01:57:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enhance add metadata on commit message.
|
2020-12-02 02:52:15 +00:00
|
|
|
func (p MessageProcessorImpl) Enhance(branch string, message string) (string, error) {
|
2021-02-15 06:23:02 +00:00
|
|
|
if p.branchesCfg.DisableIssue || p.messageCfg.IssueFooterConfig().Key == "" || hasIssueID(message, p.messageCfg.IssueFooterConfig()) {
|
2021-07-31 19:03:58 +00:00
|
|
|
return "", nil // enhance disabled
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:15:51 +00:00
|
|
|
issue, err := p.IssueID(branch)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if issue == "" {
|
|
|
|
return "", fmt.Errorf("could not find issue id using configured regex")
|
|
|
|
}
|
|
|
|
|
2021-07-18 19:20:38 +00:00
|
|
|
footer := formatIssueFooter(p.messageCfg.IssueFooterConfig(), issue)
|
2021-02-15 03:05:43 +00:00
|
|
|
if !hasFooter(message) {
|
2020-12-02 02:15:51 +00:00
|
|
|
return "\n" + footer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return footer, nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 19:20:38 +00:00
|
|
|
func formatIssueFooter(cfg CommitMessageFooterConfig, issue string) string {
|
2021-07-18 20:18:00 +00:00
|
|
|
if !strings.HasPrefix(issue, cfg.AddValuePrefix) {
|
|
|
|
issue = cfg.AddValuePrefix + issue
|
|
|
|
}
|
2021-07-18 19:20:38 +00:00
|
|
|
if cfg.UseHash {
|
|
|
|
return fmt.Sprintf("%s #%s", cfg.Key, strings.TrimPrefix(issue, "#"))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s: %s", cfg.Key, issue)
|
|
|
|
}
|
|
|
|
|
2021-02-14 04:07:07 +00:00
|
|
|
// IssueID try to extract issue id from branch, return empty if not found.
|
2020-12-02 02:52:15 +00:00
|
|
|
func (p MessageProcessorImpl) IssueID(branch string) (string, error) {
|
2021-03-12 02:13:56 +00:00
|
|
|
if p.branchesCfg.DisableIssue || p.messageCfg.Issue.Regex == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:39:38 +00:00
|
|
|
rstr := fmt.Sprintf("^%s(%s)%s$", p.branchesCfg.Prefix, p.messageCfg.Issue.Regex, p.branchesCfg.Suffix)
|
2021-02-14 04:48:11 +00:00
|
|
|
r, err := regexp.Compile(rstr)
|
2020-09-01 01:28:54 +00:00
|
|
|
if err != nil {
|
2021-02-14 04:48:11 +00:00
|
|
|
return "", fmt.Errorf("could not compile issue regex: %s, error: %v", rstr, err.Error())
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
groups := r.FindStringSubmatch(branch)
|
|
|
|
if len(groups) != 4 {
|
2020-12-02 02:15:51 +00:00
|
|
|
return "", nil
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
2020-12-02 02:15:51 +00:00
|
|
|
return groups[2], nil
|
|
|
|
}
|
2020-09-01 01:28:54 +00:00
|
|
|
|
2021-02-14 04:07:07 +00:00
|
|
|
// Format a commit message returning header, body and footer.
|
2021-02-14 04:04:32 +00:00
|
|
|
func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string) {
|
2020-12-02 02:15:51 +00:00
|
|
|
var header strings.Builder
|
2021-02-14 04:04:32 +00:00
|
|
|
header.WriteString(msg.Type)
|
|
|
|
if msg.Scope != "" {
|
|
|
|
header.WriteString("(" + msg.Scope + ")")
|
2020-12-02 02:15:51 +00:00
|
|
|
}
|
|
|
|
header.WriteString(": ")
|
2021-02-14 04:04:32 +00:00
|
|
|
header.WriteString(msg.Description)
|
2020-09-01 01:28:54 +00:00
|
|
|
|
2020-12-02 02:15:51 +00:00
|
|
|
var footer strings.Builder
|
2021-02-14 04:04:32 +00:00
|
|
|
if msg.BreakingMessage() != "" {
|
2021-02-15 03:05:43 +00:00
|
|
|
footer.WriteString(fmt.Sprintf("%s: %s", breakingChangeFooterKey, msg.BreakingMessage()))
|
2020-12-02 02:15:51 +00:00
|
|
|
}
|
2021-03-12 02:13:56 +00:00
|
|
|
if issue, exists := msg.Metadata[issueMetadataKey]; exists && p.messageCfg.IssueFooterConfig().Key != "" {
|
2020-12-02 02:15:51 +00:00
|
|
|
if footer.Len() > 0 {
|
|
|
|
footer.WriteString("\n")
|
|
|
|
}
|
2021-07-18 19:20:38 +00:00
|
|
|
footer.WriteString(formatIssueFooter(p.messageCfg.IssueFooterConfig(), issue))
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 04:04:32 +00:00
|
|
|
return header.String(), msg.Body, footer.String()
|
|
|
|
}
|
|
|
|
|
2021-02-14 04:07:07 +00:00
|
|
|
// Parse a commit message.
|
2021-02-14 04:04:32 +00:00
|
|
|
func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage {
|
|
|
|
commitType, scope, description, hasBreakingChange := parseSubjectMessage(subject)
|
|
|
|
|
|
|
|
metadata := make(map[string]string)
|
2021-02-14 04:48:11 +00:00
|
|
|
for key, mdCfg := range p.messageCfg.Footer {
|
2021-03-12 02:38:49 +00:00
|
|
|
if mdCfg.Key != "" {
|
|
|
|
prefixes := append([]string{mdCfg.Key}, mdCfg.KeySynonyms...)
|
|
|
|
for _, prefix := range prefixes {
|
|
|
|
if tagValue := extractFooterMetadata(prefix, body, mdCfg.UseHash); tagValue != "" {
|
|
|
|
metadata[key] = tagValue
|
|
|
|
break
|
|
|
|
}
|
2021-02-14 04:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:05:43 +00:00
|
|
|
if tagValue := extractFooterMetadata(breakingChangeFooterKey, body, false); tagValue != "" {
|
|
|
|
metadata[breakingChangeMetadataKey] = tagValue
|
2021-02-14 04:04:32 +00:00
|
|
|
hasBreakingChange = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommitMessage{
|
|
|
|
Type: commitType,
|
|
|
|
Scope: scope,
|
|
|
|
Description: description,
|
|
|
|
Body: body,
|
|
|
|
IsBreakingChange: hasBreakingChange,
|
|
|
|
Metadata: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSubjectMessage(message string) (string, string, string, bool) {
|
|
|
|
regex := regexp.MustCompile("([a-z]+)(\\((.*)\\))?(!)?: (.*)")
|
|
|
|
result := regex.FindStringSubmatch(message)
|
|
|
|
if len(result) != 6 {
|
|
|
|
return "", "", message, false
|
|
|
|
}
|
|
|
|
return result[1], result[3], strings.TrimSpace(result[5]), result[4] == "!"
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractFooterMetadata(key, text string, useHash bool) string {
|
|
|
|
var regex *regexp.Regexp
|
|
|
|
if useHash {
|
|
|
|
regex = regexp.MustCompile(key + " (#.*)")
|
|
|
|
} else {
|
|
|
|
regex = regexp.MustCompile(key + ": (.*)")
|
|
|
|
}
|
|
|
|
|
|
|
|
result := regex.FindStringSubmatch(text)
|
|
|
|
if len(result) < 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return result[1]
|
2020-09-01 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:05:43 +00:00
|
|
|
func hasFooter(message string) bool {
|
|
|
|
r := regexp.MustCompile("^[a-zA-Z-]+: .*|^[a-zA-Z-]+ #.*|^" + breakingChangeFooterKey + ": .*")
|
2020-09-01 01:28:54 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(message))
|
|
|
|
lines := 0
|
|
|
|
for scanner.Scan() {
|
|
|
|
if lines > 0 && r.MatchString(scanner.Text()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
lines++
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-15 06:23:02 +00:00
|
|
|
func hasIssueID(message string, issueConfig CommitMessageFooterConfig) bool {
|
|
|
|
var r *regexp.Regexp
|
|
|
|
if issueConfig.UseHash {
|
|
|
|
r = regexp.MustCompile(fmt.Sprintf("(?m)^%s #.+$", issueConfig.Key))
|
|
|
|
} else {
|
|
|
|
r = regexp.MustCompile(fmt.Sprintf("(?m)^%s: .+$", issueConfig.Key))
|
|
|
|
}
|
2020-09-01 01:28:54 +00:00
|
|
|
return r.MatchString(message)
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func contains(value string, content []string) bool {
|
|
|
|
for _, v := range content {
|
|
|
|
if value == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-15 02:17:04 +00:00
|
|
|
func splitCommitMessageContent(content string) (string, string) {
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(content))
|
|
|
|
|
|
|
|
scanner.Scan()
|
|
|
|
subject := scanner.Text()
|
|
|
|
|
|
|
|
var body strings.Builder
|
|
|
|
first := true
|
|
|
|
for scanner.Scan() {
|
|
|
|
if !first {
|
|
|
|
body.WriteString("\n")
|
|
|
|
}
|
|
|
|
body.WriteString(scanner.Text())
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return subject, body.String()
|
2020-08-28 01:57:55 +00:00
|
|
|
}
|