Example #1
0
def test_git_executable_not_found_error(git_repo_empty):
    """If git executable not available, should raise internally, log, and not extract any tags."""
    with mock.patch("ddtrace.ext.ci.git.log") as log:
        with mock.patch("ddtrace.ext.ci.git.subprocess.Popen") as mock_popen:
            if six.PY2:
                mock_popen.side_effect = OSError()
            else:
                mock_popen.side_effect = FileNotFoundError()
            extracted_tags = git.extract_git_metadata(cwd=git_repo_empty)
        log.error.assert_called_with("Git executable not found, cannot extract git metadata.")

    assert extracted_tags == {}
Example #2
0
def test_extract_git_metadata(git_repo):
    """Test that extract_git_metadata() sets all tags correctly."""
    expected_tags = {
        git.REPOSITORY_URL: "[email protected]:test-repo-url.git",
        git.COMMIT_MESSAGE: "this is a commit msg",
        git.COMMIT_AUTHOR_NAME: "John Doe",
        git.COMMIT_AUTHOR_EMAIL: "*****@*****.**",
        git.COMMIT_AUTHOR_DATE: "2021-01-19T09:24:53-0400",
        git.COMMIT_COMMITTER_NAME: "Jane Doe",
        git.COMMIT_COMMITTER_EMAIL: "*****@*****.**",
        git.COMMIT_COMMITTER_DATE: "2021-01-20T04:37:21-0400",
    }
    assert git.extract_git_metadata(cwd=git_repo) == expected_tags
Example #3
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}
Example #4
0
def test_extract_git_metadata(git_repo):
    """Test that extract_git_metadata() sets all tags correctly."""
    extracted_tags = git.extract_git_metadata(cwd=git_repo)

    assert extracted_tags[
        "git.repository_url"] == "[email protected]:test-repo-url.git"
    assert extracted_tags["git.commit.message"] == "this is a commit msg"
    assert extracted_tags["git.commit.author.name"] == "John Doe"
    assert extracted_tags["git.commit.author.email"] == "*****@*****.**"
    assert extracted_tags[
        "git.commit.author.date"] == "2021-01-19T09:24:53-0400"
    assert extracted_tags["git.commit.committer.name"] == "Jane Doe"
    assert extracted_tags["git.commit.committer.email"] == "*****@*****.**"
    assert extracted_tags[
        "git.commit.committer.date"] == "2021-01-20T04:37:21-0400"
    assert extracted_tags["git.branch"] == "master"
    assert extracted_tags.get(
        "git.commit.sha"
    ) is not None  # Commit hash will always vary, just ensure a value is set