-
Notifications
You must be signed in to change notification settings - Fork 3
feat(commands): add allow-commands-on-draft-prs config option #980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1d352d0
5f2e7fe
c8c71fb
366eef0
e4ab894
debd90b
f7cf949
5e9b2ed
5e594ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,9 @@ async def process_comment_webhook_data(self, pull_request: PullRequest) -> None: | |
|
|
||
| # Execute all commands in parallel | ||
| if _user_commands: | ||
| # Cache draft status once to avoid repeated API calls | ||
| is_draft = await asyncio.to_thread(lambda: pull_request.draft) | ||
|
|
||
| tasks: list[Coroutine[Any, Any, Any] | Task[Any]] = [] | ||
| for user_command in _user_commands: | ||
| task = asyncio.create_task( | ||
|
|
@@ -98,6 +101,7 @@ async def process_comment_webhook_data(self, pull_request: PullRequest) -> None: | |
| command=user_command, | ||
| reviewed_user=user_login, | ||
| issue_comment_id=self.hook_data["comment"]["id"], | ||
| is_draft=is_draft, | ||
| ) | ||
| ) | ||
| tasks.append(task) | ||
|
|
@@ -143,7 +147,7 @@ async def process_comment_webhook_data(self, pull_request: PullRequest) -> None: | |
| raise | ||
|
|
||
| async def user_commands( | ||
| self, pull_request: PullRequest, command: str, reviewed_user: str, issue_comment_id: int | ||
| self, pull_request: PullRequest, command: str, reviewed_user: str, issue_comment_id: int, *, is_draft: bool | ||
| ) -> None: | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| available_commands: list[str] = [ | ||
| COMMAND_RETEST_STR, | ||
|
|
@@ -161,6 +165,25 @@ async def user_commands( | |
| _command = command_and_args[0] | ||
| _args: str = command_and_args[1] if len(command_and_args) > 1 else "" | ||
|
|
||
| # Check if command is allowed on draft PRs | ||
| if is_draft: | ||
| allow_commands_on_draft = self.github_webhook.config.get_value("allow-commands-on-draft-prs") | ||
| # Empty list means all commands allowed; non-empty list means only those commands | ||
| if isinstance(allow_commands_on_draft, list) and len(allow_commands_on_draft) > 0: | ||
| # Sanitize: ensure all entries are strings for safe join and comparison | ||
| allow_commands_on_draft = [str(cmd) for cmd in allow_commands_on_draft] | ||
| if _command not in allow_commands_on_draft: | ||
| self.logger.debug( | ||
| f"{self.log_prefix} Command {_command} is not allowed on draft PRs. " | ||
| f"Allowed commands: {allow_commands_on_draft}" | ||
| ) | ||
| await asyncio.to_thread( | ||
| pull_request.create_issue_comment, | ||
| f"Command `/{_command}` is not allowed on draft PRs.\n" | ||
| f"Allowed commands on draft PRs: {', '.join(allow_commands_on_draft)}", | ||
| ) | ||
| return | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+168
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HIGH: Non-list config values fail open and allow all draft commands. If 🛠️ Proposed fix if is_draft:
allow_commands_on_draft = self.github_webhook.config.get_value("allow-commands-on-draft-prs")
+ if allow_commands_on_draft is None:
+ self.logger.debug(
+ f"{self.log_prefix} Draft PR commands are disabled (allow-commands-on-draft-prs not set)."
+ )
+ return
+ if not isinstance(allow_commands_on_draft, list):
+ raise ValueError(
+ f"allow-commands-on-draft-prs must be a list of command strings, "
+ f"got {type(allow_commands_on_draft).__name__}"
+ )
# Empty list means all commands allowed; non-empty list means only those commands
- if isinstance(allow_commands_on_draft, list) and len(allow_commands_on_draft) > 0:
+ if len(allow_commands_on_draft) > 0:
# Sanitize: ensure all entries are strings for safe join and comparison
allow_commands_on_draft = [str(cmd) for cmd in allow_commands_on_draft]
if _command not in allow_commands_on_draft:
self.logger.debug(
f"{self.log_prefix} Command {_command} is not allowed on draft PRs. "
f"Allowed commands: {allow_commands_on_draft}"
)🤖 Prompt for AI Agents |
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.logger.debug( | ||
| f"{self.log_prefix} User: {reviewed_user}, Command: {_command}, Command args: {_args or 'None'}" | ||
| ) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.