Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for commit SHA from tree SHA in all branches #74

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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