diff --git a/models/repo_tag.go b/models/repo_tag.go index 21e742082..c8e675d75 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,19 +42,8 @@ type TagReposSelected struct { type TagsDetail struct { TagId int64 TagName string - RepoList []RepositoryForTag -} - -type RepositoryForTag struct { - TagId int64 - TagName string - ID int64 - Name string - Description string - NumWatches int - NumStars int - NumForks int - Topics []string + TagLimit int + RepoList []Repository } func GetTagByID(id int64) (*OfficialTag, error) { @@ -85,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 { @@ -104,7 +99,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 +119,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 }