Skip to content

Conversation

@AmarYasser1
Copy link

@AmarYasser1 AmarYasser1 commented Dec 9, 2025

User description

📋 Description

Implements the feature requested in issue #38 to add a set-env command for setting environment variables from the CLI. This enhancement allows users to easily configure environment variables for User or Machine scope directly from the command line, which is particularly useful for development setup and CI/CD pipelines.

✨ Changes Made

1. New Options Class: SetEnvironmentVariableOptions.cs

  • Located in Src/AiCommitMessage/Options/
  • Uses CommandLineParser with [Verb("set-env")]
  • Accepts variable in VARIABLE_NAME=value format
  • Supports --target / -t option for User/Machine scope
  • Defaults to User scope when target not specified
[Verb("set-env", HelpText = "Set the Environment Variable.")]
public class SetEnvironmentVariableOptions
{
    [Value(0, Required = true, HelpText = "The environment variable in VAR_NAME=value format.")]
    public string Variable { get; set; }

    [Option('t', "target", Required = false, Default = "User", HelpText = "The environment variable target (User or Machine). Default is User.")]
    public string Target { get; set; }
}

2. New Service Class: EnvironmentVariableService.cs

  • Located in Src/AiCommitMessage/Services/
  • Comprehensive error handling and validation
  • Features:
    • ✅ Parses VARIABLE_NAME=value format using Split('=', 2) to support = in values
    • ✅ Validates variable format and checks for empty names
    • ✅ Case-insensitive target matching (User/Machine)
    • ✅ Trims whitespace from variable names and values
    • ✅ Handles SecurityException for Machine scope without admin privileges
    • ✅ Proper exit codes for error cases
    • ✅ User-friendly error messages using Output.ErrorLine()

3. Updated: Program.cs

  • Added SetEnvironmentVariableOptions to ParseArguments<> generic types
  • Added case handler in Run() method to call EnvironmentVariableService.SetEnvironmentVariable()
  • Updated XML documentation to reflect four command types

4. Unit Tests: EnvironmentVariableServiceTests.cs

  • Located in Tests/AiCommitMessage.Tests/Services/
  • 13 comprehensive tests covering:
    • ✅ Setting User-level variables
    • ✅ Default target behavior
    • ✅ Invalid format handling
    • ✅ Null/empty variable validation
    • ✅ Empty variable name detection
    • ✅ Invalid target validation
    • ✅ Case-insensitive target matching (Theory with InlineData)

5. Unit Tests: SetEnvironmentVariableOptionsTests.cs

  • Located in Tests/AiCommitMessage.Tests/Options/
  • Tests for Options class properties and defaults

6. Documentation: Updated README.md

  • Added set-env to Commands table
  • Added comprehensive "Environment Variable Management" section

🧪 Testing

Manual Testing Results

All tests performed using:

dotnet run --framework net10.0 -- set-env MY_VAR=test-value --target User

✅ Success Cases:

# Test 1: User scope (default)
dotnet run --framework net10.0 -- set-env TEST_VAR=test_value
# Result: Environment variable 'TEST_VAR' set to 'test_value' for User scope.

# Test 2: Explicit User target
dotnet run --framework net10.0 -- set-env TEST_VAR=test_value --target User
# Result: Environment variable 'TEST_VAR' set to 'test_value' for User scope.

# Test 3: Machine scope (Requires administrator permission)
dotnet run --framework net10.0 -- set-env TEST_VAR=test_value -t Machine
# Result: Environment variable 'TEST_VAR' set to 'test_value' for Machine scope.

✅ Error Handling:

# Test 5: Invalid format (missing =)
dotnet run --framework net10.0 -- set-env INVALID_FORMAT
# Result: Invalid variable format. Please use the format: VAR_NAME=value

# Test 6: Empty variable name
dotnet run --framework net10.0 -- set-env =value
# Result: Variable name cannot be empty.

# Test 7: Invalid target
dotnet run --framework net10.0 -- set-env TEST=value --target Custom
# Result: Invalid target 'Custom'. Please use 'User' or 'Machine'.

# Test 8: Machine target without admin
dotnet run --framework net10.0 -- set-env TEST=value --target Machine
# Result: Permission denied. Setting Machine-level environment variables requires administrator
privileges.

🔗 Closes

Closes #38

🙏 Thank You

Thank you for reviewing this contribution! I'm happy to address any feedback or make requested changes.


Description

  • Introduced a new set-env command for managing environment variables from the CLI.
  • Added comprehensive documentation in README.md for usage and examples.
  • Implemented SetEnvironmentVariableOptions class for command-line options.
  • Developed EnvironmentVariableService to handle setting environment variables with error handling.
  • Included unit tests to ensure functionality and robustness of the new feature.

Changes walkthrough 📝

Relevant files
Documentation
README.md
Documentation for `set-env` command                                           

README.md

  • Added documentation for the new set-env command.
  • Included syntax, parameters, target scopes, and examples.
  • +51/-0   
    Enhancement
    SetEnvironmentVariableOptions.cs
    New options class for setting environment variables           

    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs

  • Created SetEnvironmentVariableOptions class.
  • Defined properties for variable name and target scope.
  • +25/-0   
    EnvironmentVariableService.cs
    Service for managing environment variables                             

    Src/AiCommitMessage/Services/EnvironmentVariableService.cs

  • Implemented SetEnvironmentVariable method.
  • Added error handling and validation for environment variable settings.

  • +88/-0   
    Program.cs
    Updated program to include new command handling                   

    Src/AiCommitMessage/Program.cs

  • Registered SetEnvironmentVariableOptions in command-line parser.
  • Updated Run method to handle new command.
  • +10/-2   
    Tests
    EnvironmentVariableServiceTests.cs
    Unit tests for environment variable service                           

    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs

  • Added unit tests for SetEnvironmentVariable method.
  • Covered various scenarios including valid and invalid inputs.
  • +192/-0 

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

    Summary by CodeRabbit

    • New Features

      • Added a set-env command to configure environment variables with User or Machine scope.
    • Tests

      • Added comprehensive tests covering valid/invalid inputs, target scope variations, and exit-code behavior.
    • Refactor

      • Console output now uses enhanced console formatting for clearer messages.
    • Documentation

      • Added usage docs for set-env, including syntax, parameters, scopes, and examples.

    ✏️ Tip: You can customize this high-level summary in your review settings.

    - Add SetEnvironmentVariableOptions and EnvironmentVariableService
    - Update Program.cs to register the command
    - Add unit tests for various cases
    - Update documentation includes inline comments, README.md, and any related markdown files.
    @sourcery-ai
    Copy link
    Contributor

    sourcery-ai bot commented Dec 9, 2025

    Reviewer's Guide

    Implements a new set-env CLI verb that parses VAR_NAME=value and sets environment variables at User or Machine scope via a dedicated service, wires it into the main command dispatcher, and documents and tests the behavior extensively.

    Sequence diagram for set-env command execution

    sequenceDiagram
        actor User
        participant Cli as dotnet_aicommitmessage
        participant Parser
        participant Program
        participant EnvService as EnvironmentVariableService
        participant SystemEnv as Environment
        participant Output
    
        User->>Cli: set-env VAR_NAME=value --target User
        Cli->>Parser: ParseArguments(args)
        Parser-->>Program: SetEnvironmentVariableOptions instance
        Program->>Program: Run(options)
        Program->>EnvService: SetEnvironmentVariable(options)
    
        EnvService->>EnvService: Validate Variable not null or empty
        EnvService->>EnvService: Split Variable into name and value
        EnvService->>EnvService: Validate variableName not empty
        EnvService->>EnvService: Resolve EnvironmentVariableTarget (User or Machine)
    
        EnvService->>SystemEnv: SetEnvironmentVariable(name, value, target)
        SystemEnv-->>EnvService: success or exception
    
        alt success
            EnvService->>Output: InfoLine(Environment variable set message)
        else security exception
            EnvService->>Output: ErrorLine(Permission denied)
            EnvService->>SystemEnv: set ExitCode = 1
        else invalid argument or other error
            EnvService->>Output: ErrorLine(error message)
            EnvService->>SystemEnv: set ExitCode = 1
        end
    
    Loading

    Class diagram for new set-env options and service

    classDiagram
        class Program {
            +Main(string[] args)
            -Run(object options)
        }
    
        class SetEnvironmentVariableOptions {
            +string Variable
            +string Target
        }
    
        class EnvironmentVariableService {
            +SetEnvironmentVariable(SetEnvironmentVariableOptions setEnvironmentVariableOptions) void
            -ErrorLine(string message) void
        }
    
        class Output {
            +ErrorLine(string message) void
            +InfoLine(string message) void
        }
    
        class Environment {
            +SetEnvironmentVariable(string variableName, string variableValue, EnvironmentVariableTarget envTarget) void
            +ExitCode int
        }
    
        class EnvironmentVariableTarget {
            <<enumeration>>
            User
            Machine
        }
    
        Program ..> SetEnvironmentVariableOptions : parses
        Program ..> EnvironmentVariableService : calls
        EnvironmentVariableService ..> SetEnvironmentVariableOptions : uses
        EnvironmentVariableService ..> Output : uses
        EnvironmentVariableService ..> Environment : uses
        Environment ..> EnvironmentVariableTarget : uses
    
    Loading

    File-Level Changes

    Change Details Files
    Introduce CLI verb/options to represent the set-env command and its arguments.
    • Add SetEnvironmentVariableOptions with CommandLineParser [Verb("set-env")] attribute.
    • Define required positional Variable argument in VAR_NAME=value format.
    • Define optional --target/-t argument defaulting to "User" and restricted to User/Machine by convention.
    • Add unit tests verifying default values and option binding.
    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    Tests/AiCommitMessage.Tests/Options/SetEnvironmentVariableOptionsTests.cs
    Add EnvironmentVariableService to perform parsing, validation, and setting of environment variables with robust error handling.
    • Implement parsing of the VAR_NAME=value string using Split('=', 2) and trimming whitespace.
    • Validate non-null/empty variable string, non-empty variable name, and supported target values (User or Machine) using case-insensitive comparison.
    • Map target string to EnvironmentVariableTarget.User or EnvironmentVariableTarget.Machine.
    • Call Environment.SetEnvironmentVariable and emit success messages via Output.InfoLine.
    • Centralize error reporting via a helper that writes Output.ErrorLine and sets Environment.ExitCode = 1.
    • Handle SecurityException for insufficient permissions on Machine target, along with ArgumentException and generic Exception for invalid or unexpected conditions.
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    Wire the new set-env command into the main program pipeline.
    • Extend Parser.Default.ParseArguments generic list to include SetEnvironmentVariableOptions.
    • Add a case in Run(object options) to dispatch SetEnvironmentVariableOptions to EnvironmentVariableService.SetEnvironmentVariable.
    • Update XML documentation comments on Main and Run to mention the new command and its behavior.
    Src/AiCommitMessage/Program.cs
    Add unit tests validating EnvironmentVariableService behavior and exit codes.
    • Test setting a User-scoped variable and verifying it via Environment.GetEnvironmentVariable.
    • Test default-target behavior for User scope.
    • Test invalid input scenarios: missing '=', null/empty variable, empty variable name, and invalid target, asserting Environment.ExitCode = 1.
    • Test case-insensitive handling of the target value using a Theory with multiple InlineData cases.
    • Ensure Environment.ExitCode is restored after tests and environment variables are cleaned up in finally blocks.
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    Document the new set-env command and expose it in the command list.
    • Add an "Environment Variable Management" section explaining syntax, parameters, target scopes, and usage examples for set-env.
    • Update the commands table to include set-env with a concise description of its purpose.
    • Clarify permission requirements for Machine-level environment variables across OSes.
    README.md

    Assessment against linked issues

    Issue Objective Addressed Explanation
    #38 Add a new CLI command set-env to the .NET tool that accepts a VAR_NAME=value argument and an optional --target (User Machine), defaulting to User, and sets the environment variable in the appropriate scope.
    #38 Document the new set-env command in the project README, including syntax, parameters, default behavior, and examples.

    Tips and commands

    Interacting with Sourcery

    • Trigger a new review: Comment @sourcery-ai review on the pull request.
    • Continue discussions: Reply directly to Sourcery's review comments.
    • Generate a GitHub issue from a review comment: Ask Sourcery to create an
      issue from a review comment by replying to it. You can also reply to a
      review comment with @sourcery-ai issue to create an issue from it.
    • Generate a pull request title: Write @sourcery-ai anywhere in the pull
      request title to generate a title at any time. You can also comment
      @sourcery-ai title on the pull request to (re-)generate the title at any time.
    • Generate a pull request summary: Write @sourcery-ai summary anywhere in
      the pull request body to generate a PR summary at any time exactly where you
      want it. You can also comment @sourcery-ai summary on the pull request to
      (re-)generate the summary at any time.
    • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
      request to (re-)generate the reviewer's guide at any time.
    • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
      pull request to resolve all Sourcery comments. Useful if you've already
      addressed all the comments and don't want to see them anymore.
    • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
      request to dismiss all existing Sourcery reviews. Especially useful if you
      want to start fresh with a new review - don't forget to comment
      @sourcery-ai review to trigger a new review!

    Customizing Your Experience

    Access your dashboard to:

    • Enable or disable review features such as the Sourcery-generated pull request
      summary, the reviewer's guide, and others.
    • Change the review language.
    • Add, remove or edit custom review instructions.
    • Adjust other review settings.

    Getting Help

    @coderabbitai
    Copy link
    Contributor

    coderabbitai bot commented Dec 9, 2025

    Note

    Other AI code review bot(s) detected

    CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

    Walkthrough

    Implements a new set-env CLI command: adds options parsing, a service to parse/validate VAR=value and set the environment variable for User or Machine, program integration, tests, README docs, and small console-output and project-file edits.

    Changes

    Cohort / File(s) Summary
    New CLI options & service
    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs, Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    Adds SetEnvironmentVariableOptions (verb set-env, Variable, Target) and EnvironmentVariableService.SetEnvironmentVariable which parses VAR=value, resolves User/Machine target, calls Environment.SetEnvironmentVariable, and reports errors/exit codes on invalid input or exceptions.
    Program integration
    Src/AiCommitMessage/Program.cs
    Registers SetEnvironmentVariableOptions with the argument parser and adds a Run case invoking EnvironmentVariableService.SetEnvironmentVariable.
    Tests
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    Adds unit tests covering successful User-scoped set (including default), invalid format, null/empty variable, empty name, invalid target, and case-insensitive target handling; cleans up test variables.
    Documentation
    README.md
    Documents the new set-env command under Environment Variable Management, including syntax, parameters, target scopes, and examples; lists the command in Commands.
    Minor edits
    Src/AiCommitMessage/AiCommitMessage.csproj, Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs
    Inserts a BOM at the start of the csproj file; replaces Console.WriteLine calls with AnsiConsole.WriteLine in cache service output.

    Sequence Diagram(s)

    sequenceDiagram
        autonumber
        actor User
        participant CLI as CLI Parser (Program)
        participant Service as EnvironmentVariableService
        participant OS as Environment (Runtime/OS)
    
        User->>CLI: run "set-env VAR_NAME=value --target User?"
        CLI->>Service: new SetEnvironmentVariableOptions(Variable, Target)
        CLI->>Service: Invoke SetEnvironmentVariable(options)
        Service->>Service: Validate "VAR=value" format\nParse name and value\nResolve target (User/Machine)
        Service->>OS: Environment.SetEnvironmentVariable(name, value, target)
        OS-->>Service: Success / throws exception
        Service-->>CLI: Write success or ErrorLine + set ExitCode=1
        CLI-->>User: Console output (success or error)
    
    Loading

    Estimated code review effort

    🎯 3 (Moderate) | ⏱️ ~20 minutes

    • Areas to focus:
      • Src/AiCommitMessage/Services/EnvironmentVariableService.cs — input parsing, target resolution, exception handling, and exit-code behavior.
      • Src/AiCommitMessage/Program.cs — parser registration and Run dispatch correctness.
      • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs — cleanup logic and assertions affecting process-level environment or exit code.
      • Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs — verify Spectre.Console usage matches prior console behavior.

    Poem

    🐰 A rabbit hops to set the scene,
    I parse the VAR and tidy the green,
    User or Machine, I choose the nest,
    Errors checked, the rest compressed,
    Hooray — your env now springs to be seen! 🥕

    Pre-merge checks and finishing touches

    ❌ Failed checks (2 warnings)
    Check name Status Explanation Resolution
    Out of Scope Changes check ⚠️ Warning The PR contains one out-of-scope change: modifications to CommitMessageCacheService.cs replacing Console.WriteLine with AnsiConsole.WriteLine, which is unrelated to the set-env command feature defined in issue #38. Remove the changes to CommitMessageCacheService.cs as they are unrelated to the set-env command feature and should be addressed in a separate PR.
    Docstring Coverage ⚠️ Warning Docstring coverage is 75.86% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
    ✅ Passed checks (3 passed)
    Check name Status Explanation
    Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
    Title check ✅ Passed The title 'feat: add set-env command to manage environment variables' clearly and concisely summarizes the main change—adding a new CLI command for managing environment variables.
    Linked Issues check ✅ Passed The PR implementation fully satisfies all coding requirements from issue #38: adds set-env command, accepts VAR_NAME=value format, supports optional --target flag defaulting to User, validates inputs, uses Environment.SetEnvironmentVariable with appropriate EnvironmentVariableTarget, and includes comprehensive unit tests.
    ✨ Finishing touches
    🧪 Generate unit tests (beta)
    • Create PR with unit tests
    • Post copyable unit tests in a comment

    📜 Recent review details

    Configuration used: CodeRabbit UI

    Review profile: CHILL

    Plan: Pro

    📥 Commits

    Reviewing files that changed from the base of the PR and between 2048eae and 8b15ace.

    📒 Files selected for processing (1)
    • Src/AiCommitMessage/AiCommitMessage.csproj (1 hunks)
    ✅ Files skipped from review due to trivial changes (1)
    • Src/AiCommitMessage/AiCommitMessage.csproj
    ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
    • GitHub Check: Sourcery review
    • GitHub Check: Codacy Static Code Analysis

    Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

    ❤️ Share

    Comment @coderabbitai help to get the list of available commands and usage tips.

    @deepsource-io
    Copy link

    deepsource-io bot commented Dec 9, 2025

    Here's the code health analysis summary for commits a2f03c4..8b15ace. View details on DeepSource ↗.

    Analysis Summary

    AnalyzerStatusSummaryLink
    DeepSource Test coverage LogoTest coverage⚠️ Artifact not reportedTimed out: Artifact was never reportedView Check ↗
    DeepSource Secrets LogoSecrets✅ Success
    ❗ 1 occurence introduced
    View Check ↗
    DeepSource Docker LogoDocker✅ SuccessView Check ↗
    DeepSource C# LogoC#✅ SuccessView Check ↗

    💡 If you’re a repository administrator, you can configure the quality gates from the settings.

    @gstraccini gstraccini bot added enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. hacktoberfest Participation in the Hacktoberfest event 📝 documentation Tasks related to writing or updating documentation 🕓 medium effort A task that can be completed in a few hours 🧪 tests Tasks related to testing labels Dec 9, 2025
    @gstraccini gstraccini bot requested a review from guibranco December 9, 2025 18:09
    @gstraccini gstraccini bot added the 🚦 awaiting triage Items that are awaiting triage or categorization label Dec 9, 2025
    @penify-dev penify-dev bot added Tests Review effort [1-5]: 4 High review effort required for this pull request (effort level: 4) labels Dec 9, 2025
    @penify-dev
    Copy link
    Contributor

    penify-dev bot commented Dec 9, 2025

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    4, because the PR introduces new classes, methods, and a significant amount of documentation. The complexity of handling environment variables and ensuring proper error handling adds to the review effort.

    🧪 Relevant tests

    Yes

    ⚡ Possible issues

    No

    🔒 Security concerns

    No

    @penify-dev
    Copy link
    Contributor

    penify-dev bot commented Dec 9, 2025

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Testing
    Add a test case for handling permission errors when setting Machine-level environment variables

    Add a test case to verify that setting a Machine-level variable correctly handles
    permission issues.

    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs [141]

     /// <summary>
    -/// Tests that an error is returned when target is invalid.
    +/// Tests that setting a Machine-level variable returns an error when permissions are insufficient.
     /// </summary>
    +[Fact]
    +public void SetEnvironmentVariable_Should_SetExitCode_When_SettingMachineVariableWithoutPermissions()
    +{
    +    var options = new SetEnvironmentVariableOptions
    +    {
    +        Variable = "TEST_MACHINE_VAR=machine_value",
    +        Target = "Machine"
    +    };
     
    +    var originalExitCode = Environment.ExitCode;
    +
    +    try
    +    {
    +        EnvironmentVariableService.SetEnvironmentVariable(options);
    +        Environment.ExitCode.Should().Be(1);
    +    }
    +    finally
    +    {
    +        Environment.ExitCode = originalExitCode;
    +    }
    +}
    +
    Suggestion importance[1-10]: 9

    Why: This suggestion enhances test coverage by ensuring that permission issues are handled correctly, which is vital for the reliability of the application.

    9
    Possible bug
    Add exception handling for invalid variable names when setting environment variables

    Ensure that the method handles potential exceptions when setting environment variables,
    especially for invalid characters in the variable name.

    Src/AiCommitMessage/Services/EnvironmentVariableService.cs [71]

    -Environment.SetEnvironmentVariable(variableName, variableValue, envTarget);
    +try
    +{
    +    Environment.SetEnvironmentVariable(variableName, variableValue, envTarget);
    +}
    +catch (ArgumentException ex)
    +{
    +    ErrorLine($"Invalid argument: {ex.Message}");
    +}
     
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential bug by adding exception handling for invalid variable names, which is crucial for robustness.

    8
    Possible issue
    Validate the variable value for invalid characters before setting the environment variable

    Validate the variableValue to ensure it does not contain invalid characters before
    attempting to set the environment variable.

    Src/AiCommitMessage/Services/EnvironmentVariableService.cs [47-48]

     var variableValue = variable[1].Trim();
    +if (variableValue.Any(c => !char.IsLetterOrDigit(c) && c != '_' && c != '-'))
    +{
    +    ErrorLine("Variable value contains invalid characters.");
    +    return;
    +}
     
    Suggestion importance[1-10]: 7

    Why: This suggestion improves the validation of the variable value, which is important for preventing runtime errors, but it is not as critical as handling exceptions.

    7
    Enhancement
    Implement validation for the Target property to restrict it to valid values

    Consider adding validation to ensure that the Target property only accepts valid values.

    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs [23]

    -public string Target { get; set; }
    +private string target;
    +public string Target
    +{
    +    get => target;
    +    set
    +    {
    +        if (value != "User" && value != "Machine")
    +            throw new ArgumentException("Target must be 'User' or 'Machine'.");
    +        target = value;
    +    }
    +}
     
    Suggestion importance[1-10]: 6

    Why: While this suggestion improves the validation of the Target property, it is more of an enhancement than a critical fix, hence the moderate score.

    6

    Copy link
    Contributor

    @sourcery-ai sourcery-ai bot left a comment

    Choose a reason for hiding this comment

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

    Hey there - I've reviewed your changes - here's some feedback:

    Blocking issues:

    • Detected a Generic API Key, potentially exposing access to various services and sensitive operations. (link)

    General comments:

    • Consider treating a null or empty SetEnvironmentVariableOptions.Target as the default User target inside EnvironmentVariableService so the service is robust even when used outside the CommandLineParser context.
    • Relying on Environment.ExitCode mutation inside EnvironmentVariableService couples the service to process-level concerns; returning a status or throwing exceptions and letting Program decide the exit code would keep the service more reusable and easier to compose.
    Prompt for AI Agents
    Please address the comments from this code review:
    
    ## Overall Comments
    - Consider treating a null or empty `SetEnvironmentVariableOptions.Target` as the default `User` target inside `EnvironmentVariableService` so the service is robust even when used outside the CommandLineParser context.
    - Relying on `Environment.ExitCode` mutation inside `EnvironmentVariableService` couples the service to process-level concerns; returning a status or throwing exceptions and letting `Program` decide the exit code would keep the service more reusable and easier to compose.
    
    ## Individual Comments
    
    ### Comment 1
    <location> `Src/AiCommitMessage/Services/EnvironmentVariableService.cs:55-64` </location>
    <code_context>
    +        }
    +        else
    +        {
    +            ErrorLine($"Invalid target '{setEnvironmentVariableOptions.Target}'. Please use 'User' or 'Machine'.");
    +            return;
    +        }
    </code_context>
    
    <issue_to_address>
    **suggestion (bug_risk):** Align target handling with the documented default-to-User behavior and be more robust to null/empty values.
    
    The XML remarks state this should default to User scope if no target is specified, but the current logic errors on anything not exactly "User" or "Machine", including null/empty targets. To align with the docs and `[Option(..., Default = "User")]`, normalize `setEnvironmentVariableOptions.Target` first (treat null/whitespace as "User") and then validate/branch on that normalized value.
    
    ```suggestion
            var target = string.IsNullOrWhiteSpace(setEnvironmentVariableOptions.Target)
                ? "User"
                : setEnvironmentVariableOptions.Target;
    
            if (string.Equals(target, "Machine", StringComparison.OrdinalIgnoreCase))
            {
                envTarget = EnvironmentVariableTarget.Machine;
            }
            else if (string.Equals(target, "User", StringComparison.OrdinalIgnoreCase))
            {
                envTarget = EnvironmentVariableTarget.User;
            }
            else
            {
                ErrorLine($"Invalid target '{setEnvironmentVariableOptions.Target}'. Please use 'User' or 'Machine'.");
                return;
            }
    ```
    </issue_to_address>
    
    ### Comment 2
    <location> `Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs:37-14` </location>
    <code_context>
    +    /// Tests that setting a User-level environment variable works when target is not specified (default).
    +    /// </summary>
    +    [Fact]
    +    public void SetEnvironmentVariable_Should_SetUserVariable_When_TargetIsNotSpecified()
    +    {
    +        var options = new SetEnvironmentVariableOptions
    +        {
    +            Variable = "TEST_DEFAULT_VAR=default_value",
    </code_context>
    
    <issue_to_address>
    **issue (testing):** This test claims to cover the default target behavior but still sets `Target` explicitly to `"User"`, so it does not actually validate the default value.
    
    To validate the default behavior, construct `SetEnvironmentVariableOptions` without setting `Target` (or set it to `null` if you want to test defensive handling) and assert that the variable is written using `EnvironmentVariableTarget.User`. In its current form, this test just repeats the explicit-User case and doesn’t prove the default is applied.
    </issue_to_address>
    
    ### Comment 3
    <location> `Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs:70-79` </location>
    <code_context>
    +            Target = "User"
    +        };
    +
    +        var originalExitCode = Environment.ExitCode;
    +
    +        try
    +        {
    +            EnvironmentVariableService.SetEnvironmentVariable(options);
    +
    +            Environment.ExitCode.Should().Be(1);
    +        }
    +        finally
    +        {
    +            Environment.ExitCode = originalExitCode;
    +        }
    +    }
    </code_context>
    
    <issue_to_address>
    **suggestion (testing):** Multiple tests mutate the global `Environment.ExitCode`, which can cause flakiness if tests are ever run in parallel.
    
    Although each test locally saves/restores `Environment.ExitCode`, they still share a single global value, which is risky if tests run in parallel or more tests start using it. It would help to extract this pattern into a reusable helper (e.g., an `IDisposable` that manages the exit code) and/or mark these tests as non-parallel (e.g., via a collection fixture) to prevent cross-test interference.
    
    Suggested implementation:
    
    ```csharp
            var options = new SetEnvironmentVariableOptions
            {
                Variable = "INVALID_FORMAT",
                Target = "User"
            };
    
            using (new ExitCodeScope())
            {
                EnvironmentVariableService.SetEnvironmentVariable(options);
    
                Environment.ExitCode.Should().Be(1);
            }
    
    ```
    
    ```csharp
        private sealed class ExitCodeScope : IDisposable
        {
            private readonly int _originalExitCode;
    
            public ExitCodeScope()
            {
                _originalExitCode = Environment.ExitCode;
            }
    
            public void Dispose()
            {
                Environment.ExitCode = _originalExitCode;
            }
        }
    
        /// <summary>
        /// Tests that setting a User-level environment variable works when target is not specified (default).
        /// </summary>
        [Fact]
    
    ```
    
    1. Replace any other tests in `EnvironmentVariableServiceTests` (or elsewhere) that manually capture/restore `Environment.ExitCode` with `using (new ExitCodeScope()) { ... }` to centralize and harden this pattern.
    2. To fully address cross-test interference, consider defining a non-parallel xUnit collection for all tests that touch `Environment.ExitCode`:
       - Add a `[CollectionDefinition("EnvironmentExitCode", DisableParallelization = true)]` class in the test project.
       - Annotate the relevant test class(es) with `[Collection("EnvironmentExitCode")]`.
    </issue_to_address>
    
    ### Comment 4
    <location> `README.md:141` </location>
    <code_context>
    sk-abc123xyz
    </code_context>
    
    <issue_to_address>
    **security (generic-api-key):** Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
    
    *Source: gitleaks*
    </issue_to_address>

    Sourcery is free for open source - if you like our reviews please consider sharing them ✨
    Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 3

    🧹 Nitpick comments (3)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (1)

    46-47: Consider whether value trimming is always appropriate.

    Line 47 trims the variable value, which might not be desired in all cases. For example, if a user intentionally wants to set a value with leading or trailing whitespace, this will remove it.

    While trimming is generally helpful for user input, consider whether the value should be preserved as-is:

     var variableName = variable[0].Trim();
    -var variableValue = variable[1].Trim();
    +var variableValue = variable[1];

    Alternatively, document this behavior in the XML remarks or README to clarify that values are trimmed.

    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs (1)

    4-4: Remove extra blank line.

    CodeFactor flags multiple consecutive blank lines (SA1507). Remove one blank line for consistency with coding standards.

     namespace AiCommitMessage.Options;
    -
    
     
     /// <summary>
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (1)

    113-136: LGTM! Test correctly validates empty variable name handling.

    The test properly verifies that an empty variable name (format "=value") results in exit code 1 and restores the original exit code in the finally block.

    Note: CodeFactor flags extra blank lines at lines 115 and 132, but these are very minor formatting issues that can be addressed if desired:

         public void SetEnvironmentVariable_Should_SetExitCode_When_VariableNameIsEmpty()
         {
    -
             var options = new SetEnvironmentVariableOptions
             }
             finally
             {
    -
                 Environment.ExitCode = originalExitCode;
    📜 Review details

    Configuration used: CodeRabbit UI

    Review profile: CHILL

    Plan: Pro

    📥 Commits

    Reviewing files that changed from the base of the PR and between 99d1743 and 9de7159.

    📒 Files selected for processing (6)
    • README.md (2 hunks)
    • Src/AiCommitMessage/AiCommitMessage.csproj (1 hunks)
    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs (1 hunks)
    • Src/AiCommitMessage/Program.cs (4 hunks)
    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs (1 hunks)
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (1 hunks)
    🧰 Additional context used
    🧠 Learnings (4)
    📓 Common learnings
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:46.785Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader.GetEnvironmentVariable() method only reads environment variables from EnvironmentVariableTarget.User scope first, then EnvironmentVariableTarget.Machine as fallback. It does NOT read from EnvironmentVariableTarget.Process scope, so tests must set environment variables using EnvironmentVariableTarget.User to be properly detected by the application code.
    
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader class reads environment variables from EnvironmentVariableTarget.User first, then falls back to EnvironmentVariableTarget.Machine. It never reads from EnvironmentVariableTarget.Process scope, so test setups should use EnvironmentVariableTarget.User to match the application's behavior.
    
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the application code does not read environment variables from the EnvironmentVariableTarget.Process scope, so setting environment variables with this target in tests would be ineffective.
    
    📚 Learning: 2025-07-06T08:14:46.785Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:46.785Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader.GetEnvironmentVariable() method only reads environment variables from EnvironmentVariableTarget.User scope first, then EnvironmentVariableTarget.Machine as fallback. It does NOT read from EnvironmentVariableTarget.Process scope, so tests must set environment variables using EnvironmentVariableTarget.User to be properly detected by the application code.
    

    Applied to files:

    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    • README.md
    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    📚 Learning: 2025-07-06T08:14:02.619Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the application code does not read environment variables from the EnvironmentVariableTarget.Process scope, so setting environment variables with this target in tests would be ineffective.
    

    Applied to files:

    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    • README.md
    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    • Src/AiCommitMessage/Program.cs
    📚 Learning: 2025-07-06T08:14:02.619Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader class reads environment variables from EnvironmentVariableTarget.User first, then falls back to EnvironmentVariableTarget.Machine. It never reads from EnvironmentVariableTarget.Process scope, so test setups should use EnvironmentVariableTarget.User to match the application's behavior.
    

    Applied to files:

    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    • README.md
    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    🧬 Code graph analysis (3)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (1)
    Src/AiCommitMessage/Utility/Output.cs (1)
    • Output (5-70)
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (2)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (2)
    • EnvironmentVariableService (9-88)
    • SetEnvironmentVariable (31-87)
    Src/AiCommitMessage/Utility/EnvironmentLoader.cs (1)
    • GetEnvironmentVariable (169-188)
    Src/AiCommitMessage/Program.cs (1)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (2)
    • EnvironmentVariableService (9-88)
    • SetEnvironmentVariable (31-87)
    🪛 GitHub Check: CodeFactor
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs

    [notice] 115-115: Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs#L115
    An opening brace should not be followed by a blank line. (SA1505)


    [notice] 132-132: Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs#L132
    An opening brace should not be followed by a blank line. (SA1505)

    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs

    [notice] 4-4: Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs#L4
    Code should not contain multiple blank lines in a row. (SA1507)

    🪛 Gitleaks (8.30.0)
    README.md

    [high] 141-141: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.

    (generic-api-key)

    ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
    • GitHub Check: Socket Security: Pull Request Alerts
    • GitHub Check: Sourcery review
    • GitHub Check: Codacy Static Code Analysis
    🔇 Additional comments (12)
    README.md (1)

    113-162: LGTM! Comprehensive documentation for the new set-env command.

    The documentation is well-structured with clear syntax, parameter descriptions, target scope explanations, and practical examples covering both User and Machine scopes. The warning about administrator privileges for Machine scope is helpful.

    Note: The Gitleaks warning on line 141 is a false positive—it's example documentation showing the command syntax, not an actual API key.

    Src/AiCommitMessage/Program.cs (2)

    21-36: LGTM! Clean integration of SetEnvironmentVariableOptions into the parser.

    The integration follows the existing pattern for other options classes. Documentation is updated appropriately, and the new option is correctly added to the ParseArguments generic parameter list.


    55-81: LGTM! Proper routing for the set-env command.

    The new case statement correctly routes SetEnvironmentVariableOptions to EnvironmentVariableService.SetEnvironmentVariable, following the same pattern as other commands. Documentation accurately describes the behavior.

    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (3)

    15-19: LGTM! Helper method for consistent error handling.

    The ErrorLine helper correctly outputs errors and sets the exit code to 1, providing consistent error handling throughout the service.


    33-52: LGTM! Robust input validation.

    The validation logic correctly handles:

    • Null or empty variables
    • Invalid format (missing '=')
    • Split with limit 2 to preserve '=' characters in values
    • Trimmed names and values
    • Empty variable names after trimming

    54-67: LGTM! Case-insensitive target validation.

    The target validation correctly handles case-insensitive matching for "User" and "Machine", with appropriate error messaging for invalid targets.

    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs (1)

    1-25: LGTM! Options class correctly configured for CommandLineParser.

    The class is properly structured with:

    • Verb attribute for "set-env"
    • Required positional Value parameter for the variable
    • Optional Target option with sensible default ("User")
    • Clear help text for both properties
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (5)

    11-31: LGTM! Test correctly validates User target behavior.

    The test properly sets an environment variable with User target, verifies it was set correctly, and cleans up in the finally block. The use of EnvironmentVariableTarget.User is consistent with the application's environment variable handling.


    61-82: LGTM! Test correctly validates invalid format handling.

    The test properly verifies that an invalid format (missing '=') results in exit code 1 and restores the original exit code in the finally block.


    87-108: LGTM! Test correctly validates null variable handling.

    The test properly verifies that a null variable results in exit code 1 and restores the original exit code in the finally block.


    141-162: LGTM! Test correctly validates invalid target handling.

    The test properly verifies that an invalid target value results in exit code 1 and restores the original exit code in the finally block.


    167-191: LGTM! Excellent test coverage for case-insensitive targets.

    The Theory test with InlineData correctly verifies that target matching is case-insensitive for various input cases ("user", "USER", "User", "uSeR"), and properly cleans up the test variable. This ensures the implementation is user-friendly.

    @guibranco
    Copy link
    Owner

    @gstraccini csharpier

    @gstraccini
    Copy link
    Contributor

    gstraccini bot commented Dec 10, 2025

    Running CSharpier on this branch! 🔧

    @gstraccini
    Copy link
    Contributor

    gstraccini bot commented Dec 10, 2025

    CSharpier failed!

    @AmarYasser1
    Copy link
    Author

    Hi @guibranco

    Thank you so much for reviewing and approving my PR! .

    I've now addressed everything:

    Fixed:

    • Applied CSharpier formatting to all files
    • Removed extra blank lines (CodeFactor issues)
    • Fixed all linter warnings in my code
    • Build succeeded

    ⚠️ Note on Build Warnings:
    The 9 build warnings are in pre-existing Cache files (FileCacheProvider. cs, ICacheProvider.cs, CommitMessageCacheService.cs) and not related to my changes. My code builds without any warnings.

    Everything should be green now!

    Thanks again for maintaining this excellent project and for your guidance throughout the review process. I've learned a lot!

    @guibranco guibranco removed the 🚦 awaiting triage Items that are awaiting triage or categorization label Dec 11, 2025
    Copy link
    Contributor

    @coderabbitai coderabbitai bot left a comment

    Choose a reason for hiding this comment

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

    Actionable comments posted: 1

    Caution

    Some comments are outside the diff and can’t be posted inline due to platform limitations.

    ⚠️ Outside diff range comments (1)
    Src/AiCommitMessage/Program.cs (1)

    43-58: Doc comment still says “three” cases but Run now handles four.

    Update the remarks to say “four specific cases” so the documentation matches the actual switch cases.

    -    /// This method uses a switch statement to determine the type of action to perform based on the
    -    /// runtime type of the <paramref name="options"/> parameter. It handles three specific cases:
    +    /// This method uses a switch statement to determine the type of action to perform based on the
    +    /// runtime type of the <paramref name="options"/> parameter. It handles four specific cases:
    ♻️ Duplicate comments (3)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (2)

    57-84: Normalize null/whitespace Target to "User" to match remarks and intended default.

    Right now, anything other than "User"/"Machine" (including null/whitespace) is treated as invalid, even though the remarks say it should default to User scope when not specified. Normalizing to "User" first makes this method safer for non-CLI callers and aligns it with the documented behavior.

    -        EnvironmentVariableTarget envTarget;
    -        if (
    -            string.Equals(
    -                setEnvironmentVariableOptions.Target,
    -                "Machine",
    -                StringComparison.OrdinalIgnoreCase
    -            )
    -        )
    +        var target = string.IsNullOrWhiteSpace(setEnvironmentVariableOptions.Target)
    +            ? "User"
    +            : setEnvironmentVariableOptions.Target;
    +
    +        EnvironmentVariableTarget envTarget;
    +        if (
    +            string.Equals(
    +                target,
    +                "Machine",
    +                StringComparison.OrdinalIgnoreCase
    +            )
    +        )
             {
                 envTarget = EnvironmentVariableTarget.Machine;
             }
             else if (
                 string.Equals(
    -                setEnvironmentVariableOptions.Target,
    +                target,
                     "User",
                     StringComparison.OrdinalIgnoreCase
                 )
             )
             {
                 envTarget = EnvironmentVariableTarget.User;
             }
             else
             {
                 ErrorLine(
                     $"Invalid target '{setEnvironmentVariableOptions.Target}'. Please use 'User' or 'Machine'."
                 );
                 return;
             }

    86-92: Avoid logging full environment variable values (possible secret leak).

    The success message currently prints the entire value, which is risky for secrets (API keys, tokens, passwords, etc.). It’s safer to log only the name and scope, or a masked value.

    -            Output.InfoLine(
    -                $"Environment variable '{variableName}' set to '{variableValue}' for {envTarget} scope."
    -            );
    +            Output.InfoLine(
    +                $"Environment variable '{variableName}' set successfully for {envTarget} scope."
    +            );
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (1)

    41-71: “Target not specified” test currently sets Target explicitly, so it doesn’t cover the default behavior.

    The test name and XML comment say it verifies the default User scope when target is not specified, but it still sets Target = "User". Either remove the explicit assignment to actually test the default, or rename the test. Removing the assignment is preferable, especially if SetEnvironmentVariable is updated to treat null/whitespace Target as User as suggested earlier.

    -        var options = new SetEnvironmentVariableOptions
    -        {
    -            Variable = "TEST_DEFAULT_VAR=default_value",
    -            Target = "User",
    -        };
    +        var options = new SetEnvironmentVariableOptions
    +        {
    +            Variable = "TEST_DEFAULT_VAR=default_value",
    +        };
    📜 Review details

    Configuration used: CodeRabbit UI

    Review profile: CHILL

    Plan: Pro

    📥 Commits

    Reviewing files that changed from the base of the PR and between 577308e and 2048eae.

    📒 Files selected for processing (5)
    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs (1 hunks)
    • Src/AiCommitMessage/Program.cs (4 hunks)
    • Src/AiCommitMessage/Services/Cache/CommitMessageCacheService.cs (2 hunks)
    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs (1 hunks)
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (1 hunks)
    🧰 Additional context used
    🧠 Learnings (4)
    📓 Common learnings
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader class reads environment variables from EnvironmentVariableTarget.User first, then falls back to EnvironmentVariableTarget.Machine. It never reads from EnvironmentVariableTarget.Process scope, so test setups should use EnvironmentVariableTarget.User to match the application's behavior.
    
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:46.785Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader.GetEnvironmentVariable() method only reads environment variables from EnvironmentVariableTarget.User scope first, then EnvironmentVariableTarget.Machine as fallback. It does NOT read from EnvironmentVariableTarget.Process scope, so tests must set environment variables using EnvironmentVariableTarget.User to be properly detected by the application code.
    
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the application code does not read environment variables from the EnvironmentVariableTarget.Process scope, so setting environment variables with this target in tests would be ineffective.
    
    📚 Learning: 2025-07-06T08:14:02.619Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the application code does not read environment variables from the EnvironmentVariableTarget.Process scope, so setting environment variables with this target in tests would be ineffective.
    

    Applied to files:

    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    • Src/AiCommitMessage/Program.cs
    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    📚 Learning: 2025-07-06T08:14:46.785Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:46.785Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader.GetEnvironmentVariable() method only reads environment variables from EnvironmentVariableTarget.User scope first, then EnvironmentVariableTarget.Machine as fallback. It does NOT read from EnvironmentVariableTarget.Process scope, so tests must set environment variables using EnvironmentVariableTarget.User to be properly detected by the application code.
    

    Applied to files:

    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    • Src/AiCommitMessage/Program.cs
    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    📚 Learning: 2025-07-06T08:14:02.619Z
    Learnt from: krisbiradar
    Repo: guibranco/dotnet-aicommitmessage PR: 251
    File: Tests/AiCommitMessage.Tests/Services/GenerateCommitMessageServiceTests.cs:16-16
    Timestamp: 2025-07-06T08:14:02.619Z
    Learning: In the dotnet-aicommitmessage project, the EnvironmentLoader class reads environment variables from EnvironmentVariableTarget.User first, then falls back to EnvironmentVariableTarget.Machine. It never reads from EnvironmentVariableTarget.Process scope, so test setups should use EnvironmentVariableTarget.User to match the application's behavior.
    

    Applied to files:

    • Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs
    • Src/AiCommitMessage/Program.cs
    • Src/AiCommitMessage/Services/EnvironmentVariableService.cs
    • Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs
    🧬 Code graph analysis (3)
    Src/AiCommitMessage/Program.cs (1)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (2)
    • EnvironmentVariableService (9-109)
    • SetEnvironmentVariable (32-108)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (1)
    Src/AiCommitMessage/Utility/Output.cs (5)
    • ErrorLine (24-24)
    • ErrorLine (36-37)
    • Output (5-70)
    • InfoLine (7-7)
    • InfoLine (21-22)
    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (2)
    Src/AiCommitMessage/Services/EnvironmentVariableService.cs (2)
    • EnvironmentVariableService (9-109)
    • SetEnvironmentVariable (32-108)
    Src/AiCommitMessage/Utility/EnvironmentLoader.cs (1)
    • GetEnvironmentVariable (169-188)
    ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
    • GitHub Check: Codacy Static Code Analysis
    • GitHub Check: Sourcery review
    🔇 Additional comments (3)
    Src/AiCommitMessage/Options/SetEnvironmentVariableOptions.cs (1)

    5-29: Options shape for set-env looks solid.

    Verb name, required VAR_NAME=value argument, and optional --target defaulting to "User" are consistent with the rest of the CLI and the PR requirements.

    Src/AiCommitMessage/Program.cs (1)

    65-81: set-env dispatch wiring looks correct.

    Routing SetEnvironmentVariableOptions to EnvironmentVariableService.SetEnvironmentVariable fits the existing pattern for other verbs and keeps Program.cs nicely centralized.

    Tests/AiCommitMessage.Tests/Services/EnvironmentVariableServiceTests.cs (1)

    7-203: Good coverage of success and error paths.

    These tests exercise happy paths, invalid formats, null/empty variables, invalid targets, and case-insensitive target handling, and they clean up both environment variables and Environment.ExitCode properly.

    @AmarYasser1
    Copy link
    Author

    AmarYasser1 commented Dec 13, 2025

    @guibranco

    Hi! 👋 Hope you're doing well!

    Just checking in on the PR status.

    Current situation:

    • ✅ 18 checks passing successfully
    • ✅ Code approved by 2 reviewers
    • ✅ Blocking conversation resolved
    • 2 required checks (Build + GStraccini Commit) have been "Expected — Waiting for status" for ~2 days
    • 6 checks failing due to external service issues (DeepSource timeouts, Infisical, SonarCloud, etc.)

    Question:
    The pending checks appear to be waiting for workflow approval. Do these need to be manually approved for fork PRs, or is there a CI configuration issue?

    Happy to help troubleshoot if needed! 😊

    Thanks for your time!

    @AmarYasser1
    Copy link
    Author

    @guibranco

    Hi!

    I just wanted to check in and see if there’s anything blocking this on my end, or if you’re waiting for anything from me.

    Please let me know if any changes are needed. I noticed the workflows are still pending approval, and I’m happy to help troubleshoot if required.

    Thanks for your time!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. hacktoberfest Participation in the Hacktoberfest event 🕓 medium effort A task that can be completed in a few hours Review effort [1-5]: 4 High review effort required for this pull request (effort level: 4) 🧪 tests Tasks related to testing Tests

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    [FEATURE] Add Command to Set Environment Variable

    2 participants