fix: 🐛 fix the Duplication of PRs in Rrelease Draft

This commit is contained in:
Florian Beisel 2024-01-15 09:19:21 +01:00
parent 56650ff20f
commit 74ea3835ba
Signed by: florian
GPG Key ID: 79ECA2E54996FF4D
2 changed files with 43 additions and 25 deletions

View File

@ -39,7 +39,7 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
return nil, err 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 { if err != nil {
return nil, err return nil, err
} }
@ -53,13 +53,21 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea
categorizedPRs := make(map[string][]*gitea.PullRequest) categorizedPRs := make(map[string][]*gitea.PullRequest)
for _, prs := range *changelog { 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 {
categorized := false
for _, category := range cfg.Categories {
if !categorized && prHasLabel(pr, category.Labels) {
categorizedPRs[category.Title] = append(categorizedPRs[category.Title], pr) categorizedPRs[category.Title] = append(categorizedPRs[category.Title], pr)
categorized = true
break // Break out of the category loop
} }
break }
if !categorized {
// Add to a default category if not categorized
categorizedPRs["Other Changes"] = append(categorizedPRs["Other Changes"], pr)
}
if categorized {
break // Break out of the PR loop once categorized
} }
} }
} }
@ -159,8 +167,7 @@ func (a *Action) Run() error {
return nil return nil
} }
func prHasLabel(prs []*gitea.PullRequest, labels []string) bool { func prHasLabel(pr *gitea.PullRequest, labels []string) bool {
for _, pr := range prs {
for _, prLabel := range pr.Labels { for _, prLabel := range pr.Labels {
for _, label := range labels { for _, label := range labels {
if prLabel.Name == label { if prLabel.Name == label {
@ -168,6 +175,5 @@ func prHasLabel(prs []*gitea.PullRequest, labels []string) bool {
} }
} }
} }
}
return false return false
} }

View File

@ -2,14 +2,23 @@ package src
import ( import (
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"git.beisel.it/florian/gitea-release-drafter/src/config"
"github.com/sethvargo/go-githubactions" "github.com/sethvargo/go-githubactions"
) )
type Changelog map[string][]*gitea.PullRequest 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. // 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) (*Changelog, error) { func GenerateChangelog(c *gitea.Client, owner string, repo string, lastRelease *gitea.Release, categories []config.Category) (*Changelog, error) {
changelogByLabels := make(Changelog) 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 // FIXME: use pagination
prs, _, err := c.ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{ prs, _, err := c.ListRepoPullRequests(owner, repo, gitea.ListPullRequestsOptions{
@ -20,21 +29,24 @@ func GenerateChangelog(c *gitea.Client, owner string, repo string, lastRelease *
} }
for _, pr := range prs { for _, pr := range prs {
// only consider merged pull requests. note that we can't filter by that in the API
if pr.HasMerged { if pr.HasMerged {
// if there was a release, only take into account pull requests that have been merged after that githubactions.Warningf("Analyzing PR: '%s'", pr.Title)
if lastRelease == nil || lastRelease != nil && pr.Merged.After(lastRelease.CreatedAt) { if lastRelease == nil || (lastRelease != nil && pr.Merged.After(lastRelease.CreatedAt)) {
for _, l := range pr.Labels { githubactions.Warningf("PR is in the valid timeframe since last release: '%s'", pr.Title)
_, ok := changelogByLabels[l.Name] 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 { if ok {
changelogByLabels[l.Name] = append(changelogByLabels[l.Name], pr) changelogByLabels[categoryTitle] = append(changelogByLabels[categoryTitle], pr)
} else { addedToChangelog = true
changelogByLabels[l.Name] = []*gitea.PullRequest{pr} 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) githubactions.Warningf("PR #%d doesn't have any labels", pr.ID)
} }
} }