From d401e4c9ff14c9e8f294923b5125744d48f004c1 Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Mon, 15 Jan 2024 09:19:21 +0100 Subject: [PATCH 1/3] fix: :bug: attempt to fix the Duplication of PRs in Rrelease Draft --- src/action.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/action.go b/src/action.go index 2e13557..de8d520 100644 --- a/src/action.go +++ b/src/action.go @@ -53,13 +53,19 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea 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 { + for _, pr := range prs { + categorized := false + for _, category := range cfg.Categories { + if !categorized && prHasLabel(prs, category.Labels) { categorizedPRs[category.Title] = append(categorizedPRs[category.Title], pr) + categorized = true + // Break after adding the PR to a category + break } - break + } + if !categorized { + // Add to a default category if not categorized + categorizedPRs["Other Changes"] = append(categorizedPRs["Other Changes"], pr) } } } -- 2.40.1 From 1fbb0eba98d5746ba61e4f92ee65e648a133c23a Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Mon, 15 Jan 2024 09:19:21 +0100 Subject: [PATCH 2/3] fix: :bug: attempt to fix the Duplication of PRs in Rrelease Draft --- src/action.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/action.go b/src/action.go index 2e13557..42b6cb0 100644 --- a/src/action.go +++ b/src/action.go @@ -53,13 +53,21 @@ func updateOrCreateDraftRelease(a *Action, cfg *config.RepoConfig) (*gitea.Relea 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 { + 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) + 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,13 +167,11 @@ 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 - } +func prHasLabel(pr *gitea.PullRequest, labels []string) bool { + for _, prLabel := range pr.Labels { + for _, label := range labels { + if prLabel.Name == label { + return true } } } -- 2.40.1 From 85024c71c201321a443dbd63d4af62bd07fc067c Mon Sep 17 00:00:00 2001 From: Florian Beisel Date: Mon, 15 Jan 2024 10:28:30 +0100 Subject: [PATCH 3/3] Another attempt at fixing the duplication --- src/action.go | 2 +- src/changelog.go | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/action.go b/src/action.go index 42b6cb0..93dd631 100644 --- a/src/action.go +++ b/src/action.go @@ -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 } diff --git a/src/changelog.go b/src/changelog.go index f664cae..3d7d0c6 100644 --- a/src/changelog.go +++ b/src/changelog.go @@ -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) } } -- 2.40.1