Skip to content

Commit

Permalink
Merge pull request #350 from opencivicdata/add-clean-info
Browse files Browse the repository at this point in the history
Add logging to pupa clean failsafe
  • Loading branch information
antidipyramid authored Aug 22, 2023
2 parents 21d36c2 + 487d55c commit 1c9d673
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 16 additions & 3 deletions pupa/cli/commands/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def add_args(self):
action="store_true",
help="delete objects without getting user confirmation",
)
self.add_argument(
"--yes",
action="store_true",
help="assumes an answer of 'yes' to all interactive prompts",
default=False,
)

def get_stale_objects(self, window):
"""
Expand Down Expand Up @@ -88,15 +94,22 @@ def handle(self, args, other):
)
self.report_stale_objects()
else:
num_stale_objects = len(list(self.get_stale_objects(args.window)))
stale_objects = list(self.get_stale_objects(args.window))
num_stale_objects = len(stale_objects)

if args.noinput and args.yes:
self.remove_stale_objects(args.window)
sys.exit()

if args.noinput:
# Fail-safe to avoid deleting a large amount of objects
# without explicit confimation
if num_stale_objects > 10:
print(
"This command would delete more than 10 objects."
"If you're sure, re-run without --noinput to provide confirmation."
f"This command would delete {num_stale_objects} objects: "
f"\n{stale_objects}"
"\nIf you're sure, re-run without --noinput to provide confirmation."
"\nOr re-run with --yes to assume a yes answer to all prompts."
)
sys.exit(1)
else:
Expand Down
4 changes: 2 additions & 2 deletions pupa/tests/clean/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def test_clean_command(subparsers):

# Call clean command
Command(subparsers).handle(
argparse.Namespace(noinput=True, report=False, window=7), []
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
)

expected_stale_objects = {stale_person, stale_membership}
Expand Down Expand Up @@ -117,5 +117,5 @@ def test_clean_command_failsafe(subparsers):
with pytest.raises(SystemExit):
# Should trigger failsafe exist when deleting more than 10 objects
Command(subparsers).handle(
argparse.Namespace(noinput=True, report=False, window=7), []
argparse.Namespace(noinput=True, report=False, window=7, yes=False), []
)

0 comments on commit 1c9d673

Please sign in to comment.