API: Get release by tags endpoint (#12932)

Get a release based on a tag name (for which a release exists).
Based on:
https://developer.github.com/v3/repos/releases/#get-a-release-by-tag-name

Co-authored-by: 赵智超 <1012112796@qq.com>
Co-authored-by: 6543 <6543@obermui.de>
release/v1.15
Johan Van de Wauw 2020-09-25 21:11:43 +02:00 committed by GitHub
parent d71eaacbf2
commit 34d9cb335c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 0 deletions

View File

@ -7,6 +7,7 @@ package integrations
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"testing" "testing"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
@ -120,3 +121,36 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) {
createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test") createNewReleaseUsingAPI(t, session, token, owner, repo, "v0.0.1", "", "v0.0.1", "test")
} }
func TestAPIGetReleaseByTag(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)
tag := "v1.1"
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/%s/",
owner.Name, repo.Name, tag)
req := NewRequestf(t, "GET", urlStr)
resp := session.MakeRequest(t, req, http.StatusOK)
var release *api.Release
DecodeJSON(t, resp, &release)
assert.Equal(t, "testing-release", release.Title)
nonexistingtag := "nonexistingtag"
urlStr = fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/%s/",
owner.Name, repo.Name, nonexistingtag)
req = NewRequestf(t, "GET", urlStr)
resp = session.MakeRequest(t, req, http.StatusNotFound)
var err *api.APIError
DecodeJSON(t, resp, &err)
assert.True(t, strings.HasPrefix(err.Message, "release tag does not exist"))
}

View File

@ -797,6 +797,9 @@ func RegisterRoutes(m *macaron.Macaron) {
Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseAttachment) Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseAttachment)
}) })
}) })
m.Group("/tags", func() {
m.Get("/:tag", repo.GetReleaseTag)
})
}, 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)
m.Get("/editorconfig/:filename", context.RepoRef(), reqRepoReader(models.UnitTypeCode), repo.GetEditorconfig) m.Get("/editorconfig/:filename", context.RepoRef(), reqRepoReader(models.UnitTypeCode), repo.GetEditorconfig)

View File

@ -0,0 +1,60 @@
// Copyright 2020 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"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
)
// GetReleaseTag get a single release of a repository by its tagname
func GetReleaseTag(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/tags/{tag} repository repoGetReleaseTag
// ---
// summary: Get a release by tag 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: tagname of the release to get
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Release"
// "404":
// "$ref": "#/responses/notFound"
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 err := release.LoadAttributes(); err != nil {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, release.APIFormat())
}

View File

@ -7685,6 +7685,49 @@
} }
} }
}, },
"/repos/{owner}/{repo}/releases/tags/{tag}": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Get a release by tag name",
"operationId": "repoGetReleaseTag",
"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": "tagname of the release to get",
"name": "tag",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"$ref": "#/responses/Release"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/releases/{id}": { "/repos/{owner}/{repo}/releases/{id}": {
"get": { "get": {
"produces": [ "produces": [