gitea-release-drafter/src/changelog.go

58 lines
1.8 KiB
Go

package src
import (
"code.gitea.io/sdk/gitea"
"git.beisel.it/florian/gitea-release-drafter/src/config"
"github.com/sethvargo/go-githubactions"
)
type Changelog map[string][]*gitea.PullRequest
// GenerateChangelog fetches all the pull requests merged into the default branch since the last release and groups them by label.
func GenerateChangelog(c *gitea.Client, owner string, repo string, lastRelease *gitea.Release, categories []config.Category) (*Changelog, error) {
changelogByLabels := make(Changelog)
validLabels := make(map[string]string)
// Construct a set of valid labels from the categories
for _, category := range categories {
for _, label := range category.Labels {
validLabels[label] = category.Title
}
}
// FIXME: use pagination
prs, _, err := c.ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
State: gitea.StateClosed,
})
if err != nil {
return nil, err
}
for _, pr := range prs {
if pr.HasMerged {
githubactions.Warningf("Analyzing PR: '%s'", pr.Title)
if lastRelease == nil || (lastRelease != nil && pr.Merged.After(lastRelease.CreatedAt)) {
githubactions.Warningf("PR is in the valid timeframe since last release: '%s'", pr.Title)
addedToChangelog := false
for _, prLabel := range pr.Labels {
githubactions.Warningf("Analyzing Label of PR (%d): '%s'", pr.ID, prLabel.Name)
categoryTitle, ok := validLabels[prLabel.Name]
if ok {
changelogByLabels[categoryTitle] = append(changelogByLabels[categoryTitle], pr)
addedToChangelog = true
break
}
}
if !addedToChangelog && len(pr.Labels) > 0 {
githubactions.Warningf("PR #%d has labels but none match configured categories", pr.ID)
} else if len(pr.Labels) == 0 {
githubactions.Warningf("PR #%d doesn't have any labels", pr.ID)
}
}
}
}
return &changelogByLabels, nil
}