diff --git a/coverage_comment/settings.py b/coverage_comment/settings.py index 4610d891..21a55a8b 100644 --- a/coverage_comment/settings.py +++ b/coverage_comment/settings.py @@ -125,6 +125,13 @@ def GITHUB_PR_NUMBER(self) -> int | None: return int(self.GITHUB_REF.split("/")[2]) return None + @property + def GITHUB_BRANCH_NAME(self) -> str | None: + # "refs/head/my_branch_name" + if "heads" in self.GITHUB_REF: + return self.GITHUB_REF.split("/", 2)[2] + return None + # We need to type environ as a MutableMapping because that's what # os.environ is, and just saying `dict[str, str]` is not enough to make # mypy happy diff --git a/tests/unit/test_settings.py b/tests/unit/test_settings.py index edab1716..d45f6ba9 100644 --- a/tests/unit/test_settings.py +++ b/tests/unit/test_settings.py @@ -130,6 +130,17 @@ def test_config__GITHUB_PR_NUMBER(config, github_ref, github_pr_number): assert config(GITHUB_REF=github_ref).GITHUB_PR_NUMBER == github_pr_number +@pytest.mark.parametrize( + "github_ref, github_branch_name", + [ + ("refs/pull/2/merge", None), + ("refs/heads/a/b", "a/b"), + ], +) +def test_config__GITHUB_BRANCH_NAME(config, github_ref, github_branch_name): + assert config(GITHUB_REF=github_ref).GITHUB_BRANCH_NAME == github_branch_name + + def test_config__from_environ__error(): with pytest.raises(ValueError): settings.Config.from_environ({"COMMENT_FILENAME": "/a"})