summaryrefslogtreecommitdiffstats
path: root/routers/repo
diff options
context:
space:
mode:
authorAlexey Terentyev <axifnx@gmail.com>2018-06-21 12:09:46 +0300
committerLunny Xiao <xiaolunwen@gmail.com>2018-06-21 17:09:46 +0800
commit46d19c4676efe5201c5de790bcb963bfc93a95c7 (patch)
treeb99878b6b1b52bc628254e74e9c966a806f61efe /routers/repo
parent9ae7664df7caa24825cc4cee4e4121e9f1d73e59 (diff)
downloadgitea-46d19c4676efe5201c5de790bcb963bfc93a95c7.tar.gz
gitea-46d19c4676efe5201c5de790bcb963bfc93a95c7.zip
Fix topics addition (Another solution) (#4031) (#4258)
* Added topics validation, fixed repo topics duplication (#4031) Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Added tests Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Fixed fmt Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Added comments to exported functions Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Deleted RemoveDuplicateTopics function Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Fixed messages Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Added migration Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * fmt migration file Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * fixed lint Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Added Copyright Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Added query solution for duplicates Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Fixed migration query Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Changed RegExp. Fixed migration Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * fmt migration file Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Fixed test for changed regexp Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Removed validation log messages Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Renamed migration file Signed-off-by: Alexey Terentyev <axifnx@gmail.com> * Renamed validate function Signed-off-by: Alexey Terentyev <axifnx@gmail.com>
Diffstat (limited to 'routers/repo')
-rw-r--r--routers/repo/topic.go35
1 files changed, 33 insertions, 2 deletions
diff --git a/routers/repo/topic.go b/routers/repo/topic.go
index 2a43d53ff0..63fcf793f9 100644
--- a/routers/repo/topic.go
+++ b/routers/repo/topic.go
@@ -12,8 +12,8 @@ import (
"code.gitea.io/gitea/modules/log"
)
-// TopicPost response for creating repository
-func TopicPost(ctx *context.Context) {
+// TopicsPost response for creating repository
+func TopicsPost(ctx *context.Context) {
if ctx.User == nil {
ctx.JSON(403, map[string]interface{}{
"message": "Only owners could change the topics.",
@@ -27,6 +27,37 @@ func TopicPost(ctx *context.Context) {
topics = strings.Split(topicsStr, ",")
}
+ 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.ValidateTopic(topic) {
+ invalidTopics = append(invalidTopics, topic)
+ }
+ }
+ topics = topics[:i]
+
+ if len(topics) > 25 {
+ ctx.JSON(422, map[string]interface{}{
+ "invalidTopics": topics[:0],
+ "message": ctx.Tr("repo.topic.count_prompt"),
+ })
+ return
+ }
+
+ if len(invalidTopics) > 0 {
+ ctx.JSON(422, map[string]interface{}{
+ "invalidTopics": invalidTopics,
+ "message": ctx.Tr("repo.topic.format_prompt"),
+ })
+ return
+ }
+
err := models.SaveTopics(ctx.Repo.Repository.ID, topics...)
if err != nil {
log.Error(2, "SaveTopics failed: %v", err)