From 630ca5901c199da5e309868b1b872f622946882d Mon Sep 17 00:00:00 2001 From: Alexey Terentyev Date: Sun, 17 Jun 2018 14:48:20 +0300 Subject: [PATCH] Deleted RemoveDuplicateTopics function Signed-off-by: Alexey Terentyev --- models/topic.go | 16 ---------------- models/topic_test.go | 15 --------------- public/js/index.js | 5 +++-- routers/repo/topic.go | 24 +++++++++++++++--------- 4 files changed, 18 insertions(+), 42 deletions(-) diff --git a/models/topic.go b/models/topic.go index 0a67fdd2f..d4610c52e 100644 --- a/models/topic.go +++ b/models/topic.go @@ -59,22 +59,6 @@ func TopicValidator(topic string) bool { return len(topic) <= 35 && topicPattern.MatchString(topic) } -// RemoveDuplicateTopics remove duplicates from topics slice -func RemoveDuplicateTopics(topics []string) []string { - // Map to record duplicates - saved := make(map[string]struct{}, len(topics)) - i := 0 - for _, v := range topics { - v = strings.TrimSpace(strings.ToLower(v)) - if _, ok := saved[v]; !ok { - saved[v] = struct{}{} - topics[i] = v - i++ - } - } - return topics[:i] -} - // GetTopicByName retrieves topic by name func GetTopicByName(name string) (*Topic, error) { var topic Topic diff --git a/models/topic_test.go b/models/topic_test.go index ecff6e083..93137fd72 100644 --- a/models/topic_test.go +++ b/models/topic_test.go @@ -5,7 +5,6 @@ package models import ( - "sort" "testing" "github.com/stretchr/testify/assert" @@ -66,17 +65,3 @@ func TestTopicValidator(t *testing.T) { assert.False(t, TopicValidator("#fifth,test;topic")) assert.False(t, TopicValidator("#sixth-go+project.topic+with+excess_length")) } - -func TestRemoveDuplicateTopics(t *testing.T) { - topics := []string{"first", "second", "eleventh", "third", "fourth", "fifth", "sixth", "second", "seventh", - "eleventh", "first", "eighth", "ninth", "sixth", "tenth", "eleventh"} - - expectedSlice := []string{"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh"} - - topics = RemoveDuplicateTopics(topics) - assert.Len(t, topics, 11) - - sort.Strings(topics) - sort.Strings(expectedSlice) - assert.EqualValues(t, expectedSlice, topics) -} diff --git a/public/js/index.js b/public/js/index.js index e98a3fe6d..6c760e367 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -2336,8 +2336,9 @@ function initTopicbar() { }).done(function() { editDiv.hide(); viewDiv.show(); - }) - }) + }).fail(function(xhr) { + alert(xhr.responseJSON.message) + }); $('#topic_edit .dropdown').dropdown({ allowAdditions: true, diff --git a/routers/repo/topic.go b/routers/repo/topic.go index 013a5b769..85eaf674e 100644 --- a/routers/repo/topic.go +++ b/routers/repo/topic.go @@ -12,7 +12,7 @@ import ( "code.gitea.io/gitea/modules/log" ) -// TopicPost response for creating repository +// TopicsPost response for creating repository func TopicsPost(ctx *context.Context) { if ctx.User == nil { ctx.JSON(403, map[string]interface{}{ @@ -27,7 +27,20 @@ func TopicsPost(ctx *context.Context) { topics = strings.Split(topicsStr, ",") } - topics = models.RemoveDuplicateTopics(topics) + invalidTopics := make([]string, 0) + i := 0 + for _, topic := range topics { + topic = strings.TrimSpace(strings.ToLower(topic)) + // ignore empty string + if len(topic) > 0 { + topics[i] = topic + i++ + } + if !models.TopicValidator(topic) { + invalidTopics = append(invalidTopics, topic) + } + } + topics = topics[:i] if len(topics) > 25 { log.Error(2, "Incorrect number of topics(max 25)") @@ -38,13 +51,6 @@ func TopicsPost(ctx *context.Context) { return } - var invalidTopics = make([]string, 0) - for _, topic := range topics { - if !models.TopicValidator(topic) { - invalidTopics = append(invalidTopics, topic) - } - } - if len(invalidTopics) > 0 { log.Error(2, "Invalid topics: %v", invalidTopics) ctx.JSON(422, map[string]interface{}{