Skip to content

Commit 768e7e5

Browse files
committed
fix
1 parent 23687a0 commit 768e7e5

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

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

+27-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/xml"
88
"io"
99

10+
"code.gitea.io/gitea/modules/util"
1011
"code.gitea.io/gitea/modules/validation"
1112

1213
"golang.org/x/net/html/charset"
@@ -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

+34
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"strings"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/util"
11+
1012
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1114
"golang.org/x/text/encoding/charmap"
1215
)
1316

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

0 commit comments

Comments
 (0)