Add support for Pub packages (#20560)
* Added support for Pub packages. * Update docs/content/doc/packages/overview.en-us.md Co-authored-by: Gergely Nagy <algernon@users.noreply.github.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Gergely Nagy <algernon@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
d4326afb25
commit
f55af4675c
22 changed files with 908 additions and 4 deletions
154
modules/packages/pub/metadata.go
Normal file
154
modules/packages/pub/metadata.go
Normal file
|
@ -0,0 +1,154 @@
|
|||
// Copyright 2022 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 pub
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingPubspecFile = errors.New("Pubspec file is missing")
|
||||
ErrPubspecFileTooLarge = errors.New("Pubspec file is too large")
|
||||
ErrInvalidName = errors.New("Package name is invalid")
|
||||
ErrInvalidVersion = errors.New("Package version is invalid")
|
||||
)
|
||||
|
||||
var namePattern = regexp.MustCompile(`\A[a-zA-Z_][a-zA-Z0-9_]*\z`)
|
||||
|
||||
// https://github.com/dart-lang/pub-dev/blob/4d582302a8d10152a5cd6129f65bf4f4dbca239d/pkg/pub_package_reader/lib/pub_package_reader.dart#L143
|
||||
const maxPubspecFileSize = 128 * 1024
|
||||
|
||||
// Package represents a Pub package
|
||||
type Package struct {
|
||||
Name string
|
||||
Version string
|
||||
Metadata *Metadata
|
||||
}
|
||||
|
||||
// Metadata represents the metadata of a Pub package
|
||||
type Metadata struct {
|
||||
Description string `json:"description,omitempty"`
|
||||
ProjectURL string `json:"project_url,omitempty"`
|
||||
RepositoryURL string `json:"repository_url,omitempty"`
|
||||
DocumentationURL string `json:"documentation_url,omitempty"`
|
||||
Readme string `json:"readme,omitempty"`
|
||||
Pubspec interface{} `json:"pubspec"`
|
||||
}
|
||||
|
||||
type pubspecPackage struct {
|
||||
Name string `yaml:"name"`
|
||||
Version string `yaml:"version"`
|
||||
Description string `yaml:"description"`
|
||||
Homepage string `yaml:"homepage"`
|
||||
Repository string `yaml:"repository"`
|
||||
Documentation string `yaml:"documentation"`
|
||||
}
|
||||
|
||||
// ParsePackage parses the Pub package file
|
||||
func ParsePackage(r io.Reader) (*Package, error) {
|
||||
gzr, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer gzr.Close()
|
||||
|
||||
var p *Package
|
||||
var readme string
|
||||
|
||||
tr := tar.NewReader(gzr)
|
||||
for {
|
||||
hd, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if hd.Typeflag != tar.TypeReg {
|
||||
continue
|
||||
}
|
||||
|
||||
if hd.Name == "pubspec.yaml" {
|
||||
if hd.Size > maxPubspecFileSize {
|
||||
return nil, ErrPubspecFileTooLarge
|
||||
}
|
||||
p, err = ParsePubspecMetadata(tr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if strings.ToLower(hd.Name) == "readme.md" {
|
||||
data, err := io.ReadAll(tr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
readme = string(data)
|
||||
}
|
||||
}
|
||||
|
||||
if p == nil {
|
||||
return nil, ErrMissingPubspecFile
|
||||
}
|
||||
|
||||
p.Metadata.Readme = readme
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// ParsePubspecMetadata parses a Pubspec file to retrieve the metadata of a Pub package
|
||||
func ParsePubspecMetadata(r io.Reader) (*Package, error) {
|
||||
buf, err := io.ReadAll(io.LimitReader(r, maxPubspecFileSize))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var p pubspecPackage
|
||||
if err := yaml.Unmarshal(buf, &p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !namePattern.MatchString(p.Name) {
|
||||
return nil, ErrInvalidName
|
||||
}
|
||||
|
||||
v, err := version.NewSemver(p.Version)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidVersion
|
||||
}
|
||||
|
||||
if !validation.IsValidURL(p.Homepage) {
|
||||
p.Homepage = ""
|
||||
}
|
||||
if !validation.IsValidURL(p.Repository) {
|
||||
p.Repository = ""
|
||||
}
|
||||
|
||||
var pubspec interface{}
|
||||
if err := yaml.Unmarshal(buf, &pubspec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Package{
|
||||
Name: p.Name,
|
||||
Version: v.String(),
|
||||
Metadata: &Metadata{
|
||||
Description: p.Description,
|
||||
ProjectURL: p.Homepage,
|
||||
RepositoryURL: p.Repository,
|
||||
DocumentationURL: p.Documentation,
|
||||
Pubspec: pubspec,
|
||||
},
|
||||
}, nil
|
||||
}
|
136
modules/packages/pub/metadata_test.go
Normal file
136
modules/packages/pub/metadata_test.go
Normal file
|
@ -0,0 +1,136 @@
|
|||
// Copyright 2022 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 pub
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
packageName = "gitea"
|
||||
packageVersion = "1.0.1"
|
||||
description = "Package Description"
|
||||
projectURL = "https://gitea.io"
|
||||
repositoryURL = "https://gitea.io/gitea/gitea"
|
||||
documentationURL = "https://docs.gitea.io"
|
||||
)
|
||||
|
||||
const pubspecContent = `name: ` + packageName + `
|
||||
version: ` + packageVersion + `
|
||||
description: ` + description + `
|
||||
homepage: ` + projectURL + `
|
||||
repository: ` + repositoryURL + `
|
||||
documentation: ` + documentationURL + `
|
||||
|
||||
environment:
|
||||
sdk: '>=2.16.0 <3.0.0'
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
path: '>=1.8.0 <3.0.0'
|
||||
|
||||
dev_dependencies:
|
||||
http: '>=0.13.0'`
|
||||
|
||||
func TestParsePackage(t *testing.T) {
|
||||
createArchive := func(files map[string][]byte) io.Reader {
|
||||
var buf bytes.Buffer
|
||||
zw := gzip.NewWriter(&buf)
|
||||
tw := tar.NewWriter(zw)
|
||||
for filename, content := range files {
|
||||
hdr := &tar.Header{
|
||||
Name: filename,
|
||||
Mode: 0o600,
|
||||
Size: int64(len(content)),
|
||||
}
|
||||
tw.WriteHeader(hdr)
|
||||
tw.Write(content)
|
||||
}
|
||||
tw.Close()
|
||||
zw.Close()
|
||||
return &buf
|
||||
}
|
||||
|
||||
t.Run("MissingPubspecFile", func(t *testing.T) {
|
||||
data := createArchive(map[string][]byte{"dummy.txt": {}})
|
||||
|
||||
pp, err := ParsePackage(data)
|
||||
assert.Nil(t, pp)
|
||||
assert.ErrorIs(t, err, ErrMissingPubspecFile)
|
||||
})
|
||||
|
||||
t.Run("PubspecFileTooLarge", func(t *testing.T) {
|
||||
data := createArchive(map[string][]byte{"pubspec.yaml": make([]byte, 200*1024)})
|
||||
|
||||
pp, err := ParsePackage(data)
|
||||
assert.Nil(t, pp)
|
||||
assert.ErrorIs(t, err, ErrPubspecFileTooLarge)
|
||||
})
|
||||
|
||||
t.Run("InvalidPubspecFile", func(t *testing.T) {
|
||||
data := createArchive(map[string][]byte{"pubspec.yaml": {}})
|
||||
|
||||
pp, err := ParsePackage(data)
|
||||
assert.Nil(t, pp)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
data := createArchive(map[string][]byte{"pubspec.yaml": []byte(pubspecContent)})
|
||||
|
||||
pp, err := ParsePackage(data)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pp)
|
||||
assert.Empty(t, pp.Metadata.Readme)
|
||||
})
|
||||
|
||||
t.Run("ValidWithReadme", func(t *testing.T) {
|
||||
data := createArchive(map[string][]byte{"pubspec.yaml": []byte(pubspecContent), "README.md": []byte("readme")})
|
||||
|
||||
pp, err := ParsePackage(data)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pp)
|
||||
assert.Equal(t, "readme", pp.Metadata.Readme)
|
||||
})
|
||||
}
|
||||
|
||||
func TestParsePubspecMetadata(t *testing.T) {
|
||||
t.Run("InvalidName", func(t *testing.T) {
|
||||
for _, name := range []string{"123abc", "ab-cd"} {
|
||||
pp, err := ParsePubspecMetadata(strings.NewReader(`name: ` + name))
|
||||
assert.Nil(t, pp)
|
||||
assert.ErrorIs(t, err, ErrInvalidName)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("InvalidVersion", func(t *testing.T) {
|
||||
pp, err := ParsePubspecMetadata(strings.NewReader(`name: dummy
|
||||
version: invalid`))
|
||||
assert.Nil(t, pp)
|
||||
assert.ErrorIs(t, err, ErrInvalidVersion)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
pp, err := ParsePubspecMetadata(strings.NewReader(pubspecContent))
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, pp)
|
||||
|
||||
assert.Equal(t, packageName, pp.Name)
|
||||
assert.Equal(t, packageVersion, pp.Version)
|
||||
assert.Equal(t, description, pp.Metadata.Description)
|
||||
assert.Equal(t, projectURL, pp.Metadata.ProjectURL)
|
||||
assert.Equal(t, repositoryURL, pp.Metadata.RepositoryURL)
|
||||
assert.Equal(t, documentationURL, pp.Metadata.DocumentationURL)
|
||||
assert.NotNil(t, pp.Metadata.Pubspec)
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue