Add capability to delete repos and targets

This commit is contained in:
Avram Lubkin 2023-07-12 15:22:28 -04:00 коммит произвёл Avram Lubkin
Родитель 922d010da8
Коммит 59baa65ca9
3 изменённых файлов: 84 добавлений и 14 удалений

Просмотреть файл

@ -125,6 +125,12 @@ class Session:
if options.action == "add":
self.database.add_downstream_target(options.name, options.url, options.revision)
elif options.action == "delete":
if options.revision:
self.database.delete_downstream_target(options.name, options.revision)
else:
self.database.delete_repo(options.name)
def spreadsheet(self, options):
"""
Handle spreadsheet subcommand

Просмотреть файл

@ -176,6 +176,15 @@ def get_downstream_parser():
help="Add downstream target",
)
actions.add_argument(
"-D",
"--delete",
action="store_const",
const="delete",
dest="action",
help="Delete downstream target, must specify name, revision optional",
)
actions.add_argument(
"-l",
"--list",
@ -193,7 +202,7 @@ def get_downstream_parser():
parser.add_argument(
"-u",
"--url",
help="Repository URL. Required if repo in not in database",
help="Repository URL. Required for add if repo in not in database",
)
parser.add_argument(
"-r",
@ -229,12 +238,12 @@ def parse_args(args: Optional[Sequence[str]] = None):
# Check for required options
options = parser.parse_args(args)
if (
options.subcommand == "downstream"
and options.action == "add"
and None in (options.name, options.revision)
):
parser.error("URL and revision are required")
if options.subcommand == "downstream":
if options.action == "add" and None in (options.name, options.revision):
parser.error("Name and revision are required")
elif options.action == "delete" and options.name is None:
parser.error("Name is required")
# Configuration file was specified
if options.config is not None:

Просмотреть файл

@ -130,18 +130,73 @@ class DatabaseDriver:
"""
with self.get_session() as session:
# Add repo
if url:
# See if remote exists
if existing := session.query(Distros).filter_by(distroID=name).one_or_none():
# If URL is different, overwrite with warning
if url and existing.repoLink != url:
LOGGER.warning(
"Overwriting existing remote %s URL: %s", name, existing.repoLink
)
existing.repoLink = url
else:
LOGGER.info(
"Using existing remote %s at %s", existing.distroID, existing.repoLink
)
# If new remote, create
elif url:
session.add(Distros(distroID=name, repoLink=url))
LOGGER.info("Successfully added new repo %s at %s", name, url)
# If URL wasn't given, make sure repo is in database
elif (name,) not in session.query(Distros.distroID).all():
# If no URL was given, error
else:
raise CommaDataError(f"Repository '{name}' given without URL not found in database")
# Add target
# See if target exists
if (
existing := session.query(MonitoringSubjects.distroID)
.filter_by(distroID=name)
.filter_by(revision=revision)
.one_or_none()
):
LOGGER.info(
"Target already exists for revision '%s' in distro '%s'", revision, name
)
# If new target, create
else:
session.add(MonitoringSubjects(distroID=name, revision=revision))
LOGGER.info("Successfully added new revision '%s' for distro '%s'", revision, name)
LOGGER.info(
"Successfully added target for revision '%s' in distro '%s'", revision, name
)
def delete_repo(self, name):
"""
Deletes a repo and all associated monitoring subjects
"""
with self.get_session() as session:
targets = session.query(MonitoringSubjects).filter_by(distroID=name)
for target in targets:
LOGGER.info(
"Deleting downstream target: remote=%s revision=%s", name, target.revision
)
targets.delete(synchronize_session=False)
LOGGER.info("Deleting remote: %s", name)
session.query(Distros).filter_by(distroID=name).delete(synchronize_session=False)
def delete_downstream_target(self, name, revision):
"""
Remove a downstream target
"""
with self.get_session() as session:
LOGGER.info("Deleting downstream target: remote=%s revision=%s", name, revision)
session.query(MonitoringSubjects).filter_by(distroID=name).filter_by(
revision=revision
).delete(synchronize_session=False)
def get_downstream_repos(self):
"""