Skip to content

Commit

Permalink
feat: check if repo is clean before running metadata and repository c…
Browse files Browse the repository at this point in the history
…ommands
  • Loading branch information
renatav committed Aug 23, 2023
1 parent 2bd7cc2 commit 5a7f0b2
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
28 changes: 24 additions & 4 deletions taf/api/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from logging import ERROR, INFO
from pathlib import Path
from logdecorator import log_on_end, log_on_error
from taf.api.utils import check_if_clean
from taf.exceptions import TargetsMetadataUpdateError
from taf.git import GitRepository
from taf.keys import load_signing_keys
Expand All @@ -10,9 +11,13 @@
from taf.log import taf_logger


def check_expiration_dates(
path, interval=None, start_date=None, excluded_roles=None
):
@log_on_error(
ERROR,
"An error occurred while checking expiration dates: {e!r}",
logger=taf_logger,
reraise=False,
)
def check_expiration_dates(path, interval=None, start_date=None, excluded_roles=None):
"""
Check if any metadata files (roles) are expired or will expire in the next <interval> days.
Prints a list of expired roles.
Expand Down Expand Up @@ -55,6 +60,7 @@ def check_expiration_dates(
print(f"No roles will expire within the given {interval} day interval")


@check_if_clean
def update_metadata_expiration_date(
path,
roles,
Expand Down Expand Up @@ -125,7 +131,7 @@ def update_metadata_expiration_date(
@log_on_end(INFO, "Updated expiration date of {role:s}", logger=taf_logger)
@log_on_error(
ERROR,
"Could not update expiration date of {role:s} {e!r}",
"Could not update expiration date of {role:s}: {e!r}",
logger=taf_logger,
reraise=True,
)
Expand All @@ -150,6 +156,13 @@ def _update_expiration_date_of_role(
)


@log_on_end(INFO, "Updated snapshot and timestamp", logger=taf_logger)
@log_on_error(
ERROR,
"Could not update snapshot and timestamp: {e!r}",
logger=taf_logger,
reraise=True,
)
def update_snapshot_and_timestamp(
taf_repo, keystore, scheme=DEFAULT_RSA_SIGNATURE_SCHEME, write_all=True
):
Expand Down Expand Up @@ -186,6 +199,13 @@ def update_snapshot_and_timestamp(
taf_repo.writeall()


@log_on_end(INFO, "Updated target metadata", logger=taf_logger)
@log_on_error(
ERROR,
"Could not update target metadata: {e!r}",
logger=taf_logger,
reraise=True,
)
def update_target_metadata(
taf_repo,
added_targets_data,
Expand Down
25 changes: 23 additions & 2 deletions taf/api/repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from functools import partial
import json
from logging import DEBUG, INFO
from logging import DEBUG, ERROR, INFO
import click
from logdecorator import log_on_end, log_on_start
from logdecorator import log_on_end, log_on_error, log_on_start
from taf.api.metadata import update_snapshot_and_timestamp, update_target_metadata
from taf.api.utils import check_if_clean

import taf.repositoriesdb as repositoriesdb
from collections import defaultdict
Expand All @@ -25,6 +26,13 @@
DEBUG, "Adding or updating dependency {dependency_name:s}", logger=taf_logger
)
@log_on_end(DEBUG, "Finished adding or updating dependency", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while adding a new dependency: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def add_dependency(
path: str,
dependency_name: str,
Expand Down Expand Up @@ -136,6 +144,12 @@ def add_dependency(
INFO, "Creating a new authentication repository {repo_path:s}", logger=taf_logger
)
@log_on_end(INFO, "Finished creating a new repository", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while creating a new repository: {e!r}",
logger=taf_logger,
reraise=True,
)
def create_repository(
path, keystore=None, roles_key_infos=None, commit=False, test=False
):
Expand Down Expand Up @@ -290,6 +304,13 @@ def _determine_out_of_band_data(dependency, branch_name, out_of_band_commit):

@log_on_start(DEBUG, "Remove dependency {dependency_name:s}", logger=taf_logger)
@log_on_end(DEBUG, "Finished removing dependency", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while removing a dependency: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def remove_dependency(
path: str,
dependency_name: str,
Expand Down
48 changes: 46 additions & 2 deletions taf/api/roles.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from logging import DEBUG
from logging import DEBUG, ERROR
import os
import click
from collections import defaultdict
from functools import partial
import json
from pathlib import Path
from logdecorator import log_on_end, log_on_start
from logdecorator import log_on_end, log_on_error, log_on_start
from taf.api.utils import check_if_clean
from taf.repositoriesdb import REPOSITORIES_JSON_PATH
from tuf.repository_tool import TARGETS_DIRECTORY_NAME
import tuf.roledb
Expand Down Expand Up @@ -38,6 +39,13 @@

@log_on_start(DEBUG, "Adding a new role {role:s}", logger=taf_logger)
@log_on_end(DEBUG, "Finished adding a new role", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while adding a new role {role:s}: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def add_role(
path: str,
role: str,
Expand Down Expand Up @@ -114,6 +122,13 @@ def add_role(

@log_on_start(DEBUG, "Adding new paths to role {role:s}", logger=taf_logger)
@log_on_end(DEBUG, "Finished adding new paths to role", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while adding new paths to role {role:s}: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def add_role_paths(
paths, delegated_role, keystore, commit=True, auth_repo=None, auth_path=None
):
Expand Down Expand Up @@ -150,6 +165,13 @@ def add_role_paths(

@log_on_start(DEBUG, "Adding new roles", logger=taf_logger)
@log_on_end(DEBUG, "Finished adding new roles", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while adding new roles: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def add_roles(
path,
keystore=None,
Expand Down Expand Up @@ -242,6 +264,13 @@ def add_roles(

@log_on_start(DEBUG, "Adding new signing key to roles", logger=taf_logger)
@log_on_end(DEBUG, "Finished adding new signing key to roles", logger=taf_logger)
@log_on_error(
ERROR,
"AAdding new signing key to roles: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def add_signing_key(
path,
roles,
Expand Down Expand Up @@ -594,6 +623,13 @@ def _role_obj(role, repository, parent=None):

@log_on_start(DEBUG, "Removing role {role:s}", logger=taf_logger)
@log_on_end(DEBUG, "Finished removing the role", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while removing role {role:s}: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def remove_role(
path: str,
role: str,
Expand Down Expand Up @@ -688,8 +724,16 @@ def remove_role(
auth_repo.commit(commit_message)


# TODO this resolve this auth_path
@log_on_start(DEBUG, "Removing paths", logger=taf_logger)
@log_on_end(DEBUG, "Finished removing paths", logger=taf_logger)
@log_on_error(
ERROR,
"An error occurred while removing roles: {e!r}",
logger=taf_logger,
reraise=True,
)
@check_if_clean
def remove_paths(paths, keystore, commit=True, auth_repo=None, auth_path=None):
"""
Remove delegated paths. Update parent roles of the roles associated with the removed paths,
Expand Down
8 changes: 2 additions & 6 deletions taf/api/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,7 @@ def update_target_repos_from_repositories_json(
Path(auth_repo_targets_dir / "repositories.json").read_text()
)
for repo_name in repositories_json.get("repositories"):
_save_top_commit_of_repo_to_target(
library_dir, repo_name, path, add_branch
)
_save_top_commit_of_repo_to_target(library_dir, repo_name, path, add_branch)
register_target_files(path, keystore, None, True, scheme, write=True)


Expand Down Expand Up @@ -500,9 +498,7 @@ def update_and_sign_targets(
for target_name in target_names:
_save_top_commit_of_repo_to_target(library_dir, target_name, path, True)
print(f"Updated {target_name} target file")
register_target_files(
path, keystore, roles_key_infos, True, scheme, write=True
)
register_target_files(path, keystore, roles_key_infos, True, scheme, write=True)


def _update_target_repos(repo_path, targets_dir, target_repo_path, add_branch):
Expand Down
21 changes: 21 additions & 0 deletions taf/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import functools
from taf.exceptions import RepositoryNotCleanError
from taf.git import GitRepository


def check_if_clean(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
path = kwargs.get("path", None)
if path is None:
path = args[0]
repo = GitRepository(path=path)
if repo.something_to_commit():
raise RepositoryNotCleanError(
"Repository has uncommitted changes. Commit or revert the changes and run the command again."
)

# Call the original function
return func(*args, **kwargs)

return wrapper
4 changes: 4 additions & 0 deletions taf/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def __init__(self, repo_path, message):
self.message = message


class RepositoryNotCleanError(TAFError):
pass


class ScriptExecutionError(TAFError):
def __init__(self, script, error_msg):
message = (
Expand Down

0 comments on commit 5a7f0b2

Please sign in to comment.