Skip to content

Commit

Permalink
fix: skip last validated check when last validated commit is None
Browse files Browse the repository at this point in the history
  • Loading branch information
renatav committed Sep 27, 2024
1 parent 71b9b0a commit 820ce90
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 66 deletions.
2 changes: 0 additions & 2 deletions taf/updater/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ def validate_repository(
if (auth_path / "targets" / "test-auth-repo").exists()
else UpdateType.OFFICIAL
)
settings.overwrite_last_validated_commit = True
auth_repo_name = None

try:
Expand Down Expand Up @@ -629,5 +628,4 @@ def validate_repository(
raise ValidationFailedError(
f"Validation of repository {auth_repo_name or ''} failed due to error: {e}"
)
settings.overwrite_last_validated_commit = False
settings.last_validated_commit = {}
130 changes: 66 additions & 64 deletions taf/updater/updater_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _set_last_validated_commit(self):
if self.operation == OperationType.CLONE:
settings.last_validated_commit[self.state.validation_auth_repo.name] = None
self.state.last_validated_commit = None
elif not settings.overwrite_last_validated_commit:
elif not self.validate_from_commit:
users_auth_repo = AuthenticationRepository(path=self.auth_path)
last_validated_commit = users_auth_repo.last_validated_commit
settings.last_validated_commit[
Expand Down Expand Up @@ -624,39 +624,13 @@ def _clear_lvc():
self.auth_path = Path(self.library_dir, self.state.auth_repo_name)

if self.state.users_auth_repo:
default_branch = self.state.validation_auth_repo.default_branch
# validate before running the updater
# if the last validated commit is not contained by the remote repository
# either the remote repository was tempered with or the last validated commit
# was manually set to an invalid value
if self.state.last_validated_commit and not _check_if_commit_on_branch(
self.state.validation_auth_repo,
self.state.last_validated_commit,
default_branch,
):
error_msg = (
f"Last validated commit {self.state.last_validated_commit} is no longer on {default_branch} "
f"of the remote {self.state.users_auth_repo.name} repository. This could "
"either mean that there was an unauthorized push to the remote "
"repository, or that last_validated_commit file was modified. "
)
if self.force:
# if last validated commit is not in the remote and run with --force, start the
# validation from the beginning. This will set the last validated commit
taf_logger.warning(
f"{error_msg}. Starting validation from the first commit"
)
_clear_lvc()
else:
raise UpdateFailedError(
f"{error_msg}\nRun the updater with the --force flag to run the validation from the first commit"
)
# validate the top commit of the user's auth repo
# if it's not in the remote repo -> fail early
default_branch = self.state.validation_auth_repo.default_branch
users_head_sha = self.state.users_auth_repo.top_commit_of_branch(
default_branch
)
if self.state.last_validated_commit and not _check_if_commit_on_branch(
if not _check_if_commit_on_branch(
self.state.validation_auth_repo, users_head_sha, default_branch
):
error_msg = (
Expand All @@ -668,56 +642,84 @@ def _clear_lvc():
# this is always an error, force or no force
raise UpdateFailedError(error_msg)

if users_head_sha != self.state.last_validated_commit:
if not _check_if_commit_on_branch(
self.state.users_auth_repo,
self.state.last_validated_commit,
default_branch,
include_remotes=True,
if self.state.last_validated_commit:
# validate before running the updater
# if the last validated commit is not contained by the remote repository
# either the remote repository was tempered with or the last validated commit
# was manually set to an invalid value
if (
self.state.last_validated_commit
and not _check_if_commit_on_branch(
self.state.validation_auth_repo,
self.state.last_validated_commit,
default_branch,
)
):
error_msg = (
f"Last validated commit {self.state.last_validated_commit} is no longer on {default_branch} "
f"of the remote {self.state.users_auth_repo.name} repository. This could "
"either mean that there was an unauthorized push to the remote "
"repository, or that last_validated_commit file was modified. "
)
if self.force:
settings.last_validated_commit[
self.state.validation_auth_repo.name
] = None
self.state.last_validated_commit = None
# if last validated commit is not in the remote and run with --force, start the
# validation from the beginning. This will set the last validated commit
taf_logger.warning(
f"{self.state.users_auth_repo.name}: Last validated commit {users_head_sha} is not in repository {self.state.users_auth_repo.name} "
"Running the validation from the first commit."
f"{error_msg}. Starting validation from the first commit"
)
_clear_lvc()
else:
raise UpdateFailedError(
f"{self.state.users_auth_repo.name}: Last validated commit {users_head_sha} is not in repository {self.state.users_auth_repo.name} "
"\nRun the updater with the --force flag to run the validation from the first commit"
)
else:
commits_since = (
self.state.users_auth_repo.all_commits_since_commit(
since_commit=self.state.last_validated_commit,
branch=default_branch,
f"{error_msg}\nRun the updater with the --force flag to run the validation from the first commit"
)
)
# if the user's head sha is newer than last validated commit
# that can mean that the changes were pulled manually
# validation will start from the last validated commit
# and there is no need to do anything else
# if the user's head commit is not newer or equal to the last validated commit
# that could meant that the user manually removed some commits from the local
# repository
if users_head_sha not in commits_since:

if (
self.state.last_validated_commit
and users_head_sha != self.state.last_validated_commit
):
if not _check_if_commit_on_branch(
self.state.users_auth_repo,
self.state.last_validated_commit,
default_branch,
include_remotes=True,
):
if self.force:
settings.last_validated_commit[
self.state.validation_auth_repo.name
] = None
self.state.last_validated_commit = None
_clear_lvc()
taf_logger.warning(
f"{self.state.users_auth_repo.name}: Top commit of repository {self.state.users_auth_repo.name} {users_head_sha} is not equal to or newer than the last successful commit. "
f"{self.state.users_auth_repo.name}: Last validated commit {users_head_sha} is not in repository {self.state.users_auth_repo.name} "
"Running the validation from the first commit."
)
else:
raise UpdateFailedError(
f"Top commit of repository {self.state.users_auth_repo.name} {users_head_sha} is not equal to or newer than the last successful commit. "
f"{self.state.users_auth_repo.name}: Last validated commit {users_head_sha} is not in repository {self.state.users_auth_repo.name} "
"\nRun the updater with the --force flag to run the validation from the first commit"
)
else:
commits_since = (
self.state.users_auth_repo.all_commits_since_commit(
since_commit=self.state.last_validated_commit,
branch=default_branch,
)
)
# if the user's head sha is newer than last validated commit
# that can mean that the changes were pulled manually
# validation will start from the last validated commit
# and there is no need to do anything else
# if the user's head commit is not newer or equal to the last validated commit
# that could meant that the user manually removed some commits from the local
# repository
if users_head_sha not in commits_since:
if self.force:
_clear_lvc()
taf_logger.warning(
f"{self.state.users_auth_repo.name}: Top commit of repository {self.state.users_auth_repo.name} {users_head_sha} is not equal to or newer than the last successful commit. "
"Running the validation from the first commit."
)
else:
raise UpdateFailedError(
f"Top commit of repository {self.state.users_auth_repo.name} {users_head_sha} is not equal to or newer than the last successful commit. "
"\nRun the updater with the --force flag to run the validation from the first commit"
)

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

0 comments on commit 820ce90

Please sign in to comment.