2019-09-03 11:46:24 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-11-09 23:41:51 -05:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-09-03 11:46:24 -04:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 10:36:53 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-01-24 14:00:29 -05:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2019-09-03 11:46:24 -04: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 14:00:29 -05:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 00:57:38 -04:00
|
|
|
// description: page size of results
|
2020-01-24 14:00:29 -05:00
|
|
|
// type: integer
|
2019-09-03 11:46:24 -04:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TopicNames"
|
|
|
|
|
|
|
|
topics, err := models.FindTopics(&models.FindTopicOptions{
|
2020-01-24 14:00:29 -05:00
|
|
|
ListOptions: utils.GetListOptions(ctx),
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2019-09-03 11:46:24 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("ListTopics failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicNames := make([]string, len(topics))
|
|
|
|
for i, topic := range topics {
|
|
|
|
topicNames[i] = topic.Name
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
|
|
|
"topics": topicNames,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTopics updates repo with a new set of topics
|
2021-01-26 10:36:53 -05:00
|
|
|
func UpdateTopics(ctx *context.APIContext) {
|
2019-09-03 11:46:24 -04: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"
|
2019-12-20 12:07:12 -05:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
2019-09-03 11:46:24 -04:00
|
|
|
|
2021-01-26 10:36:53 -05:00
|
|
|
form := web.GetForm(ctx).(*api.RepoTopicOptions)
|
2019-09-03 11:46:24 -04:00
|
|
|
topicNames := form.Topics
|
|
|
|
validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames)
|
|
|
|
|
|
|
|
if len(validTopics) > 25 {
|
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
|
|
|
"invalidTopics": nil,
|
|
|
|
"message": "Exceeding maximum number of topics per repo",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(invalidTopics) > 0 {
|
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
|
|
|
"invalidTopics": invalidTopics,
|
|
|
|
"message": "Topic names are invalid",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := models.SaveTopics(ctx.Repo.Repository.ID, validTopics...)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SaveTopics failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTopic adds a topic name to a repo
|
|
|
|
func AddTopic(ctx *context.APIContext) {
|
2021-08-01 12:28:05 -04:00
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/topics/{topic} repository repoAddTopic
|
2019-09-03 11:46:24 -04: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"
|
2019-12-20 12:07:12 -05:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
2019-09-03 11:46:24 -04:00
|
|
|
|
|
|
|
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
|
|
|
|
|
|
|
if !models.ValidateTopic(topicName) {
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
|
|
|
"invalidTopics": topicName,
|
|
|
|
"message": "Topic name is invalid",
|
|
|
|
})
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent adding more topics than allowed to repo
|
|
|
|
topics, err := models.FindTopics(&models.FindTopicOptions{
|
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AddTopic failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(topics) >= 25 {
|
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
|
|
|
"message": "Exceeding maximum allowed topics per repo.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = models.AddTopic(ctx.Repo.Repository.ID, topicName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AddTopic failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04: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"
|
2019-12-20 12:07:12 -05:00
|
|
|
// "422":
|
|
|
|
// "$ref": "#/responses/invalidTopicsError"
|
|
|
|
|
2019-09-03 11:46:24 -04:00
|
|
|
topicName := strings.TrimSpace(strings.ToLower(ctx.Params(":topic")))
|
|
|
|
|
|
|
|
if !models.ValidateTopic(topicName) {
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.JSON(http.StatusUnprocessableEntity, map[string]interface{}{
|
|
|
|
"invalidTopics": topicName,
|
|
|
|
"message": "Topic name is invalid",
|
|
|
|
})
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topic, err := models.DeleteTopic(ctx.Repo.Repository.ID, topicName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("DeleteTopic failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if topic == nil {
|
|
|
|
ctx.NotFound()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TopicSearch search for creating topic
|
2020-01-24 14:00:29 -05:00
|
|
|
func TopicSearch(ctx *context.APIContext) {
|
2019-09-03 11:46:24 -04: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 14:00:29 -05:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
2020-06-09 00:57:38 -04:00
|
|
|
// description: page size of results
|
2020-01-24 14:00:29 -05:00
|
|
|
// type: integer
|
2019-09-03 11:46:24 -04:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TopicListResponse"
|
2019-12-20 12:07:12 -05:00
|
|
|
// "403":
|
|
|
|
// "$ref": "#/responses/forbidden"
|
|
|
|
|
2019-09-03 11:46:24 -04:00
|
|
|
if ctx.User == nil {
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.Error(http.StatusForbidden, "UserIsNil", "Only owners could change the topics.")
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:42:15 -04:00
|
|
|
kw := ctx.Form("q")
|
2019-09-03 11:46:24 -04:00
|
|
|
|
2020-01-24 14:00:29 -05:00
|
|
|
listOptions := utils.GetListOptions(ctx)
|
|
|
|
|
2019-09-03 11:46:24 -04:00
|
|
|
topics, err := models.FindTopics(&models.FindTopicOptions{
|
2020-01-24 14:00:29 -05:00
|
|
|
Keyword: kw,
|
|
|
|
ListOptions: listOptions,
|
2019-09-03 11:46:24 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error("SearchTopics failed: %v", err)
|
2019-12-20 12:07:12 -05:00
|
|
|
ctx.InternalServerError(err)
|
2019-09-03 11:46:24 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
topicResponses := make([]*api.TopicResponse, len(topics))
|
|
|
|
for i, topic := range topics {
|
|
|
|
topicResponses[i] = convert.ToTopicResponse(topic)
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
|
|
|
"topics": topicResponses,
|
|
|
|
})
|
|
|
|
}
|