From 7e3f0d7994c392860087b3f550188bca755f7e9c Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 22 Dec 2021 10:55:42 +0800 Subject: [PATCH 1/3] #981 update --- models/repo_tag.go | 102 ++++++++++++++++++++++++++++++-------------- routers/org/home.go | 4 +- 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/models/repo_tag.go b/models/repo_tag.go index 21e742082..7abe0f1e7 100644 --- a/models/repo_tag.go +++ b/models/repo_tag.go @@ -1,6 +1,7 @@ package models import ( + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" "fmt" ) @@ -12,6 +13,7 @@ type OfficialTag struct { Name string `xorm:"NOT NULL"` Code string `xorm:"NOT NULL"` Limit int `xorm:"NOT NULL default(-1)"` + Status int `xorm:"NOT NULL default(0)"` CreatedUnix timeutil.TimeStamp `xorm:"created"` UpdatedUnix timeutil.TimeStamp `xorm:"updated"` } @@ -40,21 +42,41 @@ type TagReposSelected struct { type TagsDetail struct { TagId int64 TagName string - RepoList []RepositoryForTag + TagLimit int + RepoList []Repository } type RepositoryForTag struct { - TagId int64 - TagName string - ID int64 - Name string - Description string - NumWatches int - NumStars int - NumForks int - Topics []string + TagId int64 + TagName string + Repo Repository } +// +//type RepositoryForTag struct { +// TagId int64 +// TagName string +// ID int64 +// Name string +// Description string +// NumWatches int +// NumStars int +// NumForks int +// Topics []string +// Link string +// OwnerName string +//} + +//// Link returns the repository link +//func (repo *RepositoryForTag) FullPath() string { +// return setting.AppSubURL + "/" + repo.FullName() +//} +// +//// FullName returns the repository full name +//func (repo *RepositoryForTag) FullName() string { +// return repo.OwnerName + "/" + repo.Name +//} + func GetTagByID(id int64) (*OfficialTag, error) { r := &OfficialTag{ ID: id, @@ -104,7 +126,7 @@ func UpdateTagReposByID(tagID, orgID int64, repoIdList []int64) error { func GetTagRepos(tagID, orgID int64) ([]TagReposSelected, error) { t := make([]TagReposBrief, 0) - const SQLCmd = "select t1.id as repo_id,t1.name as repo_name,t2.id as tag_id from repository t1 left join official_tag_repos t2 on (t1.id = t2.repo_id and t2.tag_id = ?) where t1.owner_id = ? and t1.is_private = false" + const SQLCmd = "select t1.id as repo_id,t1.name as repo_name,t2.id as tag_id from repository t1 left join official_tag_repos t2 on (t1.id = t2.repo_id and t2.tag_id = ?) where t1.owner_id = ? and t1.is_private = false order by t1.updated_unix desc" if err := x.SQL(SQLCmd, tagID, orgID).Find(&t); err != nil { return nil, err @@ -124,31 +146,47 @@ func GetTagRepos(tagID, orgID int64) ([]TagReposSelected, error) { return r, nil } -func GetTagsDetails(orgID int64) ([]TagsDetail, error) { - t := make([]RepositoryForTag, 0) - const SQLCmd = "select t1.tag_id ,t3.name as tag_name,t2.* from official_tag_repos t1 inner join repository t2 on t1.repo_id = t2.id inner join official_tag t3 on t1.tag_id = t3.id where t1.org_id = ?" - - if err := x.SQL(SQLCmd, orgID).Find(&t); err != nil { +func GetAllOfficialTagRepos(orgID int64, isOwner bool) ([]TagsDetail, error) { + result := make([]TagsDetail, 0) + tags, err := GetAllOfficialTags() + if err != nil { return nil, err } - r := make([]TagsDetail, 0) - tempMap := make(map[int64]TagsDetail, 0) - for _, v := range t { - tagId := v.TagId - detail, ok := tempMap[tagId] - if !ok { - detail = TagsDetail{ - TagId: v.TagId, - TagName: v.TagName, - RepoList: make([]RepositoryForTag, 0), - } + for _, tag := range tags { + repos, err := GetOfficialTagDetail(orgID, tag.ID) + if err != nil { + return nil, err } - detail.RepoList = append(detail.RepoList, v) - tempMap[tagId] = detail + if len(repos) == 0 && !isOwner { + continue + } + result = append(result, TagsDetail{ + TagId: tag.ID, + TagName: tag.Name, + TagLimit: tag.Limit, + RepoList: repos, + }) } + return result, nil +} + +func GetOfficialTagDetail(orgID, tagId int64) ([]Repository, error) { + t := make([]Repository, 0) + const SQLCmd = "select t2.* from official_tag_repos t1 inner join repository t2 on t1.repo_id = t2.id where t1.org_id = ? and t1.tag_id=? order by t2.updated_unix desc" - for _, v := range tempMap { - r = append(r, v) + if err := x.SQL(SQLCmd, orgID, tagId).Find(&t); err != nil { + return nil, err } - return r, nil + return t, nil +} + +func GetAllOfficialTags() ([]OfficialTag, error) { + //todo redis? + o := make([]OfficialTag, 0) + err := x.Where("status = ?", 0).Find(&o) + if err != nil { + log.Error("GetAllOfficialTags error,%v", err) + return nil, err + } + return o, nil } diff --git a/routers/org/home.go b/routers/org/home.go index 1a80f001f..df600d96d 100755 --- a/routers/org/home.go +++ b/routers/org/home.go @@ -131,9 +131,9 @@ func Home(ctx *context.Context) { ctx.Data["Page"] = pager //find org tag info - tags, err := models.GetTagsDetails(org.ID) + tags, err := models.GetAllOfficialTagRepos(org.ID, ctx.Org.IsOwner) if err != nil { - ctx.ServerError("GetTagsDetails", err) + ctx.ServerError("GetAllOfficialTagRepos", err) return } From e6bda98499c61b4f6c795b2afae82a795177f999 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 22 Dec 2021 10:56:57 +0800 Subject: [PATCH 2/3] #981 update --- models/repo_tag.go | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/models/repo_tag.go b/models/repo_tag.go index 7abe0f1e7..ceac2850e 100644 --- a/models/repo_tag.go +++ b/models/repo_tag.go @@ -46,37 +46,6 @@ type TagsDetail struct { RepoList []Repository } -type RepositoryForTag struct { - TagId int64 - TagName string - Repo Repository -} - -// -//type RepositoryForTag struct { -// TagId int64 -// TagName string -// ID int64 -// Name string -// Description string -// NumWatches int -// NumStars int -// NumForks int -// Topics []string -// Link string -// OwnerName string -//} - -//// Link returns the repository link -//func (repo *RepositoryForTag) FullPath() string { -// return setting.AppSubURL + "/" + repo.FullName() -//} -// -//// FullName returns the repository full name -//func (repo *RepositoryForTag) FullName() string { -// return repo.OwnerName + "/" + repo.Name -//} - func GetTagByID(id int64) (*OfficialTag, error) { r := &OfficialTag{ ID: id, From 19fae465a37f0d65969cda8a870f53da91b86120 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Fri, 24 Dec 2021 11:35:59 +0800 Subject: [PATCH 3/3] #981 fix bug --- models/repo_tag.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/models/repo_tag.go b/models/repo_tag.go index ceac2850e..c8e675d75 100644 --- a/models/repo_tag.go +++ b/models/repo_tag.go @@ -76,6 +76,10 @@ func UpdateTagReposByID(tagID, orgID int64, repoIdList []int64) error { return err } + if len(repoIdList) == 0 { + return sess.Commit() + } + //add new tag repos data := make([]*OfficialTagRepos, 0) for _, repoId := range repoIdList {