-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefreshAttestationsDB.mjs
174 lines (146 loc) · 5.86 KB
/
refreshAttestationsDB.mjs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import fs from 'fs';
import path from 'path';
import yaml from 'js-yaml';
console.debug = function () {};
function parseFile (filePath) {
try {
console.debug(`Reading file: ${filePath}`);
const content = fs.readFileSync(filePath, 'utf8');
const yamlPart = content.match(/---\n([\s\S]+?)\n---/);
if (!yamlPart) {
console.log('YAML front matter not found.');
throw new Error('YAML front matter not found');
}
const yamlContent = yamlPart[1];
console.debug('YAML content extracted:', yamlContent);
// Use js-yaml with FAILSAFE_SCHEMA to prevent date parsing
const data = yaml.load(yamlContent, { schema: yaml.FAILSAFE_SCHEMA });
console.debug('Parsed YAML data:', JSON.stringify(data));
const appId = data.appId || '';
const signer = data.signer || '';
const entries = [];
// Process reviewArchive if available
if (data.reviewArchive && data.reviewArchive != null) {
console.debug('Processing reviewArchive entries...');
data.reviewArchive.forEach(review => {
const reviewData = review;
console.debug('Review entry:', reviewData);
if ((reviewData.appHash || reviewData.appHashes) && reviewData.verdict) {
entries.push({
appId,
signer,
version: reviewData.version || '',
verdict: reviewData.verdict || '',
appHashes: reviewData.appHashes || [reviewData.appHash],
date: reviewData.date || ''
});
} else {
console.debug('Skipping review entry due to missing appHash or appHashes:', reviewData);
}
});
} else {
console.debug('No reviewArchive entries found.', data.reviewArchive);
}
// Process reviewCurrent if available
if (data.reviewCurrent && data.reviewCurrent != null) {
const reviewData = data.reviewCurrent;
if (reviewData.appHashes && reviewData.verdict) {
entries.push({
appId,
signer,
version: reviewData.version || '',
verdict: reviewData.verdict || '',
appHashes: reviewData.appHashes || [],
date: reviewData.date || ''
});
} else {
console.debug('Skipping reviewCurrent entry due to missing appHashes or verdict:', reviewData);
}
} else {
// Process current test results if available
const resultsMatch = content.match(/===== Begin Results =====([\s\S]+?)===== End Results =====/);
if (resultsMatch) {
const resultsContent = resultsMatch[1];
const currentTestResults = parseResults(resultsContent);
if (currentTestResults.appHash || currentTestResults.appHashes || data.appHash || data.appHashes) {
entries.push({
appId: currentTestResults.appId || '',
signer: currentTestResults.signer || '',
version: currentTestResults.apkVersionName || '',
verdict: currentTestResults.verdict || '',
appHashes: currentTestResults.appHashes || data.appHashes ||
[currentTestResults.appHash || data.appHash].filter(Boolean),
date: currentTestResults.date || data.date
});
} else {
console.debug('Skipping current test result due to missing appHash and appHashes:', currentTestResults);
}
} else {
console.debug('No current test results found.');
}
}
return entries; // Return only entries
} catch (error) {
console.error(`Error parsing file ${filePath}: ${error.message}`);
return [];
}
}
function parseResults (resultsString) {
const lines = resultsString.split('\n');
const result = {};
lines.forEach(line => {
if (line.includes(':')) {
const [key, value] = line.split(':').map(part => part.trim());
result[key] = value;
}
});
return result;
}
function processFilesInDirectory (directoryPath) {
const outputData = [];
const files = fs.readdirSync(directoryPath);
let filesProcessed = 0;
let folderName = path.basename(directoryPath);
folderName = folderName.startsWith('_') ? folderName.slice(1) : folderName;
files.forEach(filename => {
if (filename.endsWith('.md')) {
const filePath = path.join(directoryPath, filename);
console.debug(`Processing file: ${filePath}`);
const entries = parseFile(filePath);
entries.forEach(entry => {
entry.platform = folderName;
});
outputData.push(...entries);
filesProcessed += 1;
}
});
console.log(`Found ${outputData.length} appHashes in ${filesProcessed} files in directory: ${directoryPath}.`);
return outputData;
}
function processAllDirectories (directoryPaths) {
const attestationData = [];
directoryPaths.forEach(directoryPath => {
console.log(`Processing directory: ${directoryPath}`);
const outputData = processFilesInDirectory(directoryPath);
attestationData.push(...outputData);
});
console.log(`Total directories processed: ${directoryPaths.length}`);
// Filter attestations and write to file
const filteredAttestations = attestationData.filter(entry => entry.appHashes && entry.appHashes.length > 0);
const attestationsFile = 'assets/attestations.json';
fs.writeFileSync(attestationsFile, JSON.stringify(filteredAttestations, null, 2), 'utf8');
console.log(`${filteredAttestations.length} attestations written to ${attestationsFile}`);
}
// Direct execution check
if (import.meta.url === `file://${process.argv[1]}`) {
// Get directory paths from command-line arguments
const directoryPaths = process.argv.slice(2);
if (directoryPaths.length === 0) {
console.log('Please provide at least one directory path as a command-line argument.');
process.exit(1);
}
console.log(`Using directory paths: ${directoryPaths.join(', ')}`);
processAllDirectories(directoryPaths);
}
// Export functionality for use in other modules
export { processAllDirectories, processFilesInDirectory, parseFile };