summaryrefslogtreecommitdiffstats
path: root/models/topic.go
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 /models/topic.go
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 'models/topic.go')
-rw-r--r--models/topic.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/models/topic.go b/models/topic.go
index 3b1737f8af..247aac5fff 100644
--- a/models/topic.go
+++ b/models/topic.go
@@ -6,6 +6,7 @@ package models
import (
"fmt"
+ "regexp"
"strings"
"code.gitea.io/gitea/modules/util"
@@ -20,6 +21,8 @@ func init() {
)
}
+var topicPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`)
+
// Topic represents a topic of repositories
type Topic struct {
ID int64
@@ -51,6 +54,11 @@ func (err ErrTopicNotExist) Error() string {
return fmt.Sprintf("topic is not exist [name: %s]", err.Name)
}
+// ValidateTopic checks topics by length and match pattern rules
+func ValidateTopic(topic string) bool {
+ return len(topic) <= 35 && topicPattern.MatchString(topic)
+}
+
// GetTopicByName retrieves topic by name
func GetTopicByName(name string) (*Topic, error) {
var topic Topic
@@ -182,6 +190,13 @@ func SaveTopics(repoID int64, topicNames ...string) error {
}
}
+ topicNames = topicNames[:0]
+ if err := sess.Table("topic").Cols("name").
+ Join("INNER", "repo_topic", "topic.id = repo_topic.topic_id").
+ Where("repo_topic.repo_id = ?", repoID).Find(&topicNames); err != nil {
+ return err
+ }
+
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
Topics: topicNames,
}); err != nil {