Skip to content

Commit

Permalink
♻️ Align contributor counting with qodana cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
taport committed Jan 5, 2024
1 parent a5633cd commit b295f23
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
22 changes: 9 additions & 13 deletions core/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,14 @@ var (
)
)

const qodanaBotEmail = "qodana-support@jetbrains.com"

// author struct represents a git commit author.
type author struct {
Email string `json:"email"`
Username string `json:"username"`
}

// getId() returns the author's email if it is not empty, otherwise it returns the username.
func (a *author) getId() string {
if a.Email != "" {
return a.Email
}
return a.Username
}

// isBot returns true if the author is a bot.
func (a *author) isBot() bool {
return strings.HasSuffix(a.Email, cloud.GitHubBotSuffix) || Contains(cloud.CommonGitBots, a.Email)
Expand Down Expand Up @@ -100,6 +94,9 @@ func parseCommits(gitLogOutput []string, excludeBots bool) []commit {
if excludeBots && a.isBot() {
continue
}
if a.Email == qodanaBotEmail {
continue
}
commits = append(commits, commit{
Author: &a,
Date: fields[3],
Expand All @@ -111,17 +108,16 @@ func parseCommits(gitLogOutput []string, excludeBots bool) []commit {

// GetContributors returns the list of contributors of the git repository.
func GetContributors(repoDirs []string, days int, excludeBots bool) []contributor {
contributorMap := make(map[string]*contributor)
contributorMap := make(map[author]*contributor)
for _, repoDir := range repoDirs {
gLog := gitLog(repoDir, gitFormat, days, true)
gLog := gitLog(repoDir, gitFormat, days)
for _, c := range parseCommits(gLog, excludeBots) {
authorId := c.Author.getId()
if i, ok := contributorMap[authorId]; ok {
if i, ok := contributorMap[*c.Author]; ok {
i.Count++
i.Projects = Append(i.Projects, repoDir)
i.Commits = append(i.Commits, c)
} else {
contributorMap[authorId] = &contributor{
contributorMap[*c.Author] = &contributor{
Author: c.Author,
Count: 1,
Projects: []string{repoDir},
Expand Down
30 changes: 22 additions & 8 deletions core/contributors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ func TestGetContributors(t *testing.T) {
if len(contributors) == 0 {
t.Error("Expected at least one contributor or you need to update the test repo")
}
found := false
for _, c := range contributors {
if c.Author.Username == "dependabot[bot]" {
found = true
break
}
}
if !found {

numBotContributors := countContributors(func(c contributor) bool {
return c.Author.Username == "dependabot[bot]"
}, contributors)
if numBotContributors < 1 {
t.Error("Expected dependabot[bot] contributor")
}

numContributorsWithSameEmail := countContributors(func(c contributor) bool {
return c.Author.Email == "dmitry.golovinov@jetbrains.com"
}, contributors)
if numContributorsWithSameEmail < 2 {
t.Error("Expected contributor with same email but different username to be counted multiple times")
}
}

func TestParseCommits(t *testing.T) {
Expand All @@ -58,3 +62,13 @@ func TestParseCommits(t *testing.T) {
t.Errorf("Expected date %s, got %s", expectedDate, commits[1].Date)
}
}

func countContributors(matches func(contributor) bool, contributors []contributor) int {
result := 0
for _, c := range contributors {
if matches(c) {
result += 1
}
}
return result
}
7 changes: 2 additions & 5 deletions core/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,20 @@ func gitOutput(cwd string, args []string) []string {
}

// gitLog returns the git log of the given repository in the given format.
func gitLog(cwd string, format string, since int, mailmap bool) []string {
func gitLog(cwd string, format string, since int) []string {
args := []string{"--no-pager", "log"}
if format != "" {
args = append(args, "--pretty=format:"+format)
}
if since > 0 {
args = append(args, fmt.Sprintf("--since=%d.days", since))
}
if mailmap {
args = append(args, "--mailmap")
}
return gitOutput(cwd, args)
}

// gitRevisions returns the list of commits of the git repository in chronological order.
func gitRevisions(cwd string) []string {
return reverse(gitLog(cwd, "%H", 0, false))
return reverse(gitLog(cwd, "%H", 0))
}

// gitRemoteUrl returns the remote url of the git repository.
Expand Down

0 comments on commit b295f23

Please sign in to comment.