diff --git a/README.md b/README.md index 5db81f1..20cd417 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ commit_after_release: # define actions which have to happen after release and ou # branch with changes from commit_before_release and commit_after_release merge_request: enabled: True # if this is True merge request will be created + target_branch: hotfix-{VERSION} # optional property (default branch if empty) defining target branch for merge/pull request. Supports regexp title: Releasing version {VERSION} and preparation for next development cycle # you can use string predefined variables description: Hello world! I have just released {VERSION} # optional filed, you can use string predefined variables reviewers: @@ -107,6 +108,30 @@ contains TODO +## 🏴󠁣󠁤󠁥󠁱󠁿 variables + +**Use `{}` to evaluate variable to value f.e. `{FOOBAR}`** + +**hierarchy (from most important):** +- predefined variables +- custom variables +- environment variables + +So, if there is predefined variable, you cannot override it or if same variable exists in environment, +the value always will be as in predefined. If you define your custom variable and the same exists in environment, +the value will be as defined by you. This hierarchy protects valhalla from errors and gives ability to extends +and override values in custom use cases. + +### 🖖 predefined variables + +**Use `{}` to evaluate variable to value f.e. `{VERSION}`** + +| name | description | +|:----------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| +| `VERSION` | value extracted from branch name, for `release-1.2.14` it will be `1.2.14` | +| `VERSION_SLUG` | value extracted from branch name and with everything except 0-9 and a-z replaced with -. No leading / trailing -,
for `release-1.2.14` it will be `1-2-14`. Use in URLs, host names, domain names and file names | +| `VALHALLA_TOKEN` | token passed to CI runner which execute this job | + ### 🏭 custom variables You can define custom variables which can be used by defining them in strings using `{}` @@ -123,17 +148,8 @@ merge_request: **It is really useful with `extends` mechanism, f.e. define general template with `variables` which will be overriden in child `valhalla.yml`.** -### 🖖 predefined variables - -**Use `{}` to evaluate variable to value f.e. `{VERSION}`** - -| name | description | -|:----------------:|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| -| `VERSION` | value extracted from branch name, for `release-1.2.14` it will be `1.2.14` | -| `VERSION_SLUG` | value extracted from branch name and with everything except 0-9 and a-z replaced with -. No leading / trailing -,
for `release-1.2.14` it will be `1-2-14`. Use in URLs, host names, domain names and file names | -| `VALHALLA_TOKEN` | token passed to CI runner which execute this job | -### 🐛 Environment variables +### 🐛 environment variables Valhalla allows you to use any variable defined in your environment system, it is useful f.e when you are using GitLab CI/CD and you want to diff --git a/valhalla/ci_provider/gitlab/merge_request.py b/valhalla/ci_provider/gitlab/merge_request.py index e07968f..30a9ac8 100644 --- a/valhalla/ci_provider/gitlab/merge_request.py +++ b/valhalla/ci_provider/gitlab/merge_request.py @@ -21,18 +21,24 @@ def __init__(self): self.project = self.gl.projects.get(get_project_id(), lazy=True) def create(self, merge_request_config: MergeRequestConfig): - branch = os.environ.get('CI_COMMIT_BRANCH') - default_branch = os.environ.get('CI_DEFAULT_BRANCH') + source_branch = os.environ.get('CI_COMMIT_BRANCH') - info(f"Creating merge request from {branch} to {default_branch}") + if merge_request_config.target_branch: + info("Target branch for merge request:") + target_branch = resolve(merge_request_config.target_branch) + else: + info("target_branch not set, using default instead") + target_branch = os.environ.get('CI_DEFAULT_BRANCH') + + info(f"Creating merge request from {source_branch} to {target_branch}") if not merge_request_config.description: info("merge_request.description not specified, using default") mr = self.project.mergerequests.create( { - 'source_branch': branch, - 'target_branch': default_branch, + 'source_branch': source_branch, + 'target_branch': target_branch, 'title': resolve(merge_request_config.title), 'description': resolve(get_description(merge_request_config.description)), 'remove_source_branch': True, diff --git a/valhalla/common/get_config.py b/valhalla/common/get_config.py index 5f5afe7..10285af 100644 --- a/valhalla/common/get_config.py +++ b/valhalla/common/get_config.py @@ -6,8 +6,9 @@ class MergeRequestConfig: - def __init__(self, enabled: bool, title: str, description: str, reviewers: List[str]): + def __init__(self, enabled: bool, target_branch: str, title: str, description: str, reviewers: List[str]): self.enabled = enabled + self.target_branch = target_branch self.title = title self.description = description self.reviewers = reviewers @@ -16,6 +17,7 @@ def __repr__(self): return f"\n" \ f" MergeRequestConfig( \n" \ f" enabled={self.enabled} \n" \ + f" target_branch={self.target_branch} \n" \ f" title={self.title} \n" \ f" description={self.description} \n" \ f" reviewers={self.reviewers} \n" \ @@ -217,11 +219,13 @@ def get_merge_request_part(merge_request_dict: dict) -> MergeRequestConfig: enabled = str_to_bool(get_from_dict(merge_request_dict, 'enabled', True)) merge_request_other_options_required = enabled + target_branch = get_from_dict(merge_request_dict, 'target_branch', False) + title = get_from_dict(merge_request_dict, 'title', merge_request_other_options_required) description = get_from_dict(merge_request_dict, 'description', False) reviewers = get_from_dict(merge_request_dict, 'reviewers', False) - return MergeRequestConfig(enabled, title, description, reviewers) + return MergeRequestConfig(enabled, target_branch, title, description, reviewers) def str_to_bool(value: str) -> bool: diff --git a/valhalla/common/resolver.py b/valhalla/common/resolver.py index 920b766..6ea9ed1 100644 --- a/valhalla/common/resolver.py +++ b/valhalla/common/resolver.py @@ -32,6 +32,7 @@ def resolve(string: str): error("There is bug in valhalla! Please report it here: https://github.com/logchange/valhalla/issues") exit(1) + # hierarchy string = __resolve_predefined(string) string = __resolve_custom_variables(string) string = __resolve_from_env(string)