-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Swift files can be passed either as file or as form value #34068
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
wxiaoguang
merged 7 commits into
go-gitea:main
from
wgr1984:swift-package-manager-publishing-using-commandline-throws-error
Apr 18, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a268451
Fixes: Swift Package Manager Publishing using commandline throws erro…
wgr1984 7af142a
Improve error handling for package file uploads in Swift API (#33990)
wgr1984 559295a
Merge branch 'main' into swift-package-manager-publishing-using-comma…
lunny f8b66c5
fix
wxiaoguang c5edcd8
Merge branch 'main' into swift-package-manager-publishing-using-comma…
wxiaoguang c97d8ed
fix
wxiaoguang be2725d
fix test
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"code.gitea.io/gitea/tests" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPackageSwift(t *testing.T) { | ||
|
@@ -34,6 +35,7 @@ func TestPackageSwift(t *testing.T) { | |
packageName := "test_package" | ||
packageID := packageScope + "." + packageName | ||
packageVersion := "1.0.3" | ||
packageVersion2 := "1.0.4" | ||
packageAuthor := "KN4CK3R" | ||
packageDescription := "Gitea Test Package" | ||
packageRepositoryURL := "https://gitea.io/gitea/gitea" | ||
|
@@ -183,6 +185,94 @@ func TestPackageSwift(t *testing.T) { | |
) | ||
}) | ||
|
||
t.Run("UploadMultipart", 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) | ||
|
||
// Read the source archive content | ||
sourceContent, err := io.ReadAll(sr) | ||
assert.NoError(t, err) | ||
mpw.WriteField("source-archive", string(sourceContent)) | ||
|
||
if metadata != "" { | ||
mpw.WriteField("metadata", metadata) | ||
} | ||
|
||
mpw.Close() | ||
|
||
req := NewRequestWithBody(t, "PUT", url, &body). | ||
SetHeader("Content-Type", mpw.FormDataContentType()). | ||
SetHeader("Accept", swift_router.AcceptJSON). | ||
AddBasicAuth(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 | ||
} | ||
|
||
uploadURL := fmt.Sprintf("%s/%s/%s/%s", url, packageScope, packageName, packageVersion2) | ||
|
||
req := NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader([]byte{})) | ||
MakeRequest(t, req, http.StatusUnauthorized) | ||
|
||
// Test with metadata as form field | ||
uploadPackage( | ||
t, | ||
uploadURL, | ||
http.StatusCreated, | ||
createArchive(map[string]string{ | ||
"Package.swift": contentManifest1, | ||
"[email protected]": contentManifest2, | ||
}), | ||
`{"name":"`+packageName+`","version":"`+packageVersion2+`","description":"`+packageDescription+`","codeRepository":"`+packageRepositoryURL+`","author":{"givenName":"`+packageAuthor+`"},"repositoryURLs":["`+packageRepositoryURL+`"]}`, | ||
) | ||
|
||
pvs, err := packages.GetVersionsByPackageType(db.DefaultContext, user.ID, packages.TypeSwift) | ||
assert.NoError(t, err) | ||
require.Len(t, pvs, 2) // ATTENTION: many subtests are unable to run separately, they depend on the results of previous tests | ||
thisPackageVersion := pvs[0] | ||
pd, err := packages.GetPackageDescriptor(db.DefaultContext, thisPackageVersion) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, pd.SemVer) | ||
assert.Equal(t, packageID, pd.Package.Name) | ||
assert.Equal(t, packageVersion2, 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, thisPackageVersion.ID) | ||
assert.NoError(t, err) | ||
assert.Len(t, pfs, 1) | ||
assert.Equal(t, fmt.Sprintf("%s-%s.zip", packageName, packageVersion2), 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)() | ||
|
||
|
@@ -211,7 +301,7 @@ func TestPackageSwift(t *testing.T) { | |
SetHeader("Accept", swift_router.AcceptJSON) | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
versionURL := setting.AppURL + url[1:] + fmt.Sprintf("/%s/%s/%s", packageScope, packageName, packageVersion) | ||
versionURL := setting.AppURL + url[1:] + fmt.Sprintf("/%s/%s/%s", packageScope, packageName, packageVersion2) | ||
|
||
assert.Equal(t, "1", resp.Header().Get("Content-Version")) | ||
assert.Equal(t, fmt.Sprintf(`<%s>; rel="latest-version"`, versionURL), resp.Header().Get("Link")) | ||
|
@@ -221,9 +311,9 @@ func TestPackageSwift(t *testing.T) { | |
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) | ||
assert.Len(t, result.Releases, 2) | ||
assert.Contains(t, result.Releases, packageVersion2) | ||
assert.Equal(t, versionURL, result.Releases[packageVersion2].URL) | ||
|
||
req = NewRequest(t, "GET", fmt.Sprintf("%s/%s/%s.json", url, packageScope, packageName)). | ||
AddBasicAuth(user.Name) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.