1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

Have co-author parsing be more robust and some refactoring

This commit is contained in:
Kemal Zebari 2024-05-11 16:02:08 -07:00
parent 9ac9327c57
commit cce78121ff
2 changed files with 13 additions and 12 deletions

View File

@ -159,7 +159,7 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int
// There should be an empty line before we read the commit stats line. // There should be an empty line before we read the commit stats line.
break break
} }
coAuthorEmail, coAuthorName, err := parseCoAuthorTrailerValue(line) coAuthorName, coAuthorEmail, err := parseCoAuthorTrailerValue(line)
if err != nil { if err != nil {
continue continue
} }
@ -222,27 +222,29 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int
var errSyntax = errors.New("syntax error occurred") var errSyntax = errors.New("syntax error occurred")
func parseCoAuthorTrailerValue(value string) (email, name string, err error) { func parseCoAuthorTrailerValue(value string) (name, email string, err error) {
value = strings.TrimSpace(value) value = strings.TrimSpace(value)
if !strings.HasSuffix(value, ">") { if !strings.HasSuffix(value, ">") {
return "", "", errSyntax return "", "", errSyntax
} }
if openEmailBracketIdx := strings.LastIndex(value, "<"); openEmailBracketIdx == -1 {
closedBracketIdx := len(value) - 1
openEmailBracketIdx := strings.LastIndex(value, "<")
if openEmailBracketIdx == -1 {
return "", "", errSyntax return "", "", errSyntax
} }
parts := strings.Split(value, "<") email = value[openEmailBracketIdx+1 : closedBracketIdx]
if len(parts) < 2 {
return "", "", errSyntax
}
email = strings.TrimRight(parts[1], ">")
if _, err := mail.ParseAddress(email); err != nil { if _, err := mail.ParseAddress(email); err != nil {
return "", "", err return "", "", err
} }
name = strings.TrimSpace(parts[0])
return email, name, nil name = strings.TrimSpace(value[:openEmailBracketIdx])
if len(name) == 0 {
return "", "", errSyntax
}
return name, email, nil
} }
func generateContributorStats(genDone chan struct{}, cache cache.StringCache, cacheKey string, repo *repo_model.Repository, revision string) { func generateContributorStats(genDone chan struct{}, cache cache.StringCache, cacheKey string, repo *repo_model.Repository, revision string) {

View File

@ -88,7 +88,6 @@ func TestRepository_ContributorsGraph(t *testing.T) {
}, },
}, data["total"]) }, data["total"])
}) })
t.Run("generate contributor stats with co-authored commit", func(t *testing.T) { t.Run("generate contributor stats with co-authored commit", func(t *testing.T) {
mockCache, err := cache.NewStringCache(setting.Cache{}) mockCache, err := cache.NewStringCache(setting.Cache{})
assert.NoError(t, err) assert.NoError(t, err)