Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete tag API #13358

Merged
merged 6 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions integrations/api_releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,26 @@ func TestAPIGetReleaseByTag(t *testing.T) {
DecodeJSON(t, resp, &err)
assert.True(t, strings.HasPrefix(err.Message, "release tag does not exist"))
}

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/releases/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/releases/tags/release-tag/?token=%s",
owner.Name, repo.Name, token)

req = NewRequestf(t, http.MethodDelete, urlStr)
_ = session.MakeRequest(t, req, http.StatusConflict)
}
16 changes: 16 additions & 0 deletions models/fixtures/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@
is_prerelease: false
is_tag: false
created_unix: 946684800

-
id: 3
repo_id: 1
publisher_id: 2
tag_name: "delete-tag"
lower_tag_name: "delete-tag"
target: "master"
title: "delete-tag"
sha1: "65f1bf27bc3bf70f64657658635e66094edbcb4d"
num_commits: 10
is_draft: false
is_prerelease: false
is_tag: true
created_unix: 946684800

4 changes: 3 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ func RegisterRoutes(m *macaron.Macaron) {
})
})
m.Group("/tags", func() {
m.Get("/:tag", repo.GetReleaseTag)
m.Combo("/:tag").
Get(repo.GetReleaseTag).
Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseTag)
})
}, reqRepoReader(models.UnitTypeReleases))
m.Post("/mirror-sync", reqToken(), reqRepoWriter(models.UnitTypeCode), repo.MirrorSync)
Expand Down
53 changes: 53 additions & 0 deletions routers/api/v1/repo/release_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package repo

import (
"errors"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
releaseservice "code.gitea.io/gitea/services/release"
)

// GetReleaseTag get a single release of a repository by its tagname
Expand Down Expand Up @@ -59,3 +61,54 @@ func GetReleaseTag(ctx *context.APIContext) {
}
ctx.JSON(http.StatusOK, convert.ToRelease(release))
}

// DeleteReleaseTag delete a tag from a repository
func DeleteReleaseTag(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/releases/tags/{tag} repository repoDeleteReleaseTag
6543 marked this conversation as resolved.
Show resolved Hide resolved
// ---
// summary: Delete a release tag
// 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 the tag to delete
// type: string
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
zeripath marked this conversation as resolved.
Show resolved Hide resolved

tag := ctx.Params(":tag")

release, err := models.GetRelease(ctx.Repo.Repository.ID, tag)
if err != nil {
if models.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusNotFound, "GetRelease", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetRelease", err)
return
}

if !release.IsTag {
ctx.Error(http.StatusConflict, "IsTag", errors.New("a tag attached to a release cannot be deleted directly"))
return
}

if err := releaseservice.DeleteReleaseByID(release.ID, ctx.User, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
}

ctx.Status(http.StatusNoContent)
}
38 changes: 38 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7834,6 +7834,44 @@
"$ref": "#/responses/notFound"
}
}
},
"delete": {
"tags": [
"repository"
],
"summary": "Delete a release tag",
"operationId": "repoDeleteReleaseTag",
"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 the tag to delete",
"name": "tag",
"in": "path",
"required": true
}
],
"responses": {
"204": {
"$ref": "#/responses/empty"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/releases/{id}": {
Expand Down