Compare commits

...

6 Commits

Author SHA1 Message Date
Benoit Perigaud
ddd4d0d260 Update test to remove CI errors 2023-09-25 19:04:26 +02:00
Benoit Perigaud
f7f689f347 Changie entry 2023-09-25 18:22:52 +02:00
Benoit Perigaud
dae6759133 Add functional test 2023-09-25 18:21:05 +02:00
Benoit Perigaud
9382fd0394 Add extra context fields for git 2023-09-25 18:04:45 +02:00
Benoit Perigaud
1176ae6535 Handle project_dir config 2023-09-25 18:04:13 +02:00
Benoit Perigaud
8144749641 Add git info to context 2023-09-22 18:08:02 +02:00
5 changed files with 89 additions and 0 deletions

View File

@@ -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"

View File

@@ -26,6 +26,8 @@ from dbt.events.types import JinjaLogInfo, JinjaLogDebug
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
@@ -204,6 +206,45 @@ class BaseContext(metaclass=ContextMeta):
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

View File

@@ -83,6 +83,7 @@ setup(
"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",

View File

@@ -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", "no-mail@dbtlabs.com"], 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

View File

@@ -188,6 +188,8 @@ REQUIRED_BASE_KEYS = frozenset(
"print",
"diff_of_two_dicts",
"local_md5",
"git_branch",
"git_sha",
}
)