Skip to content

Commit

Permalink
Add GITHUB_BRANCH_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
ewjoachim committed Sep 3, 2023
1 parent 91a72f5 commit bb97e47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions coverage_comment/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down

0 comments on commit bb97e47

Please sign in to comment.