From 494bd1e7828f7ea0014da8392a3f46637f0a8851 Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Fri, 16 Jun 2023 10:33:00 +0300 Subject: [PATCH 1/6] don't parse date --- src/transform/plugins/changelog/index.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/transform/plugins/changelog/index.ts b/src/transform/plugins/changelog/index.ts index e99774e8..4d47610c 100644 --- a/src/transform/plugins/changelog/index.ts +++ b/src/transform/plugins/changelog/index.ts @@ -5,10 +5,6 @@ import {bold} from 'chalk'; import yaml from 'js-yaml'; import StateCore from 'markdown-it/lib/rules_core/state_core'; -interface Metadata { - date: string; -} - interface Options { extractChangelogs?: boolean; } @@ -58,11 +54,12 @@ function parseBody(tokens: Token[], state: StateCore) { if (metadataToken?.type !== 'fence') { throw new Error('Metadata tag not found'); } - const rawMetadata = yaml.load(metadataToken.content) as Metadata; - const metadata = { - ...rawMetadata, - date: new Date(rawMetadata.date).toISOString(), - }; + + let metadata: object = {}; + const rawMetadata = yaml.load(metadataToken.content); + if (rawMetadata && typeof rawMetadata === 'object') { + metadata = rawMetadata; + } if (!isTitle(tokens)) { throw new Error('Title tag not found'); From fc997579ce3b46e7ef00780843819b135884d5ba Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Fri, 16 Jun 2023 10:37:40 +0300 Subject: [PATCH 2/6] change type --- src/transform/plugins/changelog/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transform/plugins/changelog/types.ts b/src/transform/plugins/changelog/types.ts index e85ead75..22ff1ffe 100644 --- a/src/transform/plugins/changelog/types.ts +++ b/src/transform/plugins/changelog/types.ts @@ -6,6 +6,5 @@ export interface ChangelogItem { ratio?: string; }; description: string; - date: string; [x: string]: unknown; } From c258f7fa6e64f4ca0435bb325c5fddea1ac21330 Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 19 Jun 2023 11:17:28 +0300 Subject: [PATCH 3/6] add tests --- test/changelog.test.ts | 86 ++++++++++++++++--- test/data/changelog/changelog.md | 37 ++++++++ .../changelog_date.md} | 0 test/data/changelog/changelog_index.md | 40 +++++++++ 4 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 test/data/changelog/changelog.md rename test/data/{changelog.md => changelog/changelog_date.md} (100%) create mode 100644 test/data/changelog/changelog_index.md diff --git a/test/changelog.test.ts b/test/changelog.test.ts index 1a1871d5..a2c44155 100644 --- a/test/changelog.test.ts +++ b/test/changelog.test.ts @@ -8,10 +8,28 @@ import {Logger} from '../src/transform/log'; import {ChangelogItem} from '../src/transform/plugins/changelog/types'; describe('Changelog', () => { + function getItem(date: Date | undefined, index: number | undefined) { + return { + date, + index, + storyId: 123321, + title: 'Change log title', + image: { + alt: 'My image', + ratio: 0.5625, + src: '../src/asd.png', + }, + description: '

Change log payload

\n', + }; + } + test('Should cut changelog', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); const { result: {html, changelogs: logs}, @@ -24,10 +42,35 @@ describe('Changelog', () => { expect(logs).toBe(undefined); }); - test('Should cut changelog and write it in env', async () => { + test('Should cut changelog with date and write it in env', async () => { + expect.assertions(2); + + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog_date.md'), + 'utf8', + ); + + const { + result: {html, changelogs: logs}, + } = transform(data, { + plugins: [changelogPlugin, imsize], + extractChangelogs: true, + }); + + expect(html).toBe(`

Some changelog

\n

After changelog

\n`); + + expect(logs).toEqual( + new Array(3).fill(getItem(new Date('2023-05-10T00:00:00.000Z'), undefined)), + ); + }); + + test('Should cut changelog with index and write it in env', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog_index.md'), + 'utf8', + ); const { result: {html, changelogs: logs}, @@ -39,24 +82,39 @@ describe('Changelog', () => { expect(html).toBe(`

Some changelog

\n

After changelog

\n`); expect(logs).toEqual( - new Array(3).fill({ - date: '2023-05-10T00:00:00.000Z', - storyId: 123321, - title: 'Change log title', - image: { - alt: 'My image', - ratio: 0.5625, - src: '../src/asd.png', - }, - description: '

Change log payload

\n', + new Array(3).fill(getItem(undefined, 0)).map((item, index) => { + return {...item, index: 3 - index}; }), ); }); + test('Should cut changelog and write it in env', async () => { + expect.assertions(2); + + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); + + const { + result: {html, changelogs: logs}, + } = transform(data, { + plugins: [changelogPlugin, imsize], + extractChangelogs: true, + }); + + expect(html).toBe(`

Some changelog

\n

After changelog

\n`); + + expect(logs).toEqual(new Array(3).fill(getItem(undefined, undefined))); + }); + test('Should cut changelog and write it in variable', async () => { expect.assertions(2); - const data = await fs.promises.readFile(path.join(__dirname, 'data/changelog.md'), 'utf8'); + const data = await fs.promises.readFile( + path.join(__dirname, 'data/changelog/changelog.md'), + 'utf8', + ); const changelogs: ChangelogItem[] = []; const html = changelogCollect(data, { diff --git a/test/data/changelog/changelog.md b/test/data/changelog/changelog.md new file mode 100644 index 00000000..a72e27f2 --- /dev/null +++ b/test/data/changelog/changelog.md @@ -0,0 +1,37 @@ +# Some changelog + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + + +{% changelog %} +``` +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +After changelog diff --git a/test/data/changelog.md b/test/data/changelog/changelog_date.md similarity index 100% rename from test/data/changelog.md rename to test/data/changelog/changelog_date.md diff --git a/test/data/changelog/changelog_index.md b/test/data/changelog/changelog_index.md new file mode 100644 index 00000000..f352f5b5 --- /dev/null +++ b/test/data/changelog/changelog_index.md @@ -0,0 +1,40 @@ +# Some changelog + +{% changelog %} +``` +index: 3 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +{% changelog %} +``` +index: 2 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + + +{% changelog %} +``` +index: 1 +storyId: 123321 +``` +# Change log title +![My image](../src/asd.png =16x9) + +Change log payload + +{% endchangelog %} + +After changelog From 35e3e7b79fba038a1fe0c57ed9906ddff2b54d05 Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 19 Jun 2023 11:25:23 +0300 Subject: [PATCH 4/6] add parsed changelog count check --- src/transform/plugins/changelog/collect.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/transform/plugins/changelog/collect.ts b/src/transform/plugins/changelog/collect.ts index 0f073835..1d26a36e 100644 --- a/src/transform/plugins/changelog/collect.ts +++ b/src/transform/plugins/changelog/collect.ts @@ -51,7 +51,13 @@ const collect = (input: string, {path: filepath, log, changelogs, extractChangel } if (rawChanges.length && changelogs && extractChangelogs) { - changelogs.push(...parseChangelogs(rawChanges.join('\n\n'), filepath)); + const parsedChangelogs = parseChangelogs(rawChanges.join('\n\n'), filepath); + if (parsedChangelogs.length !== rawChanges.length) { + log.error( + `Parsed cahngelogs less than expected${filepath ? ` in ${bold(filepath)}` : ''}`, + ); + } + changelogs.push(...parsedChangelogs); } return result; From b4f4fcd4c4be3d60d0c2fcd60485855f928551a9 Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 19 Jun 2023 14:57:41 +0300 Subject: [PATCH 5/6] only json schema yaml --- src/transform/plugins/changelog/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/transform/plugins/changelog/index.ts b/src/transform/plugins/changelog/index.ts index 4d47610c..b353a7c4 100644 --- a/src/transform/plugins/changelog/index.ts +++ b/src/transform/plugins/changelog/index.ts @@ -56,7 +56,9 @@ function parseBody(tokens: Token[], state: StateCore) { } let metadata: object = {}; - const rawMetadata = yaml.load(metadataToken.content); + const rawMetadata = yaml.load(metadataToken.content, { + schema: yaml.JSON_SCHEMA, + }); if (rawMetadata && typeof rawMetadata === 'object') { metadata = rawMetadata; } From 4bef78570691fbeb29f2a35def4442fdc82190bb Mon Sep 17 00:00:00 2001 From: Anton Vikulov Date: Mon, 19 Jun 2023 15:15:33 +0300 Subject: [PATCH 6/6] fix tests --- test/changelog.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/changelog.test.ts b/test/changelog.test.ts index a2c44155..a82cd96c 100644 --- a/test/changelog.test.ts +++ b/test/changelog.test.ts @@ -8,7 +8,7 @@ import {Logger} from '../src/transform/log'; import {ChangelogItem} from '../src/transform/plugins/changelog/types'; describe('Changelog', () => { - function getItem(date: Date | undefined, index: number | undefined) { + function getItem(date: string | undefined, index: number | undefined) { return { date, index, @@ -59,9 +59,7 @@ describe('Changelog', () => { expect(html).toBe(`

Some changelog

\n

After changelog

\n`); - expect(logs).toEqual( - new Array(3).fill(getItem(new Date('2023-05-10T00:00:00.000Z'), undefined)), - ); + expect(logs).toEqual(new Array(3).fill(getItem('2023-05-10', undefined))); }); test('Should cut changelog with index and write it in env', async () => {