Browse Source

#1253 : display repository labels

pull/5260/head
isundil 7 years ago
parent
commit
c2a640584f
  1. 29
      models/repo.go
  2. 8
      pkg/context/repo.go
  3. 8
      routes/home.go
  4. 20
      routes/repo/setting.go
  5. 9
      routes/user/home.go
  6. 11
      routes/user/profile.go
  7. 5
      templates/explore/repo_list.tmpl
  8. 5
      templates/repo/header.tmpl

29
models/repo.go

@ -1754,11 +1754,20 @@ func GetRepositoryLabels(userID int64) ([]*RepositoryLabel, error) {
return labels, nil
}
func GetRepositoryLabelsForRepository(repo *Repository) ([]*RepositoryLabel, error) {
func GetRepositoryLabelsForRepository(repo *Repository, asOfView *User) ([]*RepositoryLabel, error) {
labels := make([]*RepositoryLabel, 0, 5)
if err := x.
Where("repository_id = ?", repo.ID).
Join("INNER", "repository_repo_label", "repository_repo_label.label_id = repository_label.id").
query := x.Join("INNER", "repository_repo_label", "repository_repo_label.label_id = repository_label.id")
if asOfView == nil {
query = query.Where("repository_id =? AND repository_label.is_private=0", repo.ID)
} else if asOfView.IsAdmin {
query = query.Where("repository_id =?", repo.ID)
} else {
query = query.Join("LEFT", "access", "access.repo_id=repository_repo_label.repository_id").
Where("repository_id = ? AND (access.user_id=? OR repository_label.is_private=0)", repo.ID, asOfView.ID)
}
if err := query.
Find(&labels); err != nil {
return nil, fmt.Errorf("select repository labels: %v", err)
}
@ -1813,6 +1822,18 @@ func GetRepositoryLabel(labelId int64, owner *User) (*RepositoryLabel, error) {
return label, nil
}
func GetRepositoryLabelById(labelId int64) (*RepositoryLabel, error) {
label := new(RepositoryLabel)
if has, err := x.Where("ID = ?", labelId).Get(label); err != nil {
return nil, fmt.Errorf("select repository labels: %v", err)
} else if !has {
return nil, fmt.Errorf("Label %d not found", labelId)
}
return label, nil
}
type CreateRepoLabelOptions struct {
Name string
IsPrivate bool

8
pkg/context/repo.go

@ -37,6 +37,7 @@ type Repository struct {
Commit *git.Commit
Tag *git.Tag
GitRepo *git.Repository
Labels []*models.RepositoryLabel
BranchName string
TagName string
TreePath string
@ -216,6 +217,12 @@ func RepoAssignment(pages ...bool) macaron.Handler {
}
c.Repo.GitRepo = gitRepo
if allLabels, err := models.GetRepositoryLabelsForRepository(c.Repo.Repository, c.User); err == nil {
c.Repo.Labels = allLabels
} else {
c.Repo.Labels = make([]*models.RepositoryLabel, 0, 0)
}
tags, err := c.Repo.GitRepo.GetTags()
if err != nil {
c.ServerError(fmt.Sprintf("GetTags '%s'", c.Repo.Repository.RepoPath()), err)
@ -235,6 +242,7 @@ func RepoAssignment(pages ...bool) macaron.Handler {
c.Data["DisableHTTP"] = setting.Repository.DisableHTTPGit
c.Data["CloneLink"] = repo.CloneLink()
c.Data["WikiCloneLink"] = repo.WikiCloneLink()
c.Data["RepositoryLabels"] = c.Repo.Labels
if c.IsLogged {
c.Data["IsWatchingRepo"] = models.IsWatching(c.User.ID, repo.ID)

8
routes/home.go

@ -74,6 +74,14 @@ func ExploreRepos(c *context.Context) {
}
c.Data["Repos"] = repos
labelsByRepoId := make(map[int64] []*models.RepositoryLabel)
for _, currentRepo := range repos {
if labels, err := models.GetRepositoryLabelsForRepository(currentRepo, c.User); err == nil {
labelsByRepoId[currentRepo.ID] = labels
}
}
c.Data["LabelsByRepoId"] = labelsByRepoId
c.Success(EXPLORE_REPOS)
}

20
routes/repo/setting.go

@ -36,29 +36,21 @@ func Settings(c *context.Context) {
c.Title("repo.settings")
c.PageIs("SettingsOptions")
repoLabels, err := models.GetRepositoryLabelsForRepository(c.Repo.Repository)
if err != nil {
c.ServerError("GetUserAndCollaborativeRepositories", err)
return
}
availableLabels := make([]*models.RepositoryLabel, 0, 10)
availableLabels := make([]*models.RepositoryLabel, 0, 5)
if allRepositoryLabels, err := models.GetRepositoryLabels(c.User.ID); err == nil {
for i := 0; i < len(allRepositoryLabels); i++ {
for _, repositoryLabel := range(allRepositoryLabels) {
found := false
for j := 0; j < len(repoLabels) && !found; j++ {
if repoLabels[j].ID == allRepositoryLabels[i].ID {
found = true;
}
for i := 0; i < len(c.Repo.Labels) && !found; i++ {
found = c.Repo.Labels[i].ID == repositoryLabel.ID
}
if !found {
availableLabels = append(availableLabels, allRepositoryLabels[i])
availableLabels = append(availableLabels, repositoryLabel)
}
}
} else {
c.ServerError("GetUserAndCollaborativeRepositories", err)
return
}
c.Data["RepositoryLabels"] = repoLabels
c.Data["AvailableLabels"] = availableLabels
c.Success(SETTINGS_OPTIONS)
}
@ -165,7 +157,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
repositoryLabelsToRemove := strings.Split(c.Query("repositoryLabelsToRemove"), ";")
for i := 0; i < len(repositoryLabelsToRemove) ; i++ {
if labelId, err := strconv.ParseInt(repositoryLabelsToRemove[i], 10, 64); err == nil {
if label, err := models.GetRepositoryLabel(labelId, c.User); err == nil {
if label, err := models.GetRepositoryLabelById(labelId); err == nil {
models.RemoveRepoLabelFromRepository(c.Repo.Repository, label, c.User)
}
}

9
routes/user/home.go

@ -403,6 +403,15 @@ func showOrgProfile(c *context.Context) {
}
c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
labelsByRepoId := make(map[int64] []*models.RepositoryLabel)
for _, currentRepo := range repos {
if labels, err := models.GetRepositoryLabelsForRepository(currentRepo, c.User); err == nil {
labelsByRepoId[currentRepo.ID] = labels
}
}
c.Data["LabelsByRepoId"] = labelsByRepoId
if err := org.GetMembers(); err != nil {
c.Handle(500, "GetMembers", err)
return

11
routes/user/profile.go

@ -96,7 +96,7 @@ func Profile(c *context.Context) {
}
showPrivate := c.IsLogged && (ctxUser.ID == c.User.ID || c.User.IsAdmin)
c.Data["Repos"], err = models.GetUserRepositories(&models.UserRepoOptions{
repositories, err := models.GetUserRepositories(&models.UserRepoOptions{
UserID: ctxUser.ID,
Private: showPrivate,
Page: page,
@ -106,6 +106,15 @@ func Profile(c *context.Context) {
c.Handle(500, "GetRepositories", err)
return
}
c.Data["Repos"] = repositories
labelsByRepoId := make(map[int64] []*models.RepositoryLabel)
for _, currentRepo := range repositories {
if labels, err := models.GetRepositoryLabelsForRepository(currentRepo, c.User); err == nil {
labelsByRepoId[currentRepo.ID] = labels
}
}
c.Data["LabelsByRepoId"] = labelsByRepoId
count := models.CountUserRepositories(ctxUser.ID, showPrivate)
c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)

5
templates/explore/repo_list.tmpl

@ -16,6 +16,11 @@
<span class="text grey"><i class="octicon octicon-git-branch"></i> {{.NumForks}}</span>
</div>
</div>
<div class="ui labels">
{{range index $.LabelsByRepoId .ID}}
<a class="ui label" style="background-color:{{.Color}};color:{{.ForegroundColor}}" href="{{AppSubURL}}/{{$.Owner.Name}}/labels/{{.ID}}">{{.Name}}</a>
{{end}}
</div>
{{if .Description}}<p class="has-emoji">{{.Description | Str2html}}</p>{{end}}
<p class="time">{{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Updated $.i18n.Lang}}</p>
</div>

5
templates/repo/header.tmpl

@ -44,6 +44,11 @@
</div>
{{end}}
</div>
<div class="label-list">
{{range $.RepositoryLabels}}
<div class="ui label" style="background-color:{{.Color}}; color:{{.ForegroundColor}}">{{.Name}}</div>
{{end}}
</div>
</div><!-- end column -->
</div><!-- end grid -->
</div><!-- end container -->

Loading…
Cancel
Save