1239fdee70
The transport was hand-rolled net/http against the REST API. The payload shapes were ours, in internal/wire, which meant every field Gitea learned was a field somebody here had to notice; and "does this instance have issue dependencies?" had to be guessed from a status code, because a 404 from a missing route and a 404 from a missing issue look alike. The SDK settles both. The shapes are maintained by the people who maintain the server, and the client negotiates the server's version when it is built, so the dependency endpoint is now gated on `>= 1.20.0` — verified against the release where the route appears, not assumed. Below the gate nothing is requested at all. internal/wire keeps what the SDK has no answer for: addressing. The SDK takes an owner, a name and an int64 and never parses, while `42`, `#42`, `owner/repo#42` and an issue URL are four spellings of one address, all four are what somebody has in hand, and Key is what the ledger is keyed by. The payload structs go. Four things that had to survive the move, and did: - request bodies still land in .kettle/payload/, now via an http.RoundTripper on the client the SDK is given — which is better than before, because it files every request rather than the ones a call site remembered to name; - errors still carry the HTTP status AND the response body, and a decode failure on a 2xx is deliberately not an APIError, so the dependency probe cannot read a bad decode as "feature missing"; - the number -> slug ledger is untouched, entries still outlive the files they name; - Client.For(repo) still re-points at another repository for one call. What it cost, written down in AGENTS.md where it happened. internal/mapping's layering test was a fact about the import graph — nothing in its closure could open a socket — and the SDK ships its types and its client in one package, so the test now asserts what is still true: the bridge performs no I/O, checked on direct imports plus a grep for time.Now. A run makes one extra request before it does anything. Gitea's issue edit endpoint carries no labels, so a push whose labels changed needs a second call; push makes it and says so. go.mod requires go 1.26, which the SDK sets and which is now the floor for building this binary. internal/issue and internal/project are byte-identical. The domain did not notice, which is the whole argument for the layering. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
147 lines
5.3 KiB
Go
147 lines
5.3 KiB
Go
// Copyright 2023 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 gitea
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// Package represents a package
|
|
type Package struct {
|
|
// the package's id
|
|
ID int64 `json:"id"`
|
|
// the package's owner
|
|
Owner *User `json:"owner"`
|
|
// the repo this package belongs to (if any)
|
|
Repository *Repository `json:"repository"`
|
|
// the package's creator
|
|
Creator *User `json:"creator"`
|
|
// the type of package:
|
|
Type string `json:"type"`
|
|
// the name of the package
|
|
Name string `json:"name"`
|
|
// the version of the package
|
|
Version string `json:"version"`
|
|
// the HTML URL for viewing the package
|
|
HTMLURL string `json:"html_url"`
|
|
// the date the package was uploaded
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// PackageFile represents a file from a package
|
|
type PackageFile struct {
|
|
// the file's ID
|
|
ID int64 `json:"id"`
|
|
// the size of the file in bytes
|
|
Size int64 `json:"size"`
|
|
// the name of the file
|
|
Name string `json:"name"`
|
|
// the md5 hash of the file
|
|
MD5 string `json:"md5"`
|
|
// the sha1 hash of the file
|
|
SHA1 string `json:"sha1"`
|
|
// the sha256 hash of the file
|
|
SHA256 string `json:"sha256"`
|
|
// the sha512 hash of the file
|
|
SHA512 string `json:"sha512"`
|
|
}
|
|
|
|
// ListPackagesOptions options for listing packages
|
|
type ListPackagesOptions struct {
|
|
ListOptions
|
|
// type, and q are only used for ListPackages, not ListPackageVersions
|
|
Type string
|
|
Q string
|
|
}
|
|
|
|
func (opt ListPackagesOptions) getURLQuery() url.Values {
|
|
query := opt.ListOptions.getURLQuery()
|
|
if opt.Type != "" {
|
|
query.Set("type", opt.Type)
|
|
}
|
|
if opt.Q != "" {
|
|
query.Set("q", opt.Q)
|
|
}
|
|
return query
|
|
}
|
|
|
|
// ListPackages lists all the packages owned by a given owner (user, organisation)
|
|
func (c *Client) ListPackages(owner string, opt ListPackagesOptions) ([]*Package, *Response, error) {
|
|
if err := escapeValidatePathSegments(&owner); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
opt.setDefaults()
|
|
packages := make([]*Package, 0, opt.PageSize)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/packages/%s?%s", owner, opt.getURLQuery().Encode()), nil, nil, &packages)
|
|
return packages, resp, err
|
|
}
|
|
|
|
// ListPackageVersions lists all versions of a package.
|
|
func (c *Client) ListPackageVersions(owner, packageType, name string, opt ListPackagesOptions) ([]*Package, *Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
opt.setDefaults()
|
|
packages := make([]*Package, 0, opt.PageSize)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/packages/%s/%s/%s?%s", owner, packageType, name, opt.ListOptions.getURLQuery().Encode()), nil, nil, &packages)
|
|
return packages, resp, err
|
|
}
|
|
|
|
// GetPackage gets the details of a specific package version
|
|
func (c *Client) GetPackage(owner, packageType, name, version string) (*Package, *Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name, &version); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
foundPackage := new(Package)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/packages/%s/%s/%s/%s", owner, packageType, name, version), nil, nil, foundPackage)
|
|
return foundPackage, resp, err
|
|
}
|
|
|
|
// DeletePackage deletes a specific package version
|
|
func (c *Client) DeletePackage(owner, packageType, name, version string) (*Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name, &version); err != nil {
|
|
return nil, err
|
|
}
|
|
return c.doRequestWithStatusHandle("DELETE", fmt.Sprintf("/packages/%s/%s/%s/%s", owner, packageType, name, version), nil, nil)
|
|
}
|
|
|
|
// ListPackageFiles lists the files within a package
|
|
func (c *Client) ListPackageFiles(owner, packageType, name, version string) ([]*PackageFile, *Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name, &version); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
packageFiles := make([]*PackageFile, 0)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/packages/%s/%s/%s/%s/files", owner, packageType, name, version), nil, nil, &packageFiles)
|
|
return packageFiles, resp, err
|
|
}
|
|
|
|
// GetLatestPackage gets the details of the latest version of a package
|
|
func (c *Client) GetLatestPackage(owner, packageType, name string) (*Package, *Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
foundPackage := new(Package)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/packages/%s/%s/%s/-/latest", owner, packageType, name), nil, nil, foundPackage)
|
|
return foundPackage, resp, err
|
|
}
|
|
|
|
// LinkPackage links a package to a repository
|
|
func (c *Client) LinkPackage(owner, packageType, name, repoName string) (*Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name, &repoName); err != nil {
|
|
return nil, err
|
|
}
|
|
return c.doRequestWithStatusHandle("POST", fmt.Sprintf("/packages/%s/%s/%s/-/link/%s", owner, packageType, name, repoName), nil, nil)
|
|
}
|
|
|
|
// UnlinkPackage unlinks a package from a repository
|
|
func (c *Client) UnlinkPackage(owner, packageType, name string) (*Response, error) {
|
|
if err := escapeValidatePathSegments(&owner, &packageType, &name); err != nil {
|
|
return nil, err
|
|
}
|
|
return c.doRequestWithStatusHandle("POST", fmt.Sprintf("/packages/%s/%s/%s/-/unlink", owner, packageType, name), nil, nil)
|
|
}
|