Skip to content

Commit bc90418

Browse files
authored
Merge pull request #3009 from github/mbg/auto-detect-actions
Support auto-detecting Actions workflows
2 parents a625e16 + f28436b commit bc90418

File tree

6 files changed

+85
-17
lines changed

6 files changed

+85
-17
lines changed

lib/config-utils.js

Lines changed: 35 additions & 9 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: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,58 @@ 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 (
341+
stats !== undefined &&
342+
stats.isDirectory() &&
343+
fs.readdirSync(workflowsPath).length > 0
344+
);
345+
}
346+
324347
/**
325348
* Gets the set of languages in the current repository.
326349
*/
327350
export async function getRawLanguagesInRepo(
328351
repository: RepositoryNwo,
352+
sourceRoot: string,
329353
logger: Logger,
330354
): Promise<string[]> {
331-
logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`);
355+
logger.debug(
356+
`Automatically detecting languages (${repository.owner}/${repository.repo})`,
357+
);
332358
const response = await api.getApiClient().rest.repos.listLanguages({
333359
owner: repository.owner,
334360
repo: repository.repo,
335361
});
336362

337363
logger.debug(`Languages API response: ${JSON.stringify(response)}`);
338-
return Object.keys(response.data as Record<string, number>).map((language) =>
339-
language.trim().toLowerCase(),
364+
const result = Object.keys(response.data as Record<string, number>).map(
365+
(language) => language.trim().toLowerCase(),
340366
);
367+
368+
if (hasActionsWorkflows(sourceRoot)) {
369+
logger.debug(`Found a .github/workflows directory`);
370+
result.push("actions");
371+
}
372+
373+
logger.debug(`Raw languages in repository: ${result.join(", ")}`);
374+
375+
return result;
341376
}
342377

343378
/**
@@ -354,12 +389,14 @@ export async function getLanguages(
354389
codeql: CodeQL,
355390
languagesInput: string | undefined,
356391
repository: RepositoryNwo,
392+
sourceRoot: string,
357393
logger: Logger,
358394
): Promise<Language[]> {
359395
// Obtain languages without filtering them.
360396
const { rawLanguages, autodetected } = await getRawLanguages(
361397
languagesInput,
362398
repository,
399+
sourceRoot,
363400
logger,
364401
);
365402

@@ -420,6 +457,7 @@ export function getRawLanguagesNoAutodetect(
420457
export async function getRawLanguages(
421458
languagesInput: string | undefined,
422459
repository: RepositoryNwo,
460+
sourceRoot: string,
423461
logger: Logger,
424462
): Promise<{
425463
rawLanguages: string[];
@@ -432,7 +470,7 @@ export async function getRawLanguages(
432470
}
433471
// Otherwise, autodetect languages in the repository.
434472
return {
435-
rawLanguages: await getRawLanguagesInRepo(repository, logger),
473+
rawLanguages: await getRawLanguagesInRepo(repository, sourceRoot, logger),
436474
autodetected: true,
437475
};
438476
}
@@ -481,6 +519,7 @@ export async function getDefaultConfig({
481519
repository,
482520
tempDir,
483521
codeql,
522+
sourceRoot,
484523
githubVersion,
485524
features,
486525
logger,
@@ -489,6 +528,7 @@ export async function getDefaultConfig({
489528
codeql,
490529
languagesInput,
491530
repository,
531+
sourceRoot,
492532
logger,
493533
);
494534

0 commit comments

Comments
 (0)