Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a68ed086 authored by Ibrahim Kanouche's avatar Ibrahim Kanouche
Browse files

Optimized project selection for getProjectMetadata

Test: m compliance_sbom

Change-Id: I9e60b7e138a4fca74170954d8bb83862677e750f
parent 88b02afb
Loading
Loading
Loading
Loading
+26 −3
Original line number Diff line number Diff line
@@ -233,7 +233,7 @@ func getDownloadUrl(_ *context, pm *projectmetadata.ProjectMetadata) string {
	return url
}

// getProjectMetadata returns the project metadata for the target node
// getProjectMetadata returns the optimal project metadata for the target node
func getProjectMetadata(_ *context, pmix *projectmetadata.Index,
	tn *compliance.TargetNode) (*projectmetadata.ProjectMetadata, error) {
	pms, err := pmix.MetadataForProjects(tn.Projects()...)
@@ -244,8 +244,31 @@ func getProjectMetadata(_ *context, pmix *projectmetadata.Index,
		return nil, nil
	}

	// TO DO: skip first element if it doesn't have one of the three info needed
	return pms[0], nil
	// Getting the project metadata that contains most of the info needed for sbomGenerator
	score := -1
	index := -1
	for i := 0; i < len(pms); i++ {
		tempScore := 0
		if pms[i].Name() != "" {
			tempScore += 1
		}
		if pms[i].Version() != "" {
			tempScore += 1
		}
		if pms[i].UrlsByTypeName().DownloadUrl() != "" {
			tempScore += 1
		}

		if tempScore == score {
			if pms[i].Project() < pms[index].Project() {
				index = i
			}
		} else if tempScore > score {
			score = tempScore
			index = i
		}
	}
	return pms[index], nil
}

// sbomGenerator implements the spdx bom utility
+5 −0
Original line number Diff line number Diff line
@@ -58,6 +58,11 @@ func (pm *ProjectMetadata) String() string {
	return fmt.Sprintf("project: %q\n%s", pm.project, pm.proto.String())
}

// Project returns the path to the directory containing the METADATA file
func (pm *ProjectMetadata) Project() string {
	return pm.project
}

// ProjectName returns the name of the project.
func (pm *ProjectMetadata) Name() string {
	return pm.proto.GetName()