Files
component-detection-depende…/index.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-08-25 09:12:00 -07:00
const core = require('@actions/core');
2022-10-04 09:44:26 -07:00
const github = require('@actions/github');
2022-08-25 11:50:14 -07:00
const fs = require('fs');
const glob = require('glob');
2022-08-25 09:12:00 -07:00
2022-08-25 11:50:14 -07:00
import {
PackageCache,
BuildTarget,
Package,
Snapshot,
2022-10-04 13:16:36 -07:00
Manifest,
2022-08-25 11:50:14 -07:00
submitSnapshot
} from '@github/dependency-submission-toolkit'
2022-08-25 09:12:00 -07:00
async function run() {
2022-10-04 12:03:50 -07:00
let manifests = getManifestsFromSpdxFiles(searchFiles());
2022-08-25 11:50:14 -07:00
let snapshot = new Snapshot({
name: "spdx-to-dependency-graph-action",
version: "0.0.1",
url: "https://github.com/jhutchings1/spdx-to-dependency-graph-action",
2022-10-04 09:44:26 -07:00
},
github.context,
{
correlator:`${github.context.job}`,
id: github.context.runId.toString()
2022-08-25 11:50:14 -07:00
});
2022-08-25 12:49:25 -07:00
manifests?.forEach(manifest => {
snapshot.addManifest(manifest);
2022-08-25 11:50:14 -07:00
});
submitSnapshot(snapshot);
}
2022-10-04 13:37:13 -07:00
function getManifestFromSpdxFile(document, fileName) {
2022-10-04 13:55:18 -07:00
core.debug(`getManifestFromSpdxFile processing ${fileName}`);
2022-10-04 13:37:13 -07:00
let manifest = new Manifest(document.name, fileName);
2022-10-04 14:17:41 -07:00
core.debug(`Processing ${document.packages?.length} packages`);
2022-10-04 13:37:13 -07:00
document.packages?.forEach(pkg => {
2022-10-04 14:21:09 -07:00
let packageName = pkg.name;
2022-08-25 12:49:25 -07:00
let packageVersion = pkg.packageVersion;
2022-10-04 15:38:47 -07:00
2022-10-04 15:36:21 -07:00
// SPDX 2.3 defines a purl field
let purl = pkg.purl;
2022-10-04 14:21:09 -07:00
if (purl == null || purl == undefined) {
2022-10-04 15:36:21 -07:00
purl = pkg.externalRefs?.find(ref => ref.referenceCategory === "PACKAGE-MANAGER" && ref.referenceType === "purl")?.referenceLocator;
} else if (purl == null || purl == undefined) {
2022-10-04 14:21:09 -07:00
purl = `pkg:generic/${packageName}@${packageVersion}`;
2022-10-04 15:36:21 -07:00
}
// Working around weird encoding issues from an SBOM generator
// Find the last instance of %40 and replace it with @
purl = replaceVersionEscape(purl);
2022-10-04 14:17:41 -07:00
2022-10-04 14:04:40 -07:00
let relationships = document.relationships?.find(rel => rel.relatedSpdxElement == pkg.SPDXID && rel.relationshipType == "DEPENDS_ON" && rel.spdxElementId != "SPDXRef-RootPackage");
if (relationships != null && relationships.length > 0) {
2022-10-04 14:50:23 -07:00
manifest.addIndirectDependency(new Package(purl));
2022-10-04 13:37:13 -07:00
} else {
2022-10-04 14:50:23 -07:00
manifest.addDirectDependency(new Package(purl));
2022-10-04 14:10:02 -07:00
}
});
return manifest;
2022-08-25 12:49:25 -07:00
}
2022-10-04 13:37:13 -07:00
2022-10-04 09:44:26 -07:00
function getManifestsFromSpdxFiles(files) {
2022-10-04 12:03:50 -07:00
core.debug(`Processing ${files.length} files`);
2022-08-25 12:49:25 -07:00
let manifests = [];
files?.forEach(file => {
2022-10-04 11:51:27 -07:00
core.debug(`Processing ${file}`);
2022-10-04 13:58:08 -07:00
manifests.push(getManifestFromSpdxFile(JSON.parse(fs.readFileSync(file)), file));
2022-08-25 11:50:14 -07:00
});
2022-08-25 12:49:25 -07:00
return manifests;
2022-08-25 11:50:14 -07:00
}
2022-10-04 12:03:50 -07:00
function searchFiles() {
2022-08-25 11:50:14 -07:00
let filePath = core.getInput('filePath');
let filePattern = core.getInput('filePattern');
2022-10-04 13:15:32 -07:00
return glob.sync(`${filePath}/${filePattern}`, {});
2022-08-25 09:12:00 -07:00
}
2022-10-04 15:14:04 -07:00
// Fixes issues with an escaped version string
function replaceVersionEscape(purl) {
2022-10-04 15:36:21 -07:00
//If there's an "@" in the purl, then we don't need to do anything.
2022-10-04 15:59:26 -07:00
if (purl != null && purl != undefined && !purl?.includes("@")) {
2022-10-04 15:14:04 -07:00
let index = purl.lastIndexOf("%40");
if (index > 0) {
purl = purl.substring(0, index) + "@" + purl.substring(index + 3);
}
}
return purl;
}
2022-08-25 09:12:00 -07:00
run();