use our libraries and include pr categories

This commit is contained in:
Florian Beisel 2024-01-14 10:52:48 +01:00
parent 73b432ec85
commit 56650ff20f
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
6 changed files with 48 additions and 23 deletions

2
go.mod
View File

@ -1,4 +1,4 @@
module git.andinfinity.de/gitea-release-drafter module git.beisel.it/florian/gitea-release-drafter
go 1.19 go 1.19

View File

@ -3,8 +3,8 @@ package main
import ( import (
"context" "context"
"git.andinfinity.de/gitea-release-drafter/src" "git.beisel.it/florian/gitea-release-drafter/src"
"git.andinfinity.de/gitea-release-drafter/src/config" "git.beisel.it/florian/gitea-release-drafter/src/config"
githubactions "github.com/sethvargo/go-githubactions" githubactions "github.com/sethvargo/go-githubactions"
) )

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"code.gitea.io/sdk/gitea" "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" "github.com/sethvargo/go-githubactions"
) )
@ -49,30 +49,36 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
return nil, nil return nil, nil
} }
// render changelog // Create a map to hold categorized PRs
var b strings.Builder categorizedPRs := make(map[string][]*gitea.PullRequest)
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))
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 { 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)
} }
break
b.WriteString("\n")
} }
} }
} }
// 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) nextVersion, err := ResolveVersion(cfg, last, changelog)
if err != nil { if err != nil {
return nil, err return nil, err
@ -152,3 +158,16 @@ func (a *Action) Run() error {
return nil 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
}

View File

@ -20,6 +20,11 @@ type DrafterConfig struct {
ConfigPath string 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 // NewFromInputs creates a new drafter config by using the action inputs and the github context
func NewFromInputs(action *githubactions.Action) (*DrafterConfig, error) { func NewFromInputs(action *githubactions.Action) (*DrafterConfig, error) {
actionCtx, err := action.Context() actionCtx, err := action.Context()

View File

@ -14,7 +14,8 @@ type RepoConfig struct {
// NameTemplate template for the release name // NameTemplate template for the release name
NameTemplate string `mapstructure:"name-template"` NameTemplate string `mapstructure:"name-template"`
// TagTemplate template for the release tag // TagTemplate template for the release tag
TagTemplate string `mapstructure:"tag-template"` TagTemplate string `mapstructure:"tag-template"`
Categories []Category `yaml:"categories"`
VersionResolver struct { VersionResolver struct {
Major struct { Major struct {
Labels []string Labels []string

View File

@ -2,7 +2,7 @@ package src
import ( import (
"code.gitea.io/sdk/gitea" "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" "github.com/Masterminds/semver"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
) )