Skip to content

Add fallback AI provider support to code review workflow#1

Merged
satya-blend360 merged 4 commits into
mainfrom
feat/fallback-ai-providers
May 5, 2026
Merged

Add fallback AI provider support to code review workflow#1
satya-blend360 merged 4 commits into
mainfrom
feat/fallback-ai-providers

Conversation

@satya-blend360
Copy link
Copy Markdown
Collaborator

If the primary provider (Claude or OpenAI) fails due to an API error, quota limit, or outage, the workflow automatically retries with the other provider and labels the review comment accordingly.

Business Context

What problem does this PR solve?

Key Changes

  • Files/classes affected:

Edge Cases

Testing Done

satya-blend360 and others added 2 commits April 24, 2026 14:46
If the primary provider (Claude or OpenAI) fails due to an API error,
quota limit, or outage, the workflow automatically retries with the
other provider and labels the review comment accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
GitHub org repos restrict the default GITHUB_TOKEN. Explicitly granting
pull-requests:write lets the workflow post review comments on PRs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

🤖 AI Code Review — GPT-4o-mini (OpenAI) ⚠️ fallback from Claude

Here’s a review of the changes made in the ai-review.yml file, addressing potential bugs, logic errors, security vulnerabilities, code quality, style violations, and adherence to the global rules.

Feedback:

  1. Error Handling:

    • The current implementation does not fully utilize try/catch for error handling in the API calls. Instead, it relies on || { echo "::error::..."; exit 1; }, which is less robust. Consider wrapping the API calls in a function that uses try/catch to handle errors more gracefully. This will improve readability and maintainability.
    • Lines 48-86: Implement proper error handling for both API calls to ensure that any unexpected errors are caught and logged appropriately.
  2. Input Validation:

    • There is no validation for the AI_PROVIDER environment variable before using it in the conditional checks. Ensure that it is validated to be either "claude" or "openai" before proceeding with API calls.
    • Lines 20-21: Add a validation step to check if AI_PROVIDER is one of the expected values.
  3. Security Vulnerabilities:

    • Ensure that sensitive information (like API keys) is not exposed in logs. While the current implementation does not log API keys, be cautious about any future changes that may inadvertently expose them.
    • General Security: Consider using environment variables securely and ensure that the secrets are not logged or printed.
  4. Code Quality and Style:

    • The code style is generally consistent, but there are some areas where readability can be improved. For example, using consistent spacing and indentation in the JSON payloads can enhance clarity.
    • Lines 54-86: Consider using a function to encapsulate the API call logic to reduce duplication and improve readability. This will also help to avoid duplicate logic when switching between providers.
  5. Null/Undefined Checks:

    • Ensure that the response from the API calls is checked for null or undefined values before attempting to access properties. This can prevent runtime errors if the API response structure changes or if there is an unexpected error.
    • Lines 65-86: Add checks to ensure that RESPONSE is valid before attempting to access its properties.
  6. Logging:

    • Avoid using echo "::error::..."; for logging error messages. Instead, consider using a structured logging approach that can be more easily parsed and understood.
    • Lines 65-86: Replace inline error messages with structured logging.
  7. General Improvements:

    • The fallback mechanism is well-implemented, but ensure that the logic is clear and concise. Consider adding comments to explain the purpose of each block, especially where the fallback logic is applied.
    • Lines 48-86: Add comments to clarify the purpose of each API call and the conditions under which they are executed.

Summary:

The changes made in this PR are a good step towards implementing a robust fallback mechanism for AI provider calls. However, improvements in error handling, input validation, security practices, and code quality can enhance the overall reliability and maintainability of the workflow. Addressing these points will help ensure that the implementation adheres to best practices and the global rules outlined.

Claude API key is present but not used as primary until verified.
Fallback still attempts Claude if OpenAI fails.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

🤖 AI Code Review — GPT-4o-mini (OpenAI)

Code Review Feedback

General Observations

The changes made to the workflow for handling API calls to Claude and OpenAI look well-structured and follow a retry mechanism. However, there are several areas that need attention regarding error handling, input validation, and adherence to global rules.

Specific Feedback

  1. Error Handling:

    • The error handling for API calls is done using || { echo "::error::..."; exit 1; }, which is acceptable for shell scripts but does not follow the global rule of using try/catch. Although this is a shell script, consider documenting this as a known limitation or refactoring to a more robust error handling mechanism if possible.
    • For example, in the fallback provider step, if both providers fail, it logs an error but does not provide a clear exit strategy. Consider adding a final exit code to indicate failure to the workflow. (Lines 81-82, 106-107)
  2. Input Validation:

    • There is no validation for the AI_PROVIDER environment variable. Ensure that it is set to either "claude" or "openai". This could lead to unexpected behavior if an invalid value is passed. You can add a simple check at the beginning of the run step:
      if [[ "$AI_PROVIDER" != "claude" && "$AI_PROVIDER" != "openai" ]]; then
        echo "::error::Invalid AI provider specified: $AI_PROVIDER"
        exit 1
      fi
    • This should be placed right after the env declaration in the review job. (Lines 15-16)
  3. Null/Undefined Checks:

    • When parsing the JSON response from the API calls, ensure that you are checking for the existence of the fields you are accessing. For instance, in the line REVIEW=$(echo "$RESPONSE" | jq -re '.content[0].text'), if the response does not contain content, it will cause an error. Use // to provide a default value or handle the error gracefully. (Lines 54, 82)
  4. Avoiding Duplicate Logic:

    • The logic for constructing the payload and making the API call is duplicated between the primary and fallback provider steps. Consider creating a function or a separate script to handle this logic to reduce duplication and improve maintainability. This can also help in ensuring consistent error handling across both providers. (Lines 54-82)
  5. Security Vulnerabilities:

    • Ensure that the API keys are not logged or exposed in any way. The current implementation does not log them, which is good, but be cautious if you decide to add any debugging or logging in the future. (Lines 46-48, 81-83)
  6. Code Quality and Style Violations:

    • Ensure consistent formatting and style throughout the script. For instance, the JSON payloads are formatted differently in different sections. Consistency improves readability. (Lines 54-82)
    • Consider using double quotes for strings consistently, especially in JSON payloads. This is a minor style issue but contributes to overall code quality.
  7. Documentation:

    • Add comments to clarify the purpose of each step, especially around the error handling sections. This will help future maintainers understand the logic and flow of the workflow better.

Summary

The changes overall improve the workflow's resilience by implementing a fallback mechanism. However, attention to error handling, input validation, and code duplication is needed to align with global rules and best practices. Addressing these issues will enhance the robustness and maintainability of the code.

- ai-review-reusable.yml: Central workflow other repos call
- templates/ai-review-starter.yml: 10-line file teams copy
- templates/review-guidelines.md: Sample rules to customize
- SETUP.md: Step-by-step guide for teams
- Updated ai-review.yml to use reusable workflow (dogfooding)

Teams can now add AI code review to their repo by:
1. Copy the starter template to .github/workflows/ai-review.yml
2. Add OPENAI_API_KEY secret
3. Open a PR

Co-Authored-By: Claude <noreply@anthropic.com>
@satya-blend360 satya-blend360 merged commit cbdbe45 into main May 5, 2026
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.

1 participant