2019-11-17 16:17:24 +00:00
|
|
|
package sv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-02-13 18:40:09 +00:00
|
|
|
"github.com/Masterminds/semver/v3"
|
2019-11-17 16:17:24 +00:00
|
|
|
)
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// ReleaseNotesConfig release notes preferences.
|
|
|
|
type ReleaseNotesConfig struct {
|
|
|
|
Sections []ReleaseNotesSectionConfig `yaml:"sections"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg ReleaseNotesConfig) sectionConfig(sectionType string) *ReleaseNotesSectionConfig {
|
|
|
|
for _, sectionCfg := range cfg.Sections {
|
|
|
|
if sectionCfg.SectionType == sectionType {
|
|
|
|
return §ionCfg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseNotesSectionConfig preferences for a single section on release notes.
|
|
|
|
type ReleaseNotesSectionConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
SectionType string `yaml:"section-type"`
|
|
|
|
CommitTypes []string `yaml:"commit-types,flow,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ReleaseNotesSectionTypeCommits ReleaseNotesSectionConfig.SectionType value.
|
|
|
|
ReleaseNotesSectionTypeCommits = "commits"
|
|
|
|
// ReleaseNotesSectionTypeBreakingChanges ReleaseNotesSectionConfig.SectionType value.
|
|
|
|
ReleaseNotesSectionTypeBreakingChanges = "breaking-changes"
|
|
|
|
)
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
// ReleaseNoteProcessor release note processor interface.
|
|
|
|
type ReleaseNoteProcessor interface {
|
2023-10-15 19:29:29 +00:00
|
|
|
Create(version *semver.Version, tag string, date time.Time, commits []CommitLog) ReleaseNote
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
// BaseReleaseNoteProcessor release note based on commit log.
|
|
|
|
type BaseReleaseNoteProcessor struct {
|
2021-02-14 05:32:23 +00:00
|
|
|
cfg ReleaseNotesConfig
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReleaseNoteProcessor ReleaseNoteProcessor constructor.
|
2023-10-15 19:29:29 +00:00
|
|
|
func NewReleaseNoteProcessor(cfg ReleaseNotesConfig) *BaseReleaseNoteProcessor {
|
|
|
|
return &BaseReleaseNoteProcessor{cfg: cfg}
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 00:00:53 +00:00
|
|
|
// Create create a release note based on commits.
|
2023-10-15 19:29:29 +00:00
|
|
|
func (p BaseReleaseNoteProcessor) Create(
|
2023-10-12 14:18:25 +00:00
|
|
|
version *semver.Version,
|
|
|
|
tag string,
|
|
|
|
date time.Time,
|
2023-10-15 19:29:29 +00:00
|
|
|
commits []CommitLog,
|
2023-10-12 14:18:25 +00:00
|
|
|
) ReleaseNote {
|
2022-02-07 23:40:53 +00:00
|
|
|
mapping := commitSectionMapping(p.cfg.Sections)
|
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
sections := make(map[string]ReleaseNoteCommitsSection)
|
2022-02-07 01:06:46 +00:00
|
|
|
authors := make(map[string]struct{})
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
var breakingChanges []string
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
for _, commit := range commits {
|
2022-02-07 01:06:46 +00:00
|
|
|
authors[commit.AuthorName] = struct{}{}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 23:40:53 +00:00
|
|
|
if sectionCfg, exists := mapping[commit.Message.Type]; exists {
|
|
|
|
section, sexists := sections[sectionCfg.Name]
|
2019-11-17 16:17:24 +00:00
|
|
|
if !sexists {
|
2022-02-08 01:49:32 +00:00
|
|
|
section = ReleaseNoteCommitsSection{Name: sectionCfg.Name, Types: sectionCfg.CommitTypes}
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
section.Items = append(section.Items, commit)
|
2022-02-07 23:40:53 +00:00
|
|
|
sections[sectionCfg.Name] = section
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2023-10-28 20:25:07 +00:00
|
|
|
if commit.Message.IsBreakingChange {
|
2021-02-14 02:35:31 +00:00
|
|
|
breakingChanges = append(breakingChanges, commit.Message.BreakingMessage())
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
var breakingChangeSection ReleaseNoteBreakingChangeSection
|
|
|
|
if bcCfg := p.cfg.sectionConfig(ReleaseNotesSectionTypeBreakingChanges); bcCfg != nil && len(breakingChanges) > 0 {
|
|
|
|
breakingChangeSection = ReleaseNoteBreakingChangeSection{Name: bcCfg.Name, Messages: breakingChanges}
|
2021-02-15 04:47:20 +00:00
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
|
|
|
return ReleaseNote{
|
|
|
|
Version: version,
|
|
|
|
Tag: tag,
|
|
|
|
Date: date.Truncate(time.Minute),
|
|
|
|
Sections: p.toReleaseNoteSections(sections, breakingChangeSection),
|
|
|
|
AuthorsNames: authors,
|
|
|
|
}
|
2022-02-08 01:49:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 19:29:29 +00:00
|
|
|
func (p BaseReleaseNoteProcessor) toReleaseNoteSections(
|
2023-10-12 14:18:25 +00:00
|
|
|
commitSections map[string]ReleaseNoteCommitsSection,
|
|
|
|
breakingChange ReleaseNoteBreakingChangeSection,
|
|
|
|
) []ReleaseNoteSection {
|
2022-02-08 01:49:32 +00:00
|
|
|
hasBreaking := 0
|
|
|
|
if breakingChange.Name != "" {
|
|
|
|
hasBreaking = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sections := make([]ReleaseNoteSection, len(commitSections)+hasBreaking)
|
|
|
|
i := 0
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
for _, cfg := range p.cfg.Sections {
|
|
|
|
if cfg.SectionType == ReleaseNotesSectionTypeBreakingChanges && hasBreaking > 0 {
|
|
|
|
sections[i] = breakingChange
|
|
|
|
i++
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
if s, exists := commitSections[cfg.Name]; cfg.SectionType == ReleaseNotesSectionTypeCommits && exists {
|
|
|
|
sections[i] = s
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sections
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 23:40:53 +00:00
|
|
|
func commitSectionMapping(sections []ReleaseNotesSectionConfig) map[string]ReleaseNotesSectionConfig {
|
|
|
|
mapping := make(map[string]ReleaseNotesSectionConfig)
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 23:40:53 +00:00
|
|
|
for _, section := range sections {
|
|
|
|
if section.SectionType == ReleaseNotesSectionTypeCommits {
|
|
|
|
for _, commitType := range section.CommitTypes {
|
|
|
|
mapping[commitType] = section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 14:18:25 +00:00
|
|
|
|
2022-02-07 23:40:53 +00:00
|
|
|
return mapping
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
// ReleaseNote release note.
|
|
|
|
type ReleaseNote struct {
|
2022-02-08 01:49:32 +00:00
|
|
|
Version *semver.Version
|
|
|
|
Tag string
|
|
|
|
Date time.Time
|
|
|
|
Sections []ReleaseNoteSection
|
|
|
|
AuthorsNames map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseNoteSection section in release notes.
|
|
|
|
type ReleaseNoteSection interface {
|
|
|
|
SectionType() string
|
|
|
|
SectionName() string
|
2021-02-15 04:47:20 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
// ReleaseNoteBreakingChangeSection breaking change section.
|
|
|
|
type ReleaseNoteBreakingChangeSection struct {
|
2021-02-15 04:47:20 +00:00
|
|
|
Name string
|
|
|
|
Messages []string
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 01:49:32 +00:00
|
|
|
// SectionType section type.
|
|
|
|
func (ReleaseNoteBreakingChangeSection) SectionType() string {
|
|
|
|
return ReleaseNotesSectionTypeBreakingChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionName section name.
|
|
|
|
func (s ReleaseNoteBreakingChangeSection) SectionName() string {
|
|
|
|
return s.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseNoteCommitsSection release note section.
|
|
|
|
type ReleaseNoteCommitsSection struct {
|
2019-11-17 16:17:24 +00:00
|
|
|
Name string
|
2022-02-07 01:06:46 +00:00
|
|
|
Types []string
|
2023-10-15 19:29:29 +00:00
|
|
|
Items []CommitLog
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
2022-02-08 01:49:32 +00:00
|
|
|
|
|
|
|
// SectionType section type.
|
|
|
|
func (ReleaseNoteCommitsSection) SectionType() string {
|
|
|
|
return ReleaseNotesSectionTypeCommits
|
|
|
|
}
|
|
|
|
|
|
|
|
// SectionName section name.
|
|
|
|
func (s ReleaseNoteCommitsSection) SectionName() string {
|
|
|
|
return s.Name
|
|
|
|
}
|
2022-03-01 00:17:37 +00:00
|
|
|
|
|
|
|
// HasMultipleTypes return true if has more than one commit type.
|
|
|
|
func (s ReleaseNoteCommitsSection) HasMultipleTypes() bool {
|
|
|
|
return len(s.Types) > 1
|
|
|
|
}
|