From e4fcd6b7ad7f5d383ed133d3d87c9ed9ff40c370 Mon Sep 17 00:00:00 2001 From: isundil Date: Sat, 7 Apr 2018 21:47:25 +0000 Subject: [PATCH] #1253 : add label to repositories --- models/repo.go | 31 +++++++++++++++++++++++----- public/js/gogs.js | 15 ++++++++++++++ routes/repo/setting.go | 26 +++++++++++++++++++++-- templates/repo/settings/options.tmpl | 25 +++++++++++++++------- 4 files changed, 83 insertions(+), 14 deletions(-) diff --git a/models/repo.go b/models/repo.go index 3b8aaff35..b128ba118 100644 --- a/models/repo.go +++ b/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) diff --git a/public/js/gogs.js b/public/js/gogs.js index 1ab7ee4ca..6026916ca 100644 --- a/public/js/gogs.js +++ b/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("" +$target.data('name') +""); + break; + } + target = target.parentElement; + } + }); } // Branches diff --git a/routes/repo/setting.go b/routes/repo/setting.go index 748d73b0f..a994ac30f 100644 --- a/routes/repo/setting.go +++ b/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") diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index e2205617a..58db8839e 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -99,16 +99,27 @@
{{range .RepositoryLabels}} - - - - {{.Name}} - - + + + {{.Name}} + - + {{end}}
+
+ +