2019-09-03 17:46:24 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-09-03 17:46:24 +02:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-03-29 04:38:16 +01:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-12 16:48:20 +01:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-09-03 17:46:24 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 16:36:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-01-24 20:00:29 +01:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2024-02-27 08:12:22 +01:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 03:57:15 +01:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2019-09-03 17:46:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListTopics returns list of current topics for repo
|
|
|
|
func ListTopics(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/topics repository repoListTopics
|
|
|
|
// ---
|
|
|
|
// summary: Get list of topics that a repository has
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
2020-01-24 20:00:29 +01:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 06:57:38 +02:00
|
|
|
// description: page size of results
|
2020-01-24 20:00:29 +01:00
|
|
|
// type: integer
|
2019-09-03 17:46:24 +02:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TopicNames"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-09-03 17:46:24 +02:00
|
|
|
|
2021-12-12 16:48:20 +01:00
|
|
|
opts := &repo_model.FindTopicOptions{
|
2020-01-24 20:00:29 +01:00
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2021-08-12 14:43:08 +02:00
|
|
|
}
|
|
|
|
|
2024-03-29 04:38:16 +01:00
|
|
|
topics, total, err := db.FindAndCount[repo_model.Topic](ctx, opts)
|
2019-09-03 17:46:24 +02:00
|
|
|
if err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicNames := make([]string, len(topics))
|
|
|
|
for i, topic := range topics {
|
|
|
|
topicNames[i] = topic.Name
|
|
|
|
}
|
2021-08-12 14:43:08 +02:00
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(total)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
2019-09-03 17:46:24 +02:00
|
|
|
"topics": topicNames,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTopics updates repo with a new set of topics
|
2021-01-26 16:36:53 +01:00
|
|
|
func UpdateTopics(ctx *context.APIContext) {
|
2019-09-03 17:46:24 +02:00
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/topics repository repoUpdateTopics
|
|
|
|
// ---
|
|
|
|
// summary: Replace list of topics for a repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: body
|
|
|
|
// in: body
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/RepoTopicOptions"
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 18:07:12 +01:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
2019-09-03 17:46:24 +02:00
|
|
|
|
2021-01-26 16:36:53 +01:00
|
|
|
form := web.GetForm(ctx).(*api.RepoTopicOptions)
|
2019-09-03 17:46:24 +02:00
|
|
|
topicNames := form.Topics
|
2021-12-12 16:48:20 +01:00
|
|
|
validTopics, invalidTopics := repo_model.SanitizeAndValidateTopics(topicNames)
|
2019-09-03 17:46:24 +02:00
|
|
|
|
|
|
|
if len(validTopics) > 25 {
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
|
2019-09-03 17:46:24 +02:00
|
|
|
"invalidTopics": nil,
|
|
|
|
"message": "Exceeding maximum number of topics per repo",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(invalidTopics) > 0 {
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
|
2019-09-03 17:46:24 +02:00
|
|
|
"invalidTopics": invalidTopics,
|
|
|
|
"message": "Topic names are invalid",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:39:12 +02:00
|
|
|
err := repo_model.SaveTopics(ctx, ctx.Repo.Repository.ID, validTopics...)
|
2019-09-03 17:46:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("SaveTopics failed: %v", err)
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTopic adds a topic name to a repo
|
|
|
|
func AddTopic(ctx *context.APIContext) {
|
2021-08-01 18:28:05 +02:00
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/topics/{topic} repository repoAddTopic
|
2019-09-03 17:46:24 +02:00
|
|
|
// ---
|
|
|
|
// summary: Add a topic to a repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: topic
|
|
|
|
// in: path
|
|
|
|
// description: name of the topic to add
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 18:07:12 +01:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
2019-09-03 17:46:24 +02:00
|
|
|
|
2024-06-19 00:32:45 +02:00
|
|
|
topicName := strings.TrimSpace(strings.ToLower(ctx.PathParam(":topic")))
|
2019-09-03 17:46:24 +02:00
|
|
|
|
2021-12-12 16:48:20 +01:00
|
|
|
if !repo_model.ValidateTopic(topicName) {
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
|
2019-12-20 18:07:12 +01:00
|
|
|
"invalidTopics": topicName,
|
|
|
|
"message": "Topic name is invalid",
|
|
|
|
})
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent adding more topics than allowed to repo
|
2024-03-29 04:38:16 +01:00
|
|
|
count, err := db.Count[repo_model.Topic](ctx, &repo_model.FindTopicOptions{
|
2019-09-03 17:46:24 +02:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-08-12 14:43:08 +02:00
|
|
|
log.Error("CountTopics failed: %v", err)
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-12 14:43:08 +02:00
|
|
|
if count >= 25 {
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
|
2019-09-03 17:46:24 +02:00
|
|
|
"message": "Exceeding maximum allowed topics per repo.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:39:12 +02:00
|
|
|
_, err = repo_model.AddTopic(ctx, ctx.Repo.Repository.ID, topicName)
|
2019-09-03 17:46:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("AddTopic failed: %v", err)
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTopic removes topic name from repo
|
|
|
|
func DeleteTopic(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/topics/{topic} repository repoDeleteTopic
|
|
|
|
// ---
|
|
|
|
// summary: Delete a topic from a repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: topic
|
|
|
|
// in: path
|
|
|
|
// description: name of the topic to delete
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 18:07:12 +01:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
|
|
|
|
2024-06-19 00:32:45 +02:00
|
|
|
topicName := strings.TrimSpace(strings.ToLower(ctx.PathParam(":topic")))
|
2019-09-03 17:46:24 +02:00
|
|
|
|
2021-12-12 16:48:20 +01:00
|
|
|
if !repo_model.ValidateTopic(topicName) {
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]any{
|
2019-12-20 18:07:12 +01:00
|
|
|
"invalidTopics": topicName,
|
|
|
|
"message": "Topic name is invalid",
|
|
|
|
})
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-16 16:39:12 +02:00
|
|
|
topic, err := repo_model.DeleteTopic(ctx, ctx.Repo.Repository.ID, topicName)
|
2019-09-03 17:46:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("DeleteTopic failed: %v", err)
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic == nil {
|
|
|
|
ctx.NotFound()
|
2022-07-13 19:39:19 +02:00
|
|
|
return
|
2019-09-03 17:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TopicSearch search for creating topic
|
2020-01-24 20:00:29 +01:00
|
|
|
func TopicSearch(ctx *context.APIContext) {
|
2019-09-03 17:46:24 +02:00
|
|
|
// swagger:operation GET /topics/search repository topicSearch
|
|
|
|
// ---
|
|
|
|
// summary: search topics via keyword
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: q
|
|
|
|
// in: query
|
|
|
|
// description: keywords to search
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2020-01-24 20:00:29 +01:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 06:57:38 +02:00
|
|
|
// description: page size of results
|
2020-01-24 20:00:29 +01:00
|
|
|
// type: integer
|
2019-09-03 17:46:24 +02:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TopicListResponse"
|
2019-12-20 18:07:12 +01:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 18:07:12 +01:00
|
|
|
|
2021-12-12 16:48:20 +01:00
|
|
|
opts := &repo_model.FindTopicOptions{
|
2021-08-12 14:43:08 +02:00
|
|
|
Keyword: ctx.FormString("q"),
|
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
2019-09-03 17:46:24 +02:00
|
|
|
}
|
|
|
|
|
2024-03-29 04:38:16 +01:00
|
|
|
topics, total, err := db.FindAndCount[repo_model.Topic](ctx, opts)
|
2019-09-03 17:46:24 +02:00
|
|
|
if err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 17:46:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicResponses := make([]*api.TopicResponse, len(topics))
|
|
|
|
for i, topic := range topics {
|
|
|
|
topicResponses[i] = convert.ToTopicResponse(topic)
|
|
|
|
}
|
2021-08-12 14:43:08 +02:00
|
|
|
|
|
|
|
ctx.SetTotalCountHeader(total)
|
2023-07-04 20:36:08 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]any{
|
2019-09-03 17:46:24 +02:00
|
|
|
"topics": topicResponses,
|
|
|
|
})
|
|
|
|
}
|