Skip to content

Commit

Permalink
Merge pull request #11 from logchange/mr-target-branch
Browse files Browse the repository at this point in the history
Added target_branch for merge requests to allow creating custom scena…
  • Loading branch information
marwin1991 authored May 18, 2024
2 parents d8f7132 + ce4cb09 commit d7262f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 -, <br/>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 `{}`
Expand All @@ -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 -, <br/>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
Expand Down
16 changes: 11 additions & 5 deletions valhalla/ci_provider/gitlab/merge_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions valhalla/common/get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" \
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions valhalla/common/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d7262f5

Please sign in to comment.