Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add git info to context #8693

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230925-182237.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add git_branch and git_sha to the Jinja context
time: 2023-09-25T18:22:37.142445+02:00
custom:
Author: b-per
Issue: "8690"
41 changes: 41 additions & 0 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from dbt.events.contextvars import get_node_info
from dbt.version import __version__ as dbt_version

from pygit2 import Repository, GitError # type: ignore

# These modules are added to the context. Consider alternative
# approaches which will extend well to potentially many modules
import pytz
Expand Down Expand Up @@ -204,6 +206,45 @@ def to_dict(self) -> Dict[str, Any]:
self._ctx.update(builtins)
return self._ctx

@contextproperty()
def git_branch(self) -> str:
"""The `git_branch` variable returns the current branch name if the
code is version controlled by git.
Otherwise it returns an empty string.
"""

if hasattr(get_flags(), "PROJECT_DIR"):
project_dir = get_flags().PROJECT_DIR
else:
project_dir = "."

try:
branch_name = Repository(project_dir).head.shorthand
except GitError:
branch_name = ""

return branch_name

@contextproperty()
def git_sha(self) -> str:
"""The `git_sha` variable returns the sha of the last commit
if the dbt code is version controlled in git.
Otherwise it returns an empty string.
"""

if hasattr(get_flags(), "PROJECT_DIR"):
project_dir = get_flags().PROJECT_DIR
else:
project_dir = "."

try:
repo = Repository(project_dir)
sha = repo.head.target.hex
except GitError:
sha = ""

return sha

@contextproperty()
def dbt_version(self) -> str:
"""The `dbt_version` variable returns the installed version of dbt that
Expand Down
1 change: 1 addition & 0 deletions core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"pytz>=2015.7",
"pyyaml>=6.0",
"typing-extensions>=3.7.4",
"pygit2 >= 1.0.0",
# ----
# Match snowflake-connector-python, to ensure compatibility in dbt-snowflake
"cffi>=1.9,<2.0.0",
Expand Down
39 changes: 39 additions & 0 deletions tests/functional/context_values/test_git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
import subprocess
import pytest

from dbt.tests.util import run_dbt_and_capture

# we use a macro to print the value and check the logs when testing
on_run_start_macro_assert_git_branch = """
{% macro assert_git_branch_name() %}
{{ log("git branch name: " ~ git_branch, 1) }}
{% endmacro %}
"""


class TestContextGitValues:
@pytest.fixture(scope="class")
def macros(self):
return {
"assert_git_branch_name.sql": on_run_start_macro_assert_git_branch,
}

@pytest.fixture(scope="class")
def project_config_update(self):
return {
"on-run-start": "{{ assert_git_branch_name() }}",
}

def test_git_values(self, project):
os.chdir(project.project_root)
# Initialize a new git repository
subprocess.run(["git", "init"], check=True)
subprocess.run(["git", "config", "user.email", "[email protected]"], check=True)
subprocess.run(["git", "config", "user.name", "dbt Labs"], check=True)
subprocess.run(["git", "checkout", "-b" "new_branch_for_testing"], check=True)
subprocess.run(["git", "add", "*"], check=True)
subprocess.run(["git", "commit", "-m", "commit to git"], check=True)

_, run_logs = run_dbt_and_capture(["run"])
assert "git branch name: new_branch_for_testing" in run_logs
2 changes: 2 additions & 0 deletions tests/unit/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ def assert_has_keys(required_keys: Set[str], maybe_keys: Set[str], ctx: Dict[str
"print",
"diff_of_two_dicts",
"local_md5",
"git_branch",
"git_sha",
}
)

Expand Down
Loading