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

View File

@ -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"
)

View File

@ -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,29 +49,35 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
return nil, nil
}
// render changelog
// 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 {
categorizedPRs[category.Title] = append(categorizedPRs[category.Title], pr)
}
break
}
}
}
// Build the changelog string
var b strings.Builder
b.WriteString("# What's Changed\n\n")
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 _, 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 {
@ -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
}

View File

@ -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()

View File

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

View File

@ -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"
)