Skip to content

Commit b039909

Browse files
authored
fix(plugins): unify contributor format defaults and add show-name option (#237)
1 parent 5021cd3 commit b039909

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

internal/config/config_plugins.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ type ContributorsConfig struct {
369369
// Icon is the icon/emoji for the contributors section header (optional).
370370
Icon string `yaml:"icon,omitempty"`
371371

372+
// ShowName controls whether contributor names are displayed alongside their username link.
373+
// Default: true. Set to false to show only the username link (e.g., "[@user](...)").
374+
ShowName *bool `yaml:"show-name,omitempty"`
375+
372376
// ShowNewContributors enables the "New Contributors" section showing first-time contributors.
373377
// Default: true when contributors are enabled.
374378
ShowNewContributors *bool `yaml:"show-new-contributors,omitempty"`
@@ -382,6 +386,14 @@ type ContributorsConfig struct {
382386
NewContributorsIcon string `yaml:"new-contributors-icon,omitempty"`
383387
}
384388

389+
// GetShowName returns the show-name setting with default true.
390+
func (c *ContributorsConfig) GetShowName() bool {
391+
if c.ShowName == nil {
392+
return true
393+
}
394+
return *c.ShowName
395+
}
396+
385397
// GetShowNewContributors returns the show-new-contributors setting with default true.
386398
func (c *ContributorsConfig) GetShowNewContributors() bool {
387399
if c.ShowNewContributors == nil {

internal/plugins/changeloggenerator/config.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ const DefaultNewContributorsIcon = "\U0001F389" // party popper 🎉
2727
// DefaultBreakingChangesIcon is the default icon for the breaking changes section.
2828
const DefaultBreakingChangesIcon = "\u26A0\uFE0F" // warning sign ⚠️
2929

30+
// DefaultContributorFormat is the default Go template for contributor entries (with name).
31+
const DefaultContributorFormat = "- {{.Name}} ([@{{.Username}}](https://{{.Host}}/{{.Username}}))"
32+
33+
// DefaultContributorFormatNoName is the Go template for contributor entries without name.
34+
const DefaultContributorFormatNoName = "- [@{{.Username}}](https://{{.Host}}/{{.Username}})"
35+
3036
// Config holds the internal configuration for the changelog generator plugin.
3137
type Config struct {
3238
// Enabled controls whether the plugin is active.
@@ -111,6 +117,7 @@ type ContributorsConfig struct {
111117
Enabled bool
112118
Format string
113119
Icon string
120+
ShowName bool
114121
ShowNewContributors bool
115122
NewContributorsFormat string
116123
NewContributorsIcon string
@@ -132,7 +139,8 @@ func DefaultConfig() *Config {
132139
ExcludePatterns: DefaultExcludePatterns(),
133140
Contributors: &ContributorsConfig{
134141
Enabled: true,
135-
Format: "- [@{{.Username}}](https://{{.Host}}/{{.Username}})",
142+
Format: DefaultContributorFormat,
143+
ShowName: true,
136144
ShowNewContributors: true,
137145
},
138146
}
@@ -263,15 +271,19 @@ func convertContributorsConfig(cfg *config.ChangelogGeneratorConfig) *Contributo
263271
return defaultContributorsConfig(cfg.UseDefaultIcons)
264272
}
265273

274+
showName := cfg.Contributors.GetShowName()
275+
266276
contrib := &ContributorsConfig{
267277
Enabled: cfg.Contributors.Enabled,
268278
Format: cfg.Contributors.Format,
269279
Icon: cfg.Contributors.Icon,
280+
ShowName: showName,
270281
ShowNewContributors: cfg.Contributors.GetShowNewContributors(),
271282
NewContributorsFormat: cfg.Contributors.NewContributorsFormat,
272283
NewContributorsIcon: cfg.Contributors.NewContributorsIcon,
273284
}
274285

286+
applyDefaultContributorFormat(contrib)
275287
applyDefaultContributorIcons(contrib, cfg.UseDefaultIcons)
276288
return contrib
277289
}
@@ -280,7 +292,8 @@ func convertContributorsConfig(cfg *config.ChangelogGeneratorConfig) *Contributo
280292
func defaultContributorsConfig(useDefaultIcons bool) *ContributorsConfig {
281293
contrib := &ContributorsConfig{
282294
Enabled: true,
283-
Format: "- {{.Name}} ([@{{.Username}}](https://{{.Host}}/{{.Username}}))",
295+
Format: DefaultContributorFormat,
296+
ShowName: true,
284297
ShowNewContributors: true,
285298
}
286299
if useDefaultIcons {
@@ -290,6 +303,19 @@ func defaultContributorsConfig(useDefaultIcons bool) *ContributorsConfig {
290303
return contrib
291304
}
292305

306+
// applyDefaultContributorFormat sets the default format when no custom format is specified.
307+
// When ShowName is false and no custom format is set, it uses the no-name format.
308+
func applyDefaultContributorFormat(contrib *ContributorsConfig) {
309+
if contrib.Format != "" {
310+
return
311+
}
312+
if contrib.ShowName {
313+
contrib.Format = DefaultContributorFormat
314+
} else {
315+
contrib.Format = DefaultContributorFormatNoName
316+
}
317+
}
318+
293319
// applyDefaultContributorIcons applies default icons if UseDefaultIcons is enabled.
294320
func applyDefaultContributorIcons(contrib *ContributorsConfig, useDefaultIcons bool) {
295321
if !useDefaultIcons {

internal/plugins/changeloggenerator/generator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ func (g *Generator) writeContributorEntry(sb *strings.Builder, contrib Contribut
206206
// Get format template
207207
format := g.config.Contributors.Format
208208
if format == "" {
209-
format = "- [@{{.Username}}](https://{{.Host}}/{{.Username}})"
209+
if g.config.Contributors.ShowName {
210+
format = DefaultContributorFormat
211+
} else {
212+
format = DefaultContributorFormatNoName
213+
}
210214
}
211215

212216
// Parse template once (thread-safe)

internal/plugins/changeloggenerator/generator_contributors_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestWriteContributorEntry_CustomFormat(t *testing.T) {
8989
format: "",
9090
contrib: Contributor{Name: "Dave", Username: "dave", Host: "github.com"},
9191
remote: &RemoteInfo{Host: "github.com", Owner: "test", Repo: "repo"},
92-
expected: "- [@dave](https://github.com/dave)\n",
92+
expected: "- Dave ([@dave](https://github.com/dave))\n",
9393
},
9494
{
9595
name: "Fallback on invalid template",
@@ -121,6 +121,28 @@ func TestWriteContributorEntry_CustomFormat(t *testing.T) {
121121
}
122122
}
123123

124+
func TestWriteContributorEntry_ShowNameFalse(t *testing.T) {
125+
cfg := DefaultConfig()
126+
cfg.Contributors.Format = "" // no custom format
127+
cfg.Contributors.ShowName = false
128+
g, err := NewGenerator(cfg, NewGitOps())
129+
if err != nil {
130+
t.Fatalf("unexpected error: %v", err)
131+
}
132+
133+
contrib := Contributor{Name: "Alice Smith", Username: "alice", Host: "github.com"}
134+
remote := &RemoteInfo{Host: "github.com", Owner: "test", Repo: "repo"}
135+
136+
var sb strings.Builder
137+
g.writeContributorEntry(&sb, contrib, remote)
138+
got := sb.String()
139+
140+
expected := "- [@alice](https://github.com/alice)\n"
141+
if got != expected {
142+
t.Errorf("writeContributorEntry() with ShowName=false = %q, want %q", got, expected)
143+
}
144+
}
145+
124146
func TestWriteContributorEntry_NoHost(t *testing.T) {
125147

126148
cfg := DefaultConfig()

0 commit comments

Comments
 (0)