-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjob-configurator.js
More file actions
282 lines (242 loc) · 8.84 KB
/
job-configurator.js
File metadata and controls
282 lines (242 loc) · 8.84 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Language detection and matrix generation for code scanning
*/
import { loadRepoConfig } from '../../codeql-action/src/config-loader.js';
/**
* Maps GitHub language names to CodeQL scanner language names
*/
const LANGUAGE_MAPPING = {
JavaScript: 'javascript',
TypeScript: 'typescript',
Python: 'python',
Go: 'go',
Swift: 'swift',
Java: 'java',
Kotlin: 'java', // Kotlin uses java scanner in CodeQL
'C++': 'cpp',
C: 'cpp',
'C#': 'csharp',
Ruby: 'ruby',
};
/**
* Default scanner configurations for each language
*/
const DEFAULT_CONFIGS = {
javascript: { language: 'javascript-typescript' },
typescript: { language: 'javascript-typescript' },
python: { language: 'python' },
go: { language: 'go' },
java: {
language: 'java-kotlin',
build_mode: 'manual',
build_command: './mvnw compile',
},
swift: { language: 'swift' },
cpp: { language: 'cpp' },
csharp: { language: 'csharp' },
ruby: { language: 'ruby' },
actions: { language: 'actions' },
};
/**
* Detects languages from GitHub API response
* @param {Object} githubLanguages - GitHub API languages response
* @returns {string[]} Array of detected language names for scanning
*/
export function detectLanguages(githubLanguages) {
if (!githubLanguages || typeof githubLanguages !== 'object') {
console.warn('Invalid GitHub languages data');
return [];
}
const detectedLanguages = new Set();
for (const [githubLang] of Object.entries(githubLanguages)) {
const scannerLang = LANGUAGE_MAPPING[githubLang];
if (scannerLang) {
detectedLanguages.add(scannerLang);
}
}
const languages = Array.from(detectedLanguages);
if (languages.length === 0) {
console.warn('No supported languages detected');
return [];
}
return languages;
}
/**
* Creates scanner matrix from detected languages and custom config
* @param {string[]} detectedLanguages - Array of detected language names
* @param {Object[]} languagesConfig - Custom language configurations
* @returns {Object} Scanner matrix configuration
*/
export function createMatrix(detectedLanguages, languagesConfig = []) {
const matrixIncludes = [];
const customConfigMap = new Map();
// Index custom configs by language
for (const config of languagesConfig) {
if (config.language) {
customConfigMap.set(config.language, config);
}
}
console.error('=== MATRIX CREATION DEBUG ===');
console.error('Auto-detected languages:', detectedLanguages);
console.error('Provided custom configs:', languagesConfig);
// Remove duplicates from detected languages and always add 'actions'
const uniqueLanguages = [...new Set([...detectedLanguages, 'actions'])];
for (const lang of uniqueLanguages) {
// Check for custom config that matches this language
const customConfig = Array.from(customConfigMap.values()).find((config) => {
// Match if the custom config language matches our detected language
// or if it's the scanner language (e.g., 'java-kotlin' for 'java')
return (
config.language === lang ||
(DEFAULT_CONFIGS[lang] &&
config.language === DEFAULT_CONFIGS[lang].language)
);
});
const defaultConfig = DEFAULT_CONFIGS[lang];
// Check if language should be ignored
if (customConfig && customConfig.ignore === true) {
console.error(`⚠️ ${lang} detected but marked as ignored - skipping`);
continue;
}
if (customConfig && defaultConfig) {
// Merge custom config with default config (custom config takes priority)
const mergedConfig = { ...defaultConfig, ...customConfig };
// Remove ignore property from final config as it's only for control flow
delete mergedConfig.ignore;
matrixIncludes.push(mergedConfig);
} else if (customConfig) {
// Remove ignore property from final config as it's only for control flow
const finalConfig = { ...customConfig };
delete finalConfig.ignore;
matrixIncludes.push(finalConfig);
} else if (defaultConfig) {
matrixIncludes.push(defaultConfig);
}
}
// Deduplicate matrix entries by language
const seenLanguages = new Set();
const uniqueMatrixIncludes = matrixIncludes.filter((entry) => {
if (seenLanguages.has(entry.language)) {
return false;
}
seenLanguages.add(entry.language);
return true;
});
const matrix = { include: uniqueMatrixIncludes };
console.error('=== FINAL MATRIX DEBUG ===');
console.error('Generated matrix:', JSON.stringify(matrix, null, 2));
console.error(`Total matrix entries: ${uniqueMatrixIncludes.length}`);
console.error('============================');
return matrix;
}
// CLI functionality for when this script is run directly
async function main() {
const [detectedLanguagesJson, languagesConfigJson, repo, configDir] =
process.argv.slice(2);
if (!detectedLanguagesJson) {
console.error(
'Usage: node job-configurator.js <detected_languages_json> [languages_config_json] [repo] [config_dir]',
);
console.error(
'Example: node job-configurator.js \'{"Java": 1000, "JavaScript": 500}\' \'[{"language":"java","version":"21"}]\' \'owner/repo\' \'/path/to/configs\'',
);
process.exit(1);
}
try {
const githubLanguagesOrArray = JSON.parse(detectedLanguagesJson);
let languagesConfig = languagesConfigJson
? JSON.parse(languagesConfigJson)
: [];
// Load repo-specific config from file if repo and configDir are provided
if (repo && configDir) {
// Use shared config loader from codeql-action package
const repoConfig = await loadRepoConfig(repo, configDir);
const fileLanguagesConfig = repoConfig.languages_config || [];
// Merge configs: workflow input takes precedence over file config
// Create a map of file configs by language
const fileConfigMap = new Map();
for (const config of fileLanguagesConfig) {
if (config.language) {
fileConfigMap.set(config.language, config);
}
}
// Create a map of workflow input configs by language
const inputConfigMap = new Map();
for (const config of languagesConfig) {
if (config.language) {
inputConfigMap.set(config.language, config);
}
}
// Merge: start with file configs, then add/override with workflow inputs
const mergedConfigs = [...fileLanguagesConfig];
for (const [lang, inputConfig] of inputConfigMap.entries()) {
const fileConfig = fileConfigMap.get(lang);
if (fileConfig) {
// Merge: workflow input overrides file config
const mergedConfig = { ...fileConfig, ...inputConfig };
const index = mergedConfigs.findIndex((c) => c.language === lang);
mergedConfigs[index] = mergedConfig;
} else {
// New language only in workflow input
mergedConfigs.push(inputConfig);
}
}
languagesConfig = mergedConfigs;
}
// Handle both GitHub API format (object) and pre-processed array
let detectedLanguages;
if (Array.isArray(githubLanguagesOrArray)) {
// Already processed array of language names
detectedLanguages = githubLanguagesOrArray;
} else {
// GitHub API response format - process it
detectedLanguages = detectLanguages(githubLanguagesOrArray);
}
const matrix = createMatrix(detectedLanguages, languagesConfig);
// Output JSON to stdout for consumption by GitHub Actions
console.log(JSON.stringify(matrix));
} catch (error) {
console.error('Error parsing JSON input:', error.message);
process.exit(1);
}
}
// Only run main function when script is executed directly, not when imported
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}
/**
* Fetches language data from GitHub API
* @param {string} repo - Repository in format 'owner/repo'
* @param {string} token - GitHub token (optional)
* @returns {Promise<Object>} GitHub languages API response
*/
export async function fetchGitHubLanguages(repo, token) {
const url = `https://api.github.com/repos/${repo}/languages`;
const headers = {
Accept: 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
};
if (token) {
headers['Authorization'] = `token ${token}`;
}
try {
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(
`GitHub API error: ${response.status} ${response.statusText}`,
);
}
const data = await response.json();
console.error('=== GITHUB API RESPONSE ===');
console.error('Raw API response:', JSON.stringify(data, null, 2));
console.error('GitHub languages with byte counts:');
for (const [lang, bytes] of Object.entries(data)) {
console.error(` ${lang}: ${bytes} bytes`);
}
console.error('===========================');
return data;
} catch (error) {
console.error('Failed to fetch GitHub languages:', error.message);
return {};
}
}