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

Add API endpoint to get latest release #21267

Merged
merged 10 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Group("/releases", func() {
m.Combo("").Get(repo.ListReleases).
Post(reqToken(), reqRepoWriter(unit.TypeReleases), context.ReferencesGitRepo(), bind(api.CreateReleaseOption{}), repo.CreateRelease)
m.Combo("/latest").Get(repo.GetLatestRelease)
m.Group("/{id}", func() {
m.Combo("").Get(repo.GetRelease).
Patch(reqToken(), reqRepoWriter(unit.TypeReleases), context.ReferencesGitRepo(), bind(api.EditReleaseOption{}), repo.EditRelease).
Expand Down
41 changes: 41 additions & 0 deletions routers/api/v1/repo/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,47 @@ func GetRelease(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, convert.ToRelease(release))
}

// GetLatestRelease gets the latest stable release of a repository
lunny marked this conversation as resolved.
Show resolved Hide resolved
func GetLatestRelease(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases/latest repository repoGetLatestRelease
// ---
// summary: Gets the latest stable release
JakobDev marked this conversation as resolved.
Show resolved Hide resolved
// 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
// responses:
// "200":
// "$ref": "#/responses/Release"
// "404":
// "$ref": "#/responses/notFound"
release, err := repo_model.GetLatestReleaseByRepoID(ctx.Repo.Repository.ID)
if err != nil && !repo_model.IsErrReleaseNotExist(err) {
ctx.Error(http.StatusInternalServerError, "GetLatestRelease", err)
return
}
if err != nil && repo_model.IsErrReleaseNotExist(err) ||
release.IsTag || release.RepoID != ctx.Repo.Repository.ID {
ctx.NotFound()
return
}

if err := release.LoadAttributes(); err != nil {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
return
}
ctx.JSON(http.StatusOK, convert.ToRelease(release))
}

// ListReleases list a repository's releases
func ListReleases(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/releases repository repoListReleases
Expand Down
36 changes: 36 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9242,6 +9242,42 @@
}
}
},
"/repos/{owner}/{repo}/releases/latest": {
"get": {
"produces": [
"application/json"
],
"tags": [
"repository"
],
"summary": "Gets the latest stable release of a repository",
"operationId": "repoGetLatestRelease",
"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
}
],
"responses": {
"200": {
"$ref": "#/responses/Release"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
},
"/repos/{owner}/{repo}/releases/tags/{tag}": {
"get": {
"produces": [
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ func TestViewReleasesNoLogin(t *testing.T) {
MakeRequest(t, req, http.StatusOK)
}

func TestViewLatestRelease(t *testing.T) {
defer tests.PrepareTestEnv(t)()

session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/releases/latest")
session.MakeRequest(t, req, http.StatusOK)
}

func TestViewLatestReleaseNoLogin(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see one to two more test cases:

  1. non-public releases are omitted when not logged in. Potentially overkill.
  2. draft releases are not returned (even for site admins/ users with write rights).

defer tests.PrepareTestEnv(t)()

req := NewRequest(t, "GET", "/user2/repo1/releases/latest")
MakeRequest(t, req, http.StatusOK)
}

func TestCreateRelease(t *testing.T) {
defer tests.PrepareTestEnv(t)()

Expand Down