Skip to content

Commit

Permalink
Add ability to setup config through env (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarmet committed Jan 9, 2023
1 parent cc84ca0 commit 398f4d1
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ __pycache__
bert_e.egg-info
.tox/
settings.yml
.coverage
.pytest_cache/
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [3.7.0] - 2022-08-08
# Added
- Support config settings through environment.

## [3.6.22] - 2022-08-08
# Fixed
- Fixed configuration for Webhook authentication.
Expand Down
55 changes: 51 additions & 4 deletions bert_e/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from os.path import exists
from os.path import exists, join

import yaml
import logging
import os
from marshmallow import Schema, fields, post_load, pre_load

from bert_e.exceptions import (IncorrectSettingsFile, MalformedSettings,
Expand Down Expand Up @@ -125,14 +126,15 @@ class SettingsSchema(Schema):
frontend_url = fields.Str(missing='')

repository_owner = fields.Str(required=True)
repository_slug = fields.Str(required=True)
repository_slug = fields.Str(required=False, missing=None)

repository_host = fields.Str(required=True)

robot = fields.Nested(UserSettingSchema, required=True)
robot_email = fields.Str(required=True)

pull_request_base_url = fields.Str(required=True)
commit_base_url = fields.Str(required=True)
pull_request_base_url = fields.Str(required=False)
commit_base_url = fields.Str(required=False)

build_key = fields.Str(missing="pre-merge")

Expand Down Expand Up @@ -175,6 +177,51 @@ class SettingsSchema(Schema):
use_queues = fields.Bool(missing=True)
cmd_line_options = fields.List(fields.Str(), missing=[])

@pre_load
def load_env(self, data):
"""Load environment variables"""
for key, value in os.environ.items():
if key.startswith('BERT_E_'):
data[key[7:].lower()] = value

return data

@post_load
def base_url(self, data):
"""Add base urls for repository, commits and pull requests."""

if "repository_host_url" not in data:
hosts = {
'bitbucket': 'https://bitbucket.org',
'github': 'https://github.com',
'mock': 'https://bitbucket.org',
}
if data["repository_host"] in hosts:
data["repository_host_url"] = hosts[data["repository_host"]]
else:
raise IncorrectSettingsFile(
f'Unknown repository host: {data["repository_host"]}'
)

if "pull_request_base_url" not in data:

data["pull_request_base_url"] = join(
data["repository_host_url"],
data["repository_owner"],
data["repository_slug"],
"pull/{pr_id}"
)

if "commit_base_url" not in data:
data["commit_base_url"] = join(
data["repository_host_url"],
data["repository_owner"],
data["repository_slug"],
"commits/{commit_id}"
)

return data

@post_load
def mk_settings(self, data):
return SettingsDict(data)
Expand Down
46 changes: 46 additions & 0 deletions bert_e/tests/assets/settings-env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
frontend_url: https://bert-e.mydomain.com/bitbucket/my_company/my_repository/bert-e/

jira_keys:
- MYJIRAPROJECTKEY

prefixes:
Story: feature
Bug: bugfix
Improvement: improvement
Epic: epic

bypass_prefixes:
- documentation
- dependabot

disable_version_checks: true

pr_author_options:
username:
- bypass_author_approval
- bypass_jira_check
- bypass_build_status
- bypass_incompatible_branch
- bypass_peer_approval
- bypass_leader_approval

admins:
- username_admin_1
- username_admin_2@557042:08898ca4-5f12-4042-9942-87e167728afd

tasks:
- do this
- do that

max_commit_diff: 100

sentry_dsn: ''

always_create_integration_pull_requests: true

required_leader_approvals: 1

project_leaders:
- username_leader_1
- username_leader_2@557042:66898ca4-5f12-4042-9942-87e167728afd
29 changes: 29 additions & 0 deletions bert_e/tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

"""Unit tests fixtures."""
import os
from os.path import abspath
import pytest

from bert_e.settings import setup_settings


@pytest.fixture
def settings():
"""Simple settings fixture."""
return setup_settings(os.path.abspath('settings.sample.yml'))


@pytest.fixture
def settings_env():
"""Simple settings fixture."""
os.environ['BERT_E_REPOSITORY_HOST'] = 'github'
os.environ['BERT_E_REPOSITORY_OWNER'] = 'scality'
os.environ['BERT_E_REPOSITORY_SLUG'] = 'bert-e'
os.environ['BERT_E_ROBOT'] = 'robot_username'
os.environ['BERT_E_ROBOT_EMAIL'] = '[email protected]'
os.environ['BERT_E_BUILD_KEY'] = 'github_actions'
os.environ['BERT_E_REQUIRED_PEER_APPROVALS'] = '2'
os.environ['BERT_E_NEED_AUTHOR_APPROVAL'] = 'true'
os.environ['BERT_E_JIRA_ACCOUNT_URL'] = 'https://my_account.atlassian.net'
os.environ['BERT_E_JIRA_EMAIL'] = '[email protected]'
return setup_settings(abspath('bert_e/tests/assets/settings-env.yml'))
44 changes: 36 additions & 8 deletions bert_e/tests/unit/test_settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"""Unit tests for settings.py."""

import pytest
import logging
import os.path
from bert_e.settings import BertEContextFilter, setup_settings
import os
from distutils.util import strtobool


@pytest.fixture
def settings():
"""Simple settings fixture."""
return setup_settings(os.path.abspath('settings.sample.yml'))
from bert_e.settings import BertEContextFilter


def test_log_filter(settings):
Expand All @@ -19,3 +15,35 @@ def test_log_filter(settings):
logger.addFilter(log_filter)
logger.info('hello')
assert len(logger.filters) == 1


def test_env_settings(settings_env):
"""Setup BERT_E_REPOSITORY_HOST as github and check that it is used."""

for key, value in os.environ.items():
if key.startswith('BERT_E_'):
print(key)
config_value = settings_env[key[7:].lower()]
if isinstance(config_value, bool):
assert config_value == strtobool(value)
elif isinstance(config_value, int):
assert config_value == int(value)
else:
assert config_value == value


def test_repository_host_url(settings_env):
"""Test that repository_host_url is set correctly."""
assert settings_env['repository_host_url'] == 'https://github.com'


def test_pull_request_base_url(settings_env):
"""Test that pull_request_base_url is set correctly."""
assert settings_env['pull_request_base_url'] == \
'https://github.com/scality/bert-e/pull/{pr_id}'


def test_commit_base_url(settings_env):
"""Test that commit_base_url is set correctly."""
assert settings_env['commit_base_url'] == \
'https://github.com/scality/bert-e/commits/{commit_id}'
12 changes: 0 additions & 12 deletions settings.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ robot: robot_username
robot_email: [email protected]


# pull_request_base_url [MANDATORY]:
# The PR base url to link in status page
#
pull_request_base_url: https://bitbucket.org/foo/bar/pull-requests/{pr_id}


# commit_base_url [MANDATORY]:
# The commit base url to link in status page
#
commit_base_url: https://bitbucket.org/foo/bar/commits/{commit_id}


# build_key [OPTIONAL]:
# The label of the key to look for in githost commit statuses.
# The key "github_actions" can be set to check github actions status instead of external app checks
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ commands = flake8 bert_e/
[testenv:utests]
deps =
pip==20.0.1
commands = coverage run -am py.test -v bert_e/tests/unit/
commands = pytest bert_e/tests/unit/ {posargs}

[testenv:tests-api-mock]
deps =
Expand Down

0 comments on commit 398f4d1

Please sign in to comment.