Beispiel #1
0
def tags(env=None, cwd=None):
    # type: (Optional[MutableMapping[str, str]], Optional[str]) -> Dict[str, str]
    """Extract and set tags from provider environ, as well as git metadata."""
    env = os.environ if env is None else env
    tags = {}  # type: Dict[str, Optional[str]]
    for key, extract in PROVIDERS:
        if key in env:
            tags = extract(env)
            break

    git_info = git.extract_git_metadata(cwd=cwd)
    try:
        git_info[WORKSPACE_PATH] = git.extract_workspace_path(cwd=cwd)
    except git.GitNotFoundError:
        log.error("Git executable not found, cannot extract git metadata.")
    except ValueError:
        log.error(
            "Error extracting git metadata, received non-zero return code.",
            exc_info=True)

    # Tags collected from CI provider take precedence over extracted git metadata, but any CI provider value
    # is None or "" should be overwritten.
    tags.update({k: v for k, v in git_info.items() if not tags.get(k)})

    user_specified_git_info = git.extract_user_git_metadata(env)

    # Tags provided by the user take precedence over everything
    tags.update({k: v for k, v in user_specified_git_info.items() if v})

    tags[git.TAG] = _normalize_ref(tags.get(git.TAG))
    if tags.get(git.TAG) and git.BRANCH in tags:
        del tags[git.BRANCH]
    tags[git.BRANCH] = _normalize_ref(tags.get(git.BRANCH))
    tags[git.REPOSITORY_URL] = _filter_sensitive_info(
        tags.get(git.REPOSITORY_URL))

    workspace_path = tags.get(WORKSPACE_PATH)
    if workspace_path:
        tags[WORKSPACE_PATH] = os.path.expanduser(workspace_path)

    tags.update(_get_runtime_and_os_metadata())

    return {k: v for k, v in tags.items() if v is not None}
Beispiel #2
0
def test_git_extract_workspace_path_error(tmpdir):
    """On error, workspace path should not be extracted, and should internally raise an error."""
    with pytest.raises(ValueError):
        git.extract_workspace_path(cwd=str(tmpdir))
Beispiel #3
0
def test_git_extract_workspace_path(git_repo):
    """Make sure that workspace path is correctly extracted."""
    assert git.extract_workspace_path(cwd=git_repo) == git_repo