Browse Source

[frontend] Add CI package.json dependency version check

This check will ensure that we only allow pinned versions in package.json for dependencies.

With this commit I've also consolidated all the js ci scripts to the tools/ci folder.
Johan Åhlén 2 years ago
parent
commit
7e3760bed9

+ 14 - 8
.circleci/config.yml

@@ -102,7 +102,6 @@ commands:
             rm -f package-lock.json
             cp ~/repo/package-lock.json .
             npm install
-            npm i eslint-plugin-jest@latest --save-dev # Seems to not be found otherwise
             npm run webpack
             npm run webpack-login
             npm run webpack-workers
@@ -110,6 +109,13 @@ commands:
 
             cp -r ~/repo/docs .
 
+      - run:
+          name: run commit title format check
+          command: |
+            cd ~/repo
+
+            ./tools/ci/check_for_commit_message.sh
+
       # Run documentation lint
       - run:
           name: run documentation lints
@@ -140,13 +146,6 @@ commands:
             /usr/share/hue/build/env/bin/pip install pylint==1.7.5 pylint-django==0.7.2 configparser==4.0.2
             ./tools/ci/check_for_python_lint.sh /usr/share/hue
 
-      - run:
-          name: run commit title format check
-          command: |
-            cd ~/repo
-
-            ./tools/ci/check_for_commit_message.sh
-
       - run:
           name: run js lint
           command: |
@@ -164,6 +163,13 @@ commands:
 
             npm run style-lint
 
+      - run:
+          name: run npm version checker
+          command: |
+            cd /usr/share/hue
+
+            npm run check-pinned-versions
+
       - run:
           name: run npm license checker
           command: |

+ 4 - 0
.github/workflows/commitflow-py3.yml

@@ -22,6 +22,10 @@ jobs:
       with:
         fetch-depth: 0
 
+    - name: run commit title format check
+      run: |
+        ./tools/ci/check_for_commit_message.sh
+
     - name: Set up Python ${{ matrix.python-version }}
       uses: actions/setup-python@v2
       with:

+ 0 - 1
.github/workflows/commitflow.yml

@@ -72,7 +72,6 @@ jobs:
         cd /usr/share/hue
 
         npm install
-        npm i eslint-plugin-jest@latest --save-dev # Seems to not be found otherwise
         npm run webpack
         npm run webpack-login
         npm run webpack-workers

+ 3 - 2
package.json

@@ -177,7 +177,8 @@
     "test-coverage": "jest --coverage",
     "test-dev": "jest --watch",
     "test-clearCache": "jest --clearCache",
-    "check-license": "node ./tools/license/checkLicenses.js",
-    "check-absolute-paths": "node ./tools/detect-absolute-paths/detectAbsolutePaths.js"
+    "check-license": "node ./tools/ci/check_for_js_licenses.js",
+    "check-absolute-paths": "node ./tools/ci/check_for_absolute_paths.js",
+    "check-pinned-versions": "node ./tools/ci/check_for_pinned_versions.js"
   }
 }

+ 1 - 1
tools/detect-absolute-paths/detectAbsolutePaths.js → tools/ci/check_for_absolute_paths.js

@@ -18,7 +18,7 @@ const fs = require('fs');
 
 const TARGET_EXTENSIONS = /\.(js|map|css)$/i;
 const FOLDERS_TO_CHECK = ['desktop/core/src/desktop/static'];
-const HUE_ABSOLUTE_PATH = __dirname.replace('/tools/detect-absolute-paths', '');
+const HUE_ABSOLUTE_PATH = __dirname.replace('/tools/ci', '');
 
 const scanFile = async path =>
   new Promise(resolve => {

+ 0 - 0
tools/license/checkLicenses.js → tools/ci/check_for_js_licenses.js


+ 49 - 0
tools/ci/check_for_pinned_versions.js

@@ -0,0 +1,49 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+const fs = require('fs');
+
+const invalidVersionRegex = /^[<>~^]/;
+
+const runCheck = async () => {
+  // eslint-disable-next-line no-restricted-syntax
+  console.log('Checking if package.json contains invalid versions...');
+
+  const packageJson = await fs.promises.readFile('package.json');
+  const { dependencies, devDependencies } = JSON.parse(packageJson);
+
+  const allDependencies = Object.entries(dependencies).concat(Object.entries(devDependencies));
+  const invalidDependencies = allDependencies
+    .filter(([, version]) => invalidVersionRegex.test(version))
+    .map(([name, version]) => `${name}: ${version}`);
+
+  if (invalidDependencies.length) {
+    console.warn(
+      `Found invalid version(s):\n\n${invalidDependencies.join(
+        '\n'
+      )}\n\nPlease fix by "pinning" to a specific version (remove ~, ^, > or < prefixes).`
+    );
+    process.exitCode = 1;
+  } else {
+    // eslint-disable-next-line no-restricted-syntax
+    console.log(`Done! No invalid versions found.`);
+  }
+};
+
+runCheck().catch(e => {
+  console.error(e);
+  process.exitCode = 1;
+});