gitea源码

metadata.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package maven
  4. import (
  5. "encoding/xml"
  6. "io"
  7. "code.gitea.io/gitea/modules/util"
  8. "code.gitea.io/gitea/modules/validation"
  9. "golang.org/x/net/html/charset"
  10. )
  11. // Metadata represents the metadata of a Maven package
  12. type Metadata struct {
  13. GroupID string `json:"group_id,omitempty"`
  14. ArtifactID string `json:"artifact_id,omitempty"`
  15. Name string `json:"name,omitempty"`
  16. Description string `json:"description,omitempty"`
  17. ProjectURL string `json:"project_url,omitempty"`
  18. Licenses []string `json:"licenses,omitempty"`
  19. Dependencies []*Dependency `json:"dependencies,omitempty"`
  20. }
  21. // Dependency represents a dependency of a Maven package
  22. type Dependency struct {
  23. GroupID string `json:"group_id,omitempty"`
  24. ArtifactID string `json:"artifact_id,omitempty"`
  25. Version string `json:"version,omitempty"`
  26. }
  27. type pomStruct struct {
  28. XMLName xml.Name `xml:"project"`
  29. Parent struct {
  30. GroupID string `xml:"groupId"`
  31. ArtifactID string `xml:"artifactId"`
  32. Version string `xml:"version"`
  33. } `xml:"parent"`
  34. GroupID string `xml:"groupId"`
  35. ArtifactID string `xml:"artifactId"`
  36. Version string `xml:"version"`
  37. Name string `xml:"name"`
  38. Description string `xml:"description"`
  39. URL string `xml:"url"`
  40. Licenses []struct {
  41. Name string `xml:"name"`
  42. URL string `xml:"url"`
  43. Distribution string `xml:"distribution"`
  44. } `xml:"licenses>license"`
  45. Dependencies []struct {
  46. GroupID string `xml:"groupId"`
  47. ArtifactID string `xml:"artifactId"`
  48. Version string `xml:"version"`
  49. Scope string `xml:"scope"`
  50. } `xml:"dependencies>dependency"`
  51. }
  52. // ParsePackageMetaData parses the metadata of a pom file
  53. func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
  54. var pom pomStruct
  55. dec := xml.NewDecoder(r)
  56. dec.CharsetReader = charset.NewReaderLabel
  57. if err := dec.Decode(&pom); err != nil {
  58. return nil, err
  59. }
  60. if !validation.IsValidURL(pom.URL) {
  61. pom.URL = ""
  62. }
  63. licenses := make([]string, 0, len(pom.Licenses))
  64. for _, l := range pom.Licenses {
  65. if l.Name != "" {
  66. licenses = append(licenses, l.Name)
  67. }
  68. }
  69. dependencies := make([]*Dependency, 0, len(pom.Dependencies))
  70. for _, d := range pom.Dependencies {
  71. dependencies = append(dependencies, &Dependency{
  72. GroupID: d.GroupID,
  73. ArtifactID: d.ArtifactID,
  74. Version: d.Version,
  75. })
  76. }
  77. pomGroupID := pom.GroupID
  78. if pomGroupID == "" {
  79. // the current module could inherit parent: https://maven.apache.org/pom.html#Inheritance
  80. pomGroupID = pom.Parent.GroupID
  81. }
  82. if pomGroupID == "" {
  83. return nil, util.ErrInvalidArgument
  84. }
  85. return &Metadata{
  86. GroupID: pomGroupID,
  87. ArtifactID: pom.ArtifactID,
  88. Name: pom.Name,
  89. Description: pom.Description,
  90. ProjectURL: pom.URL,
  91. Licenses: licenses,
  92. Dependencies: dependencies,
  93. }, nil
  94. }