Skip to content

Commit

Permalink
feat: implement revert parser
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 26, 2020
1 parent 418b8f6 commit 2c800f2
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 10 deletions.
22 changes: 16 additions & 6 deletions 3_transformer/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

type Commit struct {
Hash string
Short string
Message string
Author *object.Signature
Committer *object.Signature
Field *parser.Message
Hash string
Short string
Message string
Author *object.Signature
Committer *object.Signature
Field *parser.Message
RevertCommitHash *string // revert hash
}

// https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit-message-header
Expand Down Expand Up @@ -65,6 +66,10 @@ func Transform(g *client.GitClient, splices []*extractor.ExtractSplice) ([]*Temp
}
}

if field.Revert != nil {
c.RevertCommitHash = field.Revert
}

ctx.Commits = append(ctx.Commits, c)

if field.Header != nil {
Expand Down Expand Up @@ -114,6 +119,11 @@ func Transform(g *client.GitClient, splices []*extractor.ExtractSplice) ([]*Temp
ctx.Test = make([]*Commit, 0)
}
ctx.Test = append(ctx.Test, c)
case "revert":
if ctx.Revert == nil {
ctx.Revert = make([]*Commit, 0)
}
ctx.Revert = append(ctx.Revert, c)
}

}
Expand Down
7 changes: 7 additions & 0 deletions 4_generator/template_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const DEFAULT_TEMPLATE = `
{{ template "body" .Perf }}
{{ end }}
{{if .Revert}}
### 🔙 Revert:
{{range .Revert -}}
- {{if .RevertCommitHash }}revert {{ hashURL .RevertCommitHash }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}})
{{ end }}
{{ end }}
{{if .BreakingChanges}}
### ❤️ BREAKING CHANGES:
{{ range .BreakingChanges -}}
Expand Down
7 changes: 7 additions & 0 deletions 4_generator/template_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ const FULL_TEMPLATE = `# {{ .Version }}
{{ template "body" .Docs }}
{{- end -}}
{{if .Revert}}
### 🔙 Revert:
{{range .Revert -}}
- {{if .RevertCommitHash }}revert {{ hashURL .RevertCommitHash }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}})
{{ end }}
{{ end }}
{{if .BreakingChanges}}
### BREAKING CHANGES:
{{ range .BreakingChanges -}}
Expand Down
30 changes: 26 additions & 4 deletions internal/commit/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ type Message struct {
Header *Header
Body string
Footer *Footer
Revert *string // if it is a revert commit. then this is a hash for that commit
}

var (
headerrPattern = regexp.MustCompile(`^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-]*)\))?\: (.*)$`)
footerPattern = regexp.MustCompile(`^BREAKING\sCHANGE:\s*(.*)$`)
closePattern = regexp.MustCompile(`^Closes\s(.+)$`)
headerrPattern = regexp.MustCompile(`^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-]*)\))?\: (.*)$`)
footerPattern = regexp.MustCompile(`^BREAKING\sCHANGE:\s*(.*)$`)
closePattern = regexp.MustCompile(`^(?i)Closes\s(.+)$`)
revertHeaderPattern = regexp.MustCompile(`^(?i)revert\s(.*)$`)
revertBodyPattern = regexp.MustCompile(`^(?i)This\sreverts\scommit\s(\w+)\.?$`)
)

func (m *Message) String() string {
Expand Down Expand Up @@ -87,9 +90,19 @@ func Parser(message string) *Message {
subject := headerMatchers[4]

m.Header = &Header{}
m.Header.Type = headerType
m.Header.Type = strings.ToLower(headerType)
m.Header.Scope = scope
m.Header.Subject = subject
} else {
revertHeaderMatchers := revertHeaderPattern.FindStringSubmatch(line)
if len(revertHeaderMatchers) != 0 {
subject := strings.Trim(revertHeaderMatchers[1], "\"")
subject = strings.Trim(subject, "'")
m.Header = &Header{
Type: "revert",
Subject: subject,
}
}
}
}

Expand Down Expand Up @@ -143,5 +156,14 @@ func Parser(message string) *Message {
m.Footer.BreakingChange.Content = strings.Trim(breakingChange, "\n")
}

if m.Header != nil && m.Header.Type == "revert" {
revertBodyMatcher := revertBodyPattern.FindStringSubmatch(m.Body)

if len(revertBodyMatcher) != 0 {
revertHash := revertBodyMatcher[1]
m.Revert = &revertHash
}
}

return &m
}
86 changes: 86 additions & 0 deletions internal/commit/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/stretchr/testify/assert"
)

var revertHash = "667ecc1654a317a13331b17617d973392f415f02"

func TestParser(t *testing.T) {
type args struct {
message string
Expand All @@ -15,6 +17,90 @@ func TestParser(t *testing.T) {
args args
want *Message
}{
{
name: "no format",
args: args{message: "hello world"},
want: &Message{
Title: "hello world",
Header: nil,
Body: "",
},
},
{
name: "no format with body",
args: args{message: `hello world
this is body`},
want: &Message{
Title: "hello world",
Header: nil,
Body: "this is body",
},
},
{
name: "revert style 1",
args: args{message: `revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.`},
want: &Message{
Title: "revert: feat(pencil): add 'graphiteWidth' option",
Header: &Header{
Type: "revert",
Scope: "",
Subject: "feat(pencil): add 'graphiteWidth' option",
},
Body: "This reverts commit 667ecc1654a317a13331b17617d973392f415f02.",
Revert: &revertHash,
},
},
{
name: "revert style 2",
args: args{message: `revert test1
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.`},
want: &Message{
Title: "revert test1",
Header: &Header{
Type: "revert",
Scope: "",
Subject: "test1",
},
Body: "This reverts commit 667ecc1654a317a13331b17617d973392f415f02.",
Revert: &revertHash,
},
},
{
name: "revert style 3",
args: args{message: `revert "test1"
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.`},
want: &Message{
Title: `revert "test1"`,
Header: &Header{
Type: "revert",
Scope: "",
Subject: `test1`,
},
Body: "This reverts commit 667ecc1654a317a13331b17617d973392f415f02.",
Revert: &revertHash,
},
},
{
name: "revert style 4",
args: args{message: `Revert "test1"
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.`},
want: &Message{
Title: `Revert "test1"`,
Header: &Header{
Type: "revert",
Scope: "",
Subject: `test1`,
},
Body: "This reverts commit 667ecc1654a317a13331b17617d973392f415f02.",
Revert: &revertHash,
},
},
{
name: "basic",
args: args{message: "feat: add options for function"},
Expand Down

0 comments on commit 2c800f2

Please sign in to comment.