diff --git a/integrations/api_repo_tags_test.go b/integrations/api_repo_tags_test.go index 1bd9fa616819e..af9bd2939b974 100644 --- a/integrations/api_repo_tags_test.go +++ b/integrations/api_repo_tags_test.go @@ -51,6 +51,20 @@ func TestAPIRepoTags(t *testing.T) { assert.EqualValues(t, newTag.Commit.SHA, tag.Commit.SHA) } } + + // get created tag + req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/tags/%s?token=%s", user.Name, repoName, newTag.Name, token) + resp = session.MakeRequest(t, req, http.StatusOK) + var tag *api.Tag + DecodeJSON(t, resp, &tag) + assert.EqualValues(t, newTag, tag) + + // delete tag + delReq := NewRequestf(t, "DELETE", "/api/v1/repos/%s/%s/tags/%s?token=%s", user.Name, repoName, newTag.Name, token) + resp = session.MakeRequest(t, delReq, http.StatusNoContent) + + // check if it's gone + resp = session.MakeRequest(t, req, http.StatusNotFound) } func createNewTagUsingAPI(t *testing.T, session *TestSession, token string, ownerName, repoName, name, target, msg string) *api.Tag { diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index b53568098a493..c309313bc57c6 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -775,6 +775,7 @@ func Routes() *web.Route { }, reqToken(), reqAdmin()) m.Group("/tags", func() { m.Get("", repo.ListTags) + m.Get("/{tag}", repo.GetTag) m.Post("", reqRepoWriter(models.UnitTypeCode), bind(api.CreateTagOption{}), repo.CreateTag) m.Delete("/{tag}", repo.DeleteTag) }, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(true)) diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index 745451cf58455..716bcbf16a9de 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -110,11 +110,11 @@ func GetAnnotatedTag(ctx *context.APIContext) { } } -// 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 +// GetTag get the tag of a repository +func GetTag(ctx *context.APIContext) { + // swagger:operation GET /repos/{owner}/{repo}/tags/{tag} repository repoGetTag // --- - // summary: Delete a repository's tag by name + // summary: Get the tag of a repository by tag name // produces: // - application/json // parameters: @@ -130,37 +130,22 @@ func DeleteTag(ctx *context.APIContext) { // required: true // - name: tag // in: path - // description: name of tag to delete + // description: name of tag // type: string // required: true // responses: - // "204": + // "200": // "$ref": "#/responses/empty" // "404": // "$ref": "#/responses/notFound" - // "409": - // "$ref": "#/responses/conflict" + tagName := ctx.Params("tag") - tag, err := models.GetRelease(ctx.Repo.Repository.ID, ctx.Params("tag")) + tag, err := ctx.Repo.GitRepo.GetTag(tagName) 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")) + ctx.NotFound(tagName) return } - - if err = releaseservice.DeleteReleaseByID(tag.ID, ctx.User, true); err != nil { - ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err) - } - - ctx.Status(http.StatusNoContent) + ctx.JSON(http.StatusOK, convert.ToTag(ctx.Repo.Repository, tag)) } // CreateTag create a new git tag in a repository @@ -221,3 +206,56 @@ func CreateTag(ctx *context.APIContext) { } ctx.JSON(http.StatusCreated, convert.ToTag(ctx.Repo.Repository, tag)) } + +// 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) +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 017dc824d7d62..fc571113e8da5 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3657,7 +3657,7 @@ "repository" ], "summary": "Gets the tag object of an annotated tag (not lightweight tags)", - "operationId": "GetTag", + "operationId": "GetAnnotatedTag", "parameters": [ { "type": "string", @@ -9129,6 +9129,47 @@ } }, "/repos/{owner}/{repo}/tags/{tag}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get the tag of a repository by tag name", + "operationId": "repoGetTag", + "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", + "name": "tag", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/empty" + }, + "404": { + "$ref": "#/responses/notFound" + } + } + }, "delete": { "produces": [ "application/json"