Browse Source

#1253 : add label to repositories

pull/5260/head
isundil 7 years ago
parent
commit
e4fcd6b7ad
  1. 31
      models/repo.go
  2. 15
      public/js/gogs.js
  3. 26
      routes/repo/setting.go
  4. 25
      templates/repo/settings/options.tmpl

31
models/repo.go

@ -1741,6 +1741,7 @@ func DeleteRepositoryLabel(id int64, owner *User) (err error) {
if _, err = sess.Delete(label); err != nil {
return fmt.Errorf("remove repository label '%d': %v", id, err)
}
sess.Where("label_id = ?", id).Delete(&RepositoryRepoLabel{})
return sess.Commit()
}
@ -1765,21 +1766,41 @@ func GetRepositoryLabelsForRepository(repo *Repository) ([]*RepositoryLabel, err
return labels, nil
}
func RemoveRepoLabelFromRepository(repo *Repository, labelID int64, user *User) (err error) {
func RemoveRepoLabelFromRepository(repo *Repository, label *RepositoryLabel, user *User) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
// FIXME check that user is OwnerID of the label and admin on the repository
label := &RepositoryRepoLabel{ RepositoryID: repo.ID, LabelID: labelID }
if _, err = sess.Delete(label); err != nil {
return fmt.Errorf("remove label '%d' for repository '%d' : %v", labelID, repo.ID, err)
// FIXME check that user is admin on the repository
labelRel := &RepositoryRepoLabel{ RepositoryID: repo.ID, LabelID: label.ID }
if _, err = sess.Delete(labelRel); err != nil {
return fmt.Errorf("remove label '%d' for repository '%d' : %v", label.ID, repo.ID, err)
}
return sess.Commit()
}
func AddRepoLabelToRepository(repo *Repository, label *RepositoryLabel, user *User) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
// FIXME check that user is admin on the repository
repoLabel := &RepositoryRepoLabel {
RepositoryID: repo.ID,
LabelID: label.ID,
}
if _, err = sess.Insert(repoLabel); err != nil {
return err
}
return sess.Commit()
}
func GetRepositoryLabel(labelId int64, owner *User) (*RepositoryLabel, error) {
label := new(RepositoryLabel)

15
public/js/gogs.js

@ -266,6 +266,21 @@ function initRepository() {
$('.repository-label-to-remove').val(removeLabels.join(';'));
}
});
$('.repository-available-label-list .menu').click(function(e) {
var target = e.target;
while (target != e.currentTarget) {
var $target = $(target);
if ($target.hasClass('item')) {
addLabels.push($target.data("labelid"));
$('.repository-label-to-add').val(addLabels.join(';'));
$target.remove();
$('.repository-label-list').append("<span><span class=\"column\"><span class=\"ui label\" style=\"background-color: " +$target.data('color') +"; color: " +$target.data('foreground-color') +"\">" +$target.data('name') +"</span></span></span>");
break;
}
target = target.parentElement;
}
});
}
// Branches

26
routes/repo/setting.go

@ -41,7 +41,25 @@ func Settings(c *context.Context) {
c.ServerError("GetUserAndCollaborativeRepositories", err)
return
}
availableLabels := make([]*models.RepositoryLabel, 0, 10)
if allRepositoryLabels, err := models.GetRepositoryLabels(c.User.ID); err == nil {
for i := 0; i < len(allRepositoryLabels); i++ {
found := false
for j := 0; j < len(repoLabels) && !found; j++ {
if repoLabels[j].ID == allRepositoryLabels[i].ID {
found = true;
}
}
if !found {
availableLabels = append(availableLabels, allRepositoryLabels[i])
}
}
} else {
c.ServerError("GetUserAndCollaborativeRepositories", err)
return
}
c.Data["RepositoryLabels"] = repoLabels
c.Data["AvailableLabels"] = availableLabels
c.Success(SETTINGS_OPTIONS)
}
@ -147,13 +165,17 @@ 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 {
models.RemoveRepoLabelFromRepository(c.Repo.Repository, labelId, c.User)
if label, err := models.GetRepositoryLabel(labelId, c.User); err == nil {
models.RemoveRepoLabelFromRepository(c.Repo.Repository, label, c.User)
}
}
}
repositoryLabelsToAdd := strings.Split(c.Query("repositoryLabelsToAdd"), ";")
for i := 0; i < len(repositoryLabelsToAdd) ; i++ {
if labelId, err := strconv.ParseInt(repositoryLabelsToAdd[i], 10, 64); err == nil {
// FIXME models.AddRepoLabelToRepository(c.Repo.Repository, labelId, c.User)
if label, err := models.GetRepositoryLabel(labelId, c.User); err == nil {
models.AddRepoLabelToRepository(c.Repo.Repository, label, c.User)
}
}
}
c.Redirect(repo.Link() + "/settings")

25
templates/repo/settings/options.tmpl

@ -99,16 +99,27 @@
<input type="hidden" name="repositoryLabelsToRemove" value="" class="repository-label-to-remove"/>
<div class="field repository-label-list">
{{range .RepositoryLabels}}
<span class="">
<span class="column">
<span class="ui label" style="background-color: {{.Color}}; color: {{.ForegroundColor}}" data-labelid="{{.ID}}">
{{.Name}}
<i class="delete button icon"></i>
</span>
<span><span class="column">
<span class="ui label" style="background-color: {{.Color}}; color: {{.ForegroundColor}}" data-labelid="{{.ID}}">
{{.Name}}
<i class="delete button icon"></i>
</span>
</span>
</span></span>
{{end}}
</div>
<div class="field repository-available-label-list">
<div class="ui floating jump select-label dropdown" tabindex="0">
<span class="text">
<strong>{{$.i18n.Tr "repo.settings.labels_add"}}</strong>
<span class="octicon octicon-plus"></span>
</span>
<div class="filter menu transition hidden">
{{range .AvailableLabels}}
<a class="item" href="#" data-labelid="{{.ID}}" data-color="{{.Color}}" data-name="{{.Name}}" data-foreground-color="{{.ForegroundColor}}"><span class="label color" style="background-color: {{.Color}}; color: {{.ForegroundColor}}">&nbsp;</span>{{.Name}}</a>
{{end}}
</div>
</div>
</div>
<div class="field">
<button class="ui green button submit">{{$.i18n.Tr "repo.settings.update_settings"}}</button>
</div>

Loading…
Cancel
Save