mirror of
https://github.com/dbt-labs/dbt-core
synced 2025-12-20 17:21:27 +00:00
Compare commits
6 Commits
enable-pos
...
add-git-in
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddd4d0d260 | ||
|
|
f7f689f347 | ||
|
|
dae6759133 | ||
|
|
9382fd0394 | ||
|
|
1176ae6535 | ||
|
|
8144749641 |
6
.changes/unreleased/Features-20230925-182237.yaml
Normal file
6
.changes/unreleased/Features-20230925-182237.yaml
Normal 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"
|
||||||
@@ -26,6 +26,8 @@ from dbt.events.types import JinjaLogInfo, JinjaLogDebug
|
|||||||
from dbt.events.contextvars import get_node_info
|
from dbt.events.contextvars import get_node_info
|
||||||
from dbt.version import __version__ as dbt_version
|
from dbt.version import __version__ as dbt_version
|
||||||
|
|
||||||
|
from pygit2 import Repository, GitError # type: ignore
|
||||||
|
|
||||||
# These modules are added to the context. Consider alternative
|
# These modules are added to the context. Consider alternative
|
||||||
# approaches which will extend well to potentially many modules
|
# approaches which will extend well to potentially many modules
|
||||||
import pytz
|
import pytz
|
||||||
@@ -204,6 +206,45 @@ class BaseContext(metaclass=ContextMeta):
|
|||||||
self._ctx.update(builtins)
|
self._ctx.update(builtins)
|
||||||
return self._ctx
|
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()
|
@contextproperty()
|
||||||
def dbt_version(self) -> str:
|
def dbt_version(self) -> str:
|
||||||
"""The `dbt_version` variable returns the installed version of dbt that
|
"""The `dbt_version` variable returns the installed version of dbt that
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ setup(
|
|||||||
"pytz>=2015.7",
|
"pytz>=2015.7",
|
||||||
"pyyaml>=6.0",
|
"pyyaml>=6.0",
|
||||||
"typing-extensions>=3.7.4",
|
"typing-extensions>=3.7.4",
|
||||||
|
"pygit2 >= 1.0.0",
|
||||||
# ----
|
# ----
|
||||||
# Match snowflake-connector-python, to ensure compatibility in dbt-snowflake
|
# Match snowflake-connector-python, to ensure compatibility in dbt-snowflake
|
||||||
"cffi>=1.9,<2.0.0",
|
"cffi>=1.9,<2.0.0",
|
||||||
|
|||||||
39
tests/functional/context_values/test_git.py
Normal file
39
tests/functional/context_values/test_git.py
Normal 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
|
||||||
@@ -188,6 +188,8 @@ REQUIRED_BASE_KEYS = frozenset(
|
|||||||
"print",
|
"print",
|
||||||
"diff_of_two_dicts",
|
"diff_of_two_dicts",
|
||||||
"local_md5",
|
"local_md5",
|
||||||
|
"git_branch",
|
||||||
|
"git_sha",
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user