Display draft releases (#1854)

* Display draft releases

* Include ctx.User in user cache

* Integration test
pull/1860/head
Ethan Koenig 8 years ago committed by Lunny Xiao
parent 036ce3f813
commit b900c04316
  1. 22
      integrations/release_test.go
  2. 8
      models/fixtures/repo_unit.yml
  3. 98
      routers/repo/release.go

@ -0,0 +1,22 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package integrations
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestViewReleases(t *testing.T) {
prepareTestEnv(t)
session := loginUser(t, "user2", "password")
req, err := http.NewRequest("GET", "/user2/repo1/releases", nil)
assert.NoError(t, err)
resp := session.MakeRequest(t, req)
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
}

@ -25,6 +25,14 @@
- -
id: 4 id: 4
repo_id: 1 repo_id: 1
type: 5
index: 0
config: "{}"
created_unix: 946684810
-
id: 5
repo_id: 1
type: 7 type: 7
index: 0 index: 0
config: "{}" config: "{}"

@ -5,10 +5,8 @@
package repo package repo
import ( import (
"errors"
"fmt" "fmt"
"code.gitea.io/git"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth" "code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
@ -67,35 +65,7 @@ func Releases(ctx *context.Context) {
limit = 10 limit = 10
} }
rawTags, err := ctx.Repo.GitRepo.GetTagInfos(git.TagOption{}) releases, err := models.GetReleasesByRepoID(ctx.Repo.Repository.ID, page, limit)
if err != nil {
ctx.Handle(500, "GetTags", err)
return
}
if len(rawTags) == 0 {
ctx.HTML(200, tplReleases)
return
}
if len(rawTags) <= (page-1)*limit {
ctx.Handle(500, "Releases", errors.New("no more pages"))
return
}
var tags []*git.Tag
if page*limit > len(rawTags) {
tags = rawTags[(page-1)*limit:]
} else {
tags = rawTags[(page-1)*limit : page*limit]
}
var tagNames []string
for _, t := range tags {
tagNames = append(tagNames, t.Name)
}
releases, err := models.GetReleasesByRepoIDAndNames(ctx.Repo.Repository.ID, tagNames)
if err != nil { if err != nil {
ctx.Handle(500, "GetReleasesByRepoIDAndNames", err) ctx.Handle(500, "GetReleasesByRepoIDAndNames", err)
return return
@ -107,61 +77,39 @@ func Releases(ctx *context.Context) {
return return
} }
// Temproray cache commits count of used branches to speed up. // Temporary cache commits count of used branches to speed up.
countCache := make(map[string]int64) countCache := make(map[string]int64)
var cacheUsers = make(map[int64]*models.User) cacheUsers := map[int64]*models.User{ctx.User.ID: ctx.User}
var ok bool var ok bool
releaseTags := make([]*models.Release, len(tags))
for i, rawTag := range tags {
for _, r := range releases {
if r.IsDraft && !ctx.Repo.IsOwner() {
continue
}
if r.TagName == rawTag.Name {
if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil {
if models.IsErrUserNotExist(err) {
r.Publisher = models.NewGhostUser()
} else {
ctx.Handle(500, "GetUserByID", err)
return
}
}
cacheUsers[r.PublisherID] = r.Publisher
}
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil { releasesToDisplay := make([]*models.Release, 0, len(releases))
ctx.Handle(500, "calReleaseNumCommitsBehind", err) for _, r := range releases {
if r.IsDraft && !ctx.Repo.IsOwner() {
continue
}
if r.Publisher, ok = cacheUsers[r.PublisherID]; !ok {
r.Publisher, err = models.GetUserByID(r.PublisherID)
if err != nil {
if models.IsErrUserNotExist(err) {
r.Publisher = models.NewGhostUser()
} else {
ctx.Handle(500, "GetUserByID", err)
return return
} }
r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
releaseTags[i] = r
break
} }
cacheUsers[r.PublisherID] = r.Publisher
} }
if err := calReleaseNumCommitsBehind(ctx.Repo, r, countCache); err != nil {
if releaseTags[i] == nil { ctx.Handle(500, "calReleaseNumCommitsBehind", err)
releaseTags[i] = &models.Release{ return
Title: rawTag.Name,
TagName: rawTag.Name,
Sha1: rawTag.Object.String(),
Note: rawTag.Message,
}
releaseTags[i].NumCommits, err = git.CommitsCount(ctx.Repo.GitRepo.Path, rawTag.Object.String())
if err != nil {
ctx.Handle(500, "CommitsCount", err)
return
}
releaseTags[i].NumCommitsBehind = ctx.Repo.CommitsCount - releaseTags[i].NumCommits
} }
r.Note = markdown.RenderString(r.Note, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())
releasesToDisplay = append(releasesToDisplay, r)
} }
pager := paginater.New(len(rawTags), limit, page, 5) pager := paginater.New(len(releases), limit, page, 5)
ctx.Data["Page"] = pager ctx.Data["Page"] = pager
ctx.Data["Releases"] = releaseTags ctx.Data["Releases"] = releasesToDisplay
ctx.HTML(200, tplReleases) ctx.HTML(200, tplReleases)
} }

Loading…
Cancel
Save