|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package release |
| 5 | + |
| 6 | +import ( |
| 7 | + "cmp" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "slices" |
| 11 | + "strings" |
| 12 | + |
| 13 | + issues_model "code.gitea.io/gitea/models/issues" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + user_model "code.gitea.io/gitea/models/user" |
| 16 | + "code.gitea.io/gitea/modules/container" |
| 17 | + "code.gitea.io/gitea/modules/git" |
| 18 | + "code.gitea.io/gitea/modules/util" |
| 19 | +) |
| 20 | + |
| 21 | +// GenerateReleaseNotesOptions describes how to build release notes content. |
| 22 | +type GenerateReleaseNotesOptions struct { |
| 23 | + TagName string |
| 24 | + TagTarget string |
| 25 | + PreviousTag string |
| 26 | +} |
| 27 | + |
| 28 | +// GenerateReleaseNotes builds the markdown snippet for release notes. |
| 29 | +func GenerateReleaseNotes(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts GenerateReleaseNotesOptions) (string, error) { |
| 30 | + headCommit, err := resolveHeadCommit(gitRepo, opts.TagName, opts.TagTarget) |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + |
| 35 | + if opts.PreviousTag == "" { |
| 36 | + // no previous tag, usually due to there is no tag in the repo, use the same content as GitHub |
| 37 | + content := fmt.Sprintf("**Full Changelog**: %s/commits/tag/%s\n", repo.HTMLURL(ctx), util.PathEscapeSegments(opts.TagName)) |
| 38 | + return content, nil |
| 39 | + } |
| 40 | + |
| 41 | + baseCommit, err := gitRepo.GetCommit(opts.PreviousTag) |
| 42 | + if err != nil { |
| 43 | + return "", util.ErrorWrapTranslatable(util.ErrNotExist, "repo.release.generate_notes_tag_not_found", opts.TagName) |
| 44 | + } |
| 45 | + |
| 46 | + commits, err := gitRepo.CommitsBetweenIDs(headCommit.ID.String(), baseCommit.ID.String()) |
| 47 | + if err != nil { |
| 48 | + return "", fmt.Errorf("CommitsBetweenIDs: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + prs, err := collectPullRequestsFromCommits(ctx, repo.ID, commits) |
| 52 | + if err != nil { |
| 53 | + return "", err |
| 54 | + } |
| 55 | + |
| 56 | + contributors, newContributors, err := collectContributors(ctx, repo.ID, prs) |
| 57 | + if err != nil { |
| 58 | + return "", err |
| 59 | + } |
| 60 | + |
| 61 | + content := buildReleaseNotesContent(ctx, repo, opts.TagName, opts.PreviousTag, prs, contributors, newContributors) |
| 62 | + return content, nil |
| 63 | +} |
| 64 | + |
| 65 | +func resolveHeadCommit(gitRepo *git.Repository, tagName, tagTarget string) (*git.Commit, error) { |
| 66 | + ref := tagName |
| 67 | + if !gitRepo.IsTagExist(tagName) { |
| 68 | + ref = tagTarget |
| 69 | + } |
| 70 | + |
| 71 | + commit, err := gitRepo.GetCommit(ref) |
| 72 | + if err != nil { |
| 73 | + return nil, util.ErrorWrapTranslatable(util.ErrNotExist, "repo.release.generate_notes_target_not_found", ref) |
| 74 | + } |
| 75 | + return commit, nil |
| 76 | +} |
| 77 | + |
| 78 | +func collectPullRequestsFromCommits(ctx context.Context, repoID int64, commits []*git.Commit) ([]*issues_model.PullRequest, error) { |
| 79 | + prs := make([]*issues_model.PullRequest, 0, len(commits)) |
| 80 | + |
| 81 | + for _, commit := range commits { |
| 82 | + pr, err := issues_model.GetPullRequestByMergedCommit(ctx, repoID, commit.ID.String()) |
| 83 | + if err != nil { |
| 84 | + if issues_model.IsErrPullRequestNotExist(err) { |
| 85 | + continue |
| 86 | + } |
| 87 | + return nil, fmt.Errorf("GetPullRequestByMergedCommit: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + if err = pr.LoadIssue(ctx); err != nil { |
| 91 | + return nil, fmt.Errorf("LoadIssue: %w", err) |
| 92 | + } |
| 93 | + if err = pr.Issue.LoadAttributes(ctx); err != nil { |
| 94 | + return nil, fmt.Errorf("LoadIssueAttributes: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + prs = append(prs, pr) |
| 98 | + } |
| 99 | + |
| 100 | + slices.SortFunc(prs, func(a, b *issues_model.PullRequest) int { |
| 101 | + if cmpRes := cmp.Compare(b.MergedUnix, a.MergedUnix); cmpRes != 0 { |
| 102 | + return cmpRes |
| 103 | + } |
| 104 | + return cmp.Compare(b.Issue.Index, a.Issue.Index) |
| 105 | + }) |
| 106 | + |
| 107 | + return prs, nil |
| 108 | +} |
| 109 | + |
| 110 | +func buildReleaseNotesContent(ctx context.Context, repo *repo_model.Repository, tagName, baseRef string, prs []*issues_model.PullRequest, contributors []*user_model.User, newContributors []*issues_model.PullRequest) string { |
| 111 | + var builder strings.Builder |
| 112 | + builder.WriteString("## What's Changed\n") |
| 113 | + |
| 114 | + for _, pr := range prs { |
| 115 | + prURL := pr.Issue.HTMLURL(ctx) |
| 116 | + builder.WriteString(fmt.Sprintf("* %s in [#%d](%s)\n", pr.Issue.Title, pr.Issue.Index, prURL)) |
| 117 | + } |
| 118 | + |
| 119 | + builder.WriteString("\n") |
| 120 | + |
| 121 | + if len(contributors) > 0 { |
| 122 | + builder.WriteString("## Contributors\n") |
| 123 | + for _, contributor := range contributors { |
| 124 | + builder.WriteString(fmt.Sprintf("* @%s\n", contributor.Name)) |
| 125 | + } |
| 126 | + builder.WriteString("\n") |
| 127 | + } |
| 128 | + |
| 129 | + if len(newContributors) > 0 { |
| 130 | + builder.WriteString("## New Contributors\n") |
| 131 | + for _, contributor := range newContributors { |
| 132 | + prURL := contributor.Issue.HTMLURL(ctx) |
| 133 | + builder.WriteString(fmt.Sprintf("* @%s made their first contribution in [#%d](%s)\n", contributor.Issue.Poster.Name, contributor.Issue.Index, prURL)) |
| 134 | + } |
| 135 | + builder.WriteString("\n") |
| 136 | + } |
| 137 | + |
| 138 | + builder.WriteString("**Full Changelog**: ") |
| 139 | + compareURL := fmt.Sprintf("%s/compare/%s...%s", repo.HTMLURL(ctx), util.PathEscapeSegments(baseRef), util.PathEscapeSegments(tagName)) |
| 140 | + builder.WriteString(fmt.Sprintf("[%s...%s](%s)", baseRef, tagName, compareURL)) |
| 141 | + builder.WriteByte('\n') |
| 142 | + return builder.String() |
| 143 | +} |
| 144 | + |
| 145 | +func collectContributors(ctx context.Context, repoID int64, prs []*issues_model.PullRequest) ([]*user_model.User, []*issues_model.PullRequest, error) { |
| 146 | + contributors := make([]*user_model.User, 0, len(prs)) |
| 147 | + newContributors := make([]*issues_model.PullRequest, 0, len(prs)) |
| 148 | + seenContributors := container.Set[int64]{} |
| 149 | + seenNew := container.Set[int64]{} |
| 150 | + |
| 151 | + for _, pr := range prs { |
| 152 | + poster := pr.Issue.Poster |
| 153 | + posterID := poster.ID |
| 154 | + |
| 155 | + if posterID == 0 { |
| 156 | + // Migrated PRs may not have a linked local user (PosterID == 0). Skip them for now. |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + if !seenContributors.Contains(posterID) { |
| 161 | + contributors = append(contributors, poster) |
| 162 | + seenContributors.Add(posterID) |
| 163 | + } |
| 164 | + |
| 165 | + if seenNew.Contains(posterID) { |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + isFirst, err := isFirstContribution(ctx, repoID, posterID, pr) |
| 170 | + if err != nil { |
| 171 | + return nil, nil, err |
| 172 | + } |
| 173 | + if isFirst { |
| 174 | + seenNew.Add(posterID) |
| 175 | + newContributors = append(newContributors, pr) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return contributors, newContributors, nil |
| 180 | +} |
| 181 | + |
| 182 | +func isFirstContribution(ctx context.Context, repoID, posterID int64, pr *issues_model.PullRequest) (bool, error) { |
| 183 | + hasMergedBefore, err := issues_model.HasMergedPullRequestInRepoBefore(ctx, repoID, posterID, pr.MergedUnix, pr.ID) |
| 184 | + if err != nil { |
| 185 | + return false, fmt.Errorf("check merged PRs for contributor: %w", err) |
| 186 | + } |
| 187 | + return !hasMergedBefore, nil |
| 188 | +} |
0 commit comments