0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-25 07:30:38 +00:00

Merge pull request #89 from bvieira/#85

Feature: remove carriage return from commit body
This commit is contained in:
Beatriz Vieira 2023-01-22 20:41:45 -03:00 committed by GitHub
commit 4fec01e9b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 19 deletions

View File

@ -15,9 +15,9 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Check out code - name: Check out code
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Run golangci lint - name: Run golangci lint
uses: golangci/golangci-lint-action@v2 uses: golangci/golangci-lint-action@v3
with: with:
version: latest version: latest
@ -26,9 +26,9 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Check out code - name: Check out code
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v2 uses: actions/setup-go@v3
with: with:
go-version: ^1.19 go-version: ^1.19
- name: Build - name: Build
@ -40,7 +40,7 @@ jobs:
needs: [lint, build] needs: [lint, build]
steps: steps:
- name: Check out code - name: Check out code
uses: actions/checkout@v2 uses: actions/checkout@v3
with: with:
fetch-depth: 0 fetch-depth: 0
@ -70,7 +70,7 @@ jobs:
needs: [tag] needs: [tag]
steps: steps:
- name: Check out code - name: Check out code
uses: actions/checkout@v2 uses: actions/checkout@v3
with: with:
fetch-depth: 0 fetch-depth: 0
@ -81,7 +81,7 @@ jobs:
- name: Set up Go - name: Set up Go
id: go id: go
uses: actions/setup-go@v2 uses: actions/setup-go@v3
with: with:
go-version: ^1.19 go-version: ^1.19

View File

@ -83,7 +83,7 @@ func (p MessageProcessorImpl) Validate(message string) error {
subject, body := splitCommitMessageContent(message) subject, body := splitCommitMessageContent(message)
msg, parseErr := p.Parse(subject, body) msg, parseErr := p.Parse(subject, body)
if (parseErr != nil) { if parseErr != nil {
return parseErr return parseErr
} }
@ -205,9 +205,14 @@ func (p MessageProcessorImpl) Format(msg CommitMessage) (string, string, string)
return header.String(), msg.Body, footer.String() return header.String(), msg.Body, footer.String()
} }
func removeCarriage(commit string) string {
return regexp.MustCompile(`\r`).ReplaceAllString(commit, "")
}
// Parse a commit message. // Parse a commit message.
func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error) { func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error) {
preparedSubject, err := p.prepareHeader(subject) preparedSubject, err := p.prepareHeader(subject)
commitBody := removeCarriage(body)
if err != nil { if err != nil {
return CommitMessage{}, err return CommitMessage{}, err
@ -220,14 +225,14 @@ func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error)
if mdCfg.Key != "" { if mdCfg.Key != "" {
prefixes := append([]string{mdCfg.Key}, mdCfg.KeySynonyms...) prefixes := append([]string{mdCfg.Key}, mdCfg.KeySynonyms...)
for _, prefix := range prefixes { for _, prefix := range prefixes {
if tagValue := extractFooterMetadata(prefix, body, mdCfg.UseHash); tagValue != "" { if tagValue := extractFooterMetadata(prefix, commitBody, mdCfg.UseHash); tagValue != "" {
metadata[key] = tagValue metadata[key] = tagValue
break break
} }
} }
} }
} }
if tagValue := extractFooterMetadata(breakingChangeFooterKey, body, false); tagValue != "" { if tagValue := extractFooterMetadata(breakingChangeFooterKey, commitBody, false); tagValue != "" {
metadata[breakingChangeMetadataKey] = tagValue metadata[breakingChangeMetadataKey] = tagValue
hasBreakingChange = true hasBreakingChange = true
} }
@ -236,7 +241,7 @@ func (p MessageProcessorImpl) Parse(subject, body string) (CommitMessage, error)
Type: commitType, Type: commitType,
Scope: scope, Scope: scope,
Description: description, Description: description,
Body: body, Body: commitBody,
IsBreakingChange: hasBreakingChange, IsBreakingChange: hasBreakingChange,
Metadata: metadata, Metadata: metadata,
}, nil }, nil

View File

@ -378,6 +378,9 @@ var completeBody = `some descriptions
jira: JIRA-123 jira: JIRA-123
BREAKING CHANGE: this change breaks everything` BREAKING CHANGE: this change breaks everything`
var bodyWithCarriage = "some description\r\nmore description\r\n\r\njira: JIRA-123\r"
var expectedBodyWithCarriage = "some description\nmore description\n\njira: JIRA-123"
var issueOnlyBody = `some descriptions var issueOnlyBody = `some descriptions
jira: JIRA-456` jira: JIRA-456`
@ -408,11 +411,12 @@ func TestMessageProcessorImpl_Parse(t *testing.T) {
{"breaking change with exclamation mark", ccfg, "feat!: something new", "", CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: "", IsBreakingChange: true, Metadata: map[string]string{}}}, {"breaking change with exclamation mark", ccfg, "feat!: something new", "", CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: "", IsBreakingChange: true, Metadata: map[string]string{}}},
{"hash metadata", ccfg, "feat: something new", hashMetadataBody, CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: hashMetadataBody, IsBreakingChange: false, Metadata: map[string]string{issueMetadataKey: "JIRA-999", "refs": "#123"}}}, {"hash metadata", ccfg, "feat: something new", hashMetadataBody, CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: hashMetadataBody, IsBreakingChange: false, Metadata: map[string]string{issueMetadataKey: "JIRA-999", "refs": "#123"}}},
{"empty issue cfg", ccfgEmptyIssue, "feat: something new", hashMetadataBody, CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: hashMetadataBody, IsBreakingChange: false, Metadata: map[string]string{}}}, {"empty issue cfg", ccfgEmptyIssue, "feat: something new", hashMetadataBody, CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: hashMetadataBody, IsBreakingChange: false, Metadata: map[string]string{}}},
{"carriage return on body", ccfg, "feat: something new", bodyWithCarriage, CommitMessage{Type: "feat", Scope: "", Description: "something new", Body: expectedBodyWithCarriage, IsBreakingChange: false, Metadata: map[string]string{issueMetadataKey: "JIRA-123"}}},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got, err := NewMessageProcessor(tt.cfg, newBranchCfg(false)).Parse(tt.subject, tt.body); !reflect.DeepEqual(got, tt.want) && err == nil { if got, err := NewMessageProcessor(tt.cfg, newBranchCfg(false)).Parse(tt.subject, tt.body); !reflect.DeepEqual(got, tt.want) && err == nil {
t.Errorf("MessageProcessorImpl.Parse() = %v, want %v", got, tt.want) t.Errorf("MessageProcessorImpl.Parse() = [%+v], want [%+v]", got, tt.want)
} }
}) })
} }
@ -551,3 +555,22 @@ func Test_prepareHeader(t *testing.T) {
}) })
} }
} }
func Test_removeCarriage(t *testing.T) {
tests := []struct {
name string
commit string
want string
}{
{"normal string", "normal string", "normal string"},
{"break line", "normal\nstring", "normal\nstring"},
{"carriage return", "normal\r\nstring", "normal\nstring"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := removeCarriage(tt.commit); got != tt.want {
t.Errorf("removeCarriage() = %v, want %v", got, tt.want)
}
})
}
}