< Summary

Information
Class: Orchestrator.EnvironmentHelper
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/EnvironmentHelper.cs
Line coverage
84%
Covered lines: 49
Uncovered lines: 9
Coverable lines: 58
Total lines: 135
Line coverage: 84.4%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LoadEnvironmentVariables(...)100%2276.92%
LoadCommunityKicktippCredentials(...)100%22100%
LoadFirebaseCredentials(...)80%101083.33%
EnsureRequiredEnvironmentVariableIsPresent(...)50%2266.67%

File(s)

/home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/EnvironmentHelper.cs

#LineLine coverage
 1using DotNetEnv;
 2using Microsoft.Extensions.Logging;
 3using System.Text.Json;
 4
 5namespace Orchestrator;
 6
 7public static class EnvironmentHelper
 8{
 9    private const string KicktippUsernameEnvVar = "KICKTIPP_USERNAME";
 10    private const string KicktippPasswordEnvVar = "KICKTIPP_PASSWORD";
 11
 12    public static void LoadEnvironmentVariables(ILogger logger)
 13    {
 14        try
 15        {
 16            // Use PathUtility to get the correct .env file path
 117            var envPath = PathUtility.GetEnvFilePath("Orchestrator");
 18
 119            if (File.Exists(envPath))
 20            {
 121                Env.Load(envPath);
 122                logger.LogInformation("Loaded .env file from: {EnvPath}", envPath);
 23            }
 24            else
 25            {
 126                logger.LogWarning("No .env file found at: {EnvPath}", envPath);
 127                logger.LogInformation("Please create a .env file in the secrets directory based on .env.example");
 128                logger.LogInformation("Alternatively, set environment variables directly");
 29            }
 30
 31            // Load Firebase credentials if available
 132            LoadFirebaseCredentials(logger);
 133        }
 034        catch (Exception ex)
 35        {
 036            logger.LogWarning(ex, "Could not load .env file: {Message}", ex.Message);
 037        }
 138    }
 39
 40    public static void LoadCommunityKicktippCredentials(ILogger logger, string community)
 41    {
 142        ArgumentException.ThrowIfNullOrWhiteSpace(community);
 43
 144        var envPath = PathUtility.GetEnvFilePath("Orchestrator", community.Trim());
 145        if (!File.Exists(envPath))
 46        {
 147            logger.LogWarning(
 148                "No community-specific Kicktipp credentials file found at: {EnvPath}. Existing environment variables wil
 149                envPath);
 150            return;
 51        }
 52
 153        Env.Load(envPath);
 54
 155        EnsureRequiredEnvironmentVariableIsPresent(KicktippUsernameEnvVar, envPath);
 156        EnsureRequiredEnvironmentVariableIsPresent(KicktippPasswordEnvVar, envPath);
 57
 158        logger.LogInformation("Loaded community-specific Kicktipp credentials from: {EnvPath}", envPath);
 159    }
 60
 61    private static void LoadFirebaseCredentials(ILogger logger)
 62    {
 63        try
 64        {
 65            // Check if Firebase credentials are already set via environment variables
 166            var existingFirebaseJson = Environment.GetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON");
 167            var existingProjectId = Environment.GetEnvironmentVariable("FIREBASE_PROJECT_ID");
 68
 169            if (!string.IsNullOrEmpty(existingFirebaseJson) && !string.IsNullOrEmpty(existingProjectId))
 70            {
 171                logger.LogInformation("Firebase credentials already set via environment variables");
 172                return;
 73            }
 74
 75            // Try to load from firebase.json file
 176            var firebaseJsonPath = PathUtility.GetFirebaseJsonPath();
 77
 178            if (File.Exists(firebaseJsonPath))
 79            {
 180                var firebaseJson = File.ReadAllText(firebaseJsonPath);
 81
 82                // Parse the JSON to extract project_id
 83                try
 84                {
 185                    using var document = JsonDocument.Parse(firebaseJson);
 186                    var root = document.RootElement;
 87
 188                    if (root.TryGetProperty("project_id", out var projectIdElement))
 89                    {
 190                        var projectId = projectIdElement.GetString();
 91
 192                        if (!string.IsNullOrEmpty(projectId))
 93                        {
 94                            // Set both environment variables
 195                            Environment.SetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON", firebaseJson);
 196                            Environment.SetEnvironmentVariable("FIREBASE_PROJECT_ID", projectId);
 97
 198                            logger.LogInformation("Loaded Firebase credentials from: {FirebasePath}", firebaseJsonPath);
 199                            logger.LogInformation("Firebase project ID: {ProjectId}", projectId);
 100                        }
 101                        else
 102                        {
 0103                            logger.LogWarning("Firebase JSON file is missing or has empty project_id field");
 104                        }
 105                    }
 106                    else
 107                    {
 0108                        logger.LogWarning("Firebase JSON file is missing project_id field");
 109                    }
 1110                }
 1111                catch (JsonException ex)
 112                {
 1113                    logger.LogError(ex, "Failed to parse Firebase JSON file: {Message}", ex.Message);
 1114                }
 115            }
 116            else
 117            {
 1118                logger.LogInformation("No Firebase credentials file found at: {FirebasePath}", firebaseJsonPath);
 1119                logger.LogInformation("Firebase integration will be disabled unless FIREBASE_PROJECT_ID and FIREBASE_SER
 120            }
 1121        }
 0122        catch (Exception ex)
 123        {
 0124            logger.LogWarning(ex, "Could not load Firebase credentials: {Message}", ex.Message);
 0125        }
 1126    }
 127
 128    private static void EnsureRequiredEnvironmentVariableIsPresent(string variableName, string envPath)
 129    {
 1130        if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(variableName)))
 131        {
 0132            throw new InvalidOperationException($"{Path.GetFileName(envPath)} must define {variableName}.");
 133        }
 1134    }
 135}