Skip to content

Commit bec9233

Browse files
authored
Add package version api endpoints (#34173)
Fixes #33544 Adds two new api endpoints to list a versions of a package and to get the latest version of a package by API. ⚠️ BREAKING ⚠️ the `size` field for this endpoint changes from `Size` to `size`.
1 parent 34349c0 commit bec9233

File tree

5 files changed

+332
-78
lines changed

5 files changed

+332
-78
lines changed

modules/structs/package.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type Package struct {
2323

2424
// PackageFile represents a package file
2525
type PackageFile struct {
26-
ID int64 `json:"id"`
27-
Size int64
26+
ID int64 `json:"id"`
27+
Size int64 `json:"size"`
2828
Name string `json:"name"`
2929
HashMD5 string `json:"md5"`
3030
HashSHA1 string `json:"sha1"`

routers/api/v1/api.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1544,14 +1544,19 @@ func Routes() *web.Router {
15441544
// NOTE: these are Gitea package management API - see packages.CommonRoutes and packages.DockerContainerRoutes for endpoints that implement package manager APIs
15451545
m.Group("/packages/{username}", func() {
15461546
m.Group("/{type}/{name}", func() {
1547+
m.Get("/", packages.ListPackageVersions)
1548+
15471549
m.Group("/{version}", func() {
15481550
m.Get("", packages.GetPackage)
15491551
m.Delete("", reqPackageAccess(perm.AccessModeWrite), packages.DeletePackage)
15501552
m.Get("/files", packages.ListPackageFiles)
15511553
})
15521554

1553-
m.Post("/-/link/{repo_name}", reqPackageAccess(perm.AccessModeWrite), packages.LinkPackage)
1554-
m.Post("/-/unlink", reqPackageAccess(perm.AccessModeWrite), packages.UnlinkPackage)
1555+
m.Group("/-", func() {
1556+
m.Get("/latest", packages.GetLatestPackageVersion)
1557+
m.Post("/link/{repo_name}", reqPackageAccess(perm.AccessModeWrite), packages.LinkPackage)
1558+
m.Post("/unlink", reqPackageAccess(perm.AccessModeWrite), packages.UnlinkPackage)
1559+
})
15551560
})
15561561

15571562
m.Get("/", packages.ListPackages)

routers/api/v1/packages/package.go

+141-22
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ func ListPackages(ctx *context.APIContext) {
5656

5757
listOptions := utils.GetListOptions(ctx)
5858

59-
packageType := ctx.FormTrim("type")
60-
query := ctx.FormTrim("q")
61-
62-
pvs, count, err := packages.SearchVersions(ctx, &packages.PackageSearchOptions{
59+
apiPackages, count, err := searchPackages(ctx, &packages.PackageSearchOptions{
6360
OwnerID: ctx.Package.Owner.ID,
64-
Type: packages.Type(packageType),
65-
Name: packages.SearchValue{Value: query},
61+
Type: packages.Type(ctx.FormTrim("type")),
62+
Name: packages.SearchValue{Value: ctx.FormTrim("q")},
6663
IsInternal: optional.Some(false),
6764
Paginator: &listOptions,
6865
})
@@ -71,22 +68,6 @@ func ListPackages(ctx *context.APIContext) {
7168
return
7269
}
7370

74-
pds, err := packages.GetPackageDescriptors(ctx, pvs)
75-
if err != nil {
76-
ctx.APIErrorInternal(err)
77-
return
78-
}
79-
80-
apiPackages := make([]*api.Package, 0, len(pds))
81-
for _, pd := range pds {
82-
apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
83-
if err != nil {
84-
ctx.APIErrorInternal(err)
85-
return
86-
}
87-
apiPackages = append(apiPackages, apiPackage)
88-
}
89-
9071
ctx.SetLinkHeader(int(count), listOptions.PageSize)
9172
ctx.SetTotalCountHeader(count)
9273
ctx.JSON(http.StatusOK, apiPackages)
@@ -217,6 +198,121 @@ func ListPackageFiles(ctx *context.APIContext) {
217198
ctx.JSON(http.StatusOK, apiPackageFiles)
218199
}
219200

201+
// ListPackageVersions gets all versions of a package
202+
func ListPackageVersions(ctx *context.APIContext) {
203+
// swagger:operation GET /packages/{owner}/{type}/{name} package listPackageVersions
204+
// ---
205+
// summary: Gets all versions of a package
206+
// produces:
207+
// - application/json
208+
// parameters:
209+
// - name: owner
210+
// in: path
211+
// description: owner of the package
212+
// type: string
213+
// required: true
214+
// - name: type
215+
// in: path
216+
// description: type of the package
217+
// type: string
218+
// required: true
219+
// - name: name
220+
// in: path
221+
// description: name of the package
222+
// type: string
223+
// required: true
224+
// - name: page
225+
// in: query
226+
// description: page number of results to return (1-based)
227+
// type: integer
228+
// - name: limit
229+
// in: query
230+
// description: page size of results
231+
// type: integer
232+
// responses:
233+
// "200":
234+
// "$ref": "#/responses/PackageList"
235+
// "404":
236+
// "$ref": "#/responses/notFound"
237+
238+
listOptions := utils.GetListOptions(ctx)
239+
240+
apiPackages, count, err := searchPackages(ctx, &packages.PackageSearchOptions{
241+
OwnerID: ctx.Package.Owner.ID,
242+
Type: packages.Type(ctx.PathParam("type")),
243+
Name: packages.SearchValue{Value: ctx.PathParam("name"), ExactMatch: true},
244+
IsInternal: optional.Some(false),
245+
Paginator: &listOptions,
246+
})
247+
if err != nil {
248+
ctx.APIErrorInternal(err)
249+
return
250+
}
251+
252+
ctx.SetLinkHeader(int(count), listOptions.PageSize)
253+
ctx.SetTotalCountHeader(count)
254+
ctx.JSON(http.StatusOK, apiPackages)
255+
}
256+
257+
// GetLatestPackageVersion gets the latest version of a package
258+
func GetLatestPackageVersion(ctx *context.APIContext) {
259+
// swagger:operation GET /packages/{owner}/{type}/{name}/-/latest package getLatestPackageVersion
260+
// ---
261+
// summary: Gets the latest version of a package
262+
// produces:
263+
// - application/json
264+
// parameters:
265+
// - name: owner
266+
// in: path
267+
// description: owner of the package
268+
// type: string
269+
// required: true
270+
// - name: type
271+
// in: path
272+
// description: type of the package
273+
// type: string
274+
// required: true
275+
// - name: name
276+
// in: path
277+
// description: name of the package
278+
// type: string
279+
// required: true
280+
// responses:
281+
// "200":
282+
// "$ref": "#/responses/Package"
283+
// "404":
284+
// "$ref": "#/responses/notFound"
285+
286+
pvs, _, err := packages.SearchLatestVersions(ctx, &packages.PackageSearchOptions{
287+
OwnerID: ctx.Package.Owner.ID,
288+
Type: packages.Type(ctx.PathParam("type")),
289+
Name: packages.SearchValue{Value: ctx.PathParam("name"), ExactMatch: true},
290+
IsInternal: optional.Some(false),
291+
})
292+
if err != nil {
293+
ctx.APIErrorInternal(err)
294+
return
295+
}
296+
if len(pvs) == 0 {
297+
ctx.APIError(http.StatusNotFound, err)
298+
return
299+
}
300+
301+
pd, err := packages.GetPackageDescriptor(ctx, pvs[0])
302+
if err != nil {
303+
ctx.APIErrorInternal(err)
304+
return
305+
}
306+
307+
apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
308+
if err != nil {
309+
ctx.APIErrorInternal(err)
310+
return
311+
}
312+
313+
ctx.JSON(http.StatusOK, apiPackage)
314+
}
315+
220316
// LinkPackage sets a repository link for a package
221317
func LinkPackage(ctx *context.APIContext) {
222318
// swagger:operation POST /packages/{owner}/{type}/{name}/-/link/{repo_name} package linkPackage
@@ -335,3 +431,26 @@ func UnlinkPackage(ctx *context.APIContext) {
335431
}
336432
ctx.Status(http.StatusNoContent)
337433
}
434+
435+
func searchPackages(ctx *context.APIContext, opts *packages.PackageSearchOptions) ([]*api.Package, int64, error) {
436+
pvs, count, err := packages.SearchVersions(ctx, opts)
437+
if err != nil {
438+
return nil, 0, err
439+
}
440+
441+
pds, err := packages.GetPackageDescriptors(ctx, pvs)
442+
if err != nil {
443+
return nil, 0, err
444+
}
445+
446+
apiPackages := make([]*api.Package, 0, len(pds))
447+
for _, pd := range pds {
448+
apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
449+
if err != nil {
450+
return nil, 0, err
451+
}
452+
apiPackages = append(apiPackages, apiPackage)
453+
}
454+
455+
return apiPackages, count, nil
456+
}

templates/swagger/v1_json.tmpl

+103-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)