From ad31d6b5e8856b22ddad45477be9d078a869a953 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 6 Apr 2020 20:42:30 +0200 Subject: [PATCH] Fix release counter on API repository info (#10968) Use the same mechanism as the webpage for calculating the release count Fix #10946 --- 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 ff59fac5c..d57e5daaf 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, 3, 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 8f0736ef8..a901c3476 100644 --- a/models/repo.go +++ b/models/repo.go @@ -166,7 +166,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"` @@ -353,6 +352,8 @@ func (repo *Repository) innerAPIFormat(e Engine, mode AccessMode, isParent bool) repo.mustOwner(e) + numReleases, _ := GetReleaseCountByRepoID(repo.ID, FindReleasesOptions{IncludeDrafts: false, IncludeTags: true}) + return &api.Repository{ ID: repo.ID, Owner: repo.Owner.APIFormat(), @@ -376,7 +377,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 1f6e5037c..841dcd960 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -439,7 +439,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, }) @@ -447,7 +447,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 1fc298bcb..f4edc7439 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -117,7 +117,7 @@ {{if and (.Permission.CanRead $.UnitTypeReleases) (not .IsEmptyRepo) }} - {{svg "octicon-tag" 16}} {{.i18n.Tr "repo.releases"}} {{.Repository.NumReleases}} + {{svg "octicon-tag" 16}} {{.i18n.Tr "repo.releases"}} {{.NumReleases}} {{end}}