Skip to content

Commit

Permalink
fix: clone repositories with no associated target files
Browse files Browse the repository at this point in the history
  • Loading branch information
renatav committed Sep 19, 2024
1 parent 0588d0b commit c03cc4b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 27 deletions.
12 changes: 12 additions & 0 deletions taf/tests/test_updater/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ def add_unauthenticated_commits_to_all_target_repos(target_repos: list):
update_target_files(target_repo, "Update target files")


def add_unauthenticated_commit_to_target_repo(target_repos: list, target_name: str):
for target_repo in target_repos:
if target_name in target_repo.name:
update_target_files(target_repo, "Update target files")


def add_unauthenticated_commits_to_target_repo(target_repos: list):
for target_repo in target_repos:
update_target_files(target_repo, "Update target files")
Expand Down Expand Up @@ -650,11 +656,17 @@ def update_and_sign_metadata_without_clean_check(
def update_target_files(target_repo: GitRepository, commit_message: str):
text_to_add = _generate_random_text()
# Iterate over all files in the repository directory
is_empty = True
for file_path in target_repo.path.iterdir():
if file_path.is_file():
is_empty = False
existing_content = file_path.read_text(encoding="utf-8")
new_content = existing_content + "\n" + text_to_add
file_path.write_text(new_content, encoding="utf-8")

if is_empty:
random_text = _generate_random_text()
(target_repo.path / "test.txt").write_text(random_text)
target_repo.commit(commit_message)


Expand Down
39 changes: 32 additions & 7 deletions taf/tests/test_updater/test_clone/test_clone_valid.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from taf.tests.test_updater.conftest import (
SetupManager,
add_unauthenticated_commit_to_target_repo,
add_unauthenticated_commits_to_all_target_repos,
add_valid_target_commits,
add_valid_unauthenticated_commits,
Expand All @@ -12,8 +13,8 @@
)
from taf.tests.test_updater.update_utils import (
clone_client_target_repos_without_updater,
load_target_repositories,
update_and_check_commit_shas,
verify_repos_eixsts,
)
from taf.updater.types.update import OperationType, UpdateType

Expand Down Expand Up @@ -339,9 +340,33 @@ def test_clone_when_target_empty(origin_auth_repo, client_dir):
client_dir,
expected_repo_type=UpdateType.EITHER,
)
client_repos = load_target_repositories(origin_auth_repo, client_dir)
for name, repo in client_repos.items():
if "notempty" in name:
assert repo.path.is_dir()
else:
assert not repo.path.is_dir()
verify_repos_eixsts(client_dir, origin_auth_repo)


@pytest.mark.parametrize(
"origin_auth_repo",
[
{
"targets_config": [
{"name": "target1"},
{"name": "target2", "is_empty": True},
],
},
],
indirect=True,
)
def test_clone_when_no_target_file_and_commit(origin_auth_repo, client_dir):

setup_manager = SetupManager(origin_auth_repo)
setup_manager.add_task(
add_unauthenticated_commit_to_target_repo, kwargs={"target_name": "target2"}
)
setup_manager.execute_tasks()

update_and_check_commit_shas(
OperationType.CLONE,
origin_auth_repo,
client_dir,
expected_repo_type=UpdateType.EITHER,
)
verify_repos_eixsts(client_dir, origin_auth_repo)
9 changes: 2 additions & 7 deletions taf/tests/test_updater/test_update/test_update_valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
clone_repositories,
load_target_repositories,
update_and_check_commit_shas,
verify_repos_eixsts,
)
from taf.updater.types.update import OperationType, UpdateType

Expand Down Expand Up @@ -757,10 +758,4 @@ def test_update_when_target_empty(origin_auth_repo, client_dir):
client_dir,
skip_check_last_validated=True,
)

client_repos = load_target_repositories(origin_auth_repo, client_dir)
for name, repo in client_repos.items():
if "notempty" in name:
assert repo.path.is_dir()
else:
assert not repo.path.is_dir()
verify_repos_eixsts()
29 changes: 27 additions & 2 deletions taf/tests/test_updater/update_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,19 @@ def _get_head_commit_shas(client_repos, num_of_commits_to_remove=0):


def load_target_repositories(
auth_repo, library_dir=None, excluded_target_globs=None, commits=None
auth_repo,
library_dir=None,
excluded_target_globs=None,
commits=None,
only_load_targets=False,
):
if library_dir is None:
library_dir = auth_repo.path.parent.parent

repositoriesdb.load_repositories(
auth_repo,
library_dir=library_dir,
only_load_targets=True,
only_load_targets=only_load_targets,
excluded_target_globs=excluded_target_globs,
commits=commits,
)
Expand Down Expand Up @@ -317,6 +321,27 @@ def _update_expect_error():
assert not client_repository.path.exists()


def verify_repos_eixsts(client_dir: Path, origin_auth_repo: AuthenticationRepository):
client_auth_repo = AuthenticationRepository(path=client_dir / origin_auth_repo.name)
client_target_repos = load_target_repositories(
client_auth_repo, library_dir=client_dir
)
for repo in client_target_repos.values():
assert repo.is_git_repository


def verify_repo_empty(
client_dir: Path, origin_auth_repo: AuthenticationRepository, target_name_part: str
):
client_auth_repo = AuthenticationRepository(path=client_dir / origin_auth_repo.name)
client_target_repos = load_target_repositories(
client_auth_repo, library_dir=client_dir
)
for name, repo in client_target_repos.items():
if target_name_part in name:
assert not len(repo.all_commits_on_branch())


def verify_client_repos_state(
client_dir: Path, origin_auth_repo: AuthenticationRepository
):
Expand Down
14 changes: 3 additions & 11 deletions taf/updater/updater_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def set_existing_target_repositories(self):
repositoriesdb.load_repositories(
self.state.users_auth_repo,
library_dir=self.library_dir,
only_load_targets=True,
only_load_targets=False,
excluded_target_globs=self.excluded_target_globs,
)
target_repositories = repositoriesdb.get_deduplicated_repositories(
Expand Down Expand Up @@ -740,9 +740,6 @@ def validate_commit_in_remote(repo, commit_sha):
f"Last validated commit {last_validated_commit} is not in the remote repository."
)
else:
import pdb

pdb.set_trace()
# Re-validate from the point of divergence
commits_since = validation_repo.all_commits_since_commit(
since_commit=last_validated_commit, branch=branch
Expand Down Expand Up @@ -783,7 +780,7 @@ def load_target_repositories(self):
factory=self.target_factory,
library_dir=self.library_dir,
commits=self.state.auth_commits_since_last_validated,
only_load_targets=True,
only_load_targets=False,
excluded_target_globs=self.excluded_target_globs,
)
self.state.users_target_repositories = (
Expand Down Expand Up @@ -1033,19 +1030,13 @@ def fetch_commits(repository, branch, old_head):
fetched_commits.index(old_head) + 1 :
]
else:
import pdb

pdb.set_trace()
fetched_commits_on_target_repo_branch = (
repository.all_commits_since_commit(old_head, branch)
)
for commit in fetched_commits:
if commit not in fetched_commits_on_target_repo_branch:
fetched_commits_on_target_repo_branch.append(commit)
else:
import pdb

pdb.set_trace()
fetched_commits_on_target_repo_branch = (
repository.all_commits_since_commit(old_head, branch)
)
Expand Down Expand Up @@ -1433,6 +1424,7 @@ def merge_commits(self):
_merge_commit(
repository, branch, commit_to_merge, force_revert=True
)

return self.state.update_status
except Exception as e:
self.state.errors.append(e)
Expand Down

0 comments on commit c03cc4b

Please sign in to comment.