From 0e3417b4ab7fe4a49cfe098dd5adf5f1012ce3c2 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 08:55:59 +0200 Subject: [PATCH 1/7] webhook --- docs/CI_FEEDBACK.md | 2 +- pr_agent/servers/gitlab_webhook.py | 19 ++++++++++++++++++- pr_agent/settings/configuration.toml | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/CI_FEEDBACK.md b/docs/CI_FEEDBACK.md index 4658ee67..7e14df78 100644 --- a/docs/CI_FEEDBACK.md +++ b/docs/CI_FEEDBACK.md @@ -28,4 +28,4 @@ where `{repo_name}` is the name of the repository, `{run_number}` is the run num - `enable_auto_checks_feedback` - if set to true, the tool will automatically provide feedback when a check is failed. Default is true. - `excluded_checks_list` - a list of checks to exclude from the feedback, for example: ["check1", "check2"]. Default is an empty list. - `persistent_comment` - if set to true, the tool will overwrite a previous checks comment with the new feedback. Default is true. - +- `enable_help_text=true` - if set to true, the tool will provide a help message when a user comments "/checks" on a PR. Default is true. diff --git a/pr_agent/servers/gitlab_webhook.py b/pr_agent/servers/gitlab_webhook.py index 165cd0a3..6be3ae45 100644 --- a/pr_agent/servers/gitlab_webhook.py +++ b/pr_agent/servers/gitlab_webhook.py @@ -11,7 +11,9 @@ from starlette_context import context from starlette_context.middleware import RawContextMiddleware from pr_agent.agent.pr_agent import PRAgent +from pr_agent.algo.utils import update_settings_from_args from pr_agent.config_loader import get_settings, global_settings +from pr_agent.git_providers.utils import apply_repo_settings from pr_agent.log import LoggingFormat, get_logger, setup_logger from pr_agent.secret_providers import get_secret_provider @@ -28,6 +30,20 @@ def handle_request(background_tasks: BackgroundTasks, url: str, body: str, log_c with get_logger().contextualize(**log_context): background_tasks.add_task(PRAgent().handle_request, url, body) +async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, body: dict, api_url: str, log_context: dict): + apply_repo_settings(api_url) + commands = get_settings().get(f"azure_devops_server.{commands_conf}") + for command in commands: + split_command = command.split(" ") + command = split_command[0] + args = split_command[1:] + other_args = update_settings_from_args(args) + new_command = ' '.join([command] + other_args) + if body: + get_logger().info(body) + get_logger().info(f"Performing command: {new_command}") + with get_logger().contextualize(**log_context): + await agent.handle_request(api_url, new_command) @router.post("/webhook") async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): @@ -58,7 +74,8 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): if data.get('object_kind') == 'merge_request' and data['object_attributes'].get('action') in ['open', 'reopen']: get_logger().info(f"A merge request has been opened: {data['object_attributes'].get('title')}") url = data['object_attributes'].get('url') - handle_request(background_tasks, url, "/review", log_context) + await _perform_commands_gitlab("pr_commands", PRAgent(), {}, url, log_context) + # handle_request(background_tasks, url, "/review", log_context) elif data.get('object_kind') == 'note' and data['event_type'] == 'note': if 'merge_request' in data: mr = data['merge_request'] diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 61df5417..744213da 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -113,7 +113,7 @@ enable_help_text=true enable_auto_checks_feedback=true excluded_checks_list=["lint"] # list of checks to exclude, for example: ["check1", "check2"] persistent_comment=true - +enable_help_text=true [pr_config] # /config # From 275f0d6a054a867c2ff5cd83f48ae266757cc1e7 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 09:20:28 +0200 Subject: [PATCH 2/7] Update GitLab configuration and documentation for webhook setup --- Usage.md | 11 +++++++++++ pr_agent/servers/gitlab_webhook.py | 2 +- pr_agent/settings/configuration.toml | 17 ++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Usage.md b/Usage.md index a84f854e..c2f91bd0 100644 --- a/Usage.md +++ b/Usage.md @@ -6,6 +6,7 @@ - [Online Usage](#online-usage) - [GitHub App](#working-with-github-app) - [GitHub Action](#working-with-github-action) +- [GitLab Webhook](#working-with-gitlab-webhook) - [BitBucket App](#working-with-bitbucket-self-hosted-app) - [Azure DevOps Provider](#azure-devops-provider) - [Additional Configurations Walkthrough](#appendix---additional-configurations-walkthrough) @@ -237,6 +238,16 @@ For example, you can set an environment variable: `pr_description.add_original_u [pr_description] add_original_user_description = false ``` +### Working with GitLab Webhook +After setting up a GitLab webhook, to control which commands will run automatically when a new PR is opened, you can set the `pr_commands` parameter in the configuration file, similar to the GitHub App: +``` +[gitlab] +pr_commands = [ + "/describe --pr_description.add_original_user_description=true --pr_description.keep_original_user_title=true", + "/review --pr_reviewer.num_code_suggestions=0", + "/improve", +] +``` ### Working with BitBucket Self-Hosted App Similar to GitHub app, when running PR-Agent from BitBucket App, the default [configuration file](pr_agent/settings/configuration.toml) from a pre-built docker will be initially loaded. diff --git a/pr_agent/servers/gitlab_webhook.py b/pr_agent/servers/gitlab_webhook.py index 6be3ae45..435d6967 100644 --- a/pr_agent/servers/gitlab_webhook.py +++ b/pr_agent/servers/gitlab_webhook.py @@ -32,7 +32,7 @@ def handle_request(background_tasks: BackgroundTasks, url: str, body: str, log_c async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, body: dict, api_url: str, log_context: dict): apply_repo_settings(api_url) - commands = get_settings().get(f"azure_devops_server.{commands_conf}") + commands = get_settings().get(f"gitlab.{commands_conf}") for command in commands: split_command = command.split(" ") command = split_command[0] diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 744213da..8d977e7a 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -171,17 +171,12 @@ push_commands = [ ] [gitlab] -# URL to the gitlab service -url = "https://gitlab.com" - -# Polling (either project id or namespace/project_name) syntax can be used -projects_to_monitor = ['org_name/repo_name'] - -# Polling trigger -magic_word = "AutoReview" - -# Polling interval -polling_interval_seconds = 30 +url = "https://gitlab.com" # URL to the gitlab service +pr_commands = [ + "/describe --pr_description.add_original_user_description=true --pr_description.keep_original_user_title=true", + "/review --pr_reviewer.num_code_suggestions=0", + "/improve --pr_code_suggestions.summarize=true", +] [bitbucket_app] #auto_review = true # set as config var in .pr_agent.toml From dd5386e07ee574068d41ddcd2800f11ada9e341f Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 09:27:40 +0200 Subject: [PATCH 3/7] try-except --- .../servers/azuredevops_server_webhook.py | 26 +++++++++-------- pr_agent/servers/gitlab_webhook.py | 29 ++++++++++--------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/pr_agent/servers/azuredevops_server_webhook.py b/pr_agent/servers/azuredevops_server_webhook.py index 1459b78b..a7ba8659 100644 --- a/pr_agent/servers/azuredevops_server_webhook.py +++ b/pr_agent/servers/azuredevops_server_webhook.py @@ -54,20 +54,22 @@ def authorize(credentials: HTTPBasicCredentials = Depends(security)): headers={'WWW-Authenticate': 'Basic'}, ) -async def _perform_commands(commands_conf: str, agent: PRAgent, body: dict, api_url: str, log_context: dict): + +async def _perform_commands_azure(commands_conf: str, agent: PRAgent, api_url: str, log_context: dict): apply_repo_settings(api_url) commands = get_settings().get(f"azure_devops_server.{commands_conf}") for command in commands: - split_command = command.split(" ") - command = split_command[0] - args = split_command[1:] - other_args = update_settings_from_args(args) - new_command = ' '.join([command] + other_args) - if body: - get_logger().info(body) - get_logger().info(f"Performing command: {new_command}") - with get_logger().contextualize(**log_context): - await agent.handle_request(api_url, new_command) + try: + split_command = command.split(" ") + command = split_command[0] + args = split_command[1:] + other_args = update_settings_from_args(args) + new_command = ' '.join([command] + other_args) + get_logger().info(f"Performing command: {new_command}") + with get_logger().contextualize(**log_context): + await agent.handle_request(api_url, new_command) + except Exception as e: + get_logger().error(f"Failed to perform command {command}: {e}") @router.post("/", dependencies=[Depends(authorize)]) @@ -82,7 +84,7 @@ async def handle_webhook(background_tasks: BackgroundTasks, request: Request): pr_url = data["resource"]["_links"]["web"]["href"].replace("_apis/git/repositories", "_git") log_context["event"] = data["eventType"] log_context["api_url"] = pr_url - await _perform_commands("pr_commands", PRAgent(), {}, pr_url, log_context) + await _perform_commands_azure("pr_commands", PRAgent(), pr_url, log_context) return elif data["eventType"] == "ms.vss-code.git-pullrequest-comment-event" and "content" in data["resource"]["comment"]: if available_commands_rgx.match(data["resource"]["comment"]["content"]): diff --git a/pr_agent/servers/gitlab_webhook.py b/pr_agent/servers/gitlab_webhook.py index 435d6967..2779f80d 100644 --- a/pr_agent/servers/gitlab_webhook.py +++ b/pr_agent/servers/gitlab_webhook.py @@ -30,20 +30,23 @@ def handle_request(background_tasks: BackgroundTasks, url: str, body: str, log_c with get_logger().contextualize(**log_context): background_tasks.add_task(PRAgent().handle_request, url, body) -async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, body: dict, api_url: str, log_context: dict): + +async def _perform_commands_gitlab(commands_conf: str, agent: PRAgent, api_url: str, log_context: dict): apply_repo_settings(api_url) - commands = get_settings().get(f"gitlab.{commands_conf}") + commands = get_settings().get(f"gitlab.{commands_conf}", {}) for command in commands: - split_command = command.split(" ") - command = split_command[0] - args = split_command[1:] - other_args = update_settings_from_args(args) - new_command = ' '.join([command] + other_args) - if body: - get_logger().info(body) - get_logger().info(f"Performing command: {new_command}") - with get_logger().contextualize(**log_context): - await agent.handle_request(api_url, new_command) + try: + split_command = command.split(" ") + command = split_command[0] + args = split_command[1:] + other_args = update_settings_from_args(args) + new_command = ' '.join([command] + other_args) + get_logger().info(f"Performing command: {new_command}") + with get_logger().contextualize(**log_context): + await agent.handle_request(api_url, new_command) + except Exception as e: + get_logger().error(f"Failed to perform command {command}: {e}") + @router.post("/webhook") async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): @@ -74,7 +77,7 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): if data.get('object_kind') == 'merge_request' and data['object_attributes'].get('action') in ['open', 'reopen']: get_logger().info(f"A merge request has been opened: {data['object_attributes'].get('title')}") url = data['object_attributes'].get('url') - await _perform_commands_gitlab("pr_commands", PRAgent(), {}, url, log_context) + await _perform_commands_gitlab("pr_commands", PRAgent(), url, log_context) # handle_request(background_tasks, url, "/review", log_context) elif data.get('object_kind') == 'note' and data['event_type'] == 'note': if 'merge_request' in data: From b1dfd905c46c53a0199f783392d05b38be5fa964 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 09:40:39 +0200 Subject: [PATCH 4/7] text --- pr_agent/tools/pr_help_message.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pr_agent/tools/pr_help_message.py b/pr_agent/tools/pr_help_message.py index 5677698c..01be9ade 100644 --- a/pr_agent/tools/pr_help_message.py +++ b/pr_agent/tools/pr_help_message.py @@ -58,12 +58,12 @@ class PRHelpMessage: commands.append("`/similar_issue`") checkbox_list = [] - checkbox_list.append(" - [ ] Send ") - checkbox_list.append(" - [ ] Send ") - checkbox_list.append(" - [ ] Send ") - checkbox_list.append(" - [ ] Send ") - checkbox_list.append(" - [ ] Send ") - checkbox_list.append(" - [ ] Send ") + checkbox_list.append(" - [ ] Run ") + checkbox_list.append(" - [ ] Run ") + checkbox_list.append(" - [ ] Run ") + checkbox_list.append(" - [ ] Run ") + checkbox_list.append(" - [ ] Run ") + checkbox_list.append(" - [ ] Run ") checkbox_list.append("[*]") checkbox_list.append("[*]") checkbox_list.append("[*]") @@ -72,12 +72,12 @@ class PRHelpMessage: checkbox_list.append("[*]") if isinstance(self.git_provider, GithubProvider): - pr_comment += f"" + pr_comment += f"
ToolDescriptionRun this command :gem:
" for i in range(len(tool_names)): pr_comment += f"\n\n\n" pr_comment += "
ToolDescriptionInvoke Interactively :gem:
\n\n{tool_names[i]}{descriptions[i]}\n\n{checkbox_list[i]}\n
\n\n" - pr_comment += f"""\n\n(1) Note that each tool be [invoked automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools-for-pr-actions) when a new PR is opened, or called manually by [commenting on a PR](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#online-usage).""" - pr_comment += f"""\n\n(2) Tools marked with [*] require additional parameters to be passed. For example, to invoke the `/ask` tool, you need to comment on a PR: `/ask "What is the purpose of this PR?"`. See the relevant documentation for each tool for more details.""" + pr_comment += f"""\n\n(1) Note that each tool be [triggered automatically](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#github-app-automatic-tools-for-pr-actions) when a new PR is opened, or called manually by [commenting on a PR](https://github.com/Codium-ai/pr-agent/blob/main/Usage.md#online-usage).""" + pr_comment += f"""\n\n(2) Tools marked with [*] require additional parameters to be passed. For example, to invoke the `/ask` tool, you need to comment on a PR: `/ask ""`. See the relevant documentation for each tool for more details.""" else: pr_comment += f"" for i in range(len(tool_names)): From a41a427b588c32e33c2f4a350ce331a495ea6c59 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 11:08:08 +0200 Subject: [PATCH 5/7] readme --- README.md | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index bb8dac7e..a5fe0ece 100644 --- a/README.md +++ b/README.md @@ -31,24 +31,17 @@ Making pull requests less painful with an AI agent - [Why use PR-Agent?](#why-use-pr-agent) ## News and Updates +### Feb 21, 2024 +- Added a new tool, `help`, to easily provide a list of available tools and their descriptions, and enabling to run them interactively. +- + + + + +- GitLab webhook now supports controlling which commands will [run automatically](./docs/USAGE.md#working-with-gitlab-webhook) when a new PR is opened. ### Feb 18, 2024 - Introducing the `CI Feedback` tool 💎. The tool automatically triggers when a PR has a failed check. It analyzes the failed check, and provides summarized logs and analysis. Note that this feature requires read access to GitHub 'checks' and 'actions'. See [here](./docs/CI_FEEDBACK.md) for more details. - - - - - -→
- - - - - - New ability - you can run `/ask` on specific lines of code in the PR from the PR's diff view. See [here](./docs/ASK.md#ask-lines) for more details. - - - - - Introducing support for [Azure DevOps Webhooks](./Usage.md#azure-devops-webhook), as well as bug fixes and improved support for several ADO commands. @@ -62,14 +55,6 @@ A new feature was added to the `review` tool - [Auto-approve PRs](./docs/REVIEW. Added ["PR Actions" 💎](https://www.codium.ai/images/pr_agent/pr-actions.mp4) - interactively trigger PR-Agent tools from the PR page. -### Jan 28, 2024 -- 💎 Test - A new tool, [`/test component_name`](https://github.com/Codium-ai/pr-agent/blob/main/docs/TEST.md), was added to PR-Agent Pro. The tool will generate tests for a specific component, based on the PR code changes. -- 💎 Analyze - The [`/analyze`](https://github.com/Codium-ai/pr-agent/blob/main/docs/Analyze.md) tool was updated and simplified. It now presents a summary of the code components that were changed in the PR. -### Jan 21, 2024 -- 💎 Custom suggestions - A new tool, `/custom_suggestions`, was added to PR-Agent Pro. The tool will propose only suggestions that follow specific guidelines defined by the user. -See [here](https://github.com/Codium-ai/pr-agent/blob/main/docs/CUSTOM_SUGGESTIONS.md) for more details. - - ## Overview
From 0f1614bedc4bdde3133c0dea934ba4d24d739652 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 11:10:03 +0200 Subject: [PATCH 6/7] readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a5fe0ece..13d43fca 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Making pull requests less painful with an AI agent ## News and Updates ### Feb 21, 2024 - Added a new tool, `help`, to easily provide a list of available tools and their descriptions, and enabling to run them interactively. -- + @@ -52,7 +52,7 @@ The `review` tool has been revamped, aiming to make the feedback clearer and mor A new feature was added to the `review` tool - [Auto-approve PRs](./docs/REVIEW.md#auto-approval-1). If enabled, this feature enables to automatically approve PRs that meet specific criteria, by commenting on a PR: `/review auto_approve`. ### Feb 2, 2024 -Added ["PR Actions" 💎](https://www.codium.ai/images/pr_agent/pr-actions.mp4) - interactively trigger PR-Agent tools from the PR page. +Added ["PR Actions"](https://www.codium.ai/images/pr_agent/pr-actions.mp4) 💎 - interactively trigger PR-Agent tools from the PR page. ## Overview From f3c1c61c2e74d652b2abd717a41dcc3f3ea694ce Mon Sep 17 00:00:00 2001 From: mrT23 Date: Wed, 21 Feb 2024 11:11:03 +0200 Subject: [PATCH 7/7] readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 13d43fca..c00fb63a 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,15 @@ Making pull requests less painful with an AI agent ## News and Updates ### Feb 21, 2024 -- Added a new tool, `help`, to easily provide a list of available tools and their descriptions, and enabling to run them interactively. +- Added a new command, `/help`, to easily provide a list of available tools and their descriptions, and run them interactively. + + + - GitLab webhook now supports controlling which commands will [run automatically](./docs/USAGE.md#working-with-gitlab-webhook) when a new PR is opened. ### Feb 18, 2024 - Introducing the `CI Feedback` tool 💎. The tool automatically triggers when a PR has a failed check. It analyzes the failed check, and provides summarized logs and analysis. Note that this feature requires read access to GitHub 'checks' and 'actions'. See [here](./docs/CI_FEEDBACK.md) for more details.
ToolCommandDescription