1

add: import code

портирован код из старого репозитория https://di9.ru/git/Voomra/Conventional-Commits
This commit is contained in:
2025-07-30 18:44:50 +03:00
commit af99bebf91
16 changed files with 575 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
package validator
import (
"commitlint/internal/commitlint/configuration"
"regexp"
"slices"
)
type Result int
const (
IncorrectPattern Result = iota
UnknownType
UnknownContext
OverlongLine
BlankLine
)
type ValidateError struct {
// Тип проблемы
Result Result
// Проблемная строка в коммите. Начинается с 1
Line int
// Переданный не известный тип
UnknownType string
// Переданный не известный контекст
UnknownContext string
}
func Validate(commitMessage []string, config configuration.Config) *ValidateError {
if pResult := validateFirstLine(commitMessage[0], config); pResult != nil {
return pResult
}
if len(commitMessage) > 1 {
if pResult := validateBody(commitMessage[1:], config); pResult != nil {
return pResult
}
}
return nil
}
func validateFirstLine(line string, config configuration.Config) *ValidateError {
for _, pExclude := range config.Excludes {
if pExclude.MatchString(line) {
return nil
}
}
commitFirstLine := regexp.MustCompile("^([a-z0-9]+?)!?(?:\\(([a-z0-9]+?)\\))?: .+$")
match := commitFirstLine.FindAllStringSubmatch(line, -1)
if len(match) == 0 {
return &ValidateError{Result: IncorrectPattern, Line: 1}
}
commitType := match[0][1]
if !slices.Contains(config.Types, commitType) {
return &ValidateError{Result: UnknownType, Line: 1, UnknownType: commitType}
}
commitContext := match[0][2]
if len(commitContext) > 0 && !slices.Contains(config.Contexts, commitContext) {
return &ValidateError{Result: UnknownContext, Line: 1, UnknownContext: commitContext}
}
if len(line) > config.MaxLengthLine {
return &ValidateError{Result: OverlongLine, Line: 1}
}
return nil
}
func validateBody(body []string, config configuration.Config) *ValidateError {
if len(body[0]) > 0 {
return &ValidateError{Result: BlankLine, Line: 2}
}
for i, line := range body[1:] {
if len(line) > config.MaxLengthLine {
return &ValidateError{Result: OverlongLine, Line: i + 3}
}
}
return nil
}

View File

@@ -0,0 +1,83 @@
package validator
import (
"commitlint/internal/commitlint/configuration"
"github.com/stretchr/testify/assert"
"regexp"
"testing"
)
func TestValidateFirstLine(t *testing.T) {
pConfig, err := configuration.CreateConfig(nil)
if err != nil {
panic(err)
}
pConfig.Contexts = append(pConfig.Contexts, "git")
pConfig.Excludes = append(pConfig.Excludes, regexp.MustCompile("(?i)^wip$"))
testTab := []struct {
line string
excepted *ValidateError
}{
{
line: "incorrect line",
excepted: &ValidateError{Result: IncorrectPattern, Line: 1},
},
{
line: "zed: message",
excepted: &ValidateError{Result: UnknownType, Line: 1, UnknownType: "zed"},
},
{
line: "fix(zed): message",
excepted: &ValidateError{Result: UnknownContext, Line: 1, UnknownContext: "zed"},
},
{
line: "fix: message",
excepted: nil,
},
{
line: "fix(git): message",
excepted: nil,
},
{
line: "WIP",
excepted: nil,
},
}
for _, test := range testTab {
pResult := validateFirstLine(test.line, *pConfig)
assert.Equal(t, test.excepted, pResult)
}
}
func TestValidateBody(t *testing.T) {
pConfig, err := configuration.CreateConfig(nil)
if err != nil {
panic(err)
}
pConfig.MaxLengthLine = 10
testTab := []struct {
lines []string
excepted *ValidateError
}{
{
lines: []string{" ", "Some Body"},
excepted: &ValidateError{Result: BlankLine, Line: 2},
},
{
lines: []string{"", "Some Body 1234567890"},
excepted: &ValidateError{Result: OverlongLine, Line: 3},
},
{
lines: []string{"", "Some Body"},
excepted: nil,
},
}
for _, test := range testTab {
pResult := validateBody(test.lines, *pConfig)
assert.Equal(t, test.excepted, pResult)
}
}