first-commit
This commit is contained in:
111
modules/packages/maven/metadata.go
Normal file
111
modules/packages/maven/metadata.go
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package maven
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"io"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/modules/validation"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
)
|
||||
|
||||
// Metadata represents the metadata of a Maven package
|
||||
type Metadata struct {
|
||||
GroupID string `json:"group_id,omitempty"`
|
||||
ArtifactID string `json:"artifact_id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
ProjectURL string `json:"project_url,omitempty"`
|
||||
Licenses []string `json:"licenses,omitempty"`
|
||||
Dependencies []*Dependency `json:"dependencies,omitempty"`
|
||||
}
|
||||
|
||||
// Dependency represents a dependency of a Maven package
|
||||
type Dependency struct {
|
||||
GroupID string `json:"group_id,omitempty"`
|
||||
ArtifactID string `json:"artifact_id,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
||||
|
||||
type pomStruct struct {
|
||||
XMLName xml.Name `xml:"project"`
|
||||
|
||||
Parent struct {
|
||||
GroupID string `xml:"groupId"`
|
||||
ArtifactID string `xml:"artifactId"`
|
||||
Version string `xml:"version"`
|
||||
} `xml:"parent"`
|
||||
|
||||
GroupID string `xml:"groupId"`
|
||||
ArtifactID string `xml:"artifactId"`
|
||||
Version string `xml:"version"`
|
||||
Name string `xml:"name"`
|
||||
Description string `xml:"description"`
|
||||
URL string `xml:"url"`
|
||||
|
||||
Licenses []struct {
|
||||
Name string `xml:"name"`
|
||||
URL string `xml:"url"`
|
||||
Distribution string `xml:"distribution"`
|
||||
} `xml:"licenses>license"`
|
||||
|
||||
Dependencies []struct {
|
||||
GroupID string `xml:"groupId"`
|
||||
ArtifactID string `xml:"artifactId"`
|
||||
Version string `xml:"version"`
|
||||
Scope string `xml:"scope"`
|
||||
} `xml:"dependencies>dependency"`
|
||||
}
|
||||
|
||||
// ParsePackageMetaData parses the metadata of a pom file
|
||||
func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
|
||||
var pom pomStruct
|
||||
|
||||
dec := xml.NewDecoder(r)
|
||||
dec.CharsetReader = charset.NewReaderLabel
|
||||
if err := dec.Decode(&pom); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !validation.IsValidURL(pom.URL) {
|
||||
pom.URL = ""
|
||||
}
|
||||
|
||||
licenses := make([]string, 0, len(pom.Licenses))
|
||||
for _, l := range pom.Licenses {
|
||||
if l.Name != "" {
|
||||
licenses = append(licenses, l.Name)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies := make([]*Dependency, 0, len(pom.Dependencies))
|
||||
for _, d := range pom.Dependencies {
|
||||
dependencies = append(dependencies, &Dependency{
|
||||
GroupID: d.GroupID,
|
||||
ArtifactID: d.ArtifactID,
|
||||
Version: d.Version,
|
||||
})
|
||||
}
|
||||
|
||||
pomGroupID := pom.GroupID
|
||||
if pomGroupID == "" {
|
||||
// the current module could inherit parent: https://maven.apache.org/pom.html#Inheritance
|
||||
pomGroupID = pom.Parent.GroupID
|
||||
}
|
||||
if pomGroupID == "" {
|
||||
return nil, util.ErrInvalidArgument
|
||||
}
|
||||
return &Metadata{
|
||||
GroupID: pomGroupID,
|
||||
ArtifactID: pom.ArtifactID,
|
||||
Name: pom.Name,
|
||||
Description: pom.Description,
|
||||
ProjectURL: pom.URL,
|
||||
Licenses: licenses,
|
||||
Dependencies: dependencies,
|
||||
}, nil
|
||||
}
|
123
modules/packages/maven/metadata_test.go
Normal file
123
modules/packages/maven/metadata_test.go
Normal file
@@ -0,0 +1,123 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package maven
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
)
|
||||
|
||||
const (
|
||||
groupID = "org.gitea"
|
||||
artifactID = "my-project"
|
||||
version = "1.0.1"
|
||||
name = "My Gitea Project"
|
||||
description = "Package Description"
|
||||
projectURL = "https://gitea.io"
|
||||
license = "MIT"
|
||||
dependencyGroupID = "org.gitea.core"
|
||||
dependencyArtifactID = "git"
|
||||
dependencyVersion = "5.0.0"
|
||||
)
|
||||
|
||||
const pomContent = `<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<groupId>` + groupID + `</groupId>
|
||||
<artifactId>` + artifactID + `</artifactId>
|
||||
<version>` + version + `</version>
|
||||
<name>` + name + `</name>
|
||||
<description>` + description + `</description>
|
||||
<url>` + projectURL + `</url>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>` + license + `</name>
|
||||
</license>
|
||||
</licenses>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>` + dependencyGroupID + `</groupId>
|
||||
<artifactId>` + dependencyArtifactID + `</artifactId>
|
||||
<version>` + dependencyVersion + `</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>`
|
||||
|
||||
func TestParsePackageMetaData(t *testing.T) {
|
||||
t.Run("InvalidFile", func(t *testing.T) {
|
||||
m, err := ParsePackageMetaData(strings.NewReader(""))
|
||||
assert.Nil(t, m)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
m, err := ParsePackageMetaData(strings.NewReader(pomContent))
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, m)
|
||||
|
||||
assert.Equal(t, groupID, m.GroupID)
|
||||
assert.Equal(t, artifactID, m.ArtifactID)
|
||||
assert.Equal(t, name, m.Name)
|
||||
assert.Equal(t, description, m.Description)
|
||||
assert.Equal(t, projectURL, m.ProjectURL)
|
||||
assert.Len(t, m.Licenses, 1)
|
||||
assert.Equal(t, license, m.Licenses[0])
|
||||
assert.Len(t, m.Dependencies, 1)
|
||||
assert.Equal(t, dependencyGroupID, m.Dependencies[0].GroupID)
|
||||
assert.Equal(t, dependencyArtifactID, m.Dependencies[0].ArtifactID)
|
||||
assert.Equal(t, dependencyVersion, m.Dependencies[0].Version)
|
||||
})
|
||||
|
||||
t.Run("Encoding", func(t *testing.T) {
|
||||
// UTF-8 is default but the metadata could be encoded differently
|
||||
pomContent8859_1, err := charmap.ISO8859_1.NewEncoder().String(
|
||||
strings.ReplaceAll(
|
||||
pomContent,
|
||||
`<?xml version="1.0"?>`,
|
||||
`<?xml version="1.0" encoding="ISO-8859-1"?>`,
|
||||
),
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
|
||||
m, err := ParsePackageMetaData(strings.NewReader(pomContent8859_1))
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, m)
|
||||
})
|
||||
|
||||
t.Run("ParentInherit", func(t *testing.T) {
|
||||
pom := `<?xml version="1.0"?>
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.mycompany.app</groupId>
|
||||
<artifactId>my-app</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>submodule1</artifactId>
|
||||
</project>
|
||||
`
|
||||
m, err := ParsePackageMetaData(strings.NewReader(pom))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, m)
|
||||
|
||||
assert.Equal(t, "com.mycompany.app", m.GroupID)
|
||||
assert.Equal(t, "submodule1", m.ArtifactID)
|
||||
})
|
||||
|
||||
t.Run("ParentInherit", func(t *testing.T) {
|
||||
pom := `<?xml version="1.0"?>
|
||||
<project>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId></artifactId>
|
||||
</project>
|
||||
`
|
||||
_, err := ParsePackageMetaData(strings.NewReader(pom))
|
||||
require.ErrorIs(t, err, util.ErrInvalidArgument)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user