code.gitea.io/gitea@v1.19.3/modules/packages/maven/metadata.go (about)

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