Skip to content

Commit b5a8341

Browse files
author
Nils Hillmann
committed
Merge branch 'main' of https://github.com/go-gitea/gitea into feature/oauth_userinfo
2 parents a6e86dd + 6a3ad0b commit b5a8341

File tree

13 files changed

+182
-43
lines changed

13 files changed

+182
-43
lines changed

integrations/release_test.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"testing"
1111
"time"
1212

13+
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/test"
1516

17+
"github.com/PuerkitoBio/goquery"
1618
"github.com/stretchr/testify/assert"
1719
"github.com/unknwon/i18n"
1820
)
@@ -83,7 +85,7 @@ func TestCreateRelease(t *testing.T) {
8385
session := loginUser(t, "user2")
8486
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false)
8587

86-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 2)
88+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 3)
8789
}
8890

8991
func TestCreateReleasePreRelease(t *testing.T) {
@@ -92,7 +94,7 @@ func TestCreateReleasePreRelease(t *testing.T) {
9294
session := loginUser(t, "user2")
9395
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false)
9496

95-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 2)
97+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 3)
9698
}
9799

98100
func TestCreateReleaseDraft(t *testing.T) {
@@ -101,7 +103,7 @@ func TestCreateReleaseDraft(t *testing.T) {
101103
session := loginUser(t, "user2")
102104
createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true)
103105

104-
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 2)
106+
checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 3)
105107
}
106108

107109
func TestCreateReleasePaging(t *testing.T) {
@@ -127,3 +129,80 @@ func TestCreateReleasePaging(t *testing.T) {
127129
session2 := loginUser(t, "user4")
128130
checkLatestReleaseAndCount(t, session2, "/user2/repo1", "v0.0.11", i18n.Tr("en", "repo.release.stable"), 10)
129131
}
132+
133+
func TestViewReleaseListNoLogin(t *testing.T) {
134+
defer prepareTestEnv(t)()
135+
136+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
137+
138+
link := repo.Link() + "/releases"
139+
140+
req := NewRequest(t, "GET", link)
141+
rsp := MakeRequest(t, req, http.StatusOK)
142+
143+
htmlDoc := NewHTMLParser(t, rsp.Body)
144+
releases := htmlDoc.Find("#release-list li.ui.grid")
145+
assert.Equal(t, 1, releases.Length())
146+
147+
links := make([]string, 0, 5)
148+
releases.Each(func(i int, s *goquery.Selection) {
149+
link, exist := s.Find(".release-list-title a").Attr("href")
150+
if !exist {
151+
return
152+
}
153+
links = append(links, link)
154+
})
155+
156+
assert.EqualValues(t, []string{"/user2/repo1/releases/tag/v1.1"}, links)
157+
}
158+
159+
func TestViewReleaseListLogin(t *testing.T) {
160+
defer prepareTestEnv(t)()
161+
162+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
163+
164+
link := repo.Link() + "/releases"
165+
166+
session := loginUser(t, "user1")
167+
req := NewRequest(t, "GET", link)
168+
rsp := session.MakeRequest(t, req, http.StatusOK)
169+
170+
htmlDoc := NewHTMLParser(t, rsp.Body)
171+
releases := htmlDoc.Find("#release-list li.ui.grid")
172+
assert.Equal(t, 2, releases.Length())
173+
174+
links := make([]string, 0, 5)
175+
releases.Each(func(i int, s *goquery.Selection) {
176+
link, exist := s.Find(".release-list-title a").Attr("href")
177+
if !exist {
178+
return
179+
}
180+
links = append(links, link)
181+
})
182+
183+
assert.EqualValues(t, []string{"/user2/repo1/releases/tag/draft-release",
184+
"/user2/repo1/releases/tag/v1.1"}, links)
185+
}
186+
187+
func TestViewTagsList(t *testing.T) {
188+
defer prepareTestEnv(t)()
189+
190+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
191+
192+
link := repo.Link() + "/tags"
193+
194+
session := loginUser(t, "user1")
195+
req := NewRequest(t, "GET", link)
196+
rsp := session.MakeRequest(t, req, http.StatusOK)
197+
198+
htmlDoc := NewHTMLParser(t, rsp.Body)
199+
tags := htmlDoc.Find(".tag-list tr")
200+
assert.Equal(t, 2, tags.Length())
201+
202+
tagNames := make([]string, 0, 5)
203+
tags.Each(func(i int, s *goquery.Selection) {
204+
tagNames = append(tagNames, s.Find(".tag a.df.ac").Text())
205+
})
206+
207+
assert.EqualValues(t, []string{"delete-tag", "v1.1"}, tagNames)
208+
}

models/fixtures/release.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,15 @@
4343
is_tag: true
4444
created_unix: 946684800
4545

46+
-
47+
id: 4
48+
repo_id: 1
49+
publisher_id: 2
50+
tag_name: "draft-release"
51+
lower_tag_name: "draft-release"
52+
target: "master"
53+
title: "draft-release"
54+
is_draft: true
55+
is_prerelease: false
56+
is_tag: false
57+
created_unix: 1619524806

modules/context/repo.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package context
77

88
import (
9+
"context"
910
"fmt"
1011
"io/ioutil"
1112
"net/url"
@@ -393,7 +394,7 @@ func RepoIDAssignment() func(ctx *Context) {
393394
}
394395

395396
// RepoAssignment returns a middleware to handle repository assignment
396-
func RepoAssignment(ctx *Context) {
397+
func RepoAssignment(ctx *Context) (cancel context.CancelFunc) {
397398
var (
398399
owner *models.User
399400
err error
@@ -529,12 +530,12 @@ func RepoAssignment(ctx *Context) {
529530
ctx.Repo.GitRepo = gitRepo
530531

531532
// We opened it, we should close it
532-
defer func() {
533+
cancel = func() {
533534
// If it's been set to nil then assume someone else has closed it.
534535
if ctx.Repo.GitRepo != nil {
535536
ctx.Repo.GitRepo.Close()
536537
}
537-
}()
538+
}
538539

539540
// Stop at this point when the repo is empty.
540541
if ctx.Repo.Repository.IsEmpty {
@@ -619,6 +620,7 @@ func RepoAssignment(ctx *Context) {
619620
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
620621
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
621622
}
623+
return
622624
}
623625

624626
// RepoRefType type of repo reference
@@ -643,7 +645,7 @@ const (
643645

644646
// RepoRef handles repository reference names when the ref name is not
645647
// explicitly given
646-
func RepoRef() func(*Context) {
648+
func RepoRef() func(*Context) context.CancelFunc {
647649
// since no ref name is explicitly specified, ok to just use branch
648650
return RepoRefByType(RepoRefBranch)
649651
}
@@ -722,8 +724,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
722724

723725
// RepoRefByType handles repository reference name for a specific type
724726
// of repository reference
725-
func RepoRefByType(refType RepoRefType) func(*Context) {
726-
return func(ctx *Context) {
727+
func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context) context.CancelFunc {
728+
return func(ctx *Context) (cancel context.CancelFunc) {
727729
// Empty repository does not have reference information.
728730
if ctx.Repo.Repository.IsEmpty {
729731
return
@@ -742,12 +744,12 @@ func RepoRefByType(refType RepoRefType) func(*Context) {
742744
return
743745
}
744746
// We opened it, we should close it
745-
defer func() {
747+
cancel = func() {
746748
// If it's been set to nil then assume someone else has closed it.
747749
if ctx.Repo.GitRepo != nil {
748750
ctx.Repo.GitRepo.Close()
749751
}
750-
}()
752+
}
751753
}
752754

753755
// Get default branch.
@@ -811,6 +813,9 @@ func RepoRefByType(refType RepoRefType) func(*Context) {
811813
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), refName, ctx.Repo.Commit.ID.String(), 1))))
812814
}
813815
} else {
816+
if len(ignoreNotExistErr) > 0 && ignoreNotExistErr[0] {
817+
return
818+
}
814819
ctx.NotFound("RepoRef invalid repo", fmt.Errorf("branch or tag not exist: %s", refName))
815820
return
816821
}
@@ -841,6 +846,7 @@ func RepoRefByType(refType RepoRefType) func(*Context) {
841846
return
842847
}
843848
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
849+
return
844850
}
845851
}
846852

modules/web/route.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package web
66

77
import (
8+
goctx "context"
89
"fmt"
910
"net/http"
1011
"reflect"
@@ -27,6 +28,7 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
2728
switch t := handler.(type) {
2829
case http.HandlerFunc, func(http.ResponseWriter, *http.Request),
2930
func(ctx *context.Context),
31+
func(ctx *context.Context) goctx.CancelFunc,
3032
func(*context.APIContext),
3133
func(*context.PrivateContext),
3234
func(http.Handler) http.Handler:
@@ -48,6 +50,15 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
4850
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
4951
return
5052
}
53+
case func(ctx *context.Context) goctx.CancelFunc:
54+
ctx := context.GetContext(req)
55+
cancel := t(ctx)
56+
if cancel != nil {
57+
defer cancel()
58+
}
59+
if ctx.Written() {
60+
return
61+
}
5162
case func(ctx *context.Context):
5263
ctx := context.GetContext(req)
5364
t(ctx)
@@ -94,6 +105,23 @@ func Middle(f func(ctx *context.Context)) func(netx http.Handler) http.Handler {
94105
}
95106
}
96107

108+
// MiddleCancel wrap a context function as a chi middleware
109+
func MiddleCancel(f func(ctx *context.Context) goctx.CancelFunc) func(netx http.Handler) http.Handler {
110+
return func(next http.Handler) http.Handler {
111+
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
112+
ctx := context.GetContext(req)
113+
cancel := f(ctx)
114+
if cancel != nil {
115+
defer cancel()
116+
}
117+
if ctx.Written() {
118+
return
119+
}
120+
next.ServeHTTP(ctx.Resp, ctx.Req)
121+
})
122+
}
123+
}
124+
97125
// MiddleAPI wrap a context function as a chi middleware
98126
func MiddleAPI(f func(ctx *context.APIContext)) func(netx http.Handler) http.Handler {
99127
return func(next http.Handler) http.Handler {
@@ -163,6 +191,8 @@ func (r *Route) Use(middlewares ...interface{}) {
163191
r.R.Use(t)
164192
case func(*context.Context):
165193
r.R.Use(Middle(t))
194+
case func(*context.Context) goctx.CancelFunc:
195+
r.R.Use(MiddleCancel(t))
166196
case func(*context.APIContext):
167197
r.R.Use(MiddleAPI(t))
168198
default:

routers/repo/release.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
9999
Page: ctx.QueryInt("page"),
100100
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
101101
},
102-
IncludeDrafts: writeAccess,
102+
IncludeDrafts: writeAccess && !isTagList,
103103
IncludeTags: isTagList,
104104
}
105105

@@ -141,10 +141,7 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
141141
}
142142
cacheUsers[r.PublisherID] = r.Publisher
143143
}
144-
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
145-
ctx.ServerError("calReleaseNumCommitsBehind", err)
146-
return
147-
}
144+
148145
r.Note, err = markdown.RenderString(&markup.RenderContext{
149146
URLPrefix: ctx.Repo.RepoLink,
150147
Metas: ctx.Repo.Repository.ComposeMetas(),
@@ -153,6 +150,15 @@ func releasesOrTags(ctx *context.Context, isTagList bool) {
153150
ctx.ServerError("RenderString", err)
154151
return
155152
}
153+
154+
if r.IsDraft {
155+
continue
156+
}
157+
158+
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
159+
ctx.ServerError("calReleaseNumCommitsBehind", err)
160+
return
161+
}
156162
}
157163

158164
ctx.Data["Releases"] = releases
@@ -198,9 +204,11 @@ func SingleRelease(ctx *context.Context) {
198204
return
199205
}
200206
}
201-
if err := calReleaseNumCommitsBehind(ctx.Repo, release, make(map[string]int64)); err != nil {
202-
ctx.ServerError("calReleaseNumCommitsBehind", err)
203-
return
207+
if !release.IsDraft {
208+
if err := calReleaseNumCommitsBehind(ctx.Repo, release, make(map[string]int64)); err != nil {
209+
ctx.ServerError("calReleaseNumCommitsBehind", err)
210+
return
211+
}
204212
}
205213
release.Note, err = markdown.RenderString(&markup.RenderContext{
206214
URLPrefix: ctx.Repo.RepoLink,

routers/routes/web.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ func RegisterRoutes(m *web.Route) {
913913
m.Get("/", repo.Releases)
914914
m.Get("/tag/*", repo.SingleRelease)
915915
m.Get("/latest", repo.LatestRelease)
916-
m.Get("/attachments/{uuid}", repo.GetAttachment)
917-
}, repo.MustBeNotEmpty, reqRepoReleaseReader, context.RepoRefByType(context.RepoRefTag))
916+
}, repo.MustBeNotEmpty, reqRepoReleaseReader, context.RepoRefByType(context.RepoRefTag, true))
917+
m.Get("/releases/attachments/{uuid}", repo.GetAttachment, repo.MustBeNotEmpty, reqRepoReleaseReader)
918918
m.Group("/releases", func() {
919919
m.Get("/new", repo.NewRelease)
920920
m.Post("/new", bindIgnErr(forms.NewReleaseForm{}), repo.NewReleasePost)

templates/repo/branch_dropdown.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{{end}}
2323
{{range .root.Tags}}
2424
{{if $release}}
25-
<div class="item tag {{if eq $release.TagName .}}selected{{end}}" data-url="{{$.root.RepoLink}}/compare/{{EscapePound .}}...{{if $release.TagName}}{{EscapePound $release.TagName}}{{else}}{{EscapePound $release.Sha1}}{{end}}">{{.}}</div>
25+
<div class="item tag {{if eq $release.TagName .}}selected{{end}}" data-url="{{$.root.RepoLink}}/compare/{{EscapePound .}}...{{if $release.IsDraft}}{{EscapePound $release.Target}}{{else}}}{{if $release.TagName}}{{EscapePound $release.TagName}}{{else}}{{EscapePound $release.Sha1}}{{end}}{{end}}">{{.}}</div>
2626
{{else}}
2727
<div class="item tag {{if eq $.root.BranchName .}}selected{{end}}" data-url="{{$.root.RepoLink}}/{{if $.root.PageIsCommits}}commits{{else}}src{{end}}/tag/{{EscapePound .}}{{if $.root.TreePath}}/{{EscapePound $.root.TreePath}}{{end}}">{{.}}</div>
2828
{{end}}

templates/repo/graph/commits.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
{{$refGroup := .RefGroup}}
3535
{{if eq $refGroup "pull"}}
3636
{{if or (not $.HidePRRefs) (containGeneric $.SelectedBranches .Name)}}
37-
<a class="ui labelled icon button basic tiny" href="{{$.RepoLink}}/pulls/{{.ShortName|PathEscape}}">
37+
<!-- it's intended to use issues not pulls, if it's a pull you will get redirected -->
38+
<a class="ui labelled icon button basic tiny" href="{{$.RepoLink}}/issues/{{.ShortName|PathEscape}}">
3839
{{svg "octicon-git-pull-request" 16 "mr-2"}}#{{.ShortName}}
3940
</a>
4041
{{end}}

0 commit comments

Comments
 (0)