diff --git a/docs/docs/usage-guide/additional_configurations.md b/docs/docs/usage-guide/additional_configurations.md index 2adfa360..cf32d08f 100644 --- a/docs/docs/usage-guide/additional_configurations.md +++ b/docs/docs/usage-guide/additional_configurations.md @@ -112,3 +112,36 @@ LANGSMITH_API_KEY= LANGSMITH_PROJECT= LANGSMITH_BASE_URL= ``` + +## Ignoring automatic commands in PRs + +In some cases, you may want to ignore automatic commands in PRs. For example you may want to ignore MR with a specific title, or labels or from/to specific branches. + +For example, to ignore MRs with a specific title such as "[AUTO]: foobar", you can add the following to your `configuration.toml` file: + +``` +[config] +ignore_mr_title = ["\\[AUTO\\]"] +``` + +Where the `ignore_mr_title` is a list of regex patterns to match the MR title you want to ignore. + +To ignore MRs with specific labels, you can add the following to your `configuration.toml` file: + +``` +[config] +ignore_mr_labels = ["auto"] +``` + +Where the `ignore_mr_labels` is a list of labels you want to ignore. + +To ignore MRs from specific branches, you can add the following to your `configuration.toml` file: + +``` +[config] +ignore_mr_source_branches = ['develop', 'main', 'master', 'stage'] +ignore_mr_target_branches = ["qa"] +``` + +Where the `ignore_mr_source_branches` and `ignore_mr_target_branches` are lists of regex patterns to match the source and target branches you want to ignore. +They are not mutually exclusive, you can use them together or separately. diff --git a/pr_agent/servers/github_app.py b/pr_agent/servers/github_app.py index eb726dd4..f9f179b8 100644 --- a/pr_agent/servers/github_app.py +++ b/pr_agent/servers/github_app.py @@ -138,13 +138,36 @@ async def handle_new_pr_opened(body: Dict[str, Any], if action in get_settings().github_app.handle_pr_actions: # ['opened', 'reopened', 'ready_for_review'] # logic to ignore PRs with specific titles (e.g. "[Auto] ...") apply_repo_settings(api_url) - ignore_pr_title_re = get_settings().get("GITHUB_APP.IGNORE_PR_TITLE", []) + ignore_pr_title_re = get_settings().get("CONFIG.IGNORE_PR_TITLE", []) if not isinstance(ignore_pr_title_re, list): ignore_pr_title_re = [ignore_pr_title_re] + if ignore_pr_title_re and any(re.search(regex, title) for regex in ignore_pr_title_re): get_logger().info(f"Ignoring PR with title '{title}' due to github_app.ignore_pr_title setting") return {} + # logic to ignore PRs with specific labels or source branches or target branches. + ignore_pr_labels = get_settings().get("CONFIG.IGNORE_PR_LABELS", []) + ignore_pr_source_branches = get_settings().get("CONFIG.IGNORE_PR_SOURCE_BRANCHES", []) + ignore_pr_target_branches = get_settings().get("CONFIG.IGNORE_PR_TARGET_BRANCHES", []) + + if ignore_pr_labels: + labels = [label['name'] for label in pull_request.get("labels", [])] + if any(label in ignore_pr_labels for label in labels): + labels_str = ", ".join(labels) + get_logger().info(f"Ignoring PR with labels '{labels_str}' due to github_app.ignore_pr_labels settings") + return {} + + if ignore_pr_source_branches or ignore_pr_target_branches: + source_branch = pull_request.get("head", {}).get("ref", "") + target_branch = pull_request.get("base", {}).get("ref", "") + if any(re.search(regex, source_branch) for regex in ignore_pr_source_branches): + get_logger().info(f"Ignoring PR with source branch '{source_branch}' due to github_app.ignore_pr_source_branches settings") + return {} + if any(re.search(regex, target_branch) for regex in ignore_pr_target_branches): + get_logger().info(f"Ignoring PR with target branch '{target_branch}' due to github_app.ignore_pr_target_branches settings") + return {} + if get_identity_provider().verify_eligibility("github", sender_id, api_url) is not Eligibility.NOT_ELIGIBLE: await _perform_auto_commands_github("pr_commands", agent, body, api_url, log_context) else: diff --git a/pr_agent/servers/gitlab_webhook.py b/pr_agent/servers/gitlab_webhook.py index 728e3cc6..d0f28365 100644 --- a/pr_agent/servers/gitlab_webhook.py +++ b/pr_agent/servers/gitlab_webhook.py @@ -1,4 +1,5 @@ import copy +import re import json from datetime import datetime @@ -125,6 +126,7 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): log_context["sender"] = sender if data.get('object_kind') == 'merge_request' and data['object_attributes'].get('action') in ['open', 'reopen']: + title = data['object_attributes'].get('title') url = data['object_attributes'].get('url') draft = data['object_attributes'].get('draft') get_logger().info(f"New merge request: {url}") @@ -133,12 +135,41 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): get_logger().info(f"Skipping draft MR: {url}") return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"})) + # logic to ignore MRs for titles, labels and source, target branches. + ignore_mr_title = get_settings().get("CONFIG.IGNORE_PR_TITLE", []) + ignore_mr_labels = get_settings().get("CONFIG.IGNORE_PR_LABELS", []) + ignore_mr_source_branches = get_settings().get("CONFIG.IGNORE_PR_SOURCE_BRANCHES", []) + ignore_mr_target_branches = get_settings().get("CONFIG.IGNORE_PR_TARGET_BRANCHES", []) + if ignore_mr_source_branches: + source_branch = data['object_attributes'].get('source_branch') + if any(re.search(regex, source_branch) for regex in ignore_mr_source_branches): + get_logger().info(f"Ignoring MR with source branch '{source_branch}' due to gitlab.ignore_mr_source_branches settings") + return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"})) + + if ignore_mr_target_branches: + target_branch = data['object_attributes'].get('target_branch') + if any(re.search(regex, target_branch) for regex in ignore_mr_target_branches): + get_logger().info(f"Ignoring MR with target branch '{target_branch}' due to gitlab.ignore_mr_target_branches settings") + return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"})) + + if ignore_mr_labels: + labels = [label['title'] for label in data['object_attributes'].get('labels', [])] + if any(label in ignore_mr_labels for label in labels): + labels_str = ", ".join(labels) + get_logger().info(f"Ignoring MR with labels '{labels_str}' due to gitlab.ignore_mr_labels settings") + return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"})) + + if ignore_mr_title: + if any(re.search(regex, title) for regex in ignore_mr_title): + get_logger().info(f"Ignoring MR with title '{title}' due to gitlab.ignore_mr_title settings") + return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"})) + await _perform_commands_gitlab("pr_commands", PRAgent(), url, log_context) elif data.get('object_kind') == 'note' and data.get('event_type') == 'note': # comment on MR if 'merge_request' in data: mr = data['merge_request'] url = mr.get('url') - + get_logger().info(f"A comment has been added to a merge request: {url}") body = data.get('object_attributes', {}).get('note') if data.get('object_attributes', {}).get('type') == 'DiffNote' and '/ask' in body: # /ask_line diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index b128aca0..b8b02460 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -36,6 +36,15 @@ is_auto_command=false seed=-1 # set positive value to fix the seed (and ensure temperature=0) temperature=0.2 +# a list of regular expressions to match against the PR title to ignore the PR agent +ignore_pr_title = [] +# a list of regular expressions of target branches to ignore from PR agent when an MR is created +ignore_pr_target_branches = [] +# a list of regular expressions of source branches to ignore from PR agent when an MR is created +ignore_pr_source_branches = [] +# labels to ignore from PR agent when an MR is created +ignore_pr_labels = [] + [pr_reviewer] # /review # # enable/disable features require_score_review=false @@ -187,6 +196,7 @@ base_url = "https://api.github.com" publish_inline_comments_fallback_with_verification = true try_fix_invalid_inline_comments = true app_name = "pr-agent" +ignore_bot_pr = true [github_action_config] # auto_review = true # set as env var in .github/workflows/pr-agent.yaml @@ -215,8 +225,6 @@ push_commands = [ "/describe", "/review --pr_reviewer.num_code_suggestions=0", ] -ignore_pr_title = [] -ignore_bot_pr = true [gitlab] url = "https://gitlab.com"