Skip to content

Commit

Permalink
Only Run npm-package-json-lint When package.json is Present (#2280)
Browse files Browse the repository at this point in the history
* Link to a PR rather than an issue in changelog

* Prefer list.append(x) to list += [x]

The former is slightly faster as it generally doesn't create a new list.

* Don't warn that REPOSITORY linters are missing

Some REPOSITORY linters are intentionally omitted from flavors to keep
their size small and performance fast. Hence, their absence from a
flavor isn't considered an error when FAIL_IF_MISSING_LINTER_IN_FLAVOR
is true. Therefore, omit them from the list of missing linters displayed
to the user to avoid confusion.

* Simplify check_active_linters_match_flavor

Reduce indentation by inverting test, and prefer the more Pythonic test
not list to len(list) == 0 for simplicity. Remove some comments rendered
unnecessary via this more direct expression of our intent.

* Correct capitalization of Docker in a warning

* Only lint package.json when present (#2279)

npm-package-json-lint only lints Node.js package.json files, so only run
it when package.json is present. npm-package-json-lint is correctly
omitted from most flavors. Many non-Node.js projects contain other JSON
files, so this change prevents false positives when
FAIL_IF_MISSING_LINTER_IN_FLAVOR is true.
  • Loading branch information
Kurt-von-Laven authored Jan 29, 2023
1 parent 33fe169 commit 40631ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Change name of config file for powershell formatter to avoid collision with powershell linter config
- Enhance find SARIF json in stdout output
- Pass --show-context, --show-suggestions, and --no-must-find-files to CSpell for friendlier UX
by @Kurt-von-Laven in [#2271](https://github.com/oxsecurity/megalinter/issues/2271).
by @Kurt-von-Laven in [#2275](https://github.com/oxsecurity/megalinter/pull/2275).
- Only run npm-package-json-lint when package.json is present by @Kurt-von-Laven in
[#2280](https://github.com/oxsecurity/megalinter/pull/2280).

- Documentation
- Configure jsonschema documentation formatting (see [Descriptor schema](https://megalinter.io/latest/json-schemas/descriptor.html), [Configuration schema](https://megalinter.io/latest/json-schemas/configuration.html)), by @echoix in [#2270](https://github.com/oxsecurity/megalinter/pull/2270)
Expand Down
2 changes: 2 additions & 0 deletions megalinter/descriptors/json.megalinter-descriptor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ linters:
- salesforce
file_names_regex:
- "package\\.json"
active_only_if_file_found:
- "package.json"
cli_lint_mode: project
cli_executable: npmPkgJsonLint
cli_config_arg_name: --configFile
Expand Down
58 changes: 24 additions & 34 deletions megalinter/flavor_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,31 @@ def check_active_linters_match_flavor(active_linters):
missing_linters = []
for active_linter in active_linters:
if active_linter.name not in flavor_linters:
missing_linters += [active_linter.name]
active_linter.is_active = False
# Manage cases where linters are missing in flavor
if len(missing_linters) > 0:
# Do not warn/stop if missing linters are repository ones (mostly OX.security related)
if not are_all_repository_linters(missing_linters):
missing_linters_str = ",".join(missing_linters)
logging.warning(
f"MegaLinter flavor [{flavor}] does not contain linters {missing_linters_str}.\n"
"As they are not available in this docker image, they will not be processed\n"
"To solve this problem, please either: \n"
f"- use default flavor {ML_REPO}\n"
"- add ignored linters in DISABLE or DISABLE_LINTERS variables in your .mega-linter.yml config file "
"located in your root directory\n"
"- ignore this message by setting config variable FLAVOR_SUGGESTIONS to false"
)
# Stop the process if user wanted so in case of missing linters
if config.get("FAIL_IF_MISSING_LINTER_IN_FLAVOR", "") == "true":
logging.error(
'Missing linter and FAIL_IF_MISSING_LINTER_IN_FLAVOR has been set to "true": Stop run'
)
sys.exit(84)
return False
# All good !
return True
# Ignore linters that shouldn't trigger failure when missing.
if not active_linter.name.startswith("REPOSITORY"):
missing_linters.append(active_linter.name)

if not missing_linters:
return True

missing_linters_str = ",".join(missing_linters)
logging.warning(
f"MegaLinter flavor [{flavor}] does not contain linters {missing_linters_str}.\n"
"As they are not available in this Docker image, they will not be processed\n"
"To solve this problem, please either: \n"
f"- use default flavor {ML_REPO}\n"
"- add ignored linters in DISABLE or DISABLE_LINTERS variables in your .mega-linter.yml config file "
"located in your root directory\n"
"- ignore this message by setting config variable FLAVOR_SUGGESTIONS to false"
)
# Stop the process if user wanted so in case of missing linters
if config.get("FAIL_IF_MISSING_LINTER_IN_FLAVOR", "") == "true":
logging.error(
'Missing linter and FAIL_IF_MISSING_LINTER_IN_FLAVOR has been set to "true": Stop run'
)
sys.exit(84)
return False


# Compare active linters with available flavors to make suggestions to improve CI performances
Expand Down Expand Up @@ -147,13 +147,3 @@ def get_megalinter_flavor_suggestions(active_linters):
)
new_flavor_linters_names = map(lambda linter: linter.name, new_flavor_linters)
return ["new", new_flavor_linters_names]


def are_all_repository_linters(linter_names: list[str]) -> bool:
if len(linter_names) == 0:
return False
result = True
for linter_name in linter_names:
if not linter_name.startswith("REPOSITORY"):
result = False
return result

0 comments on commit 40631ba

Please sign in to comment.