From cce78121ffddf3798fb7be50b1d832041beb7065 Mon Sep 17 00:00:00 2001 From: Kemal Zebari Date: Sat, 11 May 2024 16:02:08 -0700 Subject: [PATCH] Have co-author parsing be more robust and some refactoring --- services/repository/contributors_graph.go | 24 ++++++++++--------- .../repository/contributors_graph_test.go | 1 - 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/services/repository/contributors_graph.go b/services/repository/contributors_graph.go index e9e9dd1d2b..c22225f716 100644 --- a/services/repository/contributors_graph.go +++ b/services/repository/contributors_graph.go @@ -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. break } - coAuthorEmail, coAuthorName, err := parseCoAuthorTrailerValue(line) + coAuthorName, coAuthorEmail, err := parseCoAuthorTrailerValue(line) if err != nil { continue } @@ -222,27 +222,29 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int 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) if !strings.HasSuffix(value, ">") { return "", "", errSyntax } - if openEmailBracketIdx := strings.LastIndex(value, "<"); openEmailBracketIdx == -1 { + + closedBracketIdx := len(value) - 1 + openEmailBracketIdx := strings.LastIndex(value, "<") + if openEmailBracketIdx == -1 { return "", "", errSyntax } - parts := strings.Split(value, "<") - if len(parts) < 2 { - return "", "", errSyntax - } - - email = strings.TrimRight(parts[1], ">") + email = value[openEmailBracketIdx+1 : closedBracketIdx] if _, err := mail.ParseAddress(email); err != nil { 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) { diff --git a/services/repository/contributors_graph_test.go b/services/repository/contributors_graph_test.go index 17bbcbf51c..77e412df3e 100644 --- a/services/repository/contributors_graph_test.go +++ b/services/repository/contributors_graph_test.go @@ -88,7 +88,6 @@ func TestRepository_ContributorsGraph(t *testing.T) { }, }, data["total"]) }) - t.Run("generate contributor stats with co-authored commit", func(t *testing.T) { mockCache, err := cache.NewStringCache(setting.Cache{}) assert.NoError(t, err)