From 0cbecabff2f21a57f63495f7fbe472dbb9ea96cb Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 23 Jan 2021 16:18:59 +0000 Subject: [PATCH] Plugin Repo: Prevent faulty packages from generating commits on each check --- .eslintignore | 9 +++ .gitignore | 9 +++ packages/plugin-repo-cli/index.ts | 14 ++-- .../lib/errorsHaveChanged.test.ts | 65 +++++++++++++++++++ .../plugin-repo-cli/lib/errorsHaveChanged.ts | 11 ++++ packages/plugin-repo-cli/lib/types.ts | 7 ++ 6 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 packages/plugin-repo-cli/lib/errorsHaveChanged.test.ts create mode 100644 packages/plugin-repo-cli/lib/errorsHaveChanged.ts create mode 100644 packages/plugin-repo-cli/lib/types.ts diff --git a/.eslintignore b/.eslintignore index b319b59ee..f2f3ec3be 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1458,6 +1458,15 @@ packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.js.map packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.d.ts packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.js packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.js.map +packages/plugin-repo-cli/lib/errorsHaveChanged.d.ts +packages/plugin-repo-cli/lib/errorsHaveChanged.js +packages/plugin-repo-cli/lib/errorsHaveChanged.js.map +packages/plugin-repo-cli/lib/errorsHaveChanged.test.d.ts +packages/plugin-repo-cli/lib/errorsHaveChanged.test.js +packages/plugin-repo-cli/lib/errorsHaveChanged.test.js.map +packages/plugin-repo-cli/lib/types.d.ts +packages/plugin-repo-cli/lib/types.js +packages/plugin-repo-cli/lib/types.js.map packages/plugin-repo-cli/lib/updateReadme.d.ts packages/plugin-repo-cli/lib/updateReadme.js packages/plugin-repo-cli/lib/updateReadme.js.map diff --git a/.gitignore b/.gitignore index e3096b189..ce11d46bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1446,6 +1446,15 @@ packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.js.map packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.d.ts packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.js packages/plugin-repo-cli/lib/checkIfPluginCanBeAdded.test.js.map +packages/plugin-repo-cli/lib/errorsHaveChanged.d.ts +packages/plugin-repo-cli/lib/errorsHaveChanged.js +packages/plugin-repo-cli/lib/errorsHaveChanged.js.map +packages/plugin-repo-cli/lib/errorsHaveChanged.test.d.ts +packages/plugin-repo-cli/lib/errorsHaveChanged.test.js +packages/plugin-repo-cli/lib/errorsHaveChanged.test.js.map +packages/plugin-repo-cli/lib/types.d.ts +packages/plugin-repo-cli/lib/types.js +packages/plugin-repo-cli/lib/types.js.map packages/plugin-repo-cli/lib/updateReadme.d.ts packages/plugin-repo-cli/lib/updateReadme.js packages/plugin-repo-cli/lib/updateReadme.js.map diff --git a/packages/plugin-repo-cli/index.ts b/packages/plugin-repo-cli/index.ts index 247dbc418..8655fd7bc 100644 --- a/packages/plugin-repo-cli/index.ts +++ b/packages/plugin-repo-cli/index.ts @@ -7,12 +7,8 @@ import validatePluginId from '@joplin/lib/services/plugins/utils/validatePluginI import { execCommand2, resolveRelativePathWithinDir, gitPullTry, gitRepoCleanTry, gitRepoClean } from '@joplin/tools/tool-utils.js'; import checkIfPluginCanBeAdded from './lib/checkIfPluginCanBeAdded'; import updateReadme from './lib/updateReadme'; - -interface NpmPackage { - name: string; - version: string; - date: Date; -} +import { ImportErrors, NpmPackage } from './lib/types'; +import errorsHaveChanged from './lib/errorsHaveChanged'; function stripOffPackageOrg(name: string): string { const n = name.split('/'); @@ -167,7 +163,7 @@ async function processNpmPackage(npmPackage: NpmPackage, repoDir: string) { chdir(packageTempDir); await execCommand2('npm init --yes --loglevel silent', { quiet: true }); - const errors: any = await readJsonFile(errorsPath, {}); + const errors: ImportErrors = await readJsonFile(errorsPath, {}); delete errors[npmPackage.name]; let actionType: ProcessingActionType = ProcessingActionType.Update; @@ -191,7 +187,9 @@ async function processNpmPackage(npmPackage: NpmPackage, repoDir: string) { } if (Object.keys(errors).length) { - await fs.writeFile(errorsPath, JSON.stringify(errors, null, '\t'), 'utf8'); + if (errorsHaveChanged(await readJsonFile(errorsPath, {}), errors)) { + await fs.writeFile(errorsPath, JSON.stringify(errors, null, '\t'), 'utf8'); + } } else { await fs.remove(errorsPath); } diff --git a/packages/plugin-repo-cli/lib/errorsHaveChanged.test.ts b/packages/plugin-repo-cli/lib/errorsHaveChanged.test.ts new file mode 100644 index 000000000..5065f2958 --- /dev/null +++ b/packages/plugin-repo-cli/lib/errorsHaveChanged.test.ts @@ -0,0 +1,65 @@ +import errorsHaveChanged from './errorsHaveChanged'; + +describe('errorsHaveChanged', () => { + + test('should tell if an errors object has changed', () => { + const testCases = [ + [ + { + 'one': '111', + 'two': '222', + }, + { + 'two': '222', + 'one': '111', + }, + false, + ], + [ + { + 'one': '111', + 'two': '222', + }, + { + 'one': '111', + 'two': '222', + }, + false, + ], + [ + { + 'one': '111', + 'two': '222', + }, + { + 'one': '111', + }, + true, + ], + [ + { + 'one': '111', + 'two': '222', + }, + { + 'one': '222', + 'two': '111', + }, + true, + ], + [ + {}, + {}, + false, + ], + ]; + + for (const t of testCases) { + const [oldErrors, newErrors, expected] = t; + + const result = errorsHaveChanged(oldErrors, newErrors); + expect(result).toBe(expected); + } + }); + +}); diff --git a/packages/plugin-repo-cli/lib/errorsHaveChanged.ts b/packages/plugin-repo-cli/lib/errorsHaveChanged.ts new file mode 100644 index 000000000..0868a693c --- /dev/null +++ b/packages/plugin-repo-cli/lib/errorsHaveChanged.ts @@ -0,0 +1,11 @@ +import { ImportErrors } from './types'; + +export default function(previousErrors: ImportErrors, newErrors: ImportErrors): boolean { + if (Object.keys(previousErrors).length !== Object.keys(newErrors).length) return true; + + for (const packageName of Object.keys(previousErrors)) { + if (newErrors[packageName] !== previousErrors[packageName]) return true; + } + + return false; +} diff --git a/packages/plugin-repo-cli/lib/types.ts b/packages/plugin-repo-cli/lib/types.ts new file mode 100644 index 000000000..503eb4502 --- /dev/null +++ b/packages/plugin-repo-cli/lib/types.ts @@ -0,0 +1,7 @@ +export type ImportErrors = Record; + +export interface NpmPackage { + name: string; + version: string; + date: Date; +}