-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to extend valhalla.yml configs and keep global configur…
…ation
- Loading branch information
1 parent
00cb8b0
commit a3b6370
Showing
8 changed files
with
183 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
|
||
from valhalla.extends.merge_dicts import merge | ||
|
||
|
||
class MergeDictsTest(unittest.TestCase): | ||
|
||
def test_merge_dicts(self): | ||
parent = { | ||
"commit_before_release": { | ||
"msg": "ABC Releasing version {VERSION}", | ||
"before": [ | ||
"parent element 1", | ||
"parent element 2" | ||
], | ||
}, | ||
"merge_request": { | ||
"title": "parent title", | ||
"description": "parent description", | ||
}, | ||
} | ||
|
||
child = { | ||
"extends": [ | ||
"https://raw.githubusercontent.com/logchange/valhalla/master/valhalla-extends.yml" | ||
], | ||
"git_host": "gitlab", | ||
"commit_before_release": { | ||
"enabled": True, | ||
"username": "Test1234", | ||
"email": "test-valhalla@logchange.dev", | ||
"msg": "Releasing version {VERSION}", | ||
"before": [ | ||
"child element 1", | ||
"child element 2", | ||
"child element 3" | ||
], | ||
}, | ||
"release": { | ||
"description": { | ||
"from_command": "cat changelog / v{VERSION} / version_summary.md" | ||
}, | ||
"assets": { | ||
"links": [ | ||
{ | ||
"name": "Documentation", | ||
"url": "https: // google.com / q?={VERSION}", | ||
"link_type": "other", | ||
}, | ||
{ | ||
"name": "Docker Image", | ||
"url": "https://dockerhub.com/q?={VERSION}", | ||
"link_type": "image", | ||
}, | ||
] | ||
}, | ||
}, | ||
"commit_after_release": { | ||
"enabled": True, | ||
"username": "Test1234", | ||
"email": "test-valhalla@logchange.dev", | ||
"msg": "Preparation for next development cycle", | ||
"before": ['echo "test" > prepare_next_iteration.md'], | ||
}, | ||
"merge_request": { | ||
"enabled": True, | ||
"title": "child title", | ||
"reviewers": ["peter.zmilczak", "some_uknownwnnaa"], | ||
}, | ||
} | ||
|
||
result = merge(parent, child) | ||
|
||
self.assertEqual(result['extends'], | ||
['https://raw.githubusercontent.com/logchange/valhalla/master/valhalla-extends.yml']) | ||
self.assertEqual(result['git_host'], 'gitlab') | ||
|
||
self.assertEqual(result['commit_before_release']['before'], [ | ||
"child element 1", | ||
"child element 2", | ||
"child element 3" | ||
]) | ||
|
||
self.assertEqual(result['merge_request']['title'], 'child title') | ||
self.assertEqual(result['merge_request']['description'], 'parent description') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import List | ||
|
||
import urllib.request | ||
from yaml import safe_load | ||
|
||
from valhalla.common.logger import info, error | ||
from valhalla.extends.merge_dicts import merge | ||
|
||
|
||
def get_from_url(url): | ||
result = "" | ||
data = urllib.request.urlopen(url) | ||
for line in data: | ||
str_line = line.decode("UTF-8") | ||
result += str_line | ||
info("Loaded from ULR") | ||
info("===========================================") | ||
print(result) | ||
info("===========================================") | ||
return result | ||
|
||
|
||
class ValhallaExtends: | ||
|
||
def __init__(self, extends: List[str]): | ||
self.extends = extends | ||
|
||
def merge(self, valhalla_yml_dict: dict) -> dict: | ||
if self.extends is None or len(self.extends) < 1: | ||
info("There is nothing to extend") | ||
return valhalla_yml_dict | ||
elif len(self.extends) == 1: | ||
info("There is one file to extend") | ||
extended = get_from_url(self.extends[0]) | ||
extended_dict = safe_load(extended) | ||
info("yml data as dictionary to extends: " + str(extended_dict)) | ||
info("yml data from valhalla.yml: " + str(valhalla_yml_dict)) | ||
result = merge(extended_dict, valhalla_yml_dict) | ||
info("final yml data: " + str(result)) | ||
return result | ||
else: | ||
error("Currently you can extend only from one url!") | ||
exit(1) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import copy | ||
|
||
|
||
def merge(parent: dict, child: dict): | ||
parent_copy = copy.deepcopy(parent) | ||
child_copy = copy.deepcopy(child) | ||
|
||
return __merge(parent_copy, child_copy) | ||
|
||
|
||
def __merge(parent_org: dict, child_org: dict): | ||
parent_copy = copy.deepcopy(parent_org) | ||
child_copy = copy.deepcopy(child_org) | ||
|
||
if isinstance(child_copy, dict): | ||
for k, v in child_copy.items(): | ||
if k in parent_copy: | ||
parent_copy[k] = __merge(parent_copy.get(k), v) | ||
else: | ||
parent_copy[k] = v | ||
return parent_copy | ||
else: | ||
return child_copy |