From 55724edecf8a4b6079eb70c52a4c3d27cdd64f85 Mon Sep 17 00:00:00 2001 From: isundil Date: Sat, 7 Apr 2018 16:44:42 +0000 Subject: [PATCH] #1253 : Remove repository labels --- cmd/web.go | 1 + models/repo.go | 22 ++++++++++++++++++---- public/js/gogs.js | 22 ++++++++++++++++++++++ routes/user/setting.go | 15 +++++++++++++-- templates/user/settings/repositories.tmpl | 11 ++++++++++- 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index b67adb37d..000525a33 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -231,6 +231,7 @@ func runWeb(c *cli.Context) error { m.Get("", user.SettingsRepos) m.Post("/leave", user.SettingsLeaveRepo) m.Post("/labels", user.SettingsAddOrEditRepoLabel) + m.Post("/labels/delete", user.SettingsDeleteRepoLabel) }) m.Group("/organizations", func() { m.Get("", user.SettingsOrganizations) diff --git a/models/repo.go b/models/repo.go index acbb33e8b..2adf3bdbf 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1692,12 +1692,11 @@ func UpdateRepositoryLabel(id int64, owner *User, opts *CreateRepoLabelOptions) } // FIXME check color is /#[0-9A-Fa-F]{6}/ - repoLabel, err := GetRepositoryLabel(id) + repoLabel, err := GetRepositoryLabel(id, owner) if err != nil { return nil, err } - // FIXME check user.ID is repoLabel.OwnerID repoLabel.Name = opts.Name repoLabel.Color = opts.Color repoLabel.IsPrivate = opts.IsPrivate @@ -1720,6 +1719,21 @@ func UpdateRepositoryLabel(id int64, owner *User, opts *CreateRepoLabelOptions) 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) { labels := make([]*RepositoryLabel, 0, 5) if err := x.Where("owner_id = ?", userID).Find(&labels); err != nil { @@ -1729,10 +1743,10 @@ func GetRepositoryLabels(userID int64) ([]*RepositoryLabel, error) { return labels, nil } -func GetRepositoryLabel(labelId int64) (*RepositoryLabel, error) { +func GetRepositoryLabel(labelId int64, owner *User) (*RepositoryLabel, error) { 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) } else if !has { return nil, fmt.Errorf("Label %d not found", labelId) diff --git a/public/js/gogs.js b/public/js/gogs.js index a7d643f3a..3ff99483f 100644 --- a/public/js/gogs.js +++ b/public/js/gogs.js @@ -1132,6 +1132,28 @@ function initUserSettings() { $('.color-picker').val(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; + }); } } diff --git a/routes/user/setting.go b/routes/user/setting.go index 39349fbef..6b4ed3cf9 100644 --- a/routes/user/setting.go +++ b/routes/user/setting.go @@ -8,7 +8,6 @@ import ( "bytes" "encoding/base64" "fmt" - "strconv" "html/template" "image/png" "io/ioutil" @@ -539,7 +538,8 @@ func SettingsRepos(c *context.Context) { } func SettingsAddOrEditRepoLabel(c *context.Context) { - id, _ := strconv.ParseInt(c.Query("id"), 10, 64) + id := c.QueryInt64("id") + repoLabelOptions := &models.CreateRepoLabelOptions{ Name: c.Query("name"), 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) { repo, err := models.GetRepositoryByID(c.QueryInt64("id")) if err != nil { diff --git a/templates/user/settings/repositories.tmpl b/templates/user/settings/repositories.tmpl index 9e025cea0..2f34280bf 100644 --- a/templates/user/settings/repositories.tmpl +++ b/templates/user/settings/repositories.tmpl @@ -54,7 +54,7 @@ {{.Name}}
- {{$.i18n.Tr "repo.label_delete"}} + {{$.i18n.Tr "repo.label_delete"}} {{$.i18n.Tr "repo.issues.label_edit"}} {{$.i18n.Tr "repo.repositories" .NumRepo}}
@@ -99,6 +99,15 @@ +