[API] Add delete release by tag & fix unreleased inconsistency (#14563)

* DeleteReleaseByTag delete release not git tags

* Add api to delete tag (without release)

* fix & extend tests

* fix swagger doc
This commit is contained in:
6543 2021-02-07 19:32:18 +01:00 committed by GitHub
parent 240fea8c14
commit 5f248d0df2
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 157 additions and 36 deletions

@ -154,7 +154,7 @@ func TestAPIGetReleaseByTag(t *testing.T) {
assert.EqualValues(t, "Not Found", err.Message) assert.EqualValues(t, "Not Found", err.Message)
} }
func TestAPIDeleteTagByName(t *testing.T) { func TestAPIDeleteReleaseByTagName(t *testing.T) {
defer prepareTestEnv(t)() defer prepareTestEnv(t)()
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
@ -162,17 +162,17 @@ func TestAPIDeleteTagByName(t *testing.T) {
session := loginUser(t, owner.LowerName) session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session) token := getTokenForLoggedInUser(t, session)
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/delete-tag?token=%s", createNewReleaseUsingAPI(t, session, token, owner, repo, "release-tag", "", "Release Tag", "test")
owner.Name, repo.Name, token)
req := NewRequestf(t, http.MethodDelete, urlStr) // delete release
req := NewRequestf(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/release-tag?token=%s", owner.Name, repo.Name, token))
_ = session.MakeRequest(t, req, http.StatusNoContent) _ = session.MakeRequest(t, req, http.StatusNoContent)
// Make sure that actual releases can't be deleted outright // make sure release is deleted
createNewReleaseUsingAPI(t, session, token, owner, repo, "release-tag", "", "Release Tag", "test") req = NewRequestf(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/release-tag?token=%s", owner.Name, repo.Name, token))
urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/release-tag?token=%s", _ = session.MakeRequest(t, req, http.StatusNotFound)
owner.Name, repo.Name, token)
req = NewRequestf(t, http.MethodDelete, urlStr) // delete release tag too
_ = session.MakeRequest(t, req, http.StatusConflict) req = NewRequestf(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/tags/release-tag?token=%s", owner.Name, repo.Name, token))
_ = session.MakeRequest(t, req, http.StatusNoContent)
} }

@ -5,6 +5,7 @@
package integrations package integrations
import ( import (
"fmt"
"net/http" "net/http"
"testing" "testing"
@ -59,3 +60,26 @@ func TestAPIGitTags(t *testing.T) {
badReq := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s?token=%s", user.Name, repo.Name, commit.ID.String(), token) badReq := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/git/tags/%s?token=%s", user.Name, repo.Name, commit.ID.String(), token)
session.MakeRequest(t, badReq, http.StatusBadRequest) session.MakeRequest(t, badReq, http.StatusBadRequest)
} }
func TestAPIDeleteTagByName(t *testing.T) {
defer prepareTestEnv(t)()
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
owner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session)
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/tags/delete-tag?token=%s",
owner.Name, repo.Name, token)
req := NewRequestf(t, http.MethodDelete, urlStr)
_ = session.MakeRequest(t, req, http.StatusNoContent)
// Make sure that actual releases can't be deleted outright
createNewReleaseUsingAPI(t, session, token, owner, repo, "release-tag", "", "Release Tag", "test")
urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/tags/release-tag?token=%s",
owner.Name, repo.Name, token)
req = NewRequestf(t, http.MethodDelete, urlStr)
_ = session.MakeRequest(t, req, http.StatusConflict)
}

@ -754,6 +754,7 @@ func Routes() *web.Route {
}, reqToken(), reqAdmin()) }, reqToken(), reqAdmin())
m.Group("/tags", func() { m.Group("/tags", func() {
m.Get("", repo.ListTags) m.Get("", repo.ListTags)
m.Delete("/{tag}", repo.DeleteTag)
}, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(true)) }, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(true))
m.Group("/keys", func() { m.Group("/keys", func() {
m.Combo("").Get(repo.ListDeployKeys). m.Combo("").Get(repo.ListDeployKeys).
@ -862,8 +863,8 @@ func Routes() *web.Route {
}) })
m.Group("/tags", func() { m.Group("/tags", func() {
m.Combo("/{tag}"). m.Combo("/{tag}").
Get(repo.GetReleaseTag). Get(repo.GetReleaseByTag).
Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseTag) Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseByTag)
}) })
}, reqRepoReader(models.UnitTypeReleases)) }, reqRepoReader(models.UnitTypeReleases))
m.Post("/mirror-sync", reqToken(), reqRepoWriter(models.UnitTypeCode), repo.MirrorSync) m.Post("/mirror-sync", reqToken(), reqRepoWriter(models.UnitTypeCode), repo.MirrorSync)

@ -5,7 +5,6 @@
package repo package repo
import ( import (
"errors"
"net/http" "net/http"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -14,9 +13,9 @@ import (
releaseservice "code.gitea.io/gitea/services/release" releaseservice "code.gitea.io/gitea/services/release"
) )
// GetReleaseTag get a single release of a repository by its tagname // GetReleaseByTag get a single release of a repository by tag name
func GetReleaseTag(ctx *context.APIContext) { func GetReleaseByTag(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/tags/{tag} repository repoGetReleaseTag // swagger:operation GET /repos/{owner}/{repo}/releases/tags/{tag} repository repoGetReleaseByTag
// --- // ---
// summary: Get a release by tag name // summary: Get a release by tag name
// produces: // produces:
@ -34,7 +33,7 @@ func GetReleaseTag(ctx *context.APIContext) {
// required: true // required: true
// - name: tag // - name: tag
// in: path // in: path
// description: tagname of the release to get // description: tag name of the release to get
// type: string // type: string
// required: true // required: true
// responses: // responses:
@ -67,11 +66,11 @@ func GetReleaseTag(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, convert.ToRelease(release)) ctx.JSON(http.StatusOK, convert.ToRelease(release))
} }
// DeleteReleaseTag delete a tag from a repository // DeleteReleaseByTag delete a release from a repository by tag name
func DeleteReleaseTag(ctx *context.APIContext) { func DeleteReleaseByTag(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/releases/tags/{tag} repository repoDeleteReleaseTag // swagger:operation DELETE /repos/{owner}/{repo}/releases/tags/{tag} repository repoDeleteReleaseByTag
// --- // ---
// summary: Delete a release tag // summary: Delete a release by tag name
// parameters: // parameters:
// - name: owner // - name: owner
// in: path // in: path
@ -85,7 +84,7 @@ func DeleteReleaseTag(ctx *context.APIContext) {
// required: true // required: true
// - name: tag // - name: tag
// in: path // in: path
// description: name of the tag to delete // description: tag name of the release to delete
// type: string // type: string
// required: true // required: true
// responses: // responses:
@ -93,27 +92,25 @@ func DeleteReleaseTag(ctx *context.APIContext) {
// "$ref": "#/responses/empty" // "$ref": "#/responses/empty"
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/conflict"
tag := ctx.Params(":tag") tag := ctx.Params(":tag")
release, err := models.GetRelease(ctx.Repo.Repository.ID, tag) release, err := models.GetRelease(ctx.Repo.Repository.ID, tag)
if err != nil { if err != nil {
if models.IsErrReleaseNotExist(err) { if models.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusNotFound, "GetRelease", err) ctx.NotFound()
return return
} }
ctx.Error(http.StatusInternalServerError, "GetRelease", err) ctx.Error(http.StatusInternalServerError, "GetRelease", err)
return return
} }
if !release.IsTag { if release.IsTag {
ctx.Error(http.StatusConflict, "IsTag", errors.New("a tag attached to a release cannot be deleted directly")) ctx.NotFound()
return return
} }
if err := releaseservice.DeleteReleaseByID(release.ID, ctx.User, true); err != nil { if err = releaseservice.DeleteReleaseByID(release.ID, ctx.User, false); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err) ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
} }

@ -5,12 +5,15 @@
package repo package repo
import ( import (
"errors"
"net/http" "net/http"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/routers/api/v1/utils"
releaseservice "code.gitea.io/gitea/services/release"
) )
// ListTags list all the tags of a repository // ListTags list all the tags of a repository
@ -104,3 +107,56 @@ func GetTag(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx.Repo.Repository, tag, commit)) ctx.JSON(http.StatusOK, convert.ToAnnotatedTag(ctx.Repo.Repository, tag, commit))
} }
} }
// 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"
// "409":
// "$ref": "#/responses/conflict"
tag, err := models.GetRelease(ctx.Repo.Repository.ID, ctx.Params("tag"))
if err != nil {
if models.IsErrReleaseNotExist(err) {
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
}
if err = releaseservice.DeleteReleaseByID(tag.ID, ctx.User, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
}
ctx.Status(http.StatusNoContent)
}

@ -7964,7 +7964,7 @@
"repository" "repository"
], ],
"summary": "Get a release by tag name", "summary": "Get a release by tag name",
"operationId": "repoGetReleaseTag", "operationId": "repoGetReleaseByTag",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -7982,7 +7982,7 @@
}, },
{ {
"type": "string", "type": "string",
"description": "tagname of the release to get", "description": "tag name of the release to get",
"name": "tag", "name": "tag",
"in": "path", "in": "path",
"required": true "required": true
@ -8001,8 +8001,8 @@
"tags": [ "tags": [
"repository" "repository"
], ],
"summary": "Delete a release tag", "summary": "Delete a release by tag name",
"operationId": "repoDeleteReleaseTag", "operationId": "repoDeleteReleaseByTag",
"parameters": [ "parameters": [
{ {
"type": "string", "type": "string",
@ -8020,7 +8020,7 @@
}, },
{ {
"type": "string", "type": "string",
"description": "name of the tag to delete", "description": "tag name of the release to delete",
"name": "tag", "name": "tag",
"in": "path", "in": "path",
"required": true "required": true
@ -8032,9 +8032,6 @@
}, },
"404": { "404": {
"$ref": "#/responses/notFound" "$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/conflict"
} }
} }
} }
@ -8815,6 +8812,52 @@
} }
} }
}, },
"/repos/{owner}/{repo}/tags/{tag}": {
"delete": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Delete a repository's tag by name",
"operationId": "repoDeleteTag",
"parameters": [
{
"type": "string",
"description": "owner of the repo",
"name": "owner",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of the repo",
"name": "repo",
"in": "path",
"required": true
},
{
"type": "string",
"description": "name of tag to delete",
"name": "tag",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"$ref": "#/responses/empty"
},
"404": {
"$ref": "#/responses/notFound"
},
"409": {
"$ref": "#/responses/conflict"
}
}
}
},
"/repos/{owner}/{repo}/teams": { "/repos/{owner}/{repo}/teams": {
"get": { "get": {
"produces": [ "produces": [