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
|
|
|
)
|
|
|
|
|
|
|
|
// ReleaseNoteProcessor release note processor interface.
|
|
|
|
type ReleaseNoteProcessor interface {
|
2021-12-29 21:02:50 +00:00
|
|
|
Create(version *semver.Version, tag string, date time.Time, commits []GitCommitLog) ReleaseNote
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseNoteProcessorImpl release note based on commit log.
|
|
|
|
type ReleaseNoteProcessorImpl struct {
|
2021-02-14 05:32:23 +00:00
|
|
|
cfg ReleaseNotesConfig
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReleaseNoteProcessor ReleaseNoteProcessor constructor.
|
2021-02-14 05:32:23 +00:00
|
|
|
func NewReleaseNoteProcessor(cfg ReleaseNotesConfig) *ReleaseNoteProcessorImpl {
|
|
|
|
return &ReleaseNoteProcessorImpl{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.
|
2021-12-29 21:02:50 +00:00
|
|
|
func (p ReleaseNoteProcessorImpl) Create(version *semver.Version, tag string, date time.Time, commits []GitCommitLog) ReleaseNote {
|
2022-02-07 23:40:53 +00:00
|
|
|
mapping := commitSectionMapping(p.cfg.Sections)
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
sections := make(map[string]ReleaseNoteSection)
|
2022-02-07 01:06:46 +00:00
|
|
|
authors := make(map[string]struct{})
|
2019-11-17 16:17:24 +00:00
|
|
|
var breakingChanges []string
|
|
|
|
for _, commit := range commits {
|
2022-02-07 01:06:46 +00:00
|
|
|
authors[commit.AuthorName] = struct{}{}
|
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-07 23:40:53 +00:00
|
|
|
section = ReleaseNoteSection{Name: sectionCfg.Name, Types: sectionCfg.CommitTypes}
|
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
|
|
|
}
|
2021-02-14 02:35:31 +00:00
|
|
|
if commit.Message.BreakingMessage() != "" {
|
|
|
|
// TODO: if no message found, should use description instead?
|
|
|
|
breakingChanges = append(breakingChanges, commit.Message.BreakingMessage())
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 04:47:20 +00:00
|
|
|
var breakingChangeSection BreakingChangeSection
|
2022-02-07 23:40:53 +00:00
|
|
|
if bcCfg := p.cfg.sectionConfig(ReleaseNotesSectionTypeBreakingChange); bcCfg != nil && len(breakingChanges) > 0 {
|
|
|
|
breakingChangeSection = BreakingChangeSection{Name: bcCfg.Name, Messages: breakingChanges}
|
2021-02-15 04:47:20 +00:00
|
|
|
}
|
2022-02-07 01:06:46 +00:00
|
|
|
return ReleaseNote{Version: version, Tag: tag, Date: date.Truncate(time.Minute), Sections: sections, BreakingChanges: breakingChangeSection, AuthorsNames: authors}
|
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)
|
|
|
|
for _, section := range sections {
|
|
|
|
if section.SectionType == ReleaseNotesSectionTypeCommits {
|
|
|
|
for _, commitType := range section.CommitTypes {
|
|
|
|
mapping[commitType] = section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mapping
|
|
|
|
}
|
|
|
|
|
2019-11-17 16:17:24 +00:00
|
|
|
// ReleaseNote release note.
|
|
|
|
type ReleaseNote struct {
|
2021-01-25 20:16:56 +00:00
|
|
|
Version *semver.Version
|
2021-12-29 21:02:50 +00:00
|
|
|
Tag string
|
2019-11-17 16:17:24 +00:00
|
|
|
Date time.Time
|
|
|
|
Sections map[string]ReleaseNoteSection
|
2021-02-15 04:47:20 +00:00
|
|
|
BreakingChanges BreakingChangeSection
|
2022-02-07 01:06:46 +00:00
|
|
|
AuthorsNames map[string]struct{}
|
2021-02-15 04:47:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 19:03:58 +00:00
|
|
|
// BreakingChangeSection breaking change section.
|
2021-02-15 04:47:20 +00:00
|
|
|
type BreakingChangeSection struct {
|
|
|
|
Name string
|
|
|
|
Messages []string
|
2019-11-17 16:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseNoteSection release note section.
|
|
|
|
type ReleaseNoteSection struct {
|
|
|
|
Name string
|
2022-02-07 01:06:46 +00:00
|
|
|
Types []string
|
2019-11-17 16:17:24 +00:00
|
|
|
Items []GitCommitLog
|
|
|
|
}
|