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