-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfetch.ts
More file actions
128 lines (110 loc) · 3.19 KB
/
fetch.ts
File metadata and controls
128 lines (110 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Import Node.js Dependencies
import path from "node:path";
// Import Third-party Dependencies
import kleur from "kleur";
import * as scorecard from "@nodesecure/ossf-scorecard-sdk";
import { isHTTPError } from "@openally/httpie";
// Import Internal Dependencies
import { buildStatsFromScannerDependencies } from "./extractScannerData.ts";
import * as scanner from "./scanner.ts";
import * as localStorage from "../localStorage.ts";
import * as utils from "../utils/index.ts";
import * as CONSTANTS from "../constants.ts";
// CONSTANTS
const kNotFoundStatusCode = 404;
export async function fetchPackagesAndRepositoriesData(
verbose = true
) {
const config = localStorage.getConfig().report!;
const fetchNpm = (config.npm?.packages ?? []).length > 0;
const fetchGit = (config.git?.repositories ?? []).length > 0;
if (!fetchGit && !fetchNpm) {
throw new Error(
"No git repositories and no npm packages to fetch in the local configuration!"
);
}
const pkgStats = fetchNpm && config.npm ?
await fetchPackagesStats(
utils.formatNpmPackages(
config.npm.organizationPrefix,
config.npm.packages
),
verbose
) :
null;
const repoStats = fetchGit && config.git ?
await fetchRepositoriesStats(
config.git.repositories,
config.git.organizationUrl,
verbose
) :
null;
return {
pkgStats,
repoStats
};
}
async function fetchPackagesStats(
packages: string[],
verbose = true
) {
const jsonFiles = await utils.runInSpinner(
{
title: `[Fetcher: ${kleur.yellow().bold("NPM")}]`,
start: "Fetching NPM packages metadata on the NPM Registry",
verbose
},
async() => Promise.all(packages.map(scanner.from))
);
return buildStatsFromScannerDependencies(
jsonFiles.filter((value) => value !== null)
);
}
async function fetchRepositoriesStats(
repositories: string[],
organizationUrl: string,
verbose = true
) {
const jsonFiles = await utils.runInSpinner(
{
title: `[Fetcher: ${kleur.yellow().bold("GIT")}]`,
start: "Cloning GIT repositories",
verbose
},
async(spinner) => {
const repos = await Promise.all(
repositories.map((repositoryName) => {
const trimmedRepositoryName = repositoryName.trim();
return utils.cloneGITRepository(
path.join(CONSTANTS.DIRS.CLONES, trimmedRepositoryName),
`${organizationUrl}/${trimmedRepositoryName}.git`
);
})
);
spinner.text = "Fetching repositories metadata on the NPM Registry";
return Promise.all(repos.map(scanner.cwd));
}
);
return buildStatsFromScannerDependencies(
jsonFiles.filter((value) => value !== null)
);
}
const scoresCache = new Map<string, number>();
export async function fetchScorecardScore(
fullName: string
): Promise<number> {
if (scoresCache.has(fullName)) {
return scoresCache.get(fullName)!;
}
try {
const { score } = await scorecard.result(fullName, { resolveOnVersionControl: false });
scoresCache.set(fullName, score);
return score;
}
catch (e) {
if (isHTTPError(e) && e.statusCode === kNotFoundStatusCode) {
scoresCache.set(fullName, 0);
}
return 0;
}
}