Browse Source

#1253 : Remove repository labels

pull/5260/head
isundil 7 years ago
parent
commit
55724edecf
  1. 1
      cmd/web.go
  2. 22
      models/repo.go
  3. 22
      public/js/gogs.js
  4. 15
      routes/user/setting.go
  5. 11
      templates/user/settings/repositories.tmpl

1
cmd/web.go

@ -231,6 +231,7 @@ func runWeb(c *cli.Context) error {
m.Get("", user.SettingsRepos) m.Get("", user.SettingsRepos)
m.Post("/leave", user.SettingsLeaveRepo) m.Post("/leave", user.SettingsLeaveRepo)
m.Post("/labels", user.SettingsAddOrEditRepoLabel) m.Post("/labels", user.SettingsAddOrEditRepoLabel)
m.Post("/labels/delete", user.SettingsDeleteRepoLabel)
}) })
m.Group("/organizations", func() { m.Group("/organizations", func() {
m.Get("", user.SettingsOrganizations) m.Get("", user.SettingsOrganizations)

22
models/repo.go

@ -1692,12 +1692,11 @@ func UpdateRepositoryLabel(id int64, owner *User, opts *CreateRepoLabelOptions)
} }
// FIXME check color is /#[0-9A-Fa-F]{6}/ // FIXME check color is /#[0-9A-Fa-F]{6}/
repoLabel, err := GetRepositoryLabel(id) repoLabel, err := GetRepositoryLabel(id, owner)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// FIXME check user.ID is repoLabel.OwnerID
repoLabel.Name = opts.Name repoLabel.Name = opts.Name
repoLabel.Color = opts.Color repoLabel.Color = opts.Color
repoLabel.IsPrivate = opts.IsPrivate repoLabel.IsPrivate = opts.IsPrivate
@ -1720,6 +1719,21 @@ func UpdateRepositoryLabel(id int64, owner *User, opts *CreateRepoLabelOptions)
return repoLabel, sess.Commit() return repoLabel, sess.Commit()
} }
func DeleteRepositoryLabel(id int64, owner *User) (err error) {
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
// TODO remove Repository <> RepositoryLabel relationship
label := &RepositoryLabel{ID: id, OwnerID: owner.ID}
if _, err = sess.Delete(label); err != nil {
return fmt.Errorf("remove repository label '%d': %v", id, err)
}
return sess.Commit()
}
func GetRepositoryLabels(userID int64) ([]*RepositoryLabel, error) { func GetRepositoryLabels(userID int64) ([]*RepositoryLabel, error) {
labels := make([]*RepositoryLabel, 0, 5) labels := make([]*RepositoryLabel, 0, 5)
if err := x.Where("owner_id = ?", userID).Find(&labels); err != nil { if err := x.Where("owner_id = ?", userID).Find(&labels); err != nil {
@ -1729,10 +1743,10 @@ func GetRepositoryLabels(userID int64) ([]*RepositoryLabel, error) {
return labels, nil return labels, nil
} }
func GetRepositoryLabel(labelId int64) (*RepositoryLabel, error) { func GetRepositoryLabel(labelId int64, owner *User) (*RepositoryLabel, error) {
label := new(RepositoryLabel) label := new(RepositoryLabel)
if has, err := x.Where("ID = ?", labelId).Get(label); err != nil { if has, err := x.Where("ID = ? AND ownerID = ?", labelId, owner.ID).Get(label); err != nil {
return nil, fmt.Errorf("select repository labels: %v", err) return nil, fmt.Errorf("select repository labels: %v", err)
} else if !has { } else if !has {
return nil, fmt.Errorf("Label %d not found", labelId) return nil, fmt.Errorf("Label %d not found", labelId)

22
public/js/gogs.js

@ -1132,6 +1132,28 @@ function initUserSettings() {
$('.color-picker').val(color); $('.color-picker').val(color);
$('.minicolors-swatch-color').css("background-color", color); $('.minicolors-swatch-color').css("background-color", color);
}); });
// Delete button
$('.delete-label-button').click(function () {
var $this = $(this);
$('.delete-label.modal').modal({
closable: false,
onApprove: function () {
if ($this.data('type') == "form") {
$($this.data('form')).submit();
return;
}
$.post($this.data('url'), {
"_csrf": csrf,
"id": $this.data("id")
}).success(function (data) {
window.location.href = data.redirect;
});
}
}).modal('show');
return false;
});
} }
} }

15
routes/user/setting.go

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strconv"
"html/template" "html/template"
"image/png" "image/png"
"io/ioutil" "io/ioutil"
@ -539,7 +538,8 @@ func SettingsRepos(c *context.Context) {
} }
func SettingsAddOrEditRepoLabel(c *context.Context) { func SettingsAddOrEditRepoLabel(c *context.Context) {
id, _ := strconv.ParseInt(c.Query("id"), 10, 64) id := c.QueryInt64("id")
repoLabelOptions := &models.CreateRepoLabelOptions{ repoLabelOptions := &models.CreateRepoLabelOptions{
Name: c.Query("name"), Name: c.Query("name"),
Color: c.Query("color"), Color: c.Query("color"),
@ -562,6 +562,17 @@ func SettingsAddOrEditRepoLabel(c *context.Context) {
} }
} }
func SettingsDeleteRepoLabel(c *context.Context) {
if err := models.DeleteRepositoryLabel(c.QueryInt64("id"), c.User); err != nil {
//c.NotFound()
c.ServerError("", err)
return
}
c.JSONSuccess(map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/repositories",
})
}
func SettingsLeaveRepo(c *context.Context) { func SettingsLeaveRepo(c *context.Context) {
repo, err := models.GetRepositoryByID(c.QueryInt64("id")) repo, err := models.GetRepositoryByID(c.QueryInt64("id"))
if err != nil { if err != nil {

11
templates/user/settings/repositories.tmpl

@ -54,7 +54,7 @@
{{.Name}} {{.Name}}
</span> </span>
<div class="ui right"> <div class="ui right">
<a class="ui right delete-button" href="#" data-url="{{$.Link}}/labels/delete" data-id="{{.ID}}"><i class="octicon octicon-trashcan"></i> {{$.i18n.Tr "repo.label_delete"}}</a> <a class="ui right delete-label-button" href="#" data-url="{{$.Link}}/labels/delete" data-id="{{.ID}}"><i class="octicon octicon-trashcan"></i> {{$.i18n.Tr "repo.label_delete"}}</a>
<a class="ui right edit-label-button" href="#" data-id={{.ID}} data-name={{.Name}} data-color={{.Color}}><i class="octicon octicon-pencil"></i> {{$.i18n.Tr "repo.issues.label_edit"}}</a> <a class="ui right edit-label-button" href="#" data-id={{.ID}} data-name={{.Name}} data-color={{.Color}}><i class="octicon octicon-pencil"></i> {{$.i18n.Tr "repo.issues.label_edit"}}</a>
<a class="ui right repositories" href="/issues?labels={{.ID}}"><i class="octicon octicon-repo"></i> {{$.i18n.Tr "repo.repositories" .NumRepo}}</a> <a class="ui right repositories" href="/issues?labels={{.ID}}"><i class="octicon octicon-repo"></i> {{$.i18n.Tr "repo.repositories" .NumRepo}}</a>
</div> </div>
@ -99,6 +99,15 @@
</div> </div>
</div> </div>
<div class="ui small basic delete-label modal">
<div class="ui icon header">
{{.i18n.Tr "settings.repos.delete_label_header"}}
</div>
<div class="content">
<p>{{.i18n.Tr "settings.repos.delete_label_content"}}</p>
</div>
{{template "base/delete_modal_actions" .}}
</div>
<div class="ui small basic delete modal"> <div class="ui small basic delete modal">
<div class="ui icon header"> <div class="ui icon header">
{{.i18n.Tr "settings.repos.leave_title"}} {{.i18n.Tr "settings.repos.leave_title"}}

Loading…
Cancel
Save