diff --git a/commit_check/util.py b/commit_check/util.py index ac813c0..1b4c1e7 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -5,6 +5,7 @@ A module containing utility functions. """ +import os import subprocess import yaml from pathlib import Path, PurePath @@ -52,9 +53,15 @@ def get_branch_name() -> str: try: # Git 2.22 and above supports `git branch --show-current` commands = ["git", "branch", "--show-current"] - branch_name = cmd_output(commands) or "HEAD" + branch_name = cmd_output(commands) except CalledProcessError: branch_name = "" + + if not branch_name: + # Fallback to environment variables (GitHub Actions) + branch_name = ( + os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF_NAME") or "HEAD" + ) return branch_name.strip() diff --git a/tests/util_test.py b/tests/util_test.py index fda3e89..4e34dfc 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -27,19 +27,56 @@ def test_get_branch_name(self, mocker): @pytest.mark.benchmark def test_get_branch_name_with_exception(self, mocker): - # Must return empty string when exception raises in cmd_output. - m_cmd_output = mocker.patch( - "commit_check.util.cmd_output", return_value=" fake_branch_name " - ) - # CalledProcessError's args also dummy - dummy_ret_code, dummy_cmd_name = 1, "dcmd" - m_cmd_output.side_effect = CalledProcessError( - dummy_ret_code, dummy_cmd_name + mock_cmd_output = mocker.patch( + "commit_check.util.cmd_output", + side_effect=CalledProcessError( + returncode=1, cmd="git branch --show-current" + ), ) retval = get_branch_name() - assert m_cmd_output.call_count == 1 - assert m_cmd_output.call_args[0][0] == ["git", "branch", "--show-current"] - assert retval == "" + assert mock_cmd_output.call_count == 1 + mock_cmd_output.assert_called_once_with(["git", "branch", "--show-current"]) + assert retval == "HEAD" or retval # depending on env vars + + @pytest.mark.benchmark + def test_get_branch_name_fallback_github_head_ref(self, mocker): + """Test fallback to GITHUB_HEAD_REF.""" + mocker.patch("commit_check.util.cmd_output", return_value="") + mocker.patch( + "commit_check.util.os.getenv", + lambda key: "feature-branch" if key == "GITHUB_HEAD_REF" else None, + ) + assert get_branch_name() == "feature-branch" + + @pytest.mark.benchmark + def test_get_branch_name_fallback_github_ref_name(self, mocker): + """Test fallback to GITHUB_REF_NAME.""" + mocker.patch("commit_check.util.cmd_output", return_value="") + mocker.patch( + "commit_check.util.os.getenv", + lambda key: "develop" if key == "GITHUB_REF_NAME" else None, + ) + assert get_branch_name() == "develop" + + @pytest.mark.benchmark + def test_get_branch_name_fallback_head(self, mocker): + """Test fallback to HEAD.""" + mocker.patch("commit_check.util.cmd_output", return_value="") + mocker.patch("commit_check.util.os.getenv", return_value=None) + assert get_branch_name() == "HEAD" + + @pytest.mark.benchmark + def test_get_branch_name_fallback_priority(self, mocker): + """Test fallback priority.""" + mocker.patch("commit_check.util.cmd_output", return_value="") + mocker.patch( + "commit_check.util.os.getenv", + lambda key: { + "GITHUB_HEAD_REF": "feature-branch", + "GITHUB_REF_NAME": "develop", + }.get(key), + ) + assert get_branch_name() == "feature-branch" class TestHasCommits: @pytest.mark.benchmark