Skip to content

Commit 43638b1

Browse files
committed
Support auto-detecting Actions workflows
1 parent e2b6f0f commit 43638b1

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

lib/config-utils.js

Lines changed: 32 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/config-utils.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config-utils.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ const mockRepositoryNwo = parseRepositoryNwo("owner/repo");
11591159
codeQL,
11601160
args.languagesInput,
11611161
mockRepositoryNwo,
1162+
".",
11621163
mockLogger,
11631164
);
11641165

@@ -1171,6 +1172,7 @@ const mockRepositoryNwo = parseRepositoryNwo("owner/repo");
11711172
codeQL,
11721173
args.languagesInput,
11731174
mockRepositoryNwo,
1175+
".",
11741176
mockLogger,
11751177
),
11761178
{ message: args.expectedError },

src/config-utils.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,31 @@ export async function getSupportedLanguageMap(
321321
return supportedLanguages;
322322
}
323323

324+
const baseWorkflowsPath = ".github/workflows";
325+
326+
/**
327+
* Determines if there exists a `.github/workflows` directory with at least
328+
* one file in it, which we use as an indicator that there are Actions
329+
* workflows in the workspace. This doesn't perfectly detect whether there
330+
* are actually workflows, but should be a good approximation.
331+
*
332+
* Alternatively, we could check specifically for yaml files, or call the
333+
* API to check if it knows about workflows.
334+
*
335+
* @returns True if the non-empty directory exists, false if not.
336+
*/
337+
export function hasActionsWorkflows(sourceRoot: string): boolean {
338+
const workflowsPath = path.resolve(sourceRoot, baseWorkflowsPath);
339+
const stats = fs.lstatSync(workflowsPath);
340+
return stats.isDirectory() && fs.readdirSync(workflowsPath).length > 0;
341+
}
342+
324343
/**
325344
* Gets the set of languages in the current repository.
326345
*/
327346
export async function getRawLanguagesInRepo(
328347
repository: RepositoryNwo,
348+
sourceRoot: string,
329349
logger: Logger,
330350
): Promise<string[]> {
331351
logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`);
@@ -335,9 +355,18 @@ export async function getRawLanguagesInRepo(
335355
});
336356

337357
logger.debug(`Languages API response: ${JSON.stringify(response)}`);
338-
return Object.keys(response.data as Record<string, number>).map((language) =>
339-
language.trim().toLowerCase(),
358+
const result = Object.keys(response.data as Record<string, number>).map(
359+
(language) => language.trim().toLowerCase(),
340360
);
361+
362+
if (hasActionsWorkflows(sourceRoot)) {
363+
logger.debug(`Found a .github/workflows directory`);
364+
result.push("actions");
365+
}
366+
367+
logger.debug(`Raw languages in repository: ${result.join(", ")}`);
368+
369+
return result;
341370
}
342371

343372
/**
@@ -354,12 +383,14 @@ export async function getLanguages(
354383
codeql: CodeQL,
355384
languagesInput: string | undefined,
356385
repository: RepositoryNwo,
386+
sourceRoot: string,
357387
logger: Logger,
358388
): Promise<Language[]> {
359389
// Obtain languages without filtering them.
360390
const { rawLanguages, autodetected } = await getRawLanguages(
361391
languagesInput,
362392
repository,
393+
sourceRoot,
363394
logger,
364395
);
365396

@@ -420,6 +451,7 @@ export function getRawLanguagesNoAutodetect(
420451
export async function getRawLanguages(
421452
languagesInput: string | undefined,
422453
repository: RepositoryNwo,
454+
sourceRoot: string,
423455
logger: Logger,
424456
): Promise<{
425457
rawLanguages: string[];
@@ -432,7 +464,7 @@ export async function getRawLanguages(
432464
}
433465
// Otherwise, autodetect languages in the repository.
434466
return {
435-
rawLanguages: await getRawLanguagesInRepo(repository, logger),
467+
rawLanguages: await getRawLanguagesInRepo(repository, sourceRoot, logger),
436468
autodetected: true,
437469
};
438470
}
@@ -481,6 +513,7 @@ export async function getDefaultConfig({
481513
repository,
482514
tempDir,
483515
codeql,
516+
sourceRoot,
484517
githubVersion,
485518
features,
486519
logger,
@@ -489,6 +522,7 @@ export async function getDefaultConfig({
489522
codeql,
490523
languagesInput,
491524
repository,
525+
sourceRoot,
492526
logger,
493527
);
494528

0 commit comments

Comments
 (0)