2019-02-07 13:00:52 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-02-07 13:00:52 +01:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-02-07 19:32:18 +01:00
|
|
|
"errors"
|
2021-06-17 18:04:10 +02:00
|
|
|
"fmt"
|
2019-06-08 16:31:11 +02:00
|
|
|
"net/http"
|
2019-02-07 13:00:52 +01:00
|
|
|
|
2021-02-07 19:32:18 +01:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-08-25 04:31:57 +02:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2019-08-23 18:40:30 +02:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-06-17 18:04:10 +02:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-02-25 20:07:07 +01:00
|
|
|
"code.gitea.io/gitea/routers/api/v1/utils"
|
2022-12-29 03:57:15 +01:00
|
|
|
"code.gitea.io/gitea/services/convert"
|
2021-02-07 19:32:18 +01:00
|
|
|
releaseservice "code.gitea.io/gitea/services/release"
|
2019-02-07 13:00:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListTags list all the tags of a repository
|
|
|
|
func ListTags(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/tags repository repoListTags
|
|
|
|
// ---
|
|
|
|
// summary: List a repository's tags
|
|
|
|
// 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-02-25 20:07:07 +01:00
|
|
|
// - name: page
|
|
|
|
// in: query
|
|
|
|
// description: page number of results to return (1-based)
|
|
|
|
// type: integer
|
|
|
|
// - name: limit
|
|
|
|
// in: query
|
|
|
|
// description: page size of results, default maximum page size is 50
|
|
|
|
// type: integer
|
2019-02-07 13:00:52 +01:00
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/TagList"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-12-20 18:07:12 +01:00
|
|
|
|
2020-02-25 20:07:07 +01:00
|
|
|
listOpts := utils.GetListOptions(ctx)
|
|
|
|
|
2021-08-12 14:43:08 +02:00
|
|
|
tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
|
2019-02-07 13:00:52 +01:00
|
|
|
if err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTags", err)
|
2019-02-07 13:00:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiTags := make([]*api.Tag, len(tags))
|
|
|
|
for i := range tags {
|
|
|
|
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:43:08 +02:00
|
|
|
ctx.SetTotalCountHeader(int64(total))
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.JSON(http.StatusOK, &apiTags)
|
2019-02-07 13:00:52 +01:00
|
|
|
}
|
2019-06-08 16:31:11 +02:00
|
|
|
|
2021-06-23 23:08:47 +02:00
|
|
|
// GetAnnotatedTag get the tag of a repository.
|
|
|
|
func GetAnnotatedTag(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/git/tags/{sha} repository GetAnnotatedTag
|
2019-06-08 16:31:11 +02:00
|
|
|
// ---
|
2019-08-26 21:52:04 +02:00
|
|
|
// summary: Gets the tag object of an annotated tag (not lightweight tags)
|
2019-06-08 16:31:11 +02:00
|
|
|
// 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: sha
|
|
|
|
// in: path
|
2019-08-26 21:52:04 +02:00
|
|
|
// description: sha of the tag. The Git tags API only supports annotated tag objects, not lightweight tags.
|
2019-06-08 16:31:11 +02:00
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/AnnotatedTag"
|
2019-12-20 18:07:12 +01:00
|
|
|
// "400":
|
|
|
|
// "$ref": "#/responses/error"
|
2023-09-13 04:37:54 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-06-08 16:31:11 +02:00
|
|
|
|
|
|
|
sha := ctx.Params("sha")
|
|
|
|
if len(sha) == 0 {
|
|
|
|
ctx.Error(http.StatusBadRequest, "", "SHA not provided")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
|
2019-06-08 16:31:11 +02:00
|
|
|
} else {
|
2022-01-12 21:37:46 +01:00
|
|
|
commit, err := tag.Commit(ctx.Repo.GitRepo)
|
2019-06-08 16:31:11 +02:00
|
|
|
if err != nil {
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.Error(http.StatusBadRequest, "GetAnnotatedTag", err)
|
2019-06-08 16:31:11 +02:00
|
|
|
}
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 14:37:34 +01:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx, ctx.Repo.Repository, tag, commit))
|
2019-06-08 16:31:11 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-07 19:32:18 +01:00
|
|
|
|
2021-06-23 23:08:47 +02:00
|
|
|
// GetTag get the tag of a repository
|
|
|
|
func GetTag(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/tags/{tag} repository repoGetTag
|
2021-02-07 19:32:18 +01:00
|
|
|
// ---
|
2021-06-23 23:08:47 +02:00
|
|
|
// summary: Get the tag of a repository by tag name
|
2021-02-07 19:32:18 +01:00
|
|
|
// 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: tag
|
|
|
|
// in: path
|
2021-06-23 23:08:47 +02:00
|
|
|
// description: name of tag
|
2021-02-07 19:32:18 +01:00
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
2021-06-23 23:08:47 +02:00
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Tag"
|
2021-02-07 19:32:18 +01:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2021-06-23 23:08:47 +02:00
|
|
|
tagName := ctx.Params("*")
|
2021-02-07 19:32:18 +01:00
|
|
|
|
2021-06-23 23:08:47 +02:00
|
|
|
tag, err := ctx.Repo.GitRepo.GetTag(tagName)
|
2021-02-07 19:32:18 +01:00
|
|
|
if err != nil {
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.NotFound(tagName)
|
2021-02-07 19:32:18 +01:00
|
|
|
return
|
|
|
|
}
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToTag(ctx.Repo.Repository, tag))
|
2021-02-07 19:32:18 +01:00
|
|
|
}
|
2021-06-17 18:04:10 +02:00
|
|
|
|
|
|
|
// CreateTag create a new git tag in a repository
|
|
|
|
func CreateTag(ctx *context.APIContext) {
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/tags repository repoCreateTag
|
|
|
|
// ---
|
|
|
|
// summary: Create a new git tag in 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/CreateTagOption"
|
|
|
|
// responses:
|
|
|
|
// "200":
|
2021-06-23 23:08:47 +02:00
|
|
|
// "$ref": "#/responses/Tag"
|
2021-06-17 18:04:10 +02:00
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2022-06-16 22:03:03 +02:00
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/empty"
|
2021-06-17 18:04:10 +02:00
|
|
|
// "409":
|
|
|
|
// "$ref": "#/responses/conflict"
|
|
|
|
form := web.GetForm(ctx).(*api.CreateTagOption)
|
|
|
|
|
|
|
|
// If target is not provided use default branch
|
|
|
|
if len(form.Target) == 0 {
|
|
|
|
form.Target = ctx.Repo.Repository.DefaultBranch
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := ctx.Repo.GitRepo.GetCommit(form.Target)
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
ctx.Error(http.StatusNotFound, "target not found", fmt.Errorf("target not found: %w", err))
|
2021-06-17 18:04:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if err := releaseservice.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, commit.ID.String(), form.TagName, form.Message); err != nil {
|
2021-06-17 18:04:10 +02:00
|
|
|
if models.IsErrTagAlreadyExists(err) {
|
|
|
|
ctx.Error(http.StatusConflict, "tag exist", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-16 22:03:03 +02:00
|
|
|
if models.IsErrProtectedTagName(err) {
|
|
|
|
ctx.Error(http.StatusMethodNotAllowed, "CreateNewTag", "user not allowed to create protected tag")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:04:10 +02:00
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := ctx.Repo.GitRepo.GetTag(form.TagName)
|
|
|
|
if err != nil {
|
|
|
|
ctx.InternalServerError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag))
|
|
|
|
}
|
2021-06-23 23:08:47 +02:00
|
|
|
|
|
|
|
// DeleteTag delete a specific tag of in a repository by name
|
|
|
|
func DeleteTag(ctx *context.APIContext) {
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/tags/{tag} repository repoDeleteTag
|
|
|
|
// ---
|
|
|
|
// summary: Delete a repository's tag by name
|
|
|
|
// 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: tag
|
|
|
|
// in: path
|
|
|
|
// description: name of tag to delete
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
2022-06-16 22:03:03 +02:00
|
|
|
// "405":
|
|
|
|
// "$ref": "#/responses/empty"
|
2021-06-23 23:08:47 +02:00
|
|
|
// "409":
|
|
|
|
// "$ref": "#/responses/conflict"
|
|
|
|
tagName := ctx.Params("*")
|
|
|
|
|
2023-09-25 19:24:35 +02:00
|
|
|
tag, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, tagName)
|
2021-06-23 23:08:47 +02:00
|
|
|
if err != nil {
|
2022-08-25 04:31:57 +02:00
|
|
|
if repo_model.IsErrReleaseNotExist(err) {
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.NotFound()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetRelease", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tag.IsTag {
|
|
|
|
ctx.Error(http.StatusConflict, "IsTag", errors.New("a tag attached to a release cannot be deleted directly"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-26 00:43:23 +01:00
|
|
|
if err = releaseservice.DeleteReleaseByID(ctx, ctx.Repo.Repository, tag, ctx.Doer, true); err != nil {
|
2022-06-16 22:03:03 +02:00
|
|
|
if models.IsErrProtectedTagName(err) {
|
|
|
|
ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
|
|
|
|
return
|
|
|
|
}
|
2021-06-23 23:08:47 +02:00
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
|
2022-06-16 22:03:03 +02:00
|
|
|
return
|
2021-06-23 23:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|