This commit is contained in:
RahulGautamSingh 2025-01-23 05:49:56 +05:30 committed by GitHub
commit ee84833586
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 32 deletions

View file

@ -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', () => {
@ -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:';

View file

@ -2,9 +2,12 @@ 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(
@ -12,7 +15,10 @@ export async function createConfigMigrationBranch(
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(
@ -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;
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,

View file

@ -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({

View file

@ -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