Skip to content

Commit 8daf641

Browse files
Ignore if topic is sent twice in same request, refactoring.
Signed-off-by: David Svantesson <[email protected]>
1 parent 6ec5a0c commit 8daf641

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

models/topic.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,31 @@ func ValidateTopic(topic string) bool {
6060
}
6161

6262
// SanitizeAndValidateTopics sanitizes and checks an array or topics
63-
func SanitizeAndValidateTopics(topics []string) (invalidTopics []string) {
63+
func SanitizeAndValidateTopics(topics []string) (validTopics []string, invalidTopics []string) {
64+
validTopics = make([]string, 0)
6465
invalidTopics = make([]string, 0)
6566

66-
i := 0
67+
LOOP_TOPICS:
6768
for _, topic := range topics {
6869
topic = strings.TrimSpace(strings.ToLower(topic))
6970
// ignore empty string
7071
if len(topic) == 0 {
71-
continue
72+
continue LOOP_TOPICS
73+
}
74+
// ignore same topic twice
75+
for _, vTopic := range validTopics {
76+
if topic == vTopic {
77+
continue LOOP_TOPICS
78+
}
7279
}
73-
topics[i] = topic
74-
i++
75-
if !ValidateTopic(topic) {
80+
if ValidateTopic(topic) {
81+
validTopics = append(validTopics, topic)
82+
} else {
7683
invalidTopics = append(invalidTopics, topic)
7784
}
7885
}
79-
topics = topics[:i]
8086

81-
return invalidTopics
87+
return validTopics, invalidTopics
8288
}
8389

8490
// GetTopicByName retrieves topic by name

routers/api/v1/repo/topic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) {
8484
// "$ref": "#/responses/empty"
8585

8686
topicNames := form.Topics
87-
invalidTopics := models.SanitizeAndValidateTopics(topicNames)
87+
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
8888

89-
if len(topicNames) > 25 {
89+
if len(validTopics) > 25 {
9090
ctx.JSON(422, map[string]interface{}{
91-
"invalidTopics": topicNames[:0],
91+
"invalidTopics": nil,
9292
"message": "Exceeding maximum number of topics per repo",
9393
})
9494
return
@@ -102,7 +102,7 @@ func UpdateTopics(ctx *context.APIContext, form api.RepoTopicOptions) {
102102
return
103103
}
104104

105-
err := models.SaveTopics(ctx.Repo.Repository.ID, topicNames...)
105+
err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
106106
if err != nil {
107107
log.Error("SaveTopics failed: %v", err)
108108
ctx.JSON(500, map[string]interface{}{

routers/repo/topic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func TopicsPost(ctx *context.Context) {
2727
topics = strings.Split(topicsStr, ",")
2828
}
2929

30-
invalidTopics := models.SanitizeAndValidateTopics(topics)
30+
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topics)
3131

32-
if len(topics) > 25 {
32+
if len(validTopics) > 25 {
3333
ctx.JSON(422, map[string]interface{}{
34-
"invalidTopics": topics[:0],
34+
"invalidTopics": nil,
3535
"message": ctx.Tr("repo.topic.count_prompt"),
3636
})
3737
return
@@ -45,7 +45,7 @@ func TopicsPost(ctx *context.Context) {
4545
return
4646
}
4747

48-
err := models.SaveTopics(ctx.Repo.Repository.ID, topics...)
48+
err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
4949
if err != nil {
5050
log.Error("SaveTopics failed: %v", err)
5151
ctx.JSON(500, map[string]interface{}{

0 commit comments

Comments
 (0)