github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/packages/maven/metadata.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package maven
     7  
     8  import (
     9  	"encoding/xml"
    10  	"io"
    11  
    12  	"github.com/gitbundle/modules/validation"
    13  )
    14  
    15  // Metadata represents the metadata of a Maven package
    16  type Metadata struct {
    17  	GroupID      string        `json:"group_id,omitempty"`
    18  	ArtifactID   string        `json:"artifact_id,omitempty"`
    19  	Name         string        `json:"name,omitempty"`
    20  	Description  string        `json:"description,omitempty"`
    21  	ProjectURL   string        `json:"project_url,omitempty"`
    22  	Licenses     []string      `json:"licenses,omitempty"`
    23  	Dependencies []*Dependency `json:"dependencies,omitempty"`
    24  }
    25  
    26  // Dependency represents a dependency of a Maven package
    27  type Dependency struct {
    28  	GroupID    string `json:"group_id,omitempty"`
    29  	ArtifactID string `json:"artifact_id,omitempty"`
    30  	Version    string `json:"version,omitempty"`
    31  }
    32  
    33  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 {
    42  		Name         string `xml:"name"`
    43  		URL          string `xml:"url"`
    44  		Distribution string `xml:"distribution"`
    45  	} `xml:"licenses>license"`
    46  	Dependencies []struct {
    47  		GroupID    string `xml:"groupId"`
    48  		ArtifactID string `xml:"artifactId"`
    49  		Version    string `xml:"version"`
    50  		Scope      string `xml:"scope"`
    51  	} `xml:"dependencies>dependency"`
    52  }
    53  
    54  // ParsePackageMetaData parses the metadata of a pom file
    55  func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
    56  	var pom pomStruct
    57  	if err := xml.NewDecoder(r).Decode(&pom); err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	if !validation.IsValidURL(pom.URL) {
    62  		pom.URL = ""
    63  	}
    64  
    65  	licenses := make([]string, 0, len(pom.Licenses))
    66  	for _, l := range pom.Licenses {
    67  		if l.Name != "" {
    68  			licenses = append(licenses, l.Name)
    69  		}
    70  	}
    71  
    72  	dependencies := make([]*Dependency, 0, len(pom.Dependencies))
    73  	for _, d := range pom.Dependencies {
    74  		dependencies = append(dependencies, &Dependency{
    75  			GroupID:    d.GroupID,
    76  			ArtifactID: d.ArtifactID,
    77  			Version:    d.Version,
    78  		})
    79  	}
    80  
    81  	return &Metadata{
    82  		GroupID:      pom.GroupID,
    83  		ArtifactID:   pom.ArtifactID,
    84  		Name:         pom.Name,
    85  		Description:  pom.Description,
    86  		ProjectURL:   pom.URL,
    87  		Licenses:     licenses,
    88  		Dependencies: dependencies,
    89  	}, nil
    90  }