Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions gh2changelog/gh2changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -23,11 +24,12 @@ type releaseNoteGenerator interface {

// GH2Changelog is to output changelogs
type GH2Changelog struct {
gitPath string
repoPath string
tagPrefix string
changelogMdPath string
releaseYamlPath *string
gitPath string
repoPath string
tagPrefix string
changelogMdPath string
releaseYamlPath *string
filteredMajorVersion *uint64

owner, repo, remoteName string
outStream, errStream io.Writer
Expand Down Expand Up @@ -67,6 +69,9 @@ func New(ctx context.Context, opts ...Option) (*GH2Changelog, error) {
TagPrefix: gch.tagPrefix,
}).VersionStrings()
}
if gch.filteredMajorVersion != nil {
gch.semvers = filterByMajorVersion(gch.semvers, gch.tagPrefix, *gch.filteredMajorVersion)
}

var err error
gch.remoteName, err = gch.detectRemote()
Expand Down Expand Up @@ -274,6 +279,26 @@ func parseGitURL(u string) (*url.URL, error) {

var headBranchReg = regexp.MustCompile(`(?m)^\s*HEAD branch: (.*)$`)

func filterByMajorVersion(vers []string, tagPrefix string, major uint64) []string {
var filtered []string
for _, v := range vers {
s := strings.TrimPrefix(v, tagPrefix)
s = strings.TrimPrefix(s, "v")
parts := strings.SplitN(s, ".", 2)
if len(parts) == 0 {
continue
}
n, err := strconv.ParseUint(parts[0], 10, 64)
if err != nil {
continue
}
if n == major {
filtered = append(filtered, v)
}
}
return filtered
}

func (gch *GH2Changelog) defaultBranch() (string, error) {
// `git symbolic-ref refs/remotes/origin/HEAD` sometimes doesn't work
// So use `git remote show origin` for detecting default branch
Expand Down
6 changes: 6 additions & 0 deletions gh2changelog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ func ReleaseYamlPath(p string) Option {
gch.releaseYamlPath = &p
}
}

func FilteredMajorVersion(v uint64) Option {
return func(gch *GH2Changelog) {
gch.filteredMajorVersion = &v
}
}
3 changes: 3 additions & 0 deletions tagpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,9 @@ func (tp *tagpr) Run(ctx context.Context) error {
if tp.cfg.ReleaseYAMLPath() != "" {
opts = append(opts, gh2changelog.ReleaseYamlPath(tp.cfg.ReleaseYAMLPath()))
}
if fixedMajor, err := tp.cfg.FixedMajorVersion(); err == nil && fixedMajor != nil {
opts = append(opts, gh2changelog.FilteredMajorVersion(*fixedMajor))
}
gch, err := gh2changelog.New(ctx, opts...)
if err != nil {
return err
Expand Down
Loading