-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add Go package registry #24687
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
Add Go package registry #24687
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
642b3b4
Add Go package registry.
KN4CK3R 4cc31e2
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R c819911
lint docs
KN4CK3R 32aeab7
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 2eae004
Crop svg.
KN4CK3R a9d7574
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R 3868450
typo
KN4CK3R 5551acd
Add optional `/@latest` endpoint.
KN4CK3R 139c1a0
Update docs.
KN4CK3R c2711de
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R b9b6c10
Add test for `latest`.
KN4CK3R 583b00f
Merge branch 'main' into feature-goproxy
GiteaBot 8c30272
Merge branch 'main' into feature-goproxy
GiteaBot c6d2b8a
Merge branch 'main' into feature-goproxy
GiteaBot dc2eb45
Merge branch 'main' into feature-goproxy
GiteaBot 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
--- | ||
date: "2023-05-10T00:00:00+00:00" | ||
title: "Go Packages Repository" | ||
slug: "go" | ||
weight: 45 | ||
draft: false | ||
toc: false | ||
menu: | ||
sidebar: | ||
parent: "packages" | ||
name: "Go" | ||
weight: 45 | ||
identifier: "go" | ||
--- | ||
|
||
# Go Packages Repository | ||
|
||
Publish Go packages for your user or organization. | ||
|
||
**Table of Contents** | ||
|
||
{{< toc >}} | ||
|
||
## Publish a package | ||
|
||
To publish a Go package perform a HTTP `PUT` operation with the package content in the request body. | ||
You cannot publish a package if a package of the same name and version already exists. You must delete the existing package first. | ||
The package must follow the [documented structure](https://go.dev/ref/mod#zip-files). | ||
|
||
``` | ||
PUT https://gitea.example.com/api/packages/{owner}/go/upload | ||
``` | ||
|
||
| Parameter | Description | | ||
| --------- | ----------- | | ||
| `owner` | The owner of the package. | | ||
|
||
To authenticate to the package registry, you need to provide [custom HTTP headers or use HTTP Basic authentication]({{< relref "doc/development/api-usage.en-us.md#authentication" >}}): | ||
|
||
```shell | ||
curl --user your_username:your_password_or_token \ | ||
--upload-file path/to/file.zip \ | ||
https://gitea.example.com/api/packages/testuser/go/upload | ||
``` | ||
|
||
If you are using 2FA or OAuth use a [personal access token]({{< relref "doc/development/api-usage.en-us.md#authentication" >}}) instead of the password. | ||
|
||
The server reponds with the following HTTP Status codes. | ||
|
||
| HTTP Status Code | Meaning | | ||
| ----------------- | ------- | | ||
| `201 Created` | The package has been published. | | ||
| `400 Bad Request` | The package is invalid. | | ||
| `409 Conflict` | A package with the same name exist already. | | ||
|
||
## Install a package | ||
|
||
To install a Go package instruct Go to use the package registry as proxy: | ||
|
||
```shell | ||
# use latest version | ||
GOPROXY=https://gitea.example.com/api/packages/{owner}/go go install {package_name} | ||
# or | ||
GOPROXY=https://gitea.example.com/api/packages/{owner}/go go install {package_name}@latest | ||
# use specific version | ||
GOPROXY=https://gitea.example.com/api/packages/{owner}/go go install {package_name}@{package_version} | ||
``` | ||
|
||
| Parameter | Description | | ||
| ----------------- | ----------- | | ||
| `owner` | The owner of the package. | | ||
| `package_name` | The package name. | | ||
| `package_version` | The package version. | | ||
|
||
If the owner of the packages is private you need to provide credentials. | ||
For more information see [the Go documentation](https://go.dev/ref/mod#private-module-proxy-auth). |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package goproxy | ||
|
||
import ( | ||
"archive/zip" | ||
"fmt" | ||
"io" | ||
"path" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
const ( | ||
PropertyGoMod = "go.mod" | ||
|
||
maxGoModFileSize = 16 * 1024 * 1024 // https://go.dev/ref/mod#zip-path-size-constraints | ||
) | ||
|
||
var ( | ||
ErrInvalidStructure = util.NewInvalidArgumentErrorf("package has invalid structure") | ||
ErrGoModFileTooLarge = util.NewInvalidArgumentErrorf("go.mod file is too large") | ||
) | ||
|
||
type Package struct { | ||
Name string | ||
Version string | ||
GoMod string | ||
} | ||
|
||
// ParsePackage parses the Go package file | ||
// https://go.dev/ref/mod#zip-files | ||
func ParsePackage(r io.ReaderAt, size int64) (*Package, error) { | ||
archive, err := zip.NewReader(r, size) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var p *Package | ||
|
||
for _, file := range archive.File { | ||
nameAndVersion := path.Dir(file.Name) | ||
|
||
parts := strings.SplitN(nameAndVersion, "@", 2) | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
|
||
versionParts := strings.SplitN(parts[1], "/", 2) | ||
|
||
if p == nil { | ||
p = &Package{ | ||
Name: strings.TrimSuffix(nameAndVersion, "@"+parts[1]), | ||
Version: versionParts[0], | ||
} | ||
} | ||
|
||
if len(versionParts) > 1 { | ||
// files are expected in the "root" folder | ||
continue | ||
} | ||
|
||
if path.Base(file.Name) == "go.mod" { | ||
if file.UncompressedSize64 > maxGoModFileSize { | ||
return nil, ErrGoModFileTooLarge | ||
} | ||
|
||
f, err := archive.Open(file.Name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
bytes, err := io.ReadAll(&io.LimitedReader{R: f, N: maxGoModFileSize}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p.GoMod = string(bytes) | ||
|
||
return p, nil | ||
} | ||
} | ||
|
||
if p == nil { | ||
return nil, ErrInvalidStructure | ||
} | ||
|
||
p.GoMod = fmt.Sprintf("module %s", p.Name) | ||
|
||
return p, nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package goproxy | ||
|
||
import ( | ||
"archive/zip" | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
packageName = "gitea.com/go-gitea/gitea" | ||
packageVersion = "v0.0.1" | ||
) | ||
|
||
func TestParsePackage(t *testing.T) { | ||
createArchive := func(files map[string][]byte) *bytes.Reader { | ||
var buf bytes.Buffer | ||
zw := zip.NewWriter(&buf) | ||
for name, content := range files { | ||
w, _ := zw.Create(name) | ||
w.Write(content) | ||
} | ||
zw.Close() | ||
return bytes.NewReader(buf.Bytes()) | ||
} | ||
|
||
t.Run("EmptyPackage", func(t *testing.T) { | ||
data := createArchive(nil) | ||
|
||
p, err := ParsePackage(data, int64(data.Len())) | ||
assert.Nil(t, p) | ||
assert.ErrorIs(t, err, ErrInvalidStructure) | ||
}) | ||
|
||
t.Run("InvalidNameOrVersionStructure", func(t *testing.T) { | ||
data := createArchive(map[string][]byte{ | ||
packageName + "/" + packageVersion + "/go.mod": {}, | ||
}) | ||
|
||
p, err := ParsePackage(data, int64(data.Len())) | ||
assert.Nil(t, p) | ||
assert.ErrorIs(t, err, ErrInvalidStructure) | ||
}) | ||
|
||
t.Run("GoModFileInWrongDirectory", func(t *testing.T) { | ||
data := createArchive(map[string][]byte{ | ||
packageName + "@" + packageVersion + "/subdir/go.mod": {}, | ||
}) | ||
|
||
p, err := ParsePackage(data, int64(data.Len())) | ||
assert.NotNil(t, p) | ||
assert.NoError(t, err) | ||
assert.Equal(t, packageName, p.Name) | ||
assert.Equal(t, packageVersion, p.Version) | ||
assert.Equal(t, "module gitea.com/go-gitea/gitea", p.GoMod) | ||
}) | ||
|
||
t.Run("Valid", func(t *testing.T) { | ||
data := createArchive(map[string][]byte{ | ||
packageName + "@" + packageVersion + "/subdir/go.mod": []byte("invalid"), | ||
packageName + "@" + packageVersion + "/go.mod": []byte("valid"), | ||
}) | ||
|
||
p, err := ParsePackage(data, int64(data.Len())) | ||
assert.NotNil(t, p) | ||
assert.NoError(t, err) | ||
assert.Equal(t, packageName, p.Name) | ||
assert.Equal(t, packageVersion, p.Version) | ||
assert.Equal(t, "valid", p.GoMod) | ||
}) | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.
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.