Skip to content

Commit

Permalink
add wrapper function for applyPrettierFormatting
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
Gabriel-Ladzaretti and viceice committed Oct 23, 2022
1 parent 05c724e commit 99c92a9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
7 changes: 5 additions & 2 deletions lib/workers/repository/config-migration/branch/create.spec.ts
Expand Up @@ -8,8 +8,8 @@ import {
} from '../../../../../test/util';
import { checkoutBranch, commitFiles } from '../../../../util/git';
import { createConfigMigrationBranch } from './create';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';
import * as MigratedDataModule from './migrated-data';

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

Expand All @@ -18,7 +18,10 @@ describe('workers/repository/config-migration/branch/create', () => {
const indent = ' ';
const renovateConfig = JSON.stringify(raw, undefined, indent) + '\n';
const filename = 'renovate.json';
const prettierSpy = jest.spyOn(MigratedDataModule, 'applyPrettierFormatting');
const prettierSpy = jest.spyOn(
MigratedDataFactory,
'applyPrettierFormatting'
);

let config: RenovateConfig;
let migratedConfigData: MigratedData;
Expand Down
9 changes: 4 additions & 5 deletions lib/workers/repository/config-migration/branch/create.ts
@@ -1,12 +1,11 @@
import upath from 'upath';
import { GlobalConfig } from '../../../../config/global';
import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
import { commitAndPush } from '../../../../modules/platform/commit';
import { checkoutBranch } from '../../../../util/git';
import { getMigrationBranchName } from '../common';
import { ConfigMigrationCommitMessageFactory } from './commit-message';
import { PrettierParser, applyPrettierFormatting } from './migrated-data';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';

export async function createConfigMigrationBranch(
Expand All @@ -31,9 +30,9 @@ export async function createConfigMigrationBranch(
}

await checkoutBranch(config.defaultBranch!);
const { content, filename, indent } = migratedConfigData;
const parser = upath.extname(filename).replace('.', '') as PrettierParser;
const contents = await applyPrettierFormatting(content, parser, indent);
const contents = await MigratedDataFactory.applyPrettierFormatting(
migratedConfigData
);
return commitAndPush({
branchName: getMigrationBranchName(config),
files: [
Expand Down
11 changes: 11 additions & 0 deletions lib/workers/repository/config-migration/branch/migrated-data.ts
@@ -1,6 +1,7 @@
import detectIndent from 'detect-indent';
import JSON5 from 'json5';
import prettier, { BuiltInParserName } from 'prettier';
import upath from 'upath';
import { migrateConfig } from '../../../../config/migration';
import { logger } from '../../../../logger';
import { readLocalFile } from '../../../../util/fs';
Expand Down Expand Up @@ -33,6 +34,7 @@ const prettierConfigFilenames = new Set([
]);

export type PrettierParser = BuiltInParserName;

export async function applyPrettierFormatting(
content: string,
parser: PrettierParser,
Expand Down Expand Up @@ -94,6 +96,15 @@ export class MigratedDataFactory {
this.data = null;
}

static applyPrettierFormatting({
content,
filename,
indent,
}: MigratedData): Promise<string> {
const parser = upath.extname(filename).replace('.', '') as PrettierParser;
return applyPrettierFormatting(content, parser, indent);
}

private static async build(): Promise<MigratedData | null> {
let res: MigratedData | null = null;
try {
Expand Down
7 changes: 5 additions & 2 deletions lib/workers/repository/config-migration/branch/rebase.spec.ts
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import { checkoutBranch, commitFiles } from '../../../../util/git';
import * as MigratedDataModule from './migrated-data';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';
import { rebaseMigrationBranch } from './rebase';

Expand All @@ -20,7 +20,10 @@ const formattedMigratedData = Fixtures.getJson(
);

describe('workers/repository/config-migration/branch/rebase', () => {
const prettierSpy = jest.spyOn(MigratedDataModule, 'applyPrettierFormatting');
const prettierSpy = jest.spyOn(
MigratedDataFactory,
'applyPrettierFormatting'
);

beforeAll(() => {
GlobalConfig.set({
Expand Down
22 changes: 7 additions & 15 deletions lib/workers/repository/config-migration/branch/rebase.ts
@@ -1,5 +1,3 @@
import hasha from 'hasha';
import upath from 'upath';
import { GlobalConfig } from '../../../../config/global';
import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
Expand All @@ -12,7 +10,7 @@ import {
import { quickStringify } from '../../../../util/stringify';
import { getMigrationBranchName } from '../common';
import { ConfigMigrationCommitMessageFactory } from './commit-message';
import { PrettierParser, applyPrettierFormatting } from './migrated-data';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';

export async function rebaseMigrationBranch(
Expand All @@ -28,7 +26,7 @@ export async function rebaseMigrationBranch(
const configFileName = migratedConfigData.filename;
let contents = migratedConfigData.content;
const existingContents = await getFile(configFileName, branchName);
if (hash(contents) === hash(existingContents)) {
if (stripWhitespaces(contents) === stripWhitespaces(existingContents)) {
logger.debug('Migration branch is up to date');
return null;
}
Expand All @@ -46,10 +44,9 @@ export async function rebaseMigrationBranch(
const commitMessage = commitMessageFactory.getCommitMessage();

await checkoutBranch(config.defaultBranch!);

const { content, filename, indent } = migratedConfigData;
const parser = upath.extname(filename).replace('.', '') as PrettierParser;
contents = await applyPrettierFormatting(content, parser, indent);
contents = await MigratedDataFactory.applyPrettierFormatting(
migratedConfigData
);
return commitAndPush({
branchName,
files: [
Expand All @@ -64,14 +61,9 @@ export async function rebaseMigrationBranch(
});
}

function stripWhitespaces(str: string): string {
return quickStringify(JSON.parse(str));
}

function hash(str: string | null): string | null {
function stripWhitespaces(str: string | null): string | null {
if (!str) {
return null;
}
const stripped = stripWhitespaces(str);
return hasha(stripped);
return quickStringify(JSON.parse(str));
}

0 comments on commit 99c92a9

Please sign in to comment.