Skip to content

Add package version api endpoints #34173

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

Merged
merged 3 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions modules/structs/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type Package struct {

// PackageFile represents a package file
type PackageFile struct {
ID int64 `json:"id"`
Size int64
ID int64 `json:"id"`
Size int64 `json:"size"`
Name string `json:"name"`
HashMD5 string `json:"md5"`
HashSHA1 string `json:"sha1"`
Expand Down
9 changes: 7 additions & 2 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,14 +1544,19 @@ func Routes() *web.Router {
// NOTE: these are Gitea package management API - see packages.CommonRoutes and packages.DockerContainerRoutes for endpoints that implement package manager APIs
m.Group("/packages/{username}", func() {
m.Group("/{type}/{name}", func() {
m.Get("/", packages.ListPackageVersions)

m.Group("/{version}", func() {
m.Get("", packages.GetPackage)
m.Delete("", reqPackageAccess(perm.AccessModeWrite), packages.DeletePackage)
m.Get("/files", packages.ListPackageFiles)
})

m.Post("/-/link/{repo_name}", reqPackageAccess(perm.AccessModeWrite), packages.LinkPackage)
m.Post("/-/unlink", reqPackageAccess(perm.AccessModeWrite), packages.UnlinkPackage)
m.Group("/-", func() {
m.Get("/latest", packages.GetLatestPackageVersion)
m.Post("/link/{repo_name}", reqPackageAccess(perm.AccessModeWrite), packages.LinkPackage)
m.Post("/unlink", reqPackageAccess(perm.AccessModeWrite), packages.UnlinkPackage)
})
})

m.Get("/", packages.ListPackages)
Expand Down
163 changes: 141 additions & 22 deletions routers/api/v1/packages/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ func ListPackages(ctx *context.APIContext) {

listOptions := utils.GetListOptions(ctx)

packageType := ctx.FormTrim("type")
query := ctx.FormTrim("q")

pvs, count, err := packages.SearchVersions(ctx, &packages.PackageSearchOptions{
apiPackages, count, err := searchPackages(ctx, &packages.PackageSearchOptions{
OwnerID: ctx.Package.Owner.ID,
Type: packages.Type(packageType),
Name: packages.SearchValue{Value: query},
Type: packages.Type(ctx.FormTrim("type")),
Name: packages.SearchValue{Value: ctx.FormTrim("q")},
IsInternal: optional.Some(false),
Paginator: &listOptions,
})
Expand All @@ -71,22 +68,6 @@ func ListPackages(ctx *context.APIContext) {
return
}

pds, err := packages.GetPackageDescriptors(ctx, pvs)
if err != nil {
ctx.APIErrorInternal(err)
return
}

apiPackages := make([]*api.Package, 0, len(pds))
for _, pd := range pds {
apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
if err != nil {
ctx.APIErrorInternal(err)
return
}
apiPackages = append(apiPackages, apiPackage)
}

ctx.SetLinkHeader(int(count), listOptions.PageSize)
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, apiPackages)
Expand Down Expand Up @@ -217,6 +198,121 @@ func ListPackageFiles(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, apiPackageFiles)
}

// ListPackageVersions gets all versions of a package
func ListPackageVersions(ctx *context.APIContext) {
// swagger:operation GET /packages/{owner}/{type}/{name} package listPackageVersions
// ---
// summary: Gets all versions of a package
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the package
// type: string
// required: true
// - name: type
// in: path
// description: type of the package
// type: string
// required: true
// - name: name
// in: path
// description: name of the package
// type: string
// required: true
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/PackageList"
// "404":
// "$ref": "#/responses/notFound"

listOptions := utils.GetListOptions(ctx)

apiPackages, count, err := searchPackages(ctx, &packages.PackageSearchOptions{
OwnerID: ctx.Package.Owner.ID,
Type: packages.Type(ctx.PathParam("type")),
Name: packages.SearchValue{Value: ctx.PathParam("name"), ExactMatch: true},
IsInternal: optional.Some(false),
Paginator: &listOptions,
})
if err != nil {
ctx.APIErrorInternal(err)
return
}

ctx.SetLinkHeader(int(count), listOptions.PageSize)
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, apiPackages)
}

// GetLatestPackageVersion gets the latest version of a package
func GetLatestPackageVersion(ctx *context.APIContext) {
// swagger:operation GET /packages/{owner}/{type}/{name}/-/latest package getLatestPackageVersion
// ---
// summary: Gets the latest version of a package
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the package
// type: string
// required: true
// - name: type
// in: path
// description: type of the package
// type: string
// required: true
// - name: name
// in: path
// description: name of the package
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/Package"
// "404":
// "$ref": "#/responses/notFound"

pvs, _, err := packages.SearchLatestVersions(ctx, &packages.PackageSearchOptions{
OwnerID: ctx.Package.Owner.ID,
Type: packages.Type(ctx.PathParam("type")),
Name: packages.SearchValue{Value: ctx.PathParam("name"), ExactMatch: true},
IsInternal: optional.Some(false),
})
if err != nil {
ctx.APIErrorInternal(err)
return
}
if len(pvs) == 0 {
ctx.APIError(http.StatusNotFound, err)
return
}

pd, err := packages.GetPackageDescriptor(ctx, pvs[0])
if err != nil {
ctx.APIErrorInternal(err)
return
}

apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
if err != nil {
ctx.APIErrorInternal(err)
return
}

ctx.JSON(http.StatusOK, apiPackage)
}

// LinkPackage sets a repository link for a package
func LinkPackage(ctx *context.APIContext) {
// swagger:operation POST /packages/{owner}/{type}/{name}/-/link/{repo_name} package linkPackage
Expand Down Expand Up @@ -335,3 +431,26 @@ func UnlinkPackage(ctx *context.APIContext) {
}
ctx.Status(http.StatusNoContent)
}

func searchPackages(ctx *context.APIContext, opts *packages.PackageSearchOptions) ([]*api.Package, int64, error) {
pvs, count, err := packages.SearchVersions(ctx, opts)
if err != nil {
return nil, 0, err
}

pds, err := packages.GetPackageDescriptors(ctx, pvs)
if err != nil {
return nil, 0, err
}

apiPackages := make([]*api.Package, 0, len(pds))
for _, pd := range pds {
apiPackage, err := convert.ToPackage(ctx, pd, ctx.Doer)
if err != nil {
return nil, 0, err
}
apiPackages = append(apiPackages, apiPackage)
}

return apiPackages, count, nil
}
107 changes: 103 additions & 4 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading