Skip to content

Commit e918636

Browse files
adelowozeripath6543techknowlogick
authored
Enhance release list (#6025)
* show author for releases created outside Gitea UI. Also show the number of commits behind the default branch for tags created outside the UI don't show the tag date again for tags pushed to the repo. Since it is already on the sidebar and looks like duplication * add migration for already existing tags * update as per review * fix build * add space * fix import statments * Update models/migrations/v113.go Co-Authored-By: zeripath <[email protected]> * Update models/migrations/v114.go Co-authored-by: 6543 <[email protected]> * Update services/release/release.go Co-authored-by: 6543 <[email protected]> * impruve * remove dependency on models package * Close the gitrepos in a defer to ensure that they are closed. * gofmt Co-authored-by: zeripath <[email protected]> Co-authored-by: 6543 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 819901b commit e918636

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ var migrations = []Migration{
246246
NewMigration("add timestamps to Star, Label, Follow, Watch and Collaboration", addTimeStamps),
247247
// v155 -> v156
248248
NewMigration("add changed_protected_files column for pull_request table", addChangedProtectedFilesPullRequestColumn),
249+
// v156 -> v157
250+
NewMigration("fix publisher ID for tag releases", fixPublisherIDforTagReleases),
249251
}
250252

251253
// GetCurrentDBVersion returns the current db version

models/migrations/v156.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
"path/filepath"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/setting"
14+
15+
"xorm.io/xorm"
16+
)
17+
18+
// Copy paste from models/repo.go because we cannot import models package
19+
func repoPath(userName, repoName string) string {
20+
return filepath.Join(userPath(userName), strings.ToLower(repoName)+".git")
21+
}
22+
23+
func userPath(userName string) string {
24+
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
25+
}
26+
27+
func fixPublisherIDforTagReleases(x *xorm.Engine) error {
28+
29+
type Release struct {
30+
ID int64
31+
RepoID int64
32+
Sha1 string
33+
TagName string
34+
PublisherID int64
35+
}
36+
37+
type Repository struct {
38+
ID int64
39+
OwnerID int64
40+
Name string
41+
}
42+
43+
type User struct {
44+
ID int64
45+
Name string
46+
Email string
47+
}
48+
49+
const batchSize = 100
50+
sess := x.NewSession()
51+
defer sess.Close()
52+
53+
if err := sess.Begin(); err != nil {
54+
return err
55+
}
56+
57+
var (
58+
gitRepoCache = make(map[int64]*git.Repository)
59+
gitRepo *git.Repository
60+
repoCache = make(map[int64]*Repository)
61+
userCache = make(map[int64]*User)
62+
ok bool
63+
err error
64+
)
65+
defer func() {
66+
for i := range gitRepoCache {
67+
gitRepoCache[i].Close()
68+
}
69+
}()
70+
for start := 0; ; start += batchSize {
71+
releases := make([]*Release, 0, batchSize)
72+
73+
if err := sess.Limit(batchSize, start).Asc("id").Where("is_tag=?", true).Find(&releases); err != nil {
74+
return err
75+
}
76+
77+
if len(releases) == 0 {
78+
break
79+
}
80+
81+
for _, release := range releases {
82+
gitRepo, ok = gitRepoCache[release.RepoID]
83+
if !ok {
84+
repo, ok := repoCache[release.RepoID]
85+
if !ok {
86+
repo = new(Repository)
87+
has, err := sess.ID(release.RepoID).Get(repo)
88+
if err != nil {
89+
return err
90+
} else if !has {
91+
return fmt.Errorf("Repository %d is not exist", release.RepoID)
92+
}
93+
94+
repoCache[release.RepoID] = repo
95+
}
96+
97+
user, ok := userCache[repo.OwnerID]
98+
if !ok {
99+
user = new(User)
100+
has, err := sess.ID(repo.OwnerID).Get(user)
101+
if err != nil {
102+
return err
103+
} else if !has {
104+
return fmt.Errorf("User %d is not exist", repo.OwnerID)
105+
}
106+
107+
userCache[repo.OwnerID] = user
108+
}
109+
110+
gitRepo, err = git.OpenRepository(repoPath(user.Name, repo.Name))
111+
if err != nil {
112+
return err
113+
}
114+
gitRepoCache[release.RepoID] = gitRepo
115+
}
116+
117+
commit, err := gitRepo.GetTagCommit(release.TagName)
118+
if err != nil {
119+
return fmt.Errorf("GetTagCommit: %v", err)
120+
}
121+
122+
u := new(User)
123+
exists, err := sess.Where("email=?", commit.Author.Email).Get(u)
124+
if err != nil {
125+
return err
126+
}
127+
128+
if !exists {
129+
continue
130+
}
131+
132+
release.PublisherID = u.ID
133+
if _, err := sess.ID(release.ID).Cols("publisher_id").Update(release); err != nil {
134+
return err
135+
}
136+
}
137+
}
138+
139+
return sess.Commit()
140+
}

routers/repo/release.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Rel
5757
func Releases(ctx *context.Context) {
5858
ctx.Data["Title"] = ctx.Tr("repo.release.releases")
5959
ctx.Data["PageIsReleaseList"] = true
60+
ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
6061

6162
writeAccess := ctx.Repo.CanWrite(models.UnitTypeReleases)
6263
ctx.Data["CanCreateRelease"] = writeAccess && !ctx.Repo.Repository.IsArchived

services/release/release.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ func createTag(gitRepo *git.Repository, rel *models.Release) error {
5858
if err != nil {
5959
return fmt.Errorf("CommitsCount: %v", err)
6060
}
61+
62+
u, err := models.GetUserByEmail(commit.Author.Email)
63+
if err == nil {
64+
rel.PublisherID = u.ID
65+
}
66+
6167
} else {
6268
rel.CreatedUnix = timeutil.TimeStampNow()
6369
}

templates/repo/release/list.tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
<h4>
4141
<a href="{{$.RepoLink}}/src/tag/{{.TagName | EscapePound}}" rel="nofollow"><i class="tag icon"></i> {{.TagName}}</a>
4242
</h4>
43+
<p class="text grey">
44+
{{ if gt .Publisher.ID 0 }}
45+
<span class="author">
46+
<img class="img-10" src="{{.Publisher.RelAvatarLink}}">
47+
<a href="{{AppSubUrl}}/{{.Publisher.Name}}">{{.Publisher.Name}}</a>
48+
</span>
49+
{{ end }}
50+
<span class="ahead"><a href="{{$.RepoLink}}/compare/{{.TagName | EscapePound}}...{{.Target}}">{{$.i18n.Tr "repo.release.ahead.commits" .NumCommitsBehind | Str2html}}</a> {{$.i18n.Tr "repo.release.ahead.target" $.DefaultBranch}}</span>
51+
</p>
4352
<div class="download">
4453
{{if $.Permission.CanRead $.UnitTypeCode}}
4554
<a href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel="nofollow"><i class="code icon"></i> {{ShortSha .Sha1}}</a>

0 commit comments

Comments
 (0)