mirror of https://github.com/go-gitea/gitea
Add Swift package registry (#22404)
This PR adds a [Swift](https://www.swift.org/) package registry. ![grafik](https://user-images.githubusercontent.com/1666336/211842523-07521cbd-8fb6-400f-820c-ee8048b05ae8.png)pull/23351/head^2
parent
0a6f6354bb
commit
c709fa17a7
@ -0,0 +1,93 @@ |
||||
--- |
||||
date: "2023-01-10T00:00:00+00:00" |
||||
title: "Swift Packages Repository" |
||||
slug: "packages/swift" |
||||
draft: false |
||||
toc: false |
||||
menu: |
||||
sidebar: |
||||
parent: "packages" |
||||
name: "Swift" |
||||
weight: 95 |
||||
identifier: "swift" |
||||
--- |
||||
|
||||
# Swift Packages Repository |
||||
|
||||
Publish [Swift](hhttps://www.swift.org/) packages for your user or organization. |
||||
|
||||
**Table of Contents** |
||||
|
||||
{{< toc >}} |
||||
|
||||
## Requirements |
||||
|
||||
To work with the Swift package registry, you need to use [swift](https://www.swift.org/getting-started/) to consume and a HTTP client (like `curl`) to publish packages. |
||||
|
||||
## Configuring the package registry |
||||
|
||||
To register the package registry and provide credentials, execute: |
||||
|
||||
```shell |
||||
swift package-registry set https://gitea.example.com/api/packages/{owner}/swift -login {username} -password {password} |
||||
``` |
||||
|
||||
| Placeholder | Description | |
||||
| ------------ | ----------- | |
||||
| `owner` | The owner of the package. | |
||||
| `username` | Your Gitea username. | |
||||
| `password` | Your Gitea password. If you are using 2FA or OAuth use a [personal access token]({{< relref "doc/developers/api-usage.en-us.md#authentication" >}}) instead of the password. | |
||||
|
||||
The login is optional and only needed if the package registry is private. |
||||
|
||||
## Publish a package |
||||
|
||||
First you have to pack the contents of your package: |
||||
|
||||
```shell |
||||
swift package archive-source |
||||
``` |
||||
|
||||
To publish the package perform a HTTP PUT request with the package content in the request body. |
||||
|
||||
```shell --user your_username:your_password_or_token \ |
||||
curl -X PUT --user {username}:{password} \ |
||||
-H "Accept: application/vnd.swift.registry.v1+json" \ |
||||
-F source-archive=@/path/to/package.zip \ |
||||
-F metadata={metadata} \ |
||||
https://gitea.example.com/api/packages/{owner}/swift/{scope}/{name}/{version} |
||||
``` |
||||
|
||||
| Placeholder | Description | |
||||
| ----------- | ----------- | |
||||
| `username` | Your Gitea username. | |
||||
| `password` | Your Gitea password. If you are using 2FA or OAuth use a [personal access token]({{< relref "doc/developers/api-usage.en-us.md#authentication" >}}) instead of the password. | |
||||
| `owner` | The owner of the package. | |
||||
| `scope` | The package scope. | |
||||
| `name` | The package name. | |
||||
| `version` | The package version. | |
||||
| `metadata` | (Optional) The metadata of the package. JSON encoded subset of https://schema.org/SoftwareSourceCode | |
||||
|
||||
You cannot publish a package if a package of the same name and version already exists. You must delete the existing package first. |
||||
|
||||
## Install a package |
||||
|
||||
To install a Swift package from the package registry, add it in the `Package.swift` file dependencies list: |
||||
|
||||
``` |
||||
dependencies: [ |
||||
.package(id: "{scope}.{name}", from:"{version}") |
||||
] |
||||
``` |
||||
|
||||
| Parameter | Description | |
||||
| ----------- | ----------- | |
||||
| `scope` | The package scope. | |
||||
| `name` | The package name. | |
||||
| `version` | The package version. | |
||||
|
||||
Afterwards execute the following command to install it: |
||||
|
||||
```shell |
||||
swift package resolve |
||||
``` |
@ -0,0 +1,214 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package swift |
||||
|
||||
import ( |
||||
"archive/zip" |
||||
"fmt" |
||||
"io" |
||||
"path" |
||||
"regexp" |
||||
"strings" |
||||
|
||||
"code.gitea.io/gitea/modules/json" |
||||
"code.gitea.io/gitea/modules/util" |
||||
"code.gitea.io/gitea/modules/validation" |
||||
|
||||
"github.com/hashicorp/go-version" |
||||
) |
||||
|
||||
var ( |
||||
ErrMissingManifestFile = util.NewInvalidArgumentErrorf("Package.swift file is missing") |
||||
ErrManifestFileTooLarge = util.NewInvalidArgumentErrorf("Package.swift file is too large") |
||||
ErrInvalidManifestVersion = util.NewInvalidArgumentErrorf("manifest version is invalid") |
||||
|
||||
manifestPattern = regexp.MustCompile(`\APackage(?:@swift-(\d+(?:\.\d+)?(?:\.\d+)?))?\.swift\z`) |
||||
toolsVersionPattern = regexp.MustCompile(`\A// swift-tools-version:(\d+(?:\.\d+)?(?:\.\d+)?)`) |
||||
) |
||||
|
||||
const ( |
||||
maxManifestFileSize = 128 * 1024 |
||||
|
||||
PropertyScope = "swift.scope" |
||||
PropertyName = "swift.name" |
||||
PropertyRepositoryURL = "swift.repository_url" |
||||
) |
||||
|
||||
// Package represents a Swift package
|
||||
type Package struct { |
||||
RepositoryURLs []string |
||||
Metadata *Metadata |
||||
} |
||||
|
||||
// Metadata represents the metadata of a Swift package
|
||||
type Metadata struct { |
||||
Description string `json:"description,omitempty"` |
||||
Keywords []string `json:"keywords,omitempty"` |
||||
RepositoryURL string `json:"repository_url,omitempty"` |
||||
License string `json:"license,omitempty"` |
||||
Author Person `json:"author,omitempty"` |
||||
Manifests map[string]*Manifest `json:"manifests,omitempty"` |
||||
} |
||||
|
||||
// Manifest represents a Package.swift file
|
||||
type Manifest struct { |
||||
Content string `json:"content"` |
||||
ToolsVersion string `json:"tools_version,omitempty"` |
||||
} |
||||
|
||||
// https://schema.org/SoftwareSourceCode
|
||||
type SoftwareSourceCode struct { |
||||
Context []string `json:"@context"` |
||||
Type string `json:"@type"` |
||||
Name string `json:"name"` |
||||
Version string `json:"version"` |
||||
Description string `json:"description,omitempty"` |
||||
Keywords []string `json:"keywords,omitempty"` |
||||
CodeRepository string `json:"codeRepository,omitempty"` |
||||
License string `json:"license,omitempty"` |
||||
Author Person `json:"author"` |
||||
ProgrammingLanguage ProgrammingLanguage `json:"programmingLanguage"` |
||||
RepositoryURLs []string `json:"repositoryURLs,omitempty"` |
||||
} |
||||
|
||||
// https://schema.org/ProgrammingLanguage
|
||||
type ProgrammingLanguage struct { |
||||
Type string `json:"@type"` |
||||
Name string `json:"name"` |
||||
URL string `json:"url"` |
||||
} |
||||
|
||||
// https://schema.org/Person
|
||||
type Person struct { |
||||
Type string `json:"@type,omitempty"` |
||||
GivenName string `json:"givenName,omitempty"` |
||||
MiddleName string `json:"middleName,omitempty"` |
||||
FamilyName string `json:"familyName,omitempty"` |
||||
} |
||||
|
||||
func (p Person) String() string { |
||||
var sb strings.Builder |
||||
if p.GivenName != "" { |
||||
sb.WriteString(p.GivenName) |
||||
} |
||||
if p.MiddleName != "" { |
||||
if sb.Len() > 0 { |
||||
sb.WriteRune(' ') |
||||
} |
||||
sb.WriteString(p.MiddleName) |
||||
} |
||||
if p.FamilyName != "" { |
||||
if sb.Len() > 0 { |
||||
sb.WriteRune(' ') |
||||
} |
||||
sb.WriteString(p.FamilyName) |
||||
} |
||||
return sb.String() |
||||
} |
||||
|
||||
// ParsePackage parses the Swift package upload
|
||||
func ParsePackage(sr io.ReaderAt, size int64, mr io.Reader) (*Package, error) { |
||||
zr, err := zip.NewReader(sr, size) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
p := &Package{ |
||||
Metadata: &Metadata{ |
||||
Manifests: make(map[string]*Manifest), |
||||
}, |
||||
} |
||||
|
||||
for _, file := range zr.File { |
||||
manifestMatch := manifestPattern.FindStringSubmatch(path.Base(file.Name)) |
||||
if len(manifestMatch) == 0 { |
||||
continue |
||||
} |
||||
|
||||
if file.UncompressedSize64 > maxManifestFileSize { |
||||
return nil, ErrManifestFileTooLarge |
||||
} |
||||
|
||||
f, err := zr.Open(file.Name) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
content, err := io.ReadAll(f) |
||||
|
||||
if err := f.Close(); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
swiftVersion := "" |
||||
if len(manifestMatch) == 2 && manifestMatch[1] != "" { |
||||
v, err := version.NewSemver(manifestMatch[1]) |
||||
if err != nil { |
||||
return nil, ErrInvalidManifestVersion |
||||
} |
||||
swiftVersion = TrimmedVersionString(v) |
||||
} |
||||
|
||||
manifest := &Manifest{ |
||||
Content: string(content), |
||||
} |
||||
|
||||
toolsMatch := toolsVersionPattern.FindStringSubmatch(manifest.Content) |
||||
if len(toolsMatch) == 2 { |
||||
v, err := version.NewSemver(toolsMatch[1]) |
||||
if err != nil { |
||||
return nil, ErrInvalidManifestVersion |
||||
} |
||||
|
||||
manifest.ToolsVersion = TrimmedVersionString(v) |
||||
} |
||||
|
||||
p.Metadata.Manifests[swiftVersion] = manifest |
||||
} |
||||
|
||||
if _, found := p.Metadata.Manifests[""]; !found { |
||||
return nil, ErrMissingManifestFile |
||||
} |
||||
|
||||
if mr != nil { |
||||
var ssc *SoftwareSourceCode |
||||
if err := json.NewDecoder(mr).Decode(&ssc); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
p.Metadata.Description = ssc.Description |
||||
p.Metadata.Keywords = ssc.Keywords |
||||
p.Metadata.License = ssc.License |
||||
p.Metadata.Author = Person{ |
||||
GivenName: ssc.Author.GivenName, |
||||
MiddleName: ssc.Author.MiddleName, |
||||
FamilyName: ssc.Author.FamilyName, |
||||
} |
||||
|
||||
p.Metadata.RepositoryURL = ssc.CodeRepository |
||||
if !validation.IsValidURL(p.Metadata.RepositoryURL) { |
||||
p.Metadata.RepositoryURL = "" |
||||
} |
||||
|
||||
p.RepositoryURLs = ssc.RepositoryURLs |
||||
} |
||||
|
||||
return p, nil |
||||
} |
||||
|
||||
// TrimmedVersionString returns the version string without the patch segment if it is zero
|
||||
func TrimmedVersionString(v *version.Version) string { |
||||
segments := v.Segments64() |
||||
|
||||
var b strings.Builder |
||||
fmt.Fprintf(&b, "%d.%d", segments[0], segments[1]) |
||||
if segments[2] != 0 { |
||||
fmt.Fprintf(&b, ".%d", segments[2]) |
||||
} |
||||
return b.String() |
||||
} |
@ -0,0 +1,144 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package swift |
||||
|
||||
import ( |
||||
"archive/zip" |
||||
"bytes" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"github.com/hashicorp/go-version" |
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
const ( |
||||
packageName = "gitea" |
||||
packageVersion = "1.0.1" |
||||
packageDescription = "Package Description" |
||||
packageRepositoryURL = "https://gitea.io/gitea/gitea" |
||||
packageAuthor = "KN4CK3R" |
||||
packageLicense = "MIT" |
||||
) |
||||
|
||||
func TestParsePackage(t *testing.T) { |
||||
createArchive := func(files map[string][]byte) *bytes.Reader { |
||||
var buf bytes.Buffer |
||||
zw := zip.NewWriter(&buf) |
||||
for filename, content := range files { |
||||
w, _ := zw.Create(filename) |
||||
w.Write(content) |
||||
} |
||||
zw.Close() |
||||
return bytes.NewReader(buf.Bytes()) |
||||
} |
||||
|
||||
t.Run("MissingManifestFile", func(t *testing.T) { |
||||
data := createArchive(map[string][]byte{"dummy.txt": {}}) |
||||
|
||||
p, err := ParsePackage(data, data.Size(), nil) |
||||
assert.Nil(t, p) |
||||
assert.ErrorIs(t, err, ErrMissingManifestFile) |
||||
}) |
||||
|
||||
t.Run("ManifestFileTooLarge", func(t *testing.T) { |
||||
data := createArchive(map[string][]byte{ |
||||
"Package.swift": make([]byte, maxManifestFileSize+1), |
||||
}) |
||||
|
||||
p, err := ParsePackage(data, data.Size(), nil) |
||||
assert.Nil(t, p) |
||||
assert.ErrorIs(t, err, ErrManifestFileTooLarge) |
||||
}) |
||||
|
||||
t.Run("WithoutMetadata", func(t *testing.T) { |
||||
content1 := "// swift-tools-version:5.7\n//\n// Package.swift" |
||||
content2 := "// swift-tools-version:5.6\n//\n// Package@swift-5.6.swift" |
||||
|
||||
data := createArchive(map[string][]byte{ |
||||
"Package.swift": []byte(content1), |
||||
"Package@swift-5.5.swift": []byte(content2), |
||||
}) |
||||
|
||||
p, err := ParsePackage(data, data.Size(), nil) |
||||
assert.NotNil(t, p) |
||||
assert.NoError(t, err) |
||||
|
||||
assert.NotNil(t, p.Metadata) |
||||
assert.Empty(t, p.RepositoryURLs) |
||||
assert.Len(t, p.Metadata.Manifests, 2) |
||||
m := p.Metadata.Manifests[""] |
||||
assert.Equal(t, "5.7", m.ToolsVersion) |
||||
assert.Equal(t, content1, m.Content) |
||||
m = p.Metadata.Manifests["5.5"] |
||||
assert.Equal(t, "5.6", m.ToolsVersion) |
||||
assert.Equal(t, content2, m.Content) |
||||
}) |
||||
|
||||
t.Run("WithMetadata", func(t *testing.T) { |
||||
data := createArchive(map[string][]byte{ |
||||
"Package.swift": []byte("// swift-tools-version:5.7\n//\n// Package.swift"), |
||||
}) |
||||
|
||||
p, err := ParsePackage( |
||||
data, |
||||
data.Size(), |
||||
strings.NewReader(`{"name":"`+packageName+`","version":"`+packageVersion+`","description":"`+packageDescription+`","keywords":["swift","package"],"license":"`+packageLicense+`","codeRepository":"`+packageRepositoryURL+`","author":{"givenName":"`+packageAuthor+`"},"repositoryURLs":["`+packageRepositoryURL+`"]}`), |
||||
) |
||||
assert.NotNil(t, p) |
||||
assert.NoError(t, err) |
||||
|
||||
assert.NotNil(t, p.Metadata) |
||||
assert.Len(t, p.Metadata.Manifests, 1) |
||||
m := p.Metadata.Manifests[""] |
||||
assert.Equal(t, "5.7", m.ToolsVersion) |
||||
|
||||
assert.Equal(t, packageDescription, p.Metadata.Description) |
||||
assert.ElementsMatch(t, []string{"swift", "package"}, p.Metadata.Keywords) |
||||
assert.Equal(t, packageLicense, p.Metadata.License) |
||||
assert.Equal(t, packageAuthor, p.Metadata.Author.GivenName) |
||||
assert.Equal(t, packageRepositoryURL, p.Metadata.RepositoryURL) |
||||
assert.ElementsMatch(t, []string{packageRepositoryURL}, p.RepositoryURLs) |
||||
}) |
||||
} |
||||
|
||||
func TestTrimmedVersionString(t *testing.T) { |
||||
cases := []struct { |
||||
Version *version.Version |
||||
Expected string |
||||
}{ |
||||
{ |
||||
Version: version.Must(version.NewVersion("1")), |
||||
Expected: "1.0", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0")), |
||||
Expected: "1.0", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0.0")), |
||||
Expected: "1.0", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0.1")), |
||||
Expected: "1.0.1", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0+meta")), |
||||
Expected: "1.0", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0.0+meta")), |
||||
Expected: "1.0", |
||||
}, |
||||
{ |
||||
Version: version.Must(version.NewVersion("1.0.1+meta")), |
||||
Expected: "1.0.1", |
||||
}, |
||||
} |
||||
|
||||
for _, c := range cases { |
||||
assert.Equal(t, c.Expected, TrimmedVersionString(c.Version)) |
||||
} |
||||
} |
After Width: | Height: | Size: 1.9 KiB |
@ -0,0 +1,464 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package swift |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"io" |
||||
"net/http" |
||||
"regexp" |
||||
"sort" |
||||
"strings" |
||||
|
||||
packages_model "code.gitea.io/gitea/models/packages" |
||||
"code.gitea.io/gitea/modules/context" |
||||
"code.gitea.io/gitea/modules/json" |
||||
"code.gitea.io/gitea/modules/log" |
||||
packages_module "code.gitea.io/gitea/modules/packages" |
||||
swift_module "code.gitea.io/gitea/modules/packages/swift" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
"code.gitea.io/gitea/modules/util" |
||||
"code.gitea.io/gitea/routers/api/packages/helper" |
||||
packages_service "code.gitea.io/gitea/services/packages" |
||||
|
||||
"github.com/hashicorp/go-version" |
||||
) |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
|
||||
const ( |
||||
AcceptJSON = "application/vnd.swift.registry.v1+json" |
||||
AcceptSwift = "application/vnd.swift.registry.v1+swift" |
||||
AcceptZip = "application/vnd.swift.registry.v1+zip" |
||||
) |
||||
|
||||
var ( |
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#361-package-scope
|
||||
scopePattern = regexp.MustCompile(`\A[a-zA-Z0-9][a-zA-Z0-9-]{0,38}\z`) |
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#362-package-name
|
||||
namePattern = regexp.MustCompile(`\A[a-zA-Z0-9][a-zA-Z0-9-_]{0,99}\z`) |
||||
) |
||||
|
||||
type headers struct { |
||||
Status int |
||||
ContentType string |
||||
Digest string |
||||
Location string |
||||
Link string |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
|
||||
func setResponseHeaders(resp http.ResponseWriter, h *headers) { |
||||
if h.ContentType != "" { |
||||
resp.Header().Set("Content-Type", h.ContentType) |
||||
} |
||||
if h.Digest != "" { |
||||
resp.Header().Set("Digest", "sha256="+h.Digest) |
||||
} |
||||
if h.Location != "" { |
||||
resp.Header().Set("Location", h.Location) |
||||
} |
||||
if h.Link != "" { |
||||
resp.Header().Set("Link", h.Link) |
||||
} |
||||
resp.Header().Set("Content-Version", "1") |
||||
if h.Status != 0 { |
||||
resp.WriteHeader(h.Status) |
||||
} |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#33-error-handling
|
||||
func apiError(ctx *context.Context, status int, obj interface{}) { |
||||
// https://www.rfc-editor.org/rfc/rfc7807
|
||||
type Problem struct { |
||||
Status int `json:"status"` |
||||
Detail string `json:"detail"` |
||||
} |
||||
|
||||
helper.LogAndProcessError(ctx, status, obj, func(message string) { |
||||
setResponseHeaders(ctx.Resp, &headers{ |
||||
Status: status, |
||||
ContentType: "application/problem+json", |
||||
}) |
||||
if err := json.NewEncoder(ctx.Resp).Encode(Problem{ |
||||
Status: status, |
||||
Detail: message, |
||||
}); err != nil { |
||||
log.Error("JSON encode: %v", err) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#35-api-versioning
|
||||
func CheckAcceptMediaType(requiredAcceptHeader string) func(ctx *context.Context) { |
||||
return func(ctx *context.Context) { |
||||
accept := ctx.Req.Header.Get("Accept") |
||||
if accept != "" && accept != requiredAcceptHeader { |
||||
apiError(ctx, http.StatusBadRequest, fmt.Sprintf("Unexpected accept header. Should be '%s'.", requiredAcceptHeader)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func buildPackageID(scope, name string) string { |
||||
return scope + "." + name |
||||
} |
||||
|
||||
type Release struct { |
||||
URL string `json:"url"` |
||||
} |
||||
|
||||
type EnumeratePackageVersionsResponse struct { |
||||
Releases map[string]Release `json:"releases"` |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#41-list-package-releases
|
||||
func EnumeratePackageVersions(ctx *context.Context) { |
||||
packageScope := ctx.Params("scope") |
||||
packageName := ctx.Params("name") |
||||
|
||||
pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeSwift, buildPackageID(packageScope, packageName)) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
if len(pvs) == 0 { |
||||
apiError(ctx, http.StatusNotFound, nil) |
||||
return |
||||
} |
||||
|
||||
pds, err := packages_model.GetPackageDescriptors(ctx, pvs) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
sort.Slice(pds, func(i, j int) bool { |
||||
return pds[i].SemVer.LessThan(pds[j].SemVer) |
||||
}) |
||||
|
||||
baseURL := fmt.Sprintf("%sapi/packages/%s/swift/%s/%s/", setting.AppURL, ctx.Package.Owner.LowerName, packageScope, packageName) |
||||
|
||||
releases := make(map[string]Release) |
||||
for _, pd := range pds { |
||||
version := pd.SemVer.String() |
||||
releases[version] = Release{ |
||||
URL: baseURL + version, |
||||
} |
||||
} |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{ |
||||
Link: fmt.Sprintf(`<%s%s>; rel="latest-version"`, baseURL, pds[len(pds)-1].Version.Version), |
||||
}) |
||||
|
||||
ctx.JSON(http.StatusOK, EnumeratePackageVersionsResponse{ |
||||
Releases: releases, |
||||
}) |
||||
} |
||||
|
||||
type Resource struct { |
||||
Name string `json:"id"` |
||||
Type string `json:"type"` |
||||
Checksum string `json:"checksum"` |
||||
} |
||||
|
||||
type PackageVersionMetadataResponse struct { |
||||
ID string `json:"id"` |
||||
Version string `json:"version"` |
||||
Resources []Resource `json:"resources"` |
||||
Metadata *swift_module.SoftwareSourceCode `json:"metadata"` |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-2
|
||||
func PackageVersionMetadata(ctx *context.Context) { |
||||
id := buildPackageID(ctx.Params("scope"), ctx.Params("name")) |
||||
|
||||
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeSwift, id, ctx.Params("version")) |
||||
if err != nil { |
||||
if errors.Is(err, util.ErrNotExist) { |
||||
apiError(ctx, http.StatusNotFound, err) |
||||
} else { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
} |
||||
return |
||||
} |
||||
|
||||
pd, err := packages_model.GetPackageDescriptor(ctx, pv) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
metadata := pd.Metadata.(*swift_module.Metadata) |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{}) |
||||
|
||||
ctx.JSON(http.StatusOK, PackageVersionMetadataResponse{ |
||||
ID: id, |
||||
Version: pd.Version.Version, |
||||
Resources: []Resource{ |
||||
{ |
||||
Name: "source-archive", |
||||
Type: "application/zip", |
||||
Checksum: pd.Files[0].Blob.HashSHA256, |
||||
}, |
||||
}, |
||||
Metadata: &swift_module.SoftwareSourceCode{ |
||||
Context: []string{"http://schema.org/"}, |
||||
Type: "SoftwareSourceCode", |
||||
Name: pd.PackageProperties.GetByName(swift_module.PropertyName), |
||||
Version: pd.Version.Version, |
||||
Description: metadata.Description, |
||||
Keywords: metadata.Keywords, |
||||
CodeRepository: metadata.RepositoryURL, |
||||
License: metadata.License, |
||||
ProgrammingLanguage: swift_module.ProgrammingLanguage{ |
||||
Type: "ComputerLanguage", |
||||
Name: "Swift", |
||||
URL: "https://swift.org", |
||||
}, |
||||
Author: swift_module.Person{ |
||||
Type: "Person", |
||||
GivenName: metadata.Author.GivenName, |
||||
MiddleName: metadata.Author.MiddleName, |
||||
FamilyName: metadata.Author.FamilyName, |
||||
}, |
||||
}, |
||||
}) |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#43-fetch-manifest-for-a-package-release
|
||||
func DownloadManifest(ctx *context.Context) { |
||||
packageScope := ctx.Params("scope") |
||||
packageName := ctx.Params("name") |
||||
packageVersion := ctx.Params("version") |
||||
|
||||
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeSwift, buildPackageID(packageScope, packageName), packageVersion) |
||||
if err != nil { |
||||
if errors.Is(err, util.ErrNotExist) { |
||||
apiError(ctx, http.StatusNotFound, err) |
||||
} else { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
} |
||||
return |
||||
} |
||||
|
||||
pd, err := packages_model.GetPackageDescriptor(ctx, pv) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
swiftVersion := ctx.FormTrim("swift-version") |
||||
if swiftVersion != "" { |
||||
v, err := version.NewVersion(swiftVersion) |
||||
if err == nil { |
||||
swiftVersion = swift_module.TrimmedVersionString(v) |
||||
} |
||||
} |
||||
m, ok := pd.Metadata.(*swift_module.Metadata).Manifests[swiftVersion] |
||||
if !ok { |
||||
setResponseHeaders(ctx.Resp, &headers{ |
||||
Status: http.StatusSeeOther, |
||||
Location: fmt.Sprintf("%sapi/packages/%s/swift/%s/%s/%s/Package.swift", setting.AppURL, ctx.Package.Owner.LowerName, packageScope, packageName, packageVersion), |
||||
}) |
||||
return |
||||
} |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{}) |
||||
|
||||
filename := "Package.swift" |
||||
if swiftVersion != "" { |
||||
filename = fmt.Sprintf("Package@swift-%s.swift", swiftVersion) |
||||
} |
||||
|
||||
ctx.ServeContent(strings.NewReader(m.Content), &context.ServeHeaderOptions{ |
||||
ContentType: "text/x-swift", |
||||
Filename: filename, |
||||
LastModified: pv.CreatedUnix.AsLocalTime(), |
||||
}) |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-6
|
||||
func UploadPackageFile(ctx *context.Context) { |
||||
packageScope := ctx.Params("scope") |
||||
packageName := ctx.Params("name") |
||||
|
||||
v, err := version.NewVersion(ctx.Params("version")) |
||||
|
||||
if !scopePattern.MatchString(packageScope) || !namePattern.MatchString(packageName) || err != nil { |
||||
apiError(ctx, http.StatusBadRequest, err) |
||||
return |
||||
} |
||||
|
||||
packageVersion := v.Core().String() |
||||
|
||||
file, _, err := ctx.Req.FormFile("source-archive") |
||||
if err != nil { |
||||
apiError(ctx, http.StatusBadRequest, err) |
||||
return |
||||
} |
||||
defer file.Close() |
||||
|
||||
buf, err := packages_module.CreateHashedBufferFromReader(file, 32*1024*1024) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
defer buf.Close() |
||||
|
||||
var mr io.Reader |
||||
metadata := ctx.Req.FormValue("metadata") |
||||
if metadata != "" { |
||||
mr = strings.NewReader(metadata) |
||||
} |
||||
|
||||
pck, err := swift_module.ParsePackage(buf, buf.Size(), mr) |
||||
if err != nil { |
||||
if errors.Is(err, util.ErrInvalidArgument) { |
||||
apiError(ctx, http.StatusBadRequest, err) |
||||
} else { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
} |
||||
return |
||||
} |
||||
|
||||
if _, err := buf.Seek(0, io.SeekStart); err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
pv, _, err := packages_service.CreatePackageAndAddFile( |
||||
&packages_service.PackageCreationInfo{ |
||||
PackageInfo: packages_service.PackageInfo{ |
||||
Owner: ctx.Package.Owner, |
||||
PackageType: packages_model.TypeSwift, |
||||
Name: buildPackageID(packageScope, packageName), |
||||
Version: packageVersion, |
||||
}, |
||||
SemverCompatible: true, |
||||
Creator: ctx.Doer, |
||||
Metadata: pck.Metadata, |
||||
PackageProperties: map[string]string{ |
||||
swift_module.PropertyScope: packageScope, |
||||
swift_module.PropertyName: packageName, |
||||
}, |
||||
}, |
||||
&packages_service.PackageFileCreationInfo{ |
||||
PackageFileInfo: packages_service.PackageFileInfo{ |
||||
Filename: fmt.Sprintf("%s-%s.zip", packageName, packageVersion), |
||||
}, |
||||
Creator: ctx.Doer, |
||||
Data: buf, |
||||
IsLead: true, |
||||
}, |
||||
) |
||||
if err != nil { |
||||
switch err { |
||||
case packages_model.ErrDuplicatePackageVersion: |
||||
apiError(ctx, http.StatusConflict, err) |
||||
case packages_service.ErrQuotaTotalCount, packages_service.ErrQuotaTypeSize, packages_service.ErrQuotaTotalSize: |
||||
apiError(ctx, http.StatusForbidden, err) |
||||
default: |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
} |
||||
return |
||||
} |
||||
|
||||
for _, url := range pck.RepositoryURLs { |
||||
_, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, swift_module.PropertyRepositoryURL, url) |
||||
if err != nil { |
||||
log.Error("InsertProperty failed: %v", err) |
||||
} |
||||
} |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{}) |
||||
|
||||
ctx.Status(http.StatusCreated) |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-4
|
||||
func DownloadPackageFile(ctx *context.Context) { |
||||
pv, err := packages_model.GetVersionByNameAndVersion(ctx, ctx.Package.Owner.ID, packages_model.TypeSwift, buildPackageID(ctx.Params("scope"), ctx.Params("name")), ctx.Params("version")) |
||||
if err != nil { |
||||
if errors.Is(err, util.ErrNotExist) { |
||||
apiError(ctx, http.StatusNotFound, err) |
||||
} else { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
} |
||||
return |
||||
} |
||||
|
||||
pd, err := packages_model.GetPackageDescriptor(ctx, pv) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
pf := pd.Files[0].File |
||||
|
||||
s, _, err := packages_service.GetPackageFileStream(ctx, pf) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
defer s.Close() |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{ |
||||
Digest: pd.Files[0].Blob.HashSHA256, |
||||
}) |
||||
|
||||
ctx.ServeContent(s, &context.ServeHeaderOptions{ |
||||
Filename: pf.Name, |
||||
ContentType: "application/zip", |
||||
LastModified: pf.CreatedUnix.AsLocalTime(), |
||||
}) |
||||
} |
||||
|
||||
type LookupPackageIdentifiersResponse struct { |
||||
Identifiers []string `json:"identifiers"` |
||||
} |
||||
|
||||
// https://github.com/apple/swift-package-manager/blob/main/Documentation/Registry.md#endpoint-5
|
||||
func LookupPackageIdentifiers(ctx *context.Context) { |
||||
url := ctx.FormTrim("url") |
||||
if url == "" { |
||||
apiError(ctx, http.StatusBadRequest, nil) |
||||
return |
||||
} |
||||
|
||||
pvs, _, err := packages_model.SearchLatestVersions(ctx, &packages_model.PackageSearchOptions{ |
||||
OwnerID: ctx.Package.Owner.ID, |
||||
Type: packages_model.TypeSwift, |
||||
Properties: map[string]string{ |
||||
swift_module.PropertyRepositoryURL: url, |
||||
}, |
||||
IsInternal: util.OptionalBoolFalse, |
||||
}) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
if len(pvs) == 0 { |
||||
apiError(ctx, http.StatusNotFound, nil) |
||||
return |
||||
} |
||||
|
||||
pds, err := packages_model.GetPackageDescriptors(ctx, pvs) |
||||
if err != nil { |
||||
apiError(ctx, http.StatusInternalServerError, err) |
||||
return |
||||
} |
||||
|
||||
identifiers := make([]string, 0, len(pds)) |
||||
for _, pd := range pds { |
||||
identifiers = append(identifiers, pd.Package.Name) |
||||
} |
||||
|
||||
setResponseHeaders(ctx.Resp, &headers{}) |
||||
|
||||
ctx.JSON(http.StatusOK, LookupPackageIdentifiersResponse{ |
||||
Identifiers: identifiers, |
||||
}) |
||||
} |
@ -0,0 +1,40 @@ |
||||
{{if eq .PackageDescriptor.Package.Type "swift"}} |
||||
<h4 class="ui top attached header">{{.locale.Tr "packages.installation"}}</h4> |
||||
<div class="ui attached segment"> |
||||
<div class="ui form"> |
||||
<div class="field"> |
||||
<label>{{svg "octicon-terminal"}} {{.locale.Tr "packages.swift.registry"}}</label> |
||||
<div class="markup"><pre class="code-block"><code>swift package-registry set <gitea-origin-url data-url="{{AppSubUrl}}/api/packages/{{.PackageDescriptor.Owner.Name}}/swift"></gitea-origin-url></code></pre></div> |
||||
</div> |
||||
<div class="field"> |
||||
<label>{{svg "octicon-code"}} {{.locale.Tr "packages.swift.install" | Safe}}</label> |
||||
<div class="markup"><pre class="code-block"><code>dependencies: [ |
||||
.package(id: "{{.PackageDescriptor.Package.Name}}", from:"{{.PackageDescriptor.Version.Version}}") |
||||
]</code></pre></div> |
||||
</div> |
||||
<div class="field"> |
||||
<label>{{svg "octicon-terminal"}} {{.locale.Tr "packages.swift.install2"}}</label> |
||||
<div class="markup"><pre class="code-block"><code>swift package resolve</code></pre></div> |
||||
</div> |
||||
<div class="field"> |
||||
<label>{{.locale.Tr "packages.swift.documentation" | Safe}}</label> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
|
||||
{{if .PackageDescriptor.Metadata.Description}} |
||||
<h4 class="ui top attached header">{{.locale.Tr "packages.about"}}</h4> |
||||
<div class="ui attached segment"> |
||||
{{if .PackageDescriptor.Metadata.Description}}{{.PackageDescriptor.Metadata.Description}}{{end}} |
||||
</div> |
||||
{{end}} |
||||
|
||||
{{if .PackageDescriptor.Metadata.Keywords}} |
||||
<h4 class="ui top attached header">{{.locale.Tr "packages.keywords"}}</h4> |
||||
<div class="ui attached segment"> |
||||
{{range .PackageDescriptor.Metadata.Keywords}} |
||||
{{.}} |
||||
{{end}} |
||||
</div> |
||||
{{end}} |
||||
{{end}} |
@ -0,0 +1,4 @@ |
||||
{{if eq .PackageDescriptor.Package.Type "swift"}} |
||||
{{if .PackageDescriptor.Metadata.Author.String}}<div class="item" title="{{.locale.Tr "packages.details.author"}}">{{svg "octicon-person" 16 "mr-3"}} {{.PackageDescriptor.Metadata.Author}}</div>{{end}} |
||||
{{if .PackageDescriptor.Metadata.RepositoryURL}}<div class="item">{{svg "octicon-link-external" 16 "mr-3"}} <a href="{{.PackageDescriptor.Metadata.RepositoryURL}}" target="_blank" rel="noopener noreferrer me">{{.locale.Tr "packages.details.project_site"}}</a></div>{{end}} |
||||
{{end}} |
@ -0,0 +1,326 @@ |
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration |
||||
|
||||
import ( |
||||
"archive/zip" |
||||
"bytes" |
||||
"fmt" |
||||
"io" |
||||
"mime/multipart" |
||||
"net/http" |
||||
"strings" |
||||
"testing" |
||||
|
||||
"code.gitea.io/gitea/models/db" |
||||
"code.gitea.io/gitea/models/packages" |
||||
"code.gitea.io/gitea/models/unittest" |
||||
user_model "code.gitea.io/gitea/models/user" |
||||
swift_module "code.gitea.io/gitea/modules/packages/swift" |
||||
"code.gitea.io/gitea/modules/setting" |
||||
swift_router "code.gitea.io/gitea/routers/api/packages/swift" |
||||
"code.gitea.io/gitea/tests" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func TestPackageSwift(t *testing.T) { |
||||
defer tests.PrepareTestEnv(t)() |
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
||||
|
||||
packageScope := "test-scope" |
||||
packageName := "test_package" |
||||
packageID := packageScope + "." + packageName |
||||
packageVersion := "1.0.3" |
||||
packageAuthor := "KN4CK3R" |
||||
packageDescription := "Gitea Test Package" |
||||
packageRepositoryURL := "https://gitea.io/gitea/gitea" |
||||
contentManifest1 := "// swift-tools-version:5.7\n//\n// Package.swift" |
||||
contentManifest2 := "// swift-tools-version:5.6\n//\n// Package@swift-5.6.swift" |
||||
|
||||
url := fmt.Sprintf("/api/packages/%s/swift", user.Name) |
||||
|
||||
t.Run("CheckAcceptMediaType", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
for _, sub := range []string{ |
||||
"/scope/package", |
||||
"/scope/package.json", |
||||
"/scope/package/1.0.0", |
||||
"/scope/package/1.0.0.json", |
||||
"/scope/package/1.0.0.zip", |
||||
"/scope/package/1.0.0/Package.swift", |
||||
"/identifiers", |
||||
} { |
||||
req := NewRequest(t, "GET", url+sub) |
||||
req.Header.Add("Accept", "application/unknown") |
||||
resp := MakeRequest(t, req, http.StatusBadRequest) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type")) |
||||
} |
||||
|
||||
req := NewRequestWithBody(t, "PUT", url+"/scope/package/1.0.0", strings.NewReader("")) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
req.Header.Add("Accept", "application/unknown") |
||||
resp := MakeRequest(t, req, http.StatusBadRequest) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type")) |
||||
}) |
||||
|
||||
t.Run("Upload", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
uploadPackage := func(t *testing.T, url string, expectedStatus int, sr io.Reader, metadata string) { |
||||
var body bytes.Buffer |
||||
mpw := multipart.NewWriter(&body) |
||||
|
||||
part, _ := mpw.CreateFormFile("source-archive", "source-archive.zip") |
||||
io.Copy(part, sr) |
||||
|
||||
if metadata != "" { |
||||
mpw.WriteField("metadata", metadata) |
||||
} |
||||
|
||||
mpw.Close() |
||||
|
||||
req := NewRequestWithBody(t, "PUT", url, &body) |
||||
req.Header.Add("Content-Type", mpw.FormDataContentType()) |
||||
req.Header.Add("Accept", swift_router.AcceptJSON) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
MakeRequest(t, req, expectedStatus) |
||||
} |
||||
|
||||
createArchive := func(files map[string]string) *bytes.Buffer { |
||||
var buf bytes.Buffer |
||||
zw := zip.NewWriter(&buf) |
||||
for filename, content := range files { |
||||
w, _ := zw.Create(filename) |
||||
w.Write([]byte(content)) |
||||
} |
||||
zw.Close() |
||||
return &buf |
||||
} |
||||
|
||||
for _, triple := range []string{"/sc_ope/package/1.0.0", "/scope/pack~age/1.0.0", "/scope/package/1_0.0"} { |
||||
req := NewRequestWithBody(t, "PUT", url+triple, bytes.NewReader([]byte{})) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
resp := MakeRequest(t, req, http.StatusBadRequest) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type")) |
||||
} |
||||
|
||||
uploadURL := fmt.Sprintf("%s/%s/%s/%s", url, packageScope, packageName, packageVersion) |
||||
|
||||
req := NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{})) |
||||
MakeRequest(t, req, http.StatusUnauthorized) |
||||
|
||||
uploadPackage( |
||||
t, |
||||
uploadURL, |
||||
http.StatusCreated, |
||||
createArchive(map[string]string{ |
||||
"Package.swift": contentManifest1, |
||||
"Package@swift-5.6.swift": contentManifest2, |
||||
}), |
||||
`{"name":"`+packageName+`","version":"`+packageVersion+`","description":"`+packageDescription+`","codeRepository":"`+packageRepositoryURL+`","author":{"givenName":"`+packageAuthor+`"},"repositoryURLs":["`+packageRepositoryURL+`"]}`, |
||||
) |
||||
|
||||
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeSwift) |
||||
assert.NoError(t, err) |
||||
assert.Len(t, pvs, 1) |
||||
|
||||
pd, err := packages.GetPackageDescriptor(db.DefaultContext, pvs[0]) |
||||
assert.NoError(t, err) |
||||
assert.NotNil(t, pd.SemVer) |
||||
assert.Equal(t, packageID, pd.Package.Name) |
||||
assert.Equal(t, packageVersion, pd.Version.Version) |
||||
assert.IsType(t, &swift_module.Metadata{}, pd.Metadata) |
||||
metadata := pd.Metadata.(*swift_module.Metadata) |
||||
assert.Equal(t, packageDescription, metadata.Description) |
||||
assert.Len(t, metadata.Manifests, 2) |
||||
assert.Equal(t, contentManifest1, metadata.Manifests[""].Content) |
||||
assert.Equal(t, contentManifest2, metadata.Manifests["5.6"].Content) |
||||
assert.Len(t, pd.VersionProperties, 1) |
||||
assert.Equal(t, packageRepositoryURL, pd.VersionProperties.GetByName(swift_module.PropertyRepositoryURL)) |
||||
|
||||
pfs, err := packages.GetFilesByVersionID(db.DefaultContext, pvs[0].ID) |
||||
assert.NoError(t, err) |
||||
assert.Len(t, pfs, 1) |
||||
assert.Equal(t, fmt.Sprintf("%s-%s.zip", packageName, packageVersion), pfs[0].Name) |
||||
assert.True(t, pfs[0].IsLead) |
||||
|
||||
uploadPackage( |
||||
t, |
||||
uploadURL, |
||||
http.StatusConflict, |
||||
createArchive(map[string]string{ |
||||
"Package.swift": contentManifest1, |
||||
}), |
||||
"", |
||||
) |
||||
}) |
||||
|
||||
t.Run("Download", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/%s.zip", url, packageScope, packageName, packageVersion)) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
req.Header.Add("Accept", swift_router.AcceptZip) |
||||
resp := MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "application/zip", resp.Header().Get("Content-Type")) |
||||
|
||||
pv, err := packages.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages.TypeSwift, packageID, packageVersion) |
||||
assert.NotNil(t, pv) |
||||
assert.NoError(t, err) |
||||
|
||||
pd, err := packages.GetPackageDescriptor(db.DefaultContext, pv) |
||||
assert.NoError(t, err) |
||||
assert.Equal(t, "sha256="+pd.Files[0].Blob.HashSHA256, resp.Header().Get("Digest")) |
||||
}) |
||||
|
||||
t.Run("EnumeratePackageVersions", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s", url, packageScope, packageName)) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
req.Header.Add("Accept", swift_router.AcceptJSON) |
||||
resp := MakeRequest(t, req, http.StatusOK) |
||||
|
||||
versionURL := setting.AppURL + url[1:] + fmt.Sprintf("/%s/%s/%s", packageScope, packageName, packageVersion) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, fmt.Sprintf(`<%s>; rel="latest-version"`, versionURL), resp.Header().Get("Link")) |
||||
|
||||
body := resp.Body.String() |
||||
|
||||
var result *swift_router.EnumeratePackageVersionsResponse |
||||
DecodeJSON(t, resp, &result) |
||||
|
||||
assert.Len(t, result.Releases, 1) |
||||
assert.Contains(t, result.Releases, packageVersion) |
||||
assert.Equal(t, versionURL, result.Releases[packageVersion].URL) |
||||
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s.json", url, packageScope, packageName)) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
resp = MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, body, resp.Body.String()) |
||||
}) |
||||
|
||||
t.Run("PackageVersionMetadata", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/%s", url, packageScope, packageName, packageVersion)) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
req.Header.Add("Accept", swift_router.AcceptJSON) |
||||
resp := MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
|
||||
body := resp.Body.String() |
||||
|
||||
var result *swift_router.PackageVersionMetadataResponse |
||||
DecodeJSON(t, resp, &result) |
||||
|
||||
pv, err := packages.GetVersionByNameAndVersion(db.DefaultContext, user.ID, packages.TypeSwift, packageID, packageVersion) |
||||
assert.NotNil(t, pv) |
||||
assert.NoError(t, err) |
||||
|
||||
pd, err := packages.GetPackageDescriptor(db.DefaultContext, pv) |
||||
assert.NoError(t, err) |
||||
|
||||
assert.Equal(t, packageID, result.ID) |
||||
assert.Equal(t, packageVersion, result.Version) |
||||
assert.Len(t, result.Resources, 1) |
||||
assert.Equal(t, "source-archive", result.Resources[0].Name) |
||||
assert.Equal(t, "application/zip", result.Resources[0].Type) |
||||
assert.Equal(t, pd.Files[0].Blob.HashSHA256, result.Resources[0].Checksum) |
||||
assert.Equal(t, "SoftwareSourceCode", result.Metadata.Type) |
||||
assert.Equal(t, packageName, result.Metadata.Name) |
||||
assert.Equal(t, packageVersion, result.Metadata.Version) |
||||
assert.Equal(t, packageDescription, result.Metadata.Description) |
||||
assert.Equal(t, "Swift", result.Metadata.ProgrammingLanguage.Name) |
||||
assert.Equal(t, packageAuthor, result.Metadata.Author.GivenName) |
||||
|
||||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s/%s.json", url, packageScope, packageName, packageVersion)) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
resp = MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, body, resp.Body.String()) |
||||
}) |
||||
|
||||
t.Run("DownloadManifest", func(t *testing.T) { |
||||
manifestURL := fmt.Sprintf("%s/%s/%s/%s/Package.swift", url, packageScope, packageName, packageVersion) |
||||
|
||||
t.Run("Default", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", manifestURL) |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
req.Header.Add("Accept", swift_router.AcceptSwift) |
||||
resp := MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "text/x-swift", resp.Header().Get("Content-Type")) |
||||
assert.Equal(t, contentManifest1, resp.Body.String()) |
||||
}) |
||||
|
||||
t.Run("DifferentVersion", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", manifestURL+"?swift-version=5.6") |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
resp := MakeRequest(t, req, http.StatusOK) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "text/x-swift", resp.Header().Get("Content-Type")) |
||||
assert.Equal(t, contentManifest2, resp.Body.String()) |
||||
|
||||
req = NewRequest(t, "GET", manifestURL+"?swift-version=5.6.0") |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
MakeRequest(t, req, http.StatusOK) |
||||
}) |
||||
|
||||
t.Run("Redirect", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", manifestURL+"?swift-version=1.0") |
||||
req = AddBasicAuthHeader(req, user.Name) |
||||
resp := MakeRequest(t, req, http.StatusSeeOther) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, setting.AppURL+url[1:]+fmt.Sprintf("/%s/%s/%s/Package.swift", packageScope, packageName, packageVersion), resp.Header().Get("Location")) |
||||
}) |
||||
}) |
||||
|
||||
t.Run("LookupPackageIdentifiers", func(t *testing.T) { |
||||
defer tests.PrintCurrentTest(t)() |
||||
|
||||
req := NewRequest(t, "GET", url+"/identifiers") |
||||
req.Header.Add("Accept", swift_router.AcceptJSON) |
||||
resp := MakeRequest(t, req, http.StatusBadRequest) |
||||
|
||||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) |
||||
assert.Equal(t, "application/problem+json", resp.Header().Get("Content-Type")) |
||||
|
||||
req = NewRequest(t, "GET", url+"/identifiers?url=https://unknown.host/") |
||||
MakeRequest(t, req, http.StatusNotFound) |
||||
|
||||
req = NewRequest(t, "GET", url+"/identifiers?url="+packageRepositoryURL) |
||||
req.Header.Add("Accept", swift_router.AcceptJSON) |
||||
resp = MakeRequest(t, req, http.StatusOK) |
||||
|
||||
var result *swift_router.LookupPackageIdentifiersResponse |
||||
DecodeJSON(t, resp, &result) |
||||
|
||||
assert.Len(t, result.Identifiers, 1) |
||||
assert.Equal(t, packageID, result.Identifiers[0]) |
||||
}) |
||||
} |
After Width: | Height: | Size: 2.7 KiB |
Loading…
Reference in new issue