Skip to content

Commit

Permalink
feat: support fallback attributions when generating codeowners file (#…
Browse files Browse the repository at this point in the history
…145)

* feat: support fallback attributions when generating codeowners file
  • Loading branch information
brandonroberts authored Sep 5, 2024
1 parent 98c85f0 commit 35af4da
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/generate/codeowners/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ func getTopContributorAttributions(authorStats AuthorStats, n int, config *confi
}
}

if len(topContributors) == 0 {
for _, fallbackAttribution := range config.AttributionFallback {
topContributors = append(topContributors, &CodeownerStat{
GitHubAlias: fallbackAttribution,
})
}
}

return topContributors
}

Expand Down
42 changes: 41 additions & 1 deletion cmd/generate/codeowners/output_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package codeowners

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-sauced/pizza-cli/pkg/config"
)

func TestCleanFilename(testRunner *testing.T) {
var tests = []struct {
Expand All @@ -27,3 +33,37 @@ func TestCleanFilename(testRunner *testing.T) {
})
}
}

func TestGetTopContributorAttributions(testRunner *testing.T) {
configSpec := config.Spec{
Attributions: map[string][]string{
"brandonroberts": {"[email protected]"},
},
AttributionFallback: []string{"open-sauced/engineering"},
}

var authorStats = AuthorStats{
"brandon": {GitHubAlias: "brandon", Email: "[email protected]", Lines: 20},
"john": {GitHubAlias: "john", Email: "[email protected]", Lines: 15},
}

results := getTopContributorAttributions(authorStats, 3, &configSpec)

assert.Equal(testRunner, len(results), 1, "Expected 1 result")
assert.Equal(testRunner, results[0].GitHubAlias, "brandonroberts", "Expected brandonroberts")
}

func TestGetFallbackAttributions(testRunner *testing.T) {
configSpec := config.Spec{
Attributions: map[string][]string{
"jpmcb": {"[email protected]"},
"brandonroberts": {"[email protected]"},
},
AttributionFallback: []string{"open-sauced/engineering"},
}

results := getTopContributorAttributions(AuthorStats{}, 3, &configSpec)

assert.Equal(testRunner, len(results), 1, "Expected 1 result")
assert.Equal(testRunner, results[0].GitHubAlias, "open-sauced/engineering", "Expected open-sauced/engineering")
}
4 changes: 4 additions & 0 deletions pkg/config/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ type Spec struct {
// Example: { github_username: [ [email protected], [email protected] ]} where
// "github_username" has 2 emails attributed to them and their work.
Attributions map[string][]string `yaml:"attribution"`

// AttributionFallback is the default username/group(s) to attribute to the filename
// if no other attributions were found.
AttributionFallback []string `yaml:"attribution-fallback"`
}

0 comments on commit 35af4da

Please sign in to comment.