diff --git a/cmd/generate/codeowners/output.go b/cmd/generate/codeowners/output.go index 0925498..800b103 100644 --- a/cmd/generate/codeowners/output.go +++ b/cmd/generate/codeowners/output.go @@ -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 } diff --git a/cmd/generate/codeowners/output_test.go b/cmd/generate/codeowners/output_test.go index 8703665..9fffde6 100644 --- a/cmd/generate/codeowners/output_test.go +++ b/cmd/generate/codeowners/output_test.go @@ -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 { @@ -27,3 +33,37 @@ func TestCleanFilename(testRunner *testing.T) { }) } } + +func TestGetTopContributorAttributions(testRunner *testing.T) { + configSpec := config.Spec{ + Attributions: map[string][]string{ + "brandonroberts": {"brandon@opensauced.pizza"}, + }, + AttributionFallback: []string{"open-sauced/engineering"}, + } + + var authorStats = AuthorStats{ + "brandon": {GitHubAlias: "brandon", Email: "brandon@opensauced.pizza", Lines: 20}, + "john": {GitHubAlias: "john", Email: "john@opensauced.pizza", 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": {"jpmcb@opensauced.pizza"}, + "brandonroberts": {"brandon@opensauced.pizza"}, + }, + 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") +} diff --git a/pkg/config/spec.go b/pkg/config/spec.go index 22455f1..46b85e3 100644 --- a/pkg/config/spec.go +++ b/pkg/config/spec.go @@ -8,4 +8,8 @@ type Spec struct { // Example: { github_username: [ email1@domain.com, email2@domain.com ]} 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"` }