diff --git a/lib/modules/manager/composer/artifacts.ts b/lib/modules/manager/composer/artifacts.ts index 290a212073a523cda5488999e089b69e0c5454ba..0eae7fefdefad1bbb38ad6d91d8fefad8c73adf0 100644 --- a/lib/modules/manager/composer/artifacts.ts +++ b/lib/modules/manager/composer/artifacts.ts @@ -21,8 +21,7 @@ import { regEx } from '../../../util/regex'; import { GitTagsDatasource } from '../../datasource/git-tags'; import { PackagistDatasource } from '../../datasource/packagist'; import type { UpdateArtifact, UpdateArtifactsResult } from '../types'; -import { ComposerConfig, ComposerLock } from './schema'; -import type { AuthJson } from './types'; +import type { AuthJson, ComposerLock } from './types'; import { extractConstraints, findGithubToken, @@ -105,32 +104,12 @@ export async function updateArtifacts({ try { await writeLocalFile(packageFileName, newPackageFileContent); - const composerLockResult = ComposerLock.safeParse( - JSON.parse(existingLockFileContent) - ); - // istanbul ignore if - if (!composerLockResult.success) { - logger.warn( - { error: composerLockResult.error }, - 'Unable to parse composer.lock' - ); - return null; - } - - const newPackageFileResult = ComposerConfig.safeParse( - JSON.parse(newPackageFileContent) - ); - // istanbul ignore if - if (!newPackageFileResult.success) { - logger.warn( - { error: newPackageFileResult.error }, - 'Unable to parse composer.json' - ); - return null; - } - + const existingLockFile: ComposerLock = JSON.parse(existingLockFileContent); const constraints = { - ...extractConstraints(newPackageFileResult.data, composerLockResult.data), + ...extractConstraints( + JSON.parse(newPackageFileContent), + existingLockFile + ), ...config.constraints, }; @@ -157,7 +136,7 @@ export async function updateArtifacts({ const commands: string[] = []; // Determine whether install is required before update - if (requireComposerDependencyInstallation(composerLockResult.data)) { + if (requireComposerDependencyInstallation(existingLockFile)) { const preCmd = 'composer'; const preArgs = 'install' + getComposerArguments(config, composerToolConstraint); diff --git a/lib/modules/manager/composer/extract.ts b/lib/modules/manager/composer/extract.ts index c18c264d84ba10ff7f607688ddd8268fb2577213..ae40abec46924968a463626b2ddd7a4e9c10ec40 100644 --- a/lib/modules/manager/composer/extract.ts +++ b/lib/modules/manager/composer/extract.ts @@ -10,10 +10,10 @@ import type { PackageDependency, PackageFileContent } from '../types'; import type { ComposerConfig, ComposerLock, + ComposerManagerData, ComposerRepositories, - ComposerRepository, -} from './schema'; -import type { ComposerManagerData } from './types'; + Repo, +} from './types'; /** * The regUrl is expected to be a base URL. GitLab composer repository installation guide specifies @@ -34,7 +34,7 @@ function transformRegUrl(url: string): string { */ function parseRepositories( repoJson: ComposerRepositories, - repositories: Record<string, ComposerRepository>, + repositories: Record<string, Repo>, registryUrls: string[] ): void { try { @@ -91,7 +91,7 @@ export async function extractPackageFile( logger.debug(`Invalid JSON in ${fileName}`); return null; } - const repositories: Record<string, ComposerRepository> = {}; + const repositories: Record<string, Repo> = {}; const registryUrls: string[] = []; const res: PackageFileContent = { deps: [] }; diff --git a/lib/modules/manager/composer/schema.ts b/lib/modules/manager/composer/schema.ts deleted file mode 100644 index 35cffa60bc4b42f451069ccfd2bb1fc3a479bc2a..0000000000000000000000000000000000000000 --- a/lib/modules/manager/composer/schema.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { z } from 'zod'; - -const ComposerLockPackage = z.object({ - name: z.string(), - version: z.string(), -}); - -export const ComposerLock = z.object({ - 'plugin-api-version': z.string().optional(), - packages: z.array(ComposerLockPackage).optional(), - 'packages-dev': z.array(ComposerLockPackage).optional(), -}); -export type ComposerLock = z.infer<typeof ComposerLock>; - -const ComposerRepository = z.object({ - name: z.string().optional(), - type: z.union([ - z.literal('composer'), - z.literal('git'), - z.literal('package'), - z.literal('path'), - z.literal('vcs'), - ]), - packagist: z.boolean().optional(), - 'packagist.org': z.boolean().optional(), - url: z.string().url(), -}); -export type ComposerRepository = z.infer<typeof ComposerRepository>; - -const ComposerRepositories = z.union([ - z.record(z.string(), z.union([ComposerRepository, z.boolean()])), - z.array(ComposerRepository), -]); -export type ComposerRepositories = z.infer<typeof ComposerRepositories>; - -export const ComposerConfig = z.object({ - type: z.string().optional(), - /** - * Setting a fixed PHP version (e.g. {"php": "7.0.3"}) will let you fake the - * platform version so that you can emulate a production env or define your - * target platform in the config. - * See https://getcomposer.org/doc/06-config.md#platform - */ - config: z - .object({ platform: z.object({ php: z.string().optional() }).optional() }) - .optional(), - /** - * A repositories field can be an array of Repo objects or an object of repoName: Repo - * Also it can be a boolean (usually false) to disable packagist. - * (Yes this can be confusing, as it is also not properly documented in the composer docs) - * See https://getcomposer.org/doc/05-repositories.md#disabling-packagist-org - */ - repositories: ComposerRepositories.optional(), - require: z.record(z.string(), z.string()).optional(), - 'require-dev': z.record(z.string(), z.string()).optional(), -}); -export type ComposerConfig = z.infer<typeof ComposerConfig>; diff --git a/lib/modules/manager/composer/types.ts b/lib/modules/manager/composer/types.ts index 7261568b101a0d65afce5991f6005a9465a46b4b..c46f294d0cf861be30aa6e0156b01d7cc09f13cf 100644 --- a/lib/modules/manager/composer/types.ts +++ b/lib/modules/manager/composer/types.ts @@ -1,4 +1,49 @@ // istanbul ignore file: types only +export interface Repo { + name?: string; + type: 'composer' | 'git' | 'package' | 'path' | 'vcs'; + packagist?: boolean; + 'packagist.org'?: boolean; + url: string; +} +export type ComposerRepositories = Record<string, Repo | boolean> | Repo[]; + +export interface ComposerConfig { + type?: string; + /** + * Setting a fixed PHP version (e.g. {"php": "7.0.3"}) will let you fake the + * platform version so that you can emulate a production env or define your + * target platform in the config. + * See https://getcomposer.org/doc/06-config.md#platform + */ + config?: { + platform?: { + php?: string; + }; + }; + /** + * A repositories field can be an array of Repo objects or an object of repoName: Repo + * Also it can be a boolean (usually false) to disable packagist. + * (Yes this can be confusing, as it is also not properly documented in the composer docs) + * See https://getcomposer.org/doc/05-repositories.md#disabling-packagist-org + */ + repositories?: ComposerRepositories; + + require?: Record<string, string>; + 'require-dev'?: Record<string, string>; +} + +export interface ComposerLockPackage { + name: string; + version: string; +} + +export interface ComposerLock { + 'plugin-api-version'?: string; + packages?: ComposerLockPackage[]; + 'packages-dev'?: ComposerLockPackage[]; +} + export interface ComposerManagerData { composerJsonType?: string; } diff --git a/lib/modules/manager/composer/update-locked.ts b/lib/modules/manager/composer/update-locked.ts index 76897b49e793b9f61c268e5294751792f10bfd99..fcfaa89d2f11c9de4d93aacb80a5ed63c3305b61 100644 --- a/lib/modules/manager/composer/update-locked.ts +++ b/lib/modules/manager/composer/update-locked.ts @@ -1,7 +1,7 @@ import { logger } from '../../../logger'; import { api as composer } from '../../versioning/composer'; import type { UpdateLockedConfig, UpdateLockedResult } from '../types'; -import type { ComposerLock } from './schema'; +import type { ComposerLock } from './types'; export function updateLockedDependency( config: UpdateLockedConfig diff --git a/lib/modules/manager/composer/utils.ts b/lib/modules/manager/composer/utils.ts index 7835a2a84e4d3adf7630fe3b16bf127a44b9d6f8..330f50bc713892cfed4d7b23b8f6ee05d72b8f72 100644 --- a/lib/modules/manager/composer/utils.ts +++ b/lib/modules/manager/composer/utils.ts @@ -7,7 +7,7 @@ import type { ToolConstraint } from '../../../util/exec/types'; import { HostRuleSearch, find as findHostRule } from '../../../util/host-rules'; import { api, id as composerVersioningId } from '../../versioning/composer'; import type { UpdateArtifactsConfig } from '../types'; -import type { ComposerConfig, ComposerLock } from './schema'; +import type { ComposerConfig, ComposerLock } from './types'; export { composerVersioningId };