You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

topic.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "net/http"
  6. "strings"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // TopicsPost response for creating repository
  12. func TopicsPost(ctx *context.Context) {
  13. if ctx.Doer == nil {
  14. ctx.JSON(http.StatusForbidden, map[string]any{
  15. "message": "Only owners could change the topics.",
  16. })
  17. return
  18. }
  19. topics := make([]string, 0)
  20. topicsStr := ctx.FormTrim("topics")
  21. if len(topicsStr) > 0 {
  22. topics = strings.Split(topicsStr, ",")
  23. }
  24. validTopics, invalidTopics := repo_model.SanitizeAndValidateTopics(topics)
  25. if len(validTopics) > 25 {
  26. ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
  27. "invalidTopics": nil,
  28. "message": ctx.Tr("repo.topic.count_prompt"),
  29. })
  30. return
  31. }
  32. if len(invalidTopics) > 0 {
  33. ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
  34. "invalidTopics": invalidTopics,
  35. "message": ctx.Tr("repo.topic.format_prompt"),
  36. })
  37. return
  38. }
  39. err := repo_model.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
  40. if err != nil {
  41. log.Error("SaveTopics failed: %v", err)
  42. ctx.JSON(http.StatusInternalServerError, map[string]any{
  43. "message": "Save topics failed.",
  44. })
  45. return
  46. }
  47. ctx.JSON(http.StatusOK, map[string]any{
  48. "status": "ok",
  49. })
  50. }