Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github-actions): Use schema for dependency extraction #33584

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 17 additions & 46 deletions lib/modules/manager/github-actions/extract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import is from '@sindresorhus/is';
import { GlobalConfig } from '../../../config/global';
import { logger } from '../../../logger';
import { isNotNullOrUndefined } from '../../../util/array';
import { detectPlatform } from '../../../util/common';
import { newlineRegex, regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import { GiteaTagsDatasource } from '../../datasource/gitea-tags';
import { GithubRunnersDatasource } from '../../datasource/github-runners';
import { GithubTagsDatasource } from '../../datasource/github-tags';
Expand All @@ -15,7 +12,7 @@ import type {
PackageDependency,
PackageFileContent,
} from '../types';
import type { Workflow } from './types';
import { WorkflowJobsSchema } from './schema';

const dockerActionRe = regEx(/^\s+uses\s*: ['"]?docker:\/\/([^'"]+)\s*$/);
const actionRe = regEx(
Expand Down Expand Up @@ -135,18 +132,6 @@ function detectDatasource(registryUrl: string): PackageDependency {
};
}

function extractContainer(
container: unknown,
registryAliases: Record<string, string> | undefined,
): PackageDependency | undefined {
if (is.string(container)) {
return getDep(container, true, registryAliases);
} else if (is.plainObject(container) && is.string(container.image)) {
return getDep(container.image, true, registryAliases);
}
return undefined;
}

const runnerVersionRegex = regEx(
/^\s*(?<depName>[a-zA-Z]+)-(?<currentValue>[^\s]+)/,
);
Expand Down Expand Up @@ -179,17 +164,6 @@ function extractRunner(runner: string): PackageDependency | null {
return dependency;
}

function extractRunners(runner: unknown): PackageDependency[] {
const runners: string[] = [];
if (is.string(runner)) {
runners.push(runner);
} else if (is.array(runner, is.string)) {
runners.push(...runner);
}

return runners.map(extractRunner).filter(isNotNullOrUndefined);
}

function extractWithYAMLParser(
content: string,
packageFile: string,
Expand All @@ -198,34 +172,31 @@ function extractWithYAMLParser(
logger.trace('github-actions.extractWithYAMLParser()');
const deps: PackageDependency[] = [];

let pkg: Workflow;
try {
// TODO: use schema (#9610)
pkg = parseSingleYaml(content);
} catch (err) {
logger.debug(
{ packageFile, err },
'Failed to parse GitHub Actions Workflow YAML',
);
return [];
}
const jobs = WorkflowJobsSchema.parse(content);

Copy link
Member

@viceice viceice Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some error logging, so we know if parsing fails (probably of some github schema changes)

for (const job of Object.values(pkg?.jobs ?? {})) {
const dep = extractContainer(job?.container, config.registryAliases);
if (dep) {
dep.depType = 'container';
deps.push(dep);
for (const job of jobs) {
if (job.container) {
const dep = getDep(job.container, true, config.registryAliases);
if (dep) {
dep.depType = 'container';
deps.push(dep);
}
}

for (const service of Object.values(job?.services ?? {})) {
const dep = extractContainer(service, config.registryAliases);
for (const service of job.services) {
const dep = getDep(service, true, config.registryAliases);
if (dep) {
dep.depType = 'service';
deps.push(dep);
}
}

deps.push(...extractRunners(job?.['runs-on']));
for (const runner of job['runs-on']) {
const dep = extractRunner(runner);
if (dep) {
deps.push(dep);
}
}
}

return deps;
Expand Down
31 changes: 31 additions & 0 deletions lib/modules/manager/github-actions/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { z } from 'zod';
import { LooseRecord, Yaml } from '../../../util/schema-utils';

export const WorkflowJobsSchema = Yaml.pipe(
z.object({
jobs: LooseRecord(
z.object({
container: z
.union([
z.string(),
z.object({ image: z.string() }).transform((v) => v.image),
])
.optional()
.catch(undefined),
services: LooseRecord(
z.union([
z.object({ image: z.string() }).transform((v) => v.image),
z.string(),
]),
)
.catch({})
.transform((services) => Object.values(services)),
'runs-on': z
.union([z.string().transform((v) => [v]), z.array(z.string())])
.catch([]),
}),
).catch({}),
}),
)
.transform((v) => Object.values(v.jobs))
.catch([]);
Loading