skyl / corpora

Corpora is a self-building corpus that can help build other arbitrary corpora
GNU Affero General Public License v3.0
2 stars 0 forks source link

feat(rs/core/corpora_cli): add context for api, init command #50

Closed skyl closed 5 days ago

skyl commented 5 days ago

PR Type

Enhancement, Documentation


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
8 files
init.rs
Implement `init` Command with Context and API Integration

rs/core/corpora_cli/src/commands/init.rs
  • Implemented the init command with context support.
  • Added functionality to create a tarball from Git repository files.
  • Integrated API call to create a new corpus.
  • Included error handling for file operations and API calls.
  • +100/-3 
    context.rs
    Add Context Management and OAuth Authentication                   

    rs/core/corpora_cli/src/context.rs
  • Introduced Context struct for configuration management.
  • Implemented OAuth authentication for API access.
  • Added methods to derive configuration from .corpora.yaml or Git.
  • Provided functionality to fetch server base URL and OAuth token.
  • +143/-0 
    main.rs
    Integrate Context into Main CLI Application                           

    rs/core/corpora_cli/src/main.rs
  • Integrated Context into the main CLI application.
  • Updated command executions to use the new Context.
  • +9/-6     
    create.rs
    Add Context to Issue Create Subcommand                                     

    rs/core/corpora_cli/src/commands/issue/create.rs
  • Added context parameter to create subcommand.
  • Printed server URL from context.
  • +3/-1     
    label.rs
    Add Context to Issue Label Subcommand                                       

    rs/core/corpora_cli/src/commands/issue/label.rs
  • Added context parameter to label subcommand.
  • Printed server URL from context.
  • +3/-1     
    update.rs
    Add Context to Issue Update Subcommand                                     

    rs/core/corpora_cli/src/commands/issue/update.rs
  • Added context parameter to update subcommand.
  • Printed server URL from context.
  • +3/-1     
    sync.rs
    Add Context to Sync Command                                                           

    rs/core/corpora_cli/src/commands/sync.rs
  • Added context parameter to sync command.
  • Printed server URL from context.
  • +4/-1     
    workon.rs
    Add Context to Workon Command                                                       

    rs/core/corpora_cli/src/commands/workon.rs
  • Added context parameter to workon command.
  • Printed server URL from context.
  • +3/-1     
    Configuration changes
    1 files
    genall.sh
    Update OpenAPI Generator Command in Script                             

    rs/genall.sh - Removed template directory option from OpenAPI generator command.
    +0/-1     
    Documentation
    1 files
    corpora-name.md
    Add Linguistic Notes on "Corpora" Name                                     

    md/notes/corpora-name.md
  • Added notes on the connotations of the name "corpora" in various
    languages.
  • +43/-0   
    Dependencies
    1 files
    Cargo.toml
    Update Dependencies for Corpora CLI Project                           

    rs/core/corpora_cli/Cargo.toml
  • Added new dependencies for corpora_client, flate2, git2, reqwest,
    serde, serde_json, serde_yaml, tar, and tempfile.
  • +9/-0     

    πŸ’‘ PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    github-actions[bot] commented 5 days ago

    PR Reviewer Guide πŸ”

    (Review updated until commit https://github.com/skyl/corpora/commit/8de95283ae536240110959b0e10937bd41590c98)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 πŸ”΅πŸ”΅πŸ”΅πŸ”΅βšͺ
    πŸ§ͺ No relevant tests
    πŸ”’ No security concerns identified
    ⚑ Recommended focus areas for review

    Error Handling
    The error handling in the `create_tarball_from_git` function could be improved by providing more context or using a more robust error handling strategy. Panic on Missing Environment Variables
    The `Context::new` function panics if the `CORPORA_CLIENT_ID` or `CORPORA_CLIENT_SECRET` environment variables are not set. Consider handling this more gracefully. OAuth Token Fetching
    The `get_oauth_token` function panics if the OAuth token cannot be fetched. Consider handling this error more gracefully.
    github-actions[bot] commented 5 days ago

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Validate file path existence to prevent errors during corpus creation ___ **Validate the tarball_path to ensure it points to an existing file before attempting
    to create a corpus.** [rs/core/corpora_cli/src/commands/init.rs [7]](https://github.com/skyl/corpora/pull/50/files#diff-f464032a0fce74f27bdd1392052016915cccb793e821d99be68c5d830829d0d4R7-R7) ```diff let tarball_path = std::path::PathBuf::from("path/to/tarball.tar"); +if !tarball_path.exists() { + eprintln!("Tarball path does not exist: {:?}", tarball_path); + return; +} ```
    Suggestion importance[1-10]: 9 Why: Validating the existence of the file path before proceeding with corpus creation is crucial to prevent runtime errors and ensure that the operation is only attempted with valid inputs.
    9
    Possible bug
    Handle missing "access_token" key in OAuth response to prevent runtime panics ___ **Consider handling the case where the OAuth token response does not contain an
    "access_token" key to avoid potential runtime panics.** [rs/core/corpora_cli/src/context.rs [57-60]](https://github.com/skyl/corpora/pull/50/files#diff-d6b40156da6e3616397c19919d4a0265f644792c6b7df218f82c04421e9985a7R57-R60) ```diff -token_response["access_token"] - .as_str() - .expect("Access token not found in OAuth response") +token_response.get("access_token") + .and_then(|v| v.as_str()) + .unwrap_or_else(|| panic!("Access token not found in OAuth response")) .to_string() ```
    Suggestion importance[1-10]: 8 Why: The suggestion addresses a potential runtime panic by handling cases where the "access_token" key might be missing in the OAuth response. This improves the robustness of the code by preventing unexpected crashes.
    8
    Performance
    Switch to asynchronous requests for better performance and responsiveness ___ **Use non-blocking asynchronous requests instead of blocking ones to improve
    performance and responsiveness.** [rs/core/corpora_cli/src/context.rs [35-44]](https://github.com/skyl/corpora/pull/50/files#diff-d6b40156da6e3616397c19919d4a0265f644792c6b7df218f82c04421e9985a7R35-R44) ```diff -let client = reqwest::blocking::Client::new(); +let client = reqwest::Client::new(); let response = client .post(&token_url) .form(&[ ("grant_type", "client_credentials"), ("client_id", client_id), ("client_secret", client_secret), ]) .send() + .await .expect("Failed to perform OAuth authentication"); ```
    Suggestion importance[1-10]: 7 Why: Switching to asynchronous requests can significantly improve performance and responsiveness, especially in network-bound operations. However, this change requires additional modifications to handle async/await syntax throughout the codebase.
    7
    Best practice
    Improve error handling for missing environment variables to provide clearer context ___ **Add error handling for the env::var calls to provide more context in case of missing
    environment variables.** [rs/core/corpora_cli/src/context.rs [15-18]](https://github.com/skyl/corpora/pull/50/files#diff-d6b40156da6e3616397c19919d4a0265f644792c6b7df218f82c04421e9985a7R15-R18) ```diff let client_id = env::var("CORPORA_CLIENT_ID") - .expect("Environment variable CORPORA_CLIENT_ID is not set"); + .unwrap_or_else(|_| panic!("Environment variable CORPORA_CLIENT_ID is not set or invalid")); let client_secret = env::var("CORPORA_CLIENT_SECRET") - .expect("Environment variable CORPORA_CLIENT_SECRET is not set"); + .unwrap_or_else(|_| panic!("Environment variable CORPORA_CLIENT_SECRET is not set or invalid")); ```
    Suggestion importance[1-10]: 6 Why: The suggestion enhances error messages for missing environment variables, providing clearer context. While it improves code clarity, the functional impact is moderate since the original code already handles these errors.
    6
    skyl commented 5 days ago

    /describe

    skyl commented 5 days ago

    /review

    github-actions[bot] commented 5 days ago

    Persistent review updated to latest commit https://github.com/skyl/corpora/commit/8de95283ae536240110959b0e10937bd41590c98

    github-actions[bot] commented 5 days ago

    PR Description updated to latest commit (https://github.com/skyl/corpora/commit/8de95283ae536240110959b0e10937bd41590c98)