From 0495544b8a6dd90746fa032f470f803c461d273c Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 7 Oct 2022 18:59:42 +0800 Subject: [PATCH] Tag list should include draft releases with existing tags (#21263) (#21365) Backport #21263. Before, a tag for a draft release disappeared in the tag list, fix #21262. Co-authored-by: wxiaoguang --- models/release.go | 8 ++++++++ modules/context/repo.go | 4 +++- routers/web/repo/release.go | 14 +++++++++++--- templates/repo/release/list.tmpl | 6 ++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/models/release.go b/models/release.go index 94e803c716..1ee57b2f56 100644 --- a/models/release.go +++ b/models/release.go @@ -170,6 +170,7 @@ type FindReleasesOptions struct { IsPreRelease util.OptionalBool IsDraft util.OptionalBool TagNames []string + HasSha1 util.OptionalBool // useful to find draft releases which are created with existing tags } func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { @@ -191,6 +192,13 @@ func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond { if !opts.IsDraft.IsNone() { cond = cond.And(builder.Eq{"is_draft": opts.IsDraft.IsTrue()}) } + if !opts.HasSha1.IsNone() { + if opts.HasSha1.IsTrue() { + cond = cond.And(builder.Neq{"sha1": ""}) + } else { + cond = cond.And(builder.Eq{"sha1": ""}) + } + } return cond } diff --git a/modules/context/repo.go b/modules/context/repo.go index 4041b416ee..8829bb7b1c 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -524,7 +524,9 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { } ctx.Data["NumTags"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ - IncludeTags: true, + IncludeDrafts: true, + IncludeTags: true, + HasSha1: util.OptionalBoolTrue, // only draft releases which are created with existing tags }) if err != nil { ctx.ServerError("GetReleaseCountByRepoID", err) diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index df953fd0b9..927853ea5f 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -116,9 +116,17 @@ func releasesOrTags(ctx *context.Context, isTagList bool) { ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived opts := models.FindReleasesOptions{ - ListOptions: listOptions, - IncludeDrafts: writeAccess && !isTagList, - IncludeTags: isTagList, + ListOptions: listOptions, + } + if isTagList { + // for the tags list page, show all releases with real tags (having real commit-id), + // the drafts should also be included because a real tag might be used as a draft. + opts.IncludeDrafts = true + opts.IncludeTags = true + opts.HasSha1 = util.OptionalBoolTrue + } else { + // only show draft releases for users who can write, read-only users shouldn't see draft releases. + opts.IncludeDrafts = writeAccess } releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, opts) diff --git a/templates/repo/release/list.tmpl b/templates/repo/release/list.tmpl index 57c7bc0c92..66cb951d56 100644 --- a/templates/repo/release/list.tmpl +++ b/templates/repo/release/list.tmpl @@ -77,14 +77,12 @@ {{$.i18n.Tr "repo.release.stable"}} {{end}} - {{svg "octicon-tag" 16 "mr-2"}}{{.TagName}} + {{svg "octicon-tag" 16 "mr-2"}}{{.TagName}} - {{if not .IsDraft}} + {{if .Sha1}} {{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Sha1}} - {{end}} - {{if .Sha1 }} {{template "repo/branch_dropdown" dict "root" $ "release" .}} {{end}} {{end}}