From 0c4be64345b0daa83b8af33828281acb27038164 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 7 Apr 2020 00:13:12 +0200 Subject: [PATCH] [Backport] Fix release counter on API repository info (#10968) (#10996) * Fix release counter on API repository info (#10968) * correct Pull Count to v1.11 Fixtures --- integrations/api_repo_test.go | 22 ++++++++++++++++++++-- models/repo.go | 5 +++-- modules/context/repo.go | 3 +-- templates/repo/header.tmpl | 2 +- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index 6c3835788..fb36b3fd4 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -209,13 +209,31 @@ func getRepo(t *testing.T, repoID int64) *models.Repository { func TestAPIViewRepo(t *testing.T) { defer prepareTestEnv(t)() + var repo api.Repository + req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1") resp := MakeRequest(t, req, http.StatusOK) - - var repo api.Repository DecodeJSON(t, resp, &repo) assert.EqualValues(t, 1, repo.ID) assert.EqualValues(t, "repo1", repo.Name) + assert.EqualValues(t, 1, repo.Releases) + assert.EqualValues(t, 1, repo.OpenIssues) + assert.EqualValues(t, 2, repo.OpenPulls) + + req = NewRequest(t, "GET", "/api/v1/repos/user12/repo10") + resp = MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &repo) + assert.EqualValues(t, 10, repo.ID) + assert.EqualValues(t, "repo10", repo.Name) + assert.EqualValues(t, 1, repo.OpenPulls) + assert.EqualValues(t, 1, repo.Forks) + + req = NewRequest(t, "GET", "/api/v1/repos/user5/repo4") + resp = MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &repo) + assert.EqualValues(t, 4, repo.ID) + assert.EqualValues(t, "repo4", repo.Name) + assert.EqualValues(t, 1, repo.Stars) } func TestAPIOrgRepos(t *testing.T) { diff --git a/models/repo.go b/models/repo.go index 8d777a549..7e2cfd947 100644 --- a/models/repo.go +++ b/models/repo.go @@ -173,7 +173,6 @@ type Repository struct { NumMilestones int `xorm:"NOT NULL DEFAULT 0"` NumClosedMilestones int `xorm:"NOT NULL DEFAULT 0"` NumOpenMilestones int `xorm:"-"` - NumReleases int `xorm:"-"` IsPrivate bool `xorm:"INDEX"` IsEmpty bool `xorm:"INDEX"` @@ -364,6 +363,8 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) allowSquash = config.AllowSquash } + numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true}) + return &api.Repository{ ID: repo.ID, Owner: repo.Owner.APIFormat(), @@ -387,7 +388,7 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) Watchers: repo.NumWatches, OpenIssues: repo.NumOpenIssues, OpenPulls: repo.NumOpenPulls, - Releases: repo.NumReleases, + Releases: int(numReleases), DefaultBranch: repo.DefaultBranch, Created: repo.CreatedUnix.AsTime(), Updated: repo.UpdatedUnix.AsTime(), diff --git a/modules/context/repo.go b/modules/context/repo.go index d0a982646..e9c933ef2 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -396,7 +396,7 @@ func RepoAssignment() macaron.Handler { ctx.Data["RepoExternalIssuesLink"] = unit.ExternalTrackerConfig().ExternalTrackerURL } - count, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ + ctx.Data["NumReleases"], err = models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{ IncludeDrafts: false, IncludeTags: true, }) @@ -404,7 +404,6 @@ func RepoAssignment() macaron.Handler { ctx.ServerError("GetReleaseCountByRepoID", err) return } - ctx.Repo.Repository.NumReleases = int(count) ctx.Data["Title"] = owner.Name + "/" + repo.Name ctx.Data["Repository"] = repo diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index b27d9ae54..fbd265373 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -86,7 +86,7 @@ {{if and (.Permission.CanRead $.UnitTypeReleases) (not .IsEmptyRepo) }} - {{.i18n.Tr "repo.releases"}} {{.Repository.NumReleases}} + {{.i18n.Tr "repo.releases"}} {{.NumReleases}} {{end}}