Skip to content
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

Handle OPENAI_KEY not set error in github_action_runner.py #858

Merged
merged 1 commit into from
Apr 12, 2024

Conversation

pkvach
Copy link
Contributor

@pkvach pkvach commented Apr 12, 2024

User description

Fixes #855


Type

bug_fix


Description

  • Improved error handling for the OPENAI_KEY environment variable in github_action_runner.py to allow the GitHub Action runner to proceed even if the OPENAI_KEY is not set. This change accommodates scenarios where users might be using models not provided by OpenAI.
  • Added informative logging to notify users when the OPENAI_KEY is not set, enhancing the user experience by clarifying the non-critical nature of this environment variable for certain use cases.

Changes walkthrough

Relevant files
Error handling
github_action_runner.py
Improved Handling of Missing `OPENAI_KEY` Environment Variable

pr_agent/servers/github_action_runner.py

  • Modified the handling of the OPENAI_KEY environment variable to allow
    the GitHub Action runner to proceed without it being set.
  • Added a conditional check to set OPENAI.KEY only if OPENAI_KEY is
    present.
  • Added a message to inform the user when OPENAI_KEY is not set,
    indicating the possibility of using models not from OpenAI.
  • +5/-4     

    PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    PR Description updated to latest commit (4c83bf6)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the changes are straightforward and localized to a single file with a clear purpose: improving error handling for a specific environment variable. The logic is simple and the impact of the changes is easy to understand.

    🧪 Relevant tests

    No

    🔍 Possible issues

    Possible Bug: The new message "OPENAI_KEY not set" is printed regardless of the action's need for the OPENAI_KEY. This could potentially confuse users into thinking an error has occurred when, in fact, their workflow may not require the OPENAI_KEY. Consider adding more context to the message or checking if the action being run actually requires the OPENAI_KEY.

    🔒 Security concerns

    No


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

    The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

    • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
    /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
    
    [pr_reviewer]
    some_config1=...
    some_config2=...
    

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Best practice
    Ensure consistent handling of not set environment variables.

    Consider using a consistent approach for handling not set environment variables. Since
    GITHUB_EVENT_PATH and GITHUB_TOKEN result in early termination if not set, applying the
    same logic to OPENAI_KEY would maintain consistency. This change ensures that the script's
    behavior is predictable and uniform across different environment variable checks.

    pr_agent/servers/github_action_runner.py [54-58]

    -if OPENAI_KEY:
    -    get_settings().set("OPENAI.KEY", OPENAI_KEY)
    -else:
    -    # Might not be set if the user is using models not from OpenAI
    +if not OPENAI_KEY:
         print("OPENAI_KEY not set")
    +    return
    +get_settings().set("OPENAI.KEY", OPENAI_KEY)
     
    Maintainability
    Refactor environment variable checks and settings into a separate function.

    To improve code readability and maintainability, consider extracting the environment
    variable checks and settings assignments into a separate function. This refactoring makes
    the run_action function cleaner and focuses on its primary responsibility, while the new
    function can handle the specifics of environment setup.

    pr_agent/servers/github_action_runner.py [46-61]

    -if OPENAI_KEY:
    -    get_settings().set("OPENAI.KEY", OPENAI_KEY)
    -else:
    -    # Might not be set if the user is using models not from OpenAI
    -    print("OPENAI_KEY not set")
    +def setup_environment_variables():
    +    if not GITHUB_EVENT_PATH:
    +        print("GITHUB_EVENT_PATH not set")
    +        return False
    +    if not GITHUB_TOKEN:
    +        print("GITHUB_TOKEN not set")
    +        return False
    +    if OPENAI_KEY:
    +        get_settings().set("OPENAI.KEY", OPENAI_KEY)
    +    else:
    +        print("OPENAI_KEY not set")
    +        # Consider returning False if OPENAI_KEY is critical
    +    if OPENAI_ORG:
    +        get_settings().set("OPENAI.ORG", OPENAI_ORG)
    +    get_settings().set("GITHUB.USER_TOKEN", GITHUB_TOKEN)
    +    return True
     
    +if not setup_environment_variables():
    +    return
    +

    ✨ Improve tool usage guide:

    Overview:
    The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

    • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
    /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
    
    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    

    See the improve usage page for a comprehensive guide on using this tool.

    @mrT23
    Copy link
    Collaborator

    mrT23 commented Apr 12, 2024

    looks good

    @mrT23 mrT23 merged commit 654f88b into Codium-ai:main Apr 12, 2024
    @pkvach pkvach deleted the handle-missing-openai-key branch April 12, 2024 16:46
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    No need to check OPENAI_KEY when using non OpenAI models
    2 participants