Пример #1
0
    def get_repo_config(self, branch='master', path_to_file='pyproject.toml'):
        """
        Load configuration from the repository.


        Parameters
        ----------
        branch : `str`
            The branch to read the config file from. (Will default to 'master')

        path_to_file : `str`
            Path to the ``pyproject.toml`` file in the repository. Will default
            to the root of the repository.

        Returns
        -------
        cfg : `baldrick.config.Config`
            Configuration parameters.

        """
        # Also default to 'master' if branch is None
        branch = branch or 'master'
        app_config = current_app.conf.copy()
        fallback_config = Config()
        repo_config = Config()

        try:
            file_content = self.get_file_contents(path_to_file, branch=branch)
        except FileNotFoundError:
            logger.debug(f"No config file found in {self.repo}@{branch}.")
            file_content = None

        if file_content:
            repo_config = loads(file_content,
                                tool=current_app.bot_username) or {}
            logger.trace(
                f"Got the following config from {self.repo}@{branch}: {repo_config}"
            )
            if len(repo_config) == 0:
                logger.exception(
                    f"Failed to load config in {self.repo} on branch {branch}, despite finding a pyproject.toml file."
                )

            if getattr(current_app, "fall_back_config", None):
                fallback_config = loads(
                    file_content, tool=current_app.fall_back_config) or {}
                if len(fallback_config) == 0:
                    logger.trace(
                        f"Didn't find a fallback config in {self.repo}@{branch}."
                    )

        # Priority is 1) repo_config 2) fallback_config 3) app_config
        app_config.update_from_config(fallback_config)
        app_config.update_from_config(repo_config)

        logger.debug(
            f"Got this combined config from {self.repo}@{branch}: {app_config}"
        )

        return app_config
Пример #2
0
def create_app(name, register_blueprints=True):
    """
    Create a flask app based on Baldrick.

    Parameters
    ----------
    name : `str`
        The name to be passed to ``Flask``. This will also be used as the bot
        user name. This can be overridden with ``app.bot_username``.

    register_blueprints : `bool`
        Register the default blueprints included with Baldrick.

    Returns
    -------
    app

    """
    # Setup loguru integration, must be run before import flask.
    import baldrick.logging  # noqa

    from flask import Flask

    try:
        from werkzeug.middleware.proxy_fix import ProxyFix
    except ImportError:
        from werkzeug.contrib.fixers import ProxyFix

    from baldrick.config import load, Config
    from baldrick.blueprints import github_blueprint, circleci_blueprint

    app = Flask(name)

    app.wsgi_app = ProxyFix(app.wsgi_app)

    # Check if there is a global configuration
    app.conf = Config()
    if os.path.exists(GLOBAL_TOML):
        conf = load(GLOBAL_TOML, tool=name)
        if conf:
            app.conf = conf

    app.integration_id = int(os.environ['GITHUB_APP_INTEGRATION_ID'])
    app.private_key = os.environ['GITHUB_APP_PRIVATE_KEY']

    app.bot_username = name

    if register_blueprints:
        app.register_blueprint(github_blueprint)
        app.register_blueprint(circleci_blueprint)

    @app.route("/")
    def index():
        return "Nothing to see here"

    @app.route("/installation_authorized")
    def installation_authorized():
        return "Installation authorized"

    return app
Пример #3
0
def test_copy():
    conf = Config({'a': 1})
    conf_copy = conf.copy()
    assert isinstance(conf_copy, Config)
    assert conf_copy == {'a': 1}