A
u
Zine El Abidine Moualhi, Etienne Deneuve
Nov 15, 2023 · 4 min read

Demystifying Clean Code: Achieving Code Quality and Efficiency with MegaLinter

Side view worker wearing gloves

Unlocking Development Excellence for some might be a far-fetched dream, but what if there’s a way to at least ensure the quality of your code? Linters are tools that analyze code for potential errors and style violations whether in your local machine or in a CI pipeline and more benefits such as:

  • Bugs be gone! Use linters to identify and fix potential errors and style violations.
  • Level up your coding skills! Linters can help you to learn about best practices and improve your coding style to ease understandability and maintainability of code.
  • Make your code more stylish! Linters can help you enforce a consistent coding style across your project for increased readability.

What’s megalinter ?

MegaLinter is a free and open-source tool for analyzing code for errors and style violations. It supports a wide range of programming languages and can be run locally or on a continuous integration (CI) server. MegaLinter acts as a centralized linter, bringing together several linters and static analysis tools into a single framework to analyze code for potential mistakes and style violations. MegaLinter works with a variety of linters, including ESLint, Prettier, and many other language linters.

MegaLinter Setup

MegaLinter has a command-line installation that allows you to automatically construct configuration and CI task files, allowing you to execute code analysis at every pull request or even every time a new commit is made to a repository (requires Node.js and docker for local).

To run megalinter against your Repository, you can either the analysis locally or through CI:

Local analysis

Quite simple this one, you just run npx mega-linter-runner --flavor <megalinter-flavor>

  • ah yes megalinter has different flavors incase you dont want to run all linters against your codebase, such as Terraform, Security, Salesforce, Java and much more.

CI Analysis

I’ll be using a gitlab example (you can check other examples in Installation)

  1. You can start by generating a template with npx mega-linter-runner --install
mega-linter:
stage: test
# You can override MegaLinter flavor used to have faster performances
# More info at https://oxsecurity.github.io/megalinter/flavors/
image: oxsecurity/megalinter-terraform:v7.3.0
script: ['true'] # if script: ["true"] does not work, you may try -> script: [ "/bin/bash /entrypoint.sh" ]
variables:
# All available variables are described in documentation
# https://oxsecurity.github.io/megalinter/configuration/
DEFAULT_WORKSPACE: $CI_PROJECT_DIR
GITLAB_ACCESS_TOKEN_MEGALINTER: $GITLAB_ACCESS_TOKEN_MEGALINTER
# ADD YOUR CUSTOM ENV VARIABLES HERE TO OVERRIDE VALUES OF .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
artifacts:
when: always
paths:
- report
expire_in: 1 week
  1. Your first run might fail and display errors on your pull-request (merge-request in gitlab)

But you don’t have to correct all the flagged issues instead, you can choose between:

  • Solve the linter error.
  • Configure the linter to ignore rules on some linters (like in cspell with cspell.json config file).
  • Exclude some files from the linting process.
  • Disable the linter.
  • Define the error as non-blocking.

You can check the documentation for more info on how to configure megalinter for customized analysis and even to apply automatic fixes (fixes are not destructive you can find the fixes in separate directory generated by mega-linter) and You’re all set !

Advanced usage

MegaLinter is highly configurable, and there’s much more to check out here Megalinter by OX-Security and if the list of linters available isn’t enough for you, you can create your own descriptions and load them as plugins while MegaLinter is running:

descriptor_id: <descriptor_id>
descriptor_type: <descriptor_type>
descriptor_flavors:
- all_flavors # Applicable to CI in any language project
- <flavor_you_are_using>
file_extensions:
- '.<file_extension>'
linters:
- linter_name: <linter_name>
is_formatter: # true or false depending on whether the tool can apply fixes to the code
name: <descriptor_id>__<linter_name> # in caps
linter_repo: <linter_repo?>
linter_url: <linter_website?>
cli_lint_fix_remove_args:
- '<command_used_to_start_analysis>'
cli_lint_mode: # list_of_files or project
cli_lint_extra_args:
- <tool_extra_arguments_for_analysis> # such as debug flags
linter_text: |
linter description
examples:
- 'example of command'
install:
dockerfile:
- |
  • The descriptor format is identical to that of MegaLinter embedded ones (see json schema documentation).
  • Plugin descriptor files must be titled **.megalinter-descriptor.yml
  • Plugins must in a directory or a URL that begins with /mega-linter-plugin-/.

References

  1. https://megalinter.io/latest/install-gitlab/
  2. https://megalinter.io/latest/config-file/
Security Linters DevSecOps Megalinter

Related articles