Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
commit-analyzer-cz
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
go-semantic-release
commit-analyzer-cz
Commits
2967329c
Commit
2967329c
authored
1 year ago
by
Christoph Witzko
Browse files
Options
Downloads
Patches
Plain Diff
feat: introduce setTypeAndChange helper
parent
6fdc3bbf
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/analyzer/commit_analyzer.go
+24
-24
24 additions, 24 deletions
pkg/analyzer/commit_analyzer.go
pkg/analyzer/commit_analyzer_test.go
+40
-42
40 additions, 42 deletions
pkg/analyzer/commit_analyzer_test.go
with
64 additions
and
66 deletions
pkg/analyzer/commit_analyzer.go
+
24
−
24
View file @
2967329c
...
...
@@ -23,6 +23,29 @@ func extractMentions(re *regexp.Regexp, s string) string {
return
strings
.
Join
(
ret
,
","
)
}
func
matchesBreakingPattern
(
c
*
semrel
.
Commit
)
bool
{
return
breakingPattern
.
MatchString
(
strings
.
Join
(
c
.
Raw
,
"
\n
"
))
}
func
setTypeAndChange
(
c
*
semrel
.
Commit
)
{
found
:=
commitPattern
.
FindAllStringSubmatch
(
c
.
Raw
[
0
],
-
1
)
if
len
(
found
)
<
1
{
// commit message does not match pattern
return
}
c
.
Type
=
strings
.
ToLower
(
found
[
0
][
1
])
c
.
Scope
=
found
[
0
][
2
]
c
.
Message
=
found
[
0
][
4
]
c
.
Change
=
&
semrel
.
Change
{
// either uses the `!` convention or has a breaking change section
Major
:
found
[
0
][
3
]
==
"!"
||
matchesBreakingPattern
(
c
),
Minor
:
c
.
Type
==
"feat"
,
Patch
:
c
.
Type
==
"fix"
,
}
}
type
DefaultCommitAnalyzer
struct
{}
func
(
da
*
DefaultCommitAnalyzer
)
Init
(
_
map
[
string
]
string
)
error
{
...
...
@@ -47,30 +70,7 @@ func (da *DefaultCommitAnalyzer) analyzeSingleCommit(rawCommit *semrel.RawCommit
c
.
Annotations
[
"mentioned_issues"
]
=
extractMentions
(
mentionedIssuesPattern
,
rawCommit
.
RawMessage
)
c
.
Annotations
[
"mentioned_users"
]
=
extractMentions
(
mentionedUsersPattern
,
rawCommit
.
RawMessage
)
found
:=
commitPattern
.
FindAllStringSubmatch
(
c
.
Raw
[
0
],
-
1
)
if
len
(
found
)
<
1
{
return
c
}
c
.
Type
=
strings
.
ToLower
(
found
[
0
][
1
])
c
.
Scope
=
found
[
0
][
2
]
breakingChange
:=
found
[
0
][
3
]
c
.
Message
=
found
[
0
][
4
]
isMajorChange
:=
breakingPattern
.
MatchString
(
rawCommit
.
RawMessage
)
isMinorChange
:=
c
.
Type
==
"feat"
isPatchChange
:=
c
.
Type
==
"fix"
if
len
(
breakingChange
)
>
0
{
isMajorChange
=
true
isMinorChange
=
false
isPatchChange
=
false
}
c
.
Change
=
&
semrel
.
Change
{
Major
:
isMajorChange
,
Minor
:
isMinorChange
,
Patch
:
isPatchChange
,
}
setTypeAndChange
(
c
)
return
c
}
...
...
This diff is collapsed.
Click to expand it.
pkg/analyzer/commit_analyzer_test.go
+
40
−
42
View file @
2967329c
...
...
@@ -9,18 +9,6 @@ import (
"github.com/stretchr/testify/require"
)
func
compareCommit
(
c
*
semrel
.
Commit
,
t
,
s
string
,
change
*
semrel
.
Change
)
bool
{
if
c
.
Type
!=
t
||
c
.
Scope
!=
s
{
return
false
}
if
c
.
Change
.
Major
!=
change
.
Major
||
c
.
Change
.
Minor
!=
change
.
Minor
||
c
.
Change
.
Patch
!=
change
.
Patch
{
return
false
}
return
true
}
func
createRawCommit
(
sha
,
message
string
)
*
semrel
.
RawCommit
{
return
&
semrel
.
RawCommit
{
SHA
:
sha
,
...
...
@@ -77,13 +65,13 @@ func TestDefaultAnalyzer(t *testing.T) {
createRawCommit
(
"e"
,
"feat!: modified login endpoint"
),
"feat"
,
""
,
&
semrel
.
Change
{
Major
:
true
,
Minor
:
fals
e
,
Patch
:
false
},
&
semrel
.
Change
{
Major
:
true
,
Minor
:
tru
e
,
Patch
:
false
},
},
{
createRawCommit
(
"f"
,
"fix!: fixed a typo"
),
"fix"
,
""
,
&
semrel
.
Change
{
Major
:
true
,
Minor
:
false
,
Patch
:
fals
e
},
&
semrel
.
Change
{
Major
:
true
,
Minor
:
false
,
Patch
:
tru
e
},
},
{
createRawCommit
(
"g"
,
"refactor(parser)!: drop support for Node 6
\n\n
BREAKING CHANGE: refactor to use JavaScript features not available in Node 6."
),
...
...
@@ -103,71 +91,81 @@ func TestDefaultAnalyzer(t *testing.T) {
""
,
&
semrel
.
Change
{
Major
:
false
,
Minor
:
false
,
Patch
:
false
},
},
{
createRawCommit
(
"i"
,
"feat(deps): update deps
\n\n
BREAKING CHANGE: update to new version of dep"
),
"feat"
,
"deps"
,
&
semrel
.
Change
{
Major
:
true
,
Minor
:
true
,
Patch
:
false
},
},
}
defaultAnalyzer
:=
&
DefaultCommitAnalyzer
{}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"AnalyzeCommitMessage: %s"
,
tc
.
RawCommit
.
RawMessage
),
func
(
t
*
testing
.
T
)
{
require
.
True
(
t
,
compareCommit
(
defaultAnalyzer
.
analyzeSingleCommit
(
tc
.
RawCommit
),
tc
.
Type
,
tc
.
Scope
,
tc
.
Change
))
analyzedCommit
:=
defaultAnalyzer
.
analyzeSingleCommit
(
tc
.
RawCommit
)
require
.
Equal
(
t
,
tc
.
Type
,
analyzedCommit
.
Type
,
"Type"
)
require
.
Equal
(
t
,
tc
.
Scope
,
analyzedCommit
.
Scope
,
"Scope"
)
require
.
Equal
(
t
,
tc
.
Change
.
Major
,
analyzedCommit
.
Change
.
Major
,
"Major"
)
require
.
Equal
(
t
,
tc
.
Change
.
Minor
,
analyzedCommit
.
Change
.
Minor
,
"Minor"
)
require
.
Equal
(
t
,
tc
.
Change
.
Patch
,
analyzedCommit
.
Change
.
Patch
,
"Patch"
)
})
}
}
func
TestCommitPattern
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
rawM
essage
string
wanted
[]
string
m
essage
string
wanted
[]
string
}{
{
rawM
essage
:
"feat: new feature"
,
wanted
:
[]
string
{
"feat"
,
""
,
""
,
"new feature"
},
m
essage
:
"feat: new feature"
,
wanted
:
[]
string
{
"feat"
,
""
,
""
,
"new feature"
},
},
{
rawM
essage
:
"feat!: new feature"
,
wanted
:
[]
string
{
"feat"
,
""
,
"!"
,
"new feature"
},
m
essage
:
"feat!: new feature"
,
wanted
:
[]
string
{
"feat"
,
""
,
"!"
,
"new feature"
},
},
{
rawM
essage
:
"feat(api): new feature"
,
wanted
:
[]
string
{
"feat"
,
"api"
,
""
,
"new feature"
},
m
essage
:
"feat(api): new feature"
,
wanted
:
[]
string
{
"feat"
,
"api"
,
""
,
"new feature"
},
},
{
rawM
essage
:
"feat(api): a(b): c:"
,
wanted
:
[]
string
{
"feat"
,
"api"
,
""
,
"a(b): c:"
},
m
essage
:
"feat(api): a(b): c:"
,
wanted
:
[]
string
{
"feat"
,
"api"
,
""
,
"a(b): c:"
},
},
{
rawM
essage
:
"feat(new cool-api): feature"
,
wanted
:
[]
string
{
"feat"
,
"new cool-api"
,
""
,
"feature"
},
m
essage
:
"feat(new cool-api): feature"
,
wanted
:
[]
string
{
"feat"
,
"new cool-api"
,
""
,
"feature"
},
},
{
rawM
essage
:
"feat(😅): cool"
,
wanted
:
[]
string
{
"feat"
,
"😅"
,
""
,
"cool"
},
m
essage
:
"feat(😅): cool"
,
wanted
:
[]
string
{
"feat"
,
"😅"
,
""
,
"cool"
},
},
{
rawM
essage
:
"this-is-also(valid): cool"
,
wanted
:
[]
string
{
"this-is-also"
,
"valid"
,
""
,
"cool"
},
m
essage
:
"this-is-also(valid): cool"
,
wanted
:
[]
string
{
"this-is-also"
,
"valid"
,
""
,
"cool"
},
},
{
rawM
essage
:
"🚀(🦄): emojis!"
,
wanted
:
[]
string
{
"🚀"
,
"🦄"
,
""
,
"emojis!"
},
m
essage
:
"🚀(🦄): emojis!"
,
wanted
:
[]
string
{
"🚀"
,
"🦄"
,
""
,
"emojis!"
},
},
// invalid messages
{
rawM
essage
:
"feat (new api): feature"
,
wanted
:
nil
,
m
essage
:
"feat (new api): feature"
,
wanted
:
nil
,
},
{
rawM
essage
:
"feat((x)): test"
,
wanted
:
nil
,
m
essage
:
"feat((x)): test"
,
wanted
:
nil
,
},
{
rawM
essage
:
"feat:test"
,
wanted
:
nil
,
m
essage
:
"feat:test"
,
wanted
:
nil
,
},
}
for
_
,
tc
:=
range
testCases
{
t
.
Run
(
fmt
.
Sprintf
(
"CommitPattern: %s"
,
tc
.
rawMessage
),
func
(
t
*
testing
.
T
)
{
rawMessageLines
:=
strings
.
Split
(
tc
.
rawMessage
,
"
\n
"
)
found
:=
commitPattern
.
FindAllStringSubmatch
(
rawMessageLines
[
0
],
-
1
)
t
.
Run
(
fmt
.
Sprintf
(
"CommitPattern: %s"
,
tc
.
message
),
func
(
t
*
testing
.
T
)
{
found
:=
commitPattern
.
FindAllStringSubmatch
(
tc
.
message
,
-
1
)
if
len
(
tc
.
wanted
)
==
0
{
require
.
Len
(
t
,
found
,
0
)
return
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment