Skip to content

Commit a04bd73

Browse files
authored
Allow EXTENDS recursively (#2322)
* Allow EXTENDS recursively * [MegaLinter] Apply linters fixes --------- Co-authored-by: bdovaz <[email protected]>
1 parent e919e6a commit a04bd73

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
EXTENDS: local.mega-linter.yml
2+
DEFAULT_BRANCH: dev
3+
LOG_LEVEL: DEBUG

megalinter/config.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,45 @@ def init_config(workspace=None):
7272
# manage EXTENDS in configuration
7373
if "EXTENDS" in runtime_config:
7474
combined_config = {}
75-
extends = runtime_config["EXTENDS"]
76-
if isinstance(extends, str):
77-
extends = extends.split(",")
78-
for extends_item in extends:
79-
if extends_item.startswith("http"):
80-
r = requests.get(extends_item, allow_redirects=True)
81-
assert (
82-
r.status_code == 200
83-
), f"Unable to retrieve EXTENDS config file {config_file_name}"
84-
extends_config_data = yaml.safe_load(r.content)
85-
else:
86-
with open(
87-
workspace + os.path.sep + extends_item, "r", encoding="utf-8"
88-
) as f:
89-
extends_config_data = yaml.safe_load(f)
90-
combined_config.update(extends_config_data)
91-
CONFIG_SOURCE += f"\n[config] - extends from: {extends_item}"
92-
combined_config.update(runtime_config)
75+
CONFIG_SOURCE = combine_config(
76+
workspace, config_file_name, runtime_config, combined_config, CONFIG_SOURCE
77+
)
9378
runtime_config = combined_config
9479
# Print & set config in cache
9580
print(f"[config] {CONFIG_SOURCE}")
9681
set_config(runtime_config)
9782

9883

84+
def combine_config(workspace, config_file_name, config, combined_config, config_source):
85+
extends = config["EXTENDS"]
86+
if isinstance(extends, str):
87+
extends = extends.split(",")
88+
for extends_item in extends:
89+
if extends_item.startswith("http"):
90+
r = requests.get(extends_item, allow_redirects=True)
91+
assert (
92+
r.status_code == 200
93+
), f"Unable to retrieve EXTENDS config file {config_file_name}"
94+
extends_config_data = yaml.safe_load(r.content)
95+
else:
96+
with open(
97+
workspace + os.path.sep + extends_item, "r", encoding="utf-8"
98+
) as f:
99+
extends_config_data = yaml.safe_load(f)
100+
combined_config.update(extends_config_data)
101+
config_source += f"\n[config] - extends from: {extends_item}"
102+
if "EXTENDS" in extends_config_data:
103+
combine_config(
104+
workspace,
105+
extends_item,
106+
extends_config_data,
107+
combined_config,
108+
config_source,
109+
)
110+
combined_config.update(config)
111+
return config_source
112+
113+
99114
def get_config():
100115
global CONFIG_DATA
101116
if CONFIG_DATA is not None:

megalinter/tests/test_megalinter/config_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ def test_local_config_extends_success(self):
6161
self.assertEqual("(local)", config.get("FILTER_REGEX_INCLUDE"))
6262
self.assertEqual("false", config.get("SHOW_ELAPSED_TIME"))
6363

64+
def test_local_config_extends_recurse_success(self):
65+
local_config = "recurse.mega-linter.yml"
66+
os.environ["MEGALINTER_CONFIG"] = local_config
67+
config.init_config(
68+
REPO_HOME_DEFAULT
69+
+ os.path.sep
70+
+ ".automation"
71+
+ os.path.sep
72+
+ "test"
73+
+ os.path.sep
74+
+ "mega-linter-config-test"
75+
)
76+
self.assertEqual("(local)", config.get("FILTER_REGEX_INCLUDE"))
77+
self.assertEqual("false", config.get("SHOW_ELAPSED_TIME"))
78+
self.assertEqual("dev", config.get("DEFAULT_BRANCH"))
79+
self.assertEqual("DEBUG", config.get("LOG_LEVEL"))
80+
6481
def test_local_config_extends_error(self):
6582
local_config = "local-error.mega-linter.yml"
6683
os.environ["MEGALINTER_CONFIG"] = local_config

0 commit comments

Comments
 (0)