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(config/migration): migrate package.json config #33633

Open
wants to merge 3 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
41 changes: 39 additions & 2 deletions lib/workers/repository/config-migration/branch/create.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { codeBlock } from 'common-tags';
import type { Indent } from 'detect-indent';
import { Fixtures } from '../../../../../test/fixtures';
import type { RenovateConfig } from '../../../../../test/util';
import { partial } from '../../../../../test/util';
import { fs, partial, scm } from '../../../../../test/util';
import { getConfig } from '../../../../config/defaults';
import { scm } from '../../../../modules/platform/scm';
import { createConfigMigrationBranch } from './create';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';

jest.mock('../../../../util/fs');
jest.mock('../../../../util/git');

describe('workers/repository/config-migration/branch/create', () => {
Expand Down Expand Up @@ -79,6 +80,42 @@ describe('workers/repository/config-migration/branch/create', () => {
});
});

it('migrates renovate config in package.json', async () => {
fs.readLocalFile.mockResolvedValueOnce(codeBlock`
{
"dependencies": {
"xmldoc": "1.0.0"
},
"renovate": ${renovateConfig}
}
`);
scm.getFileList.mockResolvedValueOnce([]);
await createConfigMigrationBranch(config, {
...migratedConfigData,
filename: 'package.json',
});
expect(scm.checkoutBranch).toHaveBeenCalledWith(config.defaultBranch);
expect(scm.commitAndPush).toHaveBeenCalledWith({
branchName: 'renovate/migrate-config',
baseBranch: 'dev',
files: [
{
type: 'addition',
path: 'renovate.json',
contents: renovateConfig,
},
{
type: 'addition',
path: 'package.json',
contents: '{"dependencies":{"xmldoc":"1.0.0"}}',
},
],
message: 'Migrate config renovate.json',
platformCommit: 'auto',
force: true,
});
});

describe('applies the commitMessagePrefix value', () => {
it('to the default commit message', async () => {
config.commitMessagePrefix = 'PREFIX:';
Expand Down
48 changes: 39 additions & 9 deletions lib/workers/repository/config-migration/branch/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import { GlobalConfig } from '../../../../config/global';
import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
import { scm } from '../../../../modules/platform/scm';
import { parseJson } from '../../../../util/common';
import { readLocalFile } from '../../../../util/fs';
import type { FileChange } from '../../../../util/git/types';
import { getMigrationBranchName } from '../common';
import { ConfigMigrationCommitMessageFactory } from './commit-message';
import { MigratedDataFactory } from './migrated-data';
import { MigratedDataFactory, applyPrettierFormatting } from './migrated-data';
import type { MigratedData } from './migrated-data';

export async function createConfigMigrationBranch(
config: Partial<RenovateConfig>,
migratedConfigData: MigratedData,
): Promise<string | null> {
logger.debug('createConfigMigrationBranch()');
const configFileName = migratedConfigData.filename;
const pJsonMigration = migratedConfigData.filename === 'package.json';
const configFileName = pJsonMigration
? 'renovate.json'
: migratedConfigData.filename;
logger.debug('Creating config migration branch');

const commitMessageFactory = new ConfigMigrationCommitMessageFactory(
Expand All @@ -31,16 +37,40 @@ export async function createConfigMigrationBranch(
await scm.checkoutBranch(config.defaultBranch!);
const contents =
await MigratedDataFactory.applyPrettierFormatting(migratedConfigData);

const files: FileChange[] = [
{
type: 'addition',
path: configFileName,
contents,
},
];

if (pJsonMigration) {
const pJson = parseJson(
await readLocalFile('package.json', 'utf8'),
'package.json',
) as any;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
) as any;
) as unknown;

Don't use any

if (pJson?.renovate) {
delete pJson.renovate;
}
const pJsonContent = await applyPrettierFormatting(
'package.json',
JSON.stringify(pJson, undefined, migratedConfigData.indent.indent),
'json',
migratedConfigData.indent,
);
files.push({
type: 'addition',
path: 'package.json',
contents: pJsonContent,
});
}

return scm.commitAndPush({
baseBranch: config.baseBranch,
branchName: getMigrationBranchName(config),
files: [
{
type: 'addition',
path: configFileName,
contents,
},
],
files,
message: commitMessage.toString(),
platformCommit: config.platformCommit,
force: true,
Expand Down
13 changes: 0 additions & 13 deletions lib/workers/repository/config-migration/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,6 @@ describe('workers/repository/config-migration/index', () => {
expect(ensureConfigMigrationPr).toHaveBeenCalledTimes(0);
});

it('skips pr creation if config found in package.json', async () => {
const branchList: string[] = [];
mockedFunction(MigratedDataFactory.getAsync).mockResolvedValue({
content,
indent: partial<Indent>(),
filename: 'package.json',
});
const res = await configMigration(config, branchList);
expect(res).toMatchObject({ result: 'no-migration' });
expect(checkConfigMigrationBranch).toHaveBeenCalledTimes(0);
expect(ensureConfigMigrationPr).toHaveBeenCalledTimes(0);
});

it('creates migration pr if needed', async () => {
const branchList: string[] = [];
mockedFunction(checkConfigMigrationBranch).mockResolvedValue({
Expand Down
8 changes: 0 additions & 8 deletions lib/workers/repository/config-migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ export async function configMigration(
return { result: 'no-migration' };
}

if (migratedConfigData.filename === 'package.json') {
logger.debug(
' Using package.json for Renovate config is deprecated - please use a dedicated configuration file instead. Skipping config migration.',
);
MigratedDataFactory.reset();
return { result: 'no-migration' };
}

const res = await checkConfigMigrationBranch(config, migratedConfigData);

// migration needed but not demanded by user
Expand Down
Loading