From 90b72d9ab5de814507cbeab8c3b576bbf5a63325 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 27 Nov 2025 05:16:41 +0000 Subject: [PATCH] Fix #95 and #434: Add early channel permission check This commit addresses two related issues: - Issue #95: Unhelpful error messages when user lacks channel edit permissions - Issue #434: Files downloaded/uploaded before checking permissions Changes: - Added early permission check in commands.py after channel construction - Calls add_channel() before DOWNLOAD_FILES stage to fail fast if unauthorized - Caches root_id and channel_id to avoid duplicate API calls - Prevents wasted bandwidth and processing time Benefits: - Users get immediate feedback on authorization issues - No wasted resources downloading/uploading files when unauthorized - No Studio API changes required - uses existing endpoints - Backward compatible with existing chef scripts --- ricecooker/commands.py | 13 +++++++++++++ ricecooker/managers/tree.py | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ricecooker/commands.py b/ricecooker/commands.py index 9922b9c7..4d7b505a 100644 --- a/ricecooker/commands.py +++ b/ricecooker/commands.py @@ -134,6 +134,19 @@ def uploadchannel( # noqa: C901 config.PROGRESS_MANAGER.set_tree(create_initial_tree(channel)) tree = config.PROGRESS_MANAGER.tree + # Early permission check: Try creating the channel before downloading/uploading files + # This will fail fast if the user lacks edit permissions + # Fixes issues #95 and #434 by avoiding wasted downloads/uploads + if ( + config.PROGRESS_MANAGER.get_status_val() <= Status.CREATE_TREE.value + and command != "dryrun" + ): + config.LOGGER.info("Checking channel permissions...") + try: + tree.root_id, tree.channel_id = tree.add_channel() + except Exception: + sys.exit(1) + # Download files if they haven't been downloaded already if config.PROGRESS_MANAGER.get_status_val() <= Status.DOWNLOAD_FILES.value: config.LOGGER.info("") diff --git a/ricecooker/managers/tree.py b/ricecooker/managers/tree.py index 6ada462a..1bd6c8db 100644 --- a/ricecooker/managers/tree.py +++ b/ricecooker/managers/tree.py @@ -30,6 +30,8 @@ def __init__(self, channel): self.failed_uploads = {} self.file_map = {} self.all_nodes = [] + self.root_id = None # Will be set during early permission check + self.channel_id = None # Will be set during early permission check def validate(self): """validate: checks if tree structure is valid @@ -269,7 +271,11 @@ def upload_tree(self): from datetime import datetime start_time = datetime.now() - root, channel_id = self.add_channel() + # Use cached root_id and channel_id if already set (from early permission check) + if self.root_id is not None and self.channel_id is not None: + root, channel_id = self.root_id, self.channel_id + else: + root, channel_id = self.add_channel() self.node_count_dict = {"upload_count": 0, "total_count": self.channel.count()} config.LOGGER.info("\tPreparing fields...")