diff --git a/go.mod b/go.mod index 6e9cc65..789ae68 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.andinfinity.de/gitea-release-drafter +module git.beisel.it/florian/gitea-release-drafter go 1.19 diff --git a/main.go b/main.go index 4f5438b..528618d 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( "context" - "git.andinfinity.de/gitea-release-drafter/src" - "git.andinfinity.de/gitea-release-drafter/src/config" + "git.beisel.it/florian/gitea-release-drafter/src" + "git.beisel.it/florian/gitea-release-drafter/src/config" githubactions "github.com/sethvargo/go-githubactions" ) diff --git a/src/action.go b/src/action.go index cff446b..2e13557 100644 --- a/src/action.go +++ b/src/action.go @@ -7,7 +7,7 @@ import ( "strings" "code.gitea.io/sdk/gitea" - "git.andinfinity.de/gitea-release-drafter/src/config" + "git.beisel.it/florian/gitea-release-drafter/src/config" "github.com/sethvargo/go-githubactions" ) @@ -49,30 +49,36 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea return nil, nil } - // render changelog - var b strings.Builder - - b.WriteString("# What's Changed") - b.WriteString("\n\n") - - // TODO: group by given label categories in config - // default to dumping everything by date desc. - - if changelog != nil { - for label, prs := range *changelog { - if len(prs) > 0 { - // TODO: here we should take the label from the config and only default to the name - fmt.Fprintf(&b, "## %s\n\n", strings.Title(label)) + // Create a map to hold categorized PRs + categorizedPRs := make(map[string][]*gitea.PullRequest) + for _, prs := range *changelog { + for _, category := range cfg.Categories { + if prHasLabel(prs, category.Labels) { + // Correctly append each PR in the slice for _, pr := range prs { - fmt.Fprintf(&b, "* %s (#%d) @%s\n", pr.Title, pr.Index, pr.Poster.UserName) + categorizedPRs[category.Title] = append(categorizedPRs[category.Title], pr) } - - b.WriteString("\n") + break } } } + // Build the changelog string + var b strings.Builder + b.WriteString("# What's Changed\n\n") + + for _, category := range cfg.Categories { + prs, exists := categorizedPRs[category.Title] + if exists && len(prs) > 0 { + fmt.Fprintf(&b, "## %s\n\n", category.Title) + for _, pr := range prs { + fmt.Fprintf(&b, "* %s (#%d) @%s\n", pr.Title, pr.Index, pr.Poster.UserName) + } + b.WriteString("\n") + } + } + nextVersion, err := ResolveVersion(cfg, last, changelog) if err != nil { return nil, err @@ -152,3 +158,16 @@ func (a *Action) Run() error { return nil } + +func prHasLabel(prs []*gitea.PullRequest, labels []string) bool { + for _, pr := range prs { + for _, prLabel := range pr.Labels { + for _, label := range labels { + if prLabel.Name == label { + return true + } + } + } + } + return false +} diff --git a/src/config/action.go b/src/config/action.go index e5827c1..ec899b2 100644 --- a/src/config/action.go +++ b/src/config/action.go @@ -20,6 +20,11 @@ type DrafterConfig struct { ConfigPath string } +type Category struct { + Title string `yaml:"title"` + Labels []string `yaml:"labels"` +} + // NewFromInputs creates a new drafter config by using the action inputs and the github context func NewFromInputs(action *githubactions.Action) (*DrafterConfig, error) { actionCtx, err := action.Context() diff --git a/src/config/repo.go b/src/config/repo.go index 5c5d0b6..9322862 100644 --- a/src/config/repo.go +++ b/src/config/repo.go @@ -14,7 +14,8 @@ type RepoConfig struct { // NameTemplate template for the release name NameTemplate string `mapstructure:"name-template"` // TagTemplate template for the release tag - TagTemplate string `mapstructure:"tag-template"` + TagTemplate string `mapstructure:"tag-template"` + Categories []Category `yaml:"categories"` VersionResolver struct { Major struct { Labels []string diff --git a/src/version.go b/src/version.go index 50251a7..22913ef 100644 --- a/src/version.go +++ b/src/version.go @@ -2,7 +2,7 @@ package src import ( "code.gitea.io/sdk/gitea" - "git.andinfinity.de/gitea-release-drafter/src/config" + "git.beisel.it/florian/gitea-release-drafter/src/config" "github.com/Masterminds/semver" "golang.org/x/exp/slices" )