Another attempt at fixing the duplication
This commit is contained in:
parent
60037a3996
commit
85024c71c2
|
@ -39,7 +39,7 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
|
|||
return nil, err
|
||||
}
|
||||
|
||||
changelog, err := GenerateChangelog(a.client, a.config.RepoOwner, a.config.RepoName, last)
|
||||
changelog, err := GenerateChangelog(a.client, a.config.RepoOwner, a.config.RepoName, last, cfg.Categories)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,14 +2,23 @@ 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. note that duplicates might occur if a pull request has multiple labels.
|
||||
func GenerateChangelog(c *gitea.Client, owner string, repo string, lastRelease *gitea.Release) (*Changelog, error) {
|
||||
// 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{
|
||||
|
@ -20,21 +29,21 @@ func GenerateChangelog(c *gitea.Client, owner string, repo string, lastRelease *
|
|||
}
|
||||
|
||||
for _, pr := range prs {
|
||||
// only consider merged pull requests. note that we can't filter by that in the API
|
||||
if pr.HasMerged {
|
||||
// if there was a release, only take into account pull requests that have been merged after that
|
||||
if lastRelease == nil || lastRelease != nil && pr.Merged.After(lastRelease.CreatedAt) {
|
||||
for _, l := range pr.Labels {
|
||||
_, ok := changelogByLabels[l.Name]
|
||||
|
||||
if lastRelease == nil || (lastRelease != nil && pr.Merged.After(lastRelease.CreatedAt)) {
|
||||
addedToChangelog := false
|
||||
for _, prLabel := range pr.Labels {
|
||||
categoryTitle, ok := validLabels[prLabel.Name]
|
||||
if ok {
|
||||
changelogByLabels[l.Name] = append(changelogByLabels[l.Name], pr)
|
||||
} else {
|
||||
changelogByLabels[l.Name] = []*gitea.PullRequest{pr}
|
||||
changelogByLabels[categoryTitle] = append(changelogByLabels[categoryTitle], pr)
|
||||
addedToChangelog = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(pr.Labels) == 0 {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue