Skip to content

Commit fa35cd8

Browse files
committed
fix
1 parent 23687a0 commit fa35cd8

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

Diff for: modules/packages/maven/metadata.go

+27-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package maven
55

66
import (
7+
"code.gitea.io/gitea/modules/util"
78
"encoding/xml"
89
"io"
910

@@ -31,18 +32,27 @@ type Dependency struct {
3132
}
3233

3334
type pomStruct struct {
34-
XMLName xml.Name `xml:"project"`
35-
GroupID string `xml:"groupId"`
36-
ArtifactID string `xml:"artifactId"`
37-
Version string `xml:"version"`
38-
Name string `xml:"name"`
39-
Description string `xml:"description"`
40-
URL string `xml:"url"`
41-
Licenses []struct {
35+
XMLName xml.Name `xml:"project"`
36+
37+
Parent struct {
38+
GroupID string `xml:"groupId"`
39+
ArtifactID string `xml:"artifactId"`
40+
Version string `xml:"version"`
41+
} `xml:"parent"`
42+
43+
GroupID string `xml:"groupId"`
44+
ArtifactID string `xml:"artifactId"`
45+
Version string `xml:"version"`
46+
Name string `xml:"name"`
47+
Description string `xml:"description"`
48+
URL string `xml:"url"`
49+
50+
Licenses []struct {
4251
Name string `xml:"name"`
4352
URL string `xml:"url"`
4453
Distribution string `xml:"distribution"`
4554
} `xml:"licenses>license"`
55+
4656
Dependencies []struct {
4757
GroupID string `xml:"groupId"`
4858
ArtifactID string `xml:"artifactId"`
@@ -81,8 +91,16 @@ func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
8191
})
8292
}
8393

94+
pomGroupID := pom.GroupID
95+
if pomGroupID == "" {
96+
// the current module could inherit parent: https://maven.apache.org/pom.html#Inheritance
97+
pomGroupID = pom.Parent.GroupID
98+
}
99+
if pomGroupID == "" {
100+
return nil, util.ErrInvalidArgument
101+
}
84102
return &Metadata{
85-
GroupID: pom.GroupID,
103+
GroupID: pomGroupID,
86104
ArtifactID: pom.ArtifactID,
87105
Name: pom.Name,
88106
Description: pom.Description,

Diff for: modules/packages/maven/metadata_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
package maven
55

66
import (
7+
"code.gitea.io/gitea/modules/util"
78
"strings"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1113
"golang.org/x/text/encoding/charmap"
1214
)
1315

@@ -86,4 +88,35 @@ func TestParsePackageMetaData(t *testing.T) {
8688
assert.NoError(t, err)
8789
assert.NotNil(t, m)
8890
})
91+
92+
t.Run("ParentInherit", func(t *testing.T) {
93+
pom := `<?xml version="1.0"?>
94+
<project>
95+
<modelVersion>4.0.0</modelVersion>
96+
<parent>
97+
<groupId>com.mycompany.app</groupId>
98+
<artifactId>my-app</artifactId>
99+
<version>1.0-SNAPSHOT</version>
100+
</parent>
101+
<artifactId>submodule1</artifactId>
102+
</project>
103+
`
104+
m, err := ParsePackageMetaData(strings.NewReader(pom))
105+
require.NoError(t, err)
106+
require.NotNil(t, m)
107+
108+
assert.Equal(t, "com.mycompany.app", m.GroupID)
109+
assert.Equal(t, "submodule1", m.ArtifactID)
110+
})
111+
112+
t.Run("ParentInherit", func(t *testing.T) {
113+
pom := `<?xml version="1.0"?>
114+
<project>
115+
<modelVersion>4.0.0</modelVersion>
116+
<artifactId></artifactId>
117+
</project>
118+
`
119+
_, err := ParsePackageMetaData(strings.NewReader(pom))
120+
require.ErrorIs(t, err, util.ErrInvalidArgument)
121+
})
89122
}

0 commit comments

Comments
 (0)