Skip to content

Add configurable .pr_agent.toml branch selection via CLI/env with GitHub fallback#2411

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/allow-custom-branch-config
Draft

Add configurable .pr_agent.toml branch selection via CLI/env with GitHub fallback#2411
Copilot wants to merge 2 commits into
mainfrom
copilot/allow-custom-branch-config

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 26, 2026

PR-Agent currently reads .pr_agent.toml from the repository default branch for GitHub PRs. This change adds explicit branch override support via CLI flag and environment variable, while preserving default-branch fallback behavior when the file is missing on the requested branch.

  • Config input surface

    • Added --config-branch <branch> to the CLI parser.
    • Added PR_AGENT_CONFIG_BRANCH support when CLI flag is not provided.
    • CLI writes resolved value into CONFIG.CONFIG_BRANCH for downstream consumers.
  • GitHub provider lookup behavior

    • GithubProvider.get_repo_settings() now attempts .pr_agent.toml from the configured branch first.
    • On retrieval failure (e.g., missing file/branch/API error), it logs a warning and falls back to existing default-branch lookup.
  • Coverage for new behavior

    • Added targeted unit tests for:
      • CLI argument parsing and settings propagation.
      • Env-var-driven branch selection.
      • GitHub provider branch-first lookup and fallback to default branch.
# CLI override
pr-agent --pr_url=https://github.com/org/repo/pull/123 --config-branch feature/config review

# Env override
PR_AGENT_CONFIG_BRANCH=feature/config pr-agent --pr_url=https://github.com/org/repo/pull/123 review

Copilot AI changed the title [WIP] Add option to specify custom branch for locating .pr_agent.toml Add configurable .pr_agent.toml branch selection via CLI/env with GitHub fallback May 26, 2026
Copilot AI requested a review from naorpeled May 26, 2026 21:48
@naorpeled naorpeled closed this May 30, 2026
@naorpeled naorpeled reopened this May 30, 2026
@naorpeled naorpeled marked this pull request as ready for review May 30, 2026 19:46
Copilot AI review requested due to automatic review settings May 30, 2026 19:46
@naorpeled naorpeled marked this pull request as draft May 30, 2026 19:47
@qodo-free-for-open-source-projects
Copy link
Copy Markdown
Contributor

Review Summary by Qodo

Add configurable branch selection for .pr_agent.toml via CLI and environment

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Add --config-branch CLI flag for custom .pr_agent.toml branch selection
• Support PR_AGENT_CONFIG_BRANCH environment variable for branch override
• Implement GitHub provider fallback to default branch on config lookup failure
• Add comprehensive unit tests for CLI parsing and provider behavior
Diagram
flowchart LR
  CLI["CLI Flag<br/>--config-branch"]
  ENV["Environment Variable<br/>PR_AGENT_CONFIG_BRANCH"]
  SETTINGS["CONFIG.CONFIG_BRANCH<br/>Setting"]
  GITHUB["GitHub Provider<br/>get_repo_settings"]
  CUSTOM["Load from<br/>Custom Branch"]
  DEFAULT["Fallback to<br/>Default Branch"]
  
  CLI --> SETTINGS
  ENV --> SETTINGS
  SETTINGS --> GITHUB
  GITHUB --> CUSTOM
  CUSTOM -->|"On Failure"| DEFAULT

Loading

Grey Divider

File Changes

1. pr_agent/cli.py ✨ Enhancement +4/-0

Add CLI flag and env var for config branch selection

• Added --config-branch argument to CLI parser for specifying custom branch
• Implemented logic to resolve config branch from CLI flag or PR_AGENT_CONFIG_BRANCH environment
 variable
• Store resolved branch name in CONFIG.CONFIG_BRANCH setting for downstream consumption

pr_agent/cli.py


2. pr_agent/git_providers/github_provider.py ✨ Enhancement +18/-4

Implement branch-first lookup with default fallback

• Modified get_repo_settings() to attempt loading .pr_agent.toml from configured branch first
• Added fallback mechanism to default branch when configured branch lookup fails (missing file,
 branch, or API errors)
• Support both settings-based and environment variable-based branch configuration
• Added warning logs for failed branch lookups with error details

pr_agent/git_providers/github_provider.py


3. tests/unittest/test_cli_config_branch.py 🧪 Tests +48/-0

Add CLI config branch argument parsing tests

• Test CLI parser correctly recognizes --config-branch flag
• Test CLI flag value is propagated to CONFIG.CONFIG_BRANCH setting
• Test PR_AGENT_CONFIG_BRANCH environment variable is used when CLI flag is absent

tests/unittest/test_cli_config_branch.py


View more (1)
4. tests/unittest/test_github_provider_repo_settings.py 🧪 Tests +59/-0

Add GitHub provider branch lookup and fallback tests

• Test GitHub provider uses configured branch from settings for .pr_agent.toml lookup
• Test fallback to default branch when file is missing in configured branch
• Test environment variable is used when settings value is unavailable

tests/unittest/test_github_provider_repo_settings.py


Grey Divider

Qodo Logo

@qodo-free-for-open-source-projects
Copy link
Copy Markdown
Contributor

qodo-free-for-open-source-projects Bot commented May 30, 2026

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (1)

Grey Divider


Remediation recommended

1. Broad except Exception masks errors 📘 Rule violation ☼ Reliability
Description
get_repo_settings() catches Exception broadly and silently falls back to the default branch,
which can mask unexpected bugs and make failures harder to diagnose. The handler should be narrowed
to expected exception types (or re-raised after logging) to avoid hiding unrelated errors.
Code

pr_agent/git_providers/github_provider.py[R746-750]

Evidence
PR Compliance ID 16 requires narrow, explicit exception handling and discourages broad `except
Exception` in provider code unless immediately re-raised with preserved context. The new code adds
except Exception as e: and continues by logging a warning and falling back, potentially masking
unexpected errors.

pr_agent/git_providers/github_provider.py[735-750]
Best Practice: Learned patterns

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`get_repo_settings()` uses a broad `except Exception as e:` and then falls back to the default branch. This can hide unexpected failures and makes it harder to distinguish expected “missing branch/file” cases from real bugs.

## Issue Context
Compliance requires narrow, explicit exception handling in provider/parsing paths, with exceptions logged and broad catches avoided unless re-raised with context.

## Fix Focus Areas
- pr_agent/git_providers/github_provider.py[735-750]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. CLI whitespace blocks env 🐞 Bug ≡ Correctness
Description
In cli.run(), precedence is resolved before trimming whitespace, so a whitespace-only
--config-branch value short-circuits PR_AGENT_CONFIG_BRANCH and results in no override being
applied.
Code

pr_agent/cli.py[R80-82]

Evidence
The CLI currently selects args.config_branch before trimming; a whitespace-only CLI value is
truthy and blocks env fallback, then becomes empty after .strip(), so CONFIG.CONFIG_BRANCH is
never set.

pr_agent/cli.py[70-83]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`--config-branch` is currently combined with `PR_AGENT_CONFIG_BRANCH` via `or` and only then `.strip()` is applied. If `args.config_branch` is a whitespace-only string (truthy), it prevents the env var from being considered, and after stripping becomes empty, causing the override to be skipped.

### Issue Context
This creates a silent misconfiguration path where the user expects env fallback but gets default-branch behavior.

### Fix Focus Areas
- pr_agent/cli.py[78-83]

### Suggested change
Compute and strip CLI and env values independently, then apply precedence:
- `cli_branch = (args.config_branch or "").strip()`
- `env_branch = (os.environ.get("PR_AGENT_CONFIG_BRANCH") or "").strip()`
- `config_branch = cli_branch or env_branch`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Provider whitespace blocks env 🐞 Bug ≡ Correctness
Description
In GithubProvider.get_repo_settings(), CONFIG.CONFIG_BRANCH is chosen before trimming, so a
whitespace-only settings value suppresses PR_AGENT_CONFIG_BRANCH and disables the branch override.
Code

pr_agent/git_providers/github_provider.py[R735-738]

Evidence
The provider resolves precedence before stripping; a whitespace-only CONFIG.CONFIG_BRANCH value is
truthy and blocks env fallback, then becomes empty and bypasses the branch-specific load.

pr_agent/git_providers/github_provider.py[734-750]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`get_repo_settings()` uses `settings_value or env_value` and only afterwards strips whitespace. If the settings value is whitespace-only, it wins the `or` but becomes empty after stripping, skipping the configured-branch fetch and falling back to default branch.

### Issue Context
This can silently ignore `PR_AGENT_CONFIG_BRANCH` when a misconfigured settings value exists.

### Fix Focus Areas
- pr_agent/git_providers/github_provider.py[734-750]

### Suggested change
Normalize each candidate first, then apply precedence:
- `settings_branch = get_settings().get("CONFIG.CONFIG_BRANCH", None)`
- `settings_branch = settings_branch.strip() if isinstance(settings_branch, str) else ""`
- `env_branch = (os.environ.get("PR_AGENT_CONFIG_BRANCH") or "").strip()`
- `config_branch = settings_branch or env_branch`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for selecting the branch used to load .pr_agent.toml for GitHub PRs via CLI or environment variable, while retaining default-branch fallback behavior.

Changes:

  • Adds --config-branch CLI parsing and propagates it into runtime settings.
  • Updates GitHub repo settings lookup to try the configured branch first, then fall back.
  • Adds unit tests for CLI/env branch selection and GitHub fallback behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pr_agent/cli.py Adds CLI/env config branch resolution.
pr_agent/git_providers/github_provider.py Loads .pr_agent.toml from configured branch before default branch.
tests/unittest/test_cli_config_branch.py Covers CLI flag and env var propagation.
tests/unittest/test_github_provider_repo_settings.py Covers branch-first lookup and fallback behavior.

Comment thread pr_agent/cli.py

command = args.command.lower()
get_settings().set("CONFIG.CLI_MODE", True)
config_branch = (args.config_branch or os.environ.get("PR_AGENT_CONFIG_BRANCH") or "").strip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow specifying a custom branch for locating .pr_agent.toml

3 participants