Skip to content

Commit

Permalink
fix: Revert "refactor: safely parse composer files" (#21448)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Apr 12, 2023
1 parent c02cf6a commit 1767f76
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 92 deletions.
35 changes: 7 additions & 28 deletions lib/modules/manager/composer/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};

Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions lib/modules/manager/composer/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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: [] };

Expand Down
57 changes: 0 additions & 57 deletions lib/modules/manager/composer/schema.ts

This file was deleted.

45 changes: 45 additions & 0 deletions lib/modules/manager/composer/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/composer/update-locked.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/composer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down

0 comments on commit 1767f76

Please sign in to comment.