loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'fix: maven package where actual pom has no group-id defined, fallback to parent group-id' (#6329) from JSchlarb/forgejo:forgejo into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6329
Reviewed-by: Gusted <gusted@noreply.codeberg.org>

Gusted 5b542d6c 248977c5

+60 -1
+21 -1
modules/packages/maven/metadata.go
··· 7 7 "encoding/xml" 8 8 "io" 9 9 10 + "code.gitea.io/gitea/modules/util" 10 11 "code.gitea.io/gitea/modules/validation" 11 12 12 13 "golang.org/x/net/html/charset" ··· 49 50 Version string `xml:"version"` 50 51 Scope string `xml:"scope"` 51 52 } `xml:"dependencies>dependency"` 53 + Parent struct { 54 + GroupID string `xml:"groupId"` 55 + ArtifactID string `xml:"artifactId"` 56 + Version string `xml:"version"` 57 + RelativePath string `xml:"relativePath"` 58 + } `xml:"parent"` 52 59 } 60 + 61 + var ErrNoGroupID = util.NewInvalidArgumentErrorf("group ID is missing") 53 62 54 63 // ParsePackageMetaData parses the metadata of a pom file 55 64 func ParsePackageMetaData(r io.Reader) (*Metadata, error) { ··· 65 74 pom.URL = "" 66 75 } 67 76 77 + groupID := pom.GroupID 78 + 79 + if groupID == "" { 80 + // If a project inherits from a parent project, the groupId element is optional. 81 + // Refer to: https://maven.apache.org/pom.html#Inheritance 82 + if pom.Parent.GroupID == "" { 83 + return nil, ErrNoGroupID 84 + } 85 + groupID = pom.Parent.GroupID 86 + } 87 + 68 88 licenses := make([]string, 0, len(pom.Licenses)) 69 89 for _, l := range pom.Licenses { 70 90 if l.Name != "" { ··· 82 102 } 83 103 84 104 return &Metadata{ 85 - GroupID: pom.GroupID, 105 + GroupID: groupID, 86 106 ArtifactID: pom.ArtifactID, 87 107 Name: pom.Name, 88 108 Description: pom.Description,
+39
modules/packages/maven/metadata_test.go
··· 14 14 15 15 const ( 16 16 groupID = "org.gitea" 17 + parentGroupID = "org.gitea.parent" 17 18 artifactID = "my-project" 18 19 version = "1.0.1" 19 20 name = "My Gitea Project" ··· 27 28 28 29 const pomContent = `<?xml version="1.0"?> 29 30 <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 31 + <parent> 32 + <groupId>` + parentGroupID + `</groupId> 33 + <artifactId>parent-project</artifactId> 34 + <version>1.0.0</version> 35 + </parent> 30 36 <groupId>` + groupID + `</groupId> 31 37 <artifactId>` + artifactID + `</artifactId> 32 38 <version>` + version + `</version> ··· 47 53 </dependencies> 48 54 </project>` 49 55 56 + const pomWithParentGroupID = `<?xml version="1.0"?> 57 + <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 58 + <parent> 59 + <groupId>` + parentGroupID + `</groupId> 60 + <artifactId>parent-project</artifactId> 61 + <version>1.0.0</version> 62 + </parent> 63 + 64 + <artifactId>` + artifactID + `</artifactId> 65 + <version>` + version + `</version> 66 + </project>` 67 + 68 + const pomWithMissingGroupID = `<?xml version="1.0"?> 69 + <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 70 + <artifactId>` + artifactID + `</artifactId> 71 + <version>` + version + `</version> 72 + </project>` 73 + 50 74 func TestParsePackageMetaData(t *testing.T) { 51 75 t.Run("InvalidFile", func(t *testing.T) { 52 76 m, err := ParsePackageMetaData(strings.NewReader("")) ··· 86 110 m, err := ParsePackageMetaData(strings.NewReader(pomContent8859_1)) 87 111 require.NoError(t, err) 88 112 assert.NotNil(t, m) 113 + }) 114 + 115 + t.Run("UseParentGroupID", func(t *testing.T) { 116 + m, err := ParsePackageMetaData(strings.NewReader(pomWithParentGroupID)) 117 + require.NoError(t, err) 118 + assert.NotNil(t, m) 119 + 120 + assert.Equal(t, parentGroupID, m.GroupID) 121 + }) 122 + 123 + t.Run("MissingGroupIDThrowsError", func(t *testing.T) { 124 + m, err := ParsePackageMetaData(strings.NewReader(pomWithMissingGroupID)) 125 + assert.Nil(t, m) 126 + require.Error(t, err) 127 + assert.Equal(t, ErrNoGroupID, err) 89 128 }) 90 129 }