Skip to content

Commit

Permalink
Look for tree SHA in all branches
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-dG committed Feb 27, 2020
1 parent 9cb51e4 commit d458c91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
25 changes: 21 additions & 4 deletions tagbot/action/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,32 @@ def _create_release_branch_pr(self, version: str, branch: str) -> None:
base=self._repo.default_branch,
)

def _commit_sha_of_tree(self, tree: str) -> Optional[str]:
"""Look up the commit SHA of a tree with the given SHA."""
since = datetime.now() - self._lookback
for commit in self._repo.get_commits(since=since):
def _commit_sha_of_tree_from_branch(
self, branch: str, tree: str, since: datetime
) -> Optional[str]:
"""Look up the commit SHA of a tree with the given SHA on one branch."""
for commit in self._repo.get_commits(sha=branch, since=since):
if commit.commit.tree.sha == tree:
# TODO: Remove the string conversion when PyGithub is typed.
return str(commit.sha)
return None

def _commit_sha_of_tree(self, tree: str) -> Optional[str]:
"""Look up the commit SHA of a tree with the given SHA."""
since = datetime.now() - self._lookback
sha = self._commit_sha_of_tree_from_branch(
self._repo.default_branch, tree, since
)
if sha:
return sha
for branch in self._repo.get_branches():
if branch.name == self._repo.default_branch:
continue
sha = self._commit_sha_of_tree_from_branch(branch.name, tree, since)
if sha:
return sha
return None

def _commit_sha_of_tag(self, version: str) -> Optional[str]:
"""Look up the commit SHA of a given tag."""
try:
Expand Down
27 changes: 21 additions & 6 deletions test/action/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,31 @@ def test_create_release_branch_pr():
)


def test_commit_sha_of_tree():
def test_commit_sha_of_tree_from_branch():
r = _repo()
since = datetime.now()
r._repo.get_commits = Mock(return_value=[Mock(sha="abc"), Mock(sha="sha")])
r._repo.get_commits.return_value[1].commit.tree.sha = "tree"
assert r._commit_sha_of_tree("tree") == "sha"
assert r._commit_sha_of_tree_from_branch("master", "tree", since) == "sha"
r._repo.get_commits.assert_called_with(sha="master", since=since)
r._repo.get_commits.return_value.pop()
assert r._commit_sha_of_tree("tree") is None
since = r._repo.get_commits.mock_calls[0].kwargs["since"]
expected = datetime.now() - timedelta(days=3, hours=1)
assert expected - since < timedelta(seconds=5)
assert r._commit_sha_of_tree_from_branch("master", "tree", since) is None


def test_commit_sha_of_tree():
r = _repo()
now = datetime.now()
r._repo = Mock(default_branch="master",)
branches = r._repo.get_branches.return_value = [Mock(), Mock()]
branches[0].name = "foo"
branches[1].name = "master"
r._lookback = Mock(__rsub__=lambda x, y: now)
r._commit_sha_of_tree_from_branch = Mock(side_effect=["sha1", None, "sha2"])
assert r._commit_sha_of_tree("tree") == "sha1"
r._repo.get_branches.assert_not_called()
r._commit_sha_of_tree_from_branch.assert_called_once_with("master", "tree", now)
assert r._commit_sha_of_tree("tree") == "sha2"
r._commit_sha_of_tree_from_branch.assert_called_with("foo", "tree", now)


def test_commit_sha_of_tag():
Expand Down

0 comments on commit d458c91

Please sign in to comment.