# Path: utils/utils.py
# def base_url_and_api(direct_url):
#     try:
#         base_url = direct_url.rstrip('/') if direct_url else env_base_url()
#     except Exception:
#         print("*** ERROR: You must specify 'direct-url' if not running from Jenkins job", file=sys.stderr)
#         raise
# 
#     if base_url.startswith('http:') or base_url.startswith('https:'):
#         # Using jenkins_api
#         from .. import jenkins_api as api
#     else:
#         # Using script_api
#         from .. import script_api as api
# 
#     return base_url, api


from .utils import base_url_and_api
# Copyright (c) 2012 - 2017 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.



def set_build_description(
        description: str, replace: bool = False, separator: str = '\n',
        username: str = None, password: str = None,
        build_url: str = None, job_name: str = None, build_number: int = None,
        direct_url: str = None):
    """Utility method to set/append build description on a job build.

    If this is used from inside the hudson job you do not have to specify 'build_url' or 'job_name', 'build_number'. You do not have to specify 'direct_url' either
    but may still want to do so if JENKINS_URL points to a proxy, so that rest calls can go directly to Jenkins.
    The 'build_url' is preferred over 'job_name' and 'build_number'.

    Args:
        description: The description to set on the build.
        replace: Replace existing description, if any, instead of appending.
        separator: A separator to insert between any existing description and the new 'description' if 'replace' is not specified.
        username: User Name for Jenkin authentication with secured Jenkins.
        password: Password of Jenkins User.
        build_url: The URL of the jenkins build - preferably non-proxied. Default is os.environ['BUILD_URL'].
        job_name: Name of the job to modify a build on. Default is os.environ['JOB_NAME'].
        build_number: Build Number to modify. . Default is os.environ['BUILD_NUMBER'].
        direct_url: Jenkins URL - preferably non-proxied. If not specified, the value of JENKINS_URL or HUDSON_URL environment variables will be used.
    """


# Path: utils/utils.py
# def base_url_and_api(direct_url):
#     try:
#         base_url = direct_url.rstrip('/') if direct_url else env_base_url()
#     except Exception:
#         print("*** ERROR: You must specify 'direct-url' if not running from Jenkins job", file=sys.stderr)
#         raise
# 
#     if base_url.startswith('http:') or base_url.startswith('https:'):
#         # Using jenkins_api
#         from .. import jenkins_api as api
#     else:
#         # Using script_api
#         from .. import script_api as api
# 
#     return base_url, api


# Path: utils/set_build_description.py
from .utils import base_url_and_api

# Copyright (c) 2012 - 2017 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.



def set_build_description(
        description: str, replace: bool = False, separator: str = '\n',
        username: str = None, password: str = None,
        build_url: str = None, job_name: str = None, build_number: int = None,
        direct_url: str = None):
    """Utility method to set/append build description on a job build.

    If this is used from inside the hudson job you do not have to specify 'build_url' or 'job_name', 'build_number'. You do not have to specify 'direct_url' either
    but may still want to do so if JENKINS_URL points to a proxy, so that rest calls can go directly to Jenkins.
    The 'build_url' is preferred over 'job_name' and 'build_number'.

    Args:
        description: The description to set on the build.
        replace: Replace existing description, if any, instead of appending.
        separator: A separator to insert between any existing description and the new 'description' if 'replace' is not specified.
        username: User Name for Jenkin authentication with secured Jenkins.
        password: Password of Jenkins User.
        build_url: The URL of the jenkins build - preferably non-proxied. Default is os.environ['BUILD_URL'].
        job_name: Name of the job to modify a build on. Default is os.environ['JOB_NAME'].
        build_number: Build Number to modify. . Default is os.environ['BUILD_NUMBER'].
        direct_url: Jenkins URL - preferably non-proxied. If not specified, the value of JENKINS_URL or HUDSON_URL environment variables will be used.
    """


    base_url, api = base_url_and_api(direct_url)