Skip to content

Commit

Permalink
Allow to tag PRE_COMMANDS to run them before loading plugins
Browse files Browse the repository at this point in the history
Fixes #3936
  • Loading branch information
nvuillam committed Aug 29, 2024
1 parent d91dec2 commit 40a0c18
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1122,15 +1122,19 @@ Example in `.mega-linter.yml` config file
```yaml
PRE_COMMANDS:
- command: npm install eslint-plugin-whatever
cwd: "root" # Will be run at the root of MegaLinter docker image
cwd: root # Will be run at the root of MegaLinter docker image
secured_env: true # True by default, but if defined to false, no global variable will be hidden (for example if you need GITHUB_TOKEN)
- command: echo "pre-test command has been called"
cwd: "workspace" # Will be run at the root of the workspace (usually your repository root)
cwd: workspace # Will be run at the root of the workspace (usually your repository root)
continue_if_failed: False # Will stop the process if command is failed (return code > 0)
- command: pip install flake8-cognitive-complexity
venv: flake8 # Will be run within flake8 python virtualenv. There is one virtualenv per python-based linter, with the same name
- command: export MY_OUTPUT_VAR="my output var" && export MY_OUTPUT_VAR2="my output var2"
output_variables: ["MY_OUTPUT_VAR","MY_OUTPUT_VAR2"] # Will collect the values of output variables and update MegaLinter own ENV context
- command: echo "Some command called before loading MegaLinter plugins"
cwd: workspace # Will be run at the root of the workspace (usually your repository root)
continue_if_failed: False # Will stop the process if command is failed (return code > 0)
tag: before_plugins # Tag indicating that the command will be run before loading plugins
```

<!-- config-precommands-section-end -->
Expand Down
5 changes: 4 additions & 1 deletion megalinter/MegaLinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def __init__(self, params=None):
self.flavor_suggestions = None

# Initialize plugins
self.pre_commands_results = pre_post_factory.run_pre_commands(
self, "before_plugins"
)
plugin_factory.initialize_plugins(self.request_id)

# Copy node_modules in current folder if necessary
Expand Down Expand Up @@ -175,7 +178,7 @@ def __init__(self, params=None):
)

# Run user-defined commands
self.pre_commands_results = pre_post_factory.run_pre_commands(self)
self.pre_commands_results += pre_post_factory.run_pre_commands(self)
self.post_commands_results = []
# Initialize linters and gather criteria to browse files
self.load_linters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
"title": "Secured ENV variables",
"type": "boolean"
},
"tag": {
"examples": [
"before_plugins"
],
"title": "Tag defining at which commands entry point the command will be run",
"type": "string"
},
"venv": {
"examples": [
"flake8"
Expand Down
24 changes: 18 additions & 6 deletions megalinter/pre_post_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@


# User defined commands to run before running linters
def run_pre_commands(mega_linter):
return run_pre_post_commands("PRE_COMMANDS", "[Pre]", mega_linter)
def run_pre_commands(mega_linter, tag="default"):
return run_pre_post_commands("PRE_COMMANDS", "[Pre]", mega_linter, tag)


# User defined commands to run after running linters
def run_post_commands(mega_linter):
return run_pre_post_commands("POST_COMMANDS", "[Post]", mega_linter)
def run_post_commands(mega_linter, tag="default"):
return run_pre_post_commands("POST_COMMANDS", "[Post]", mega_linter, tag)


# Commands to run before running all linters in a descriptor
Expand Down Expand Up @@ -52,9 +52,21 @@ def run_linter_post_commands(mega_linter, linter):


# Get commands from configuration
def run_pre_post_commands(key, log_key, mega_linter):
def run_pre_post_commands(key, log_key, mega_linter, tag="default"):
pre_or_post_commands = config.get_list(mega_linter.request_id, key, None)
return run_commands(pre_or_post_commands, log_key, mega_linter)
if pre_or_post_commands is None:
logging.debug(f"{log_key} No commands declared in user configuration")
return []
# Filter commands according to tag
applicable_pre_post_commands = []
for command in pre_or_post_commands:
if (
tag == "default"
and (("tag" not in command) or (command["tag"] == "default"))
) or (tag != "default" and "tag" in command and command["tag"] == tag):
applicable_pre_post_commands += [command]
# Run matching commands
return run_commands(applicable_pre_post_commands, log_key, mega_linter)


# Perform run of commands
Expand Down

0 comments on commit 40a0c18

Please sign in to comment.