< Summary

Information
Class: Orchestrator.EnvironmentHelper
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/EnvironmentHelper.cs
Line coverage
81%
Covered lines: 35
Uncovered lines: 8
Coverable lines: 43
Total lines: 103
Line coverage: 81.3%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
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%
LoadFirebaseCredentials(...)80%101083.33%

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    public static void LoadEnvironmentVariables(ILogger logger)
 10    {
 11        try
 12        {
 13            // Use PathUtility to get the correct .env file path
 114            var envPath = PathUtility.GetEnvFilePath("Orchestrator");
 15
 116            if (File.Exists(envPath))
 17            {
 118                Env.Load(envPath);
 119                logger.LogInformation("Loaded .env file from: {EnvPath}", envPath);
 20            }
 21            else
 22            {
 123                logger.LogWarning("No .env file found at: {EnvPath}", envPath);
 124                logger.LogInformation("Please create a .env file in the secrets directory based on .env.example");
 125                logger.LogInformation("Alternatively, set environment variables directly");
 26            }
 27
 28            // Load Firebase credentials if available
 129            LoadFirebaseCredentials(logger);
 130        }
 031        catch (Exception ex)
 32        {
 033            logger.LogWarning(ex, "Could not load .env file: {Message}", ex.Message);
 034        }
 135    }
 36
 37    private static void LoadFirebaseCredentials(ILogger logger)
 38    {
 39        try
 40        {
 41            // Check if Firebase credentials are already set via environment variables
 142            var existingFirebaseJson = Environment.GetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON");
 143            var existingProjectId = Environment.GetEnvironmentVariable("FIREBASE_PROJECT_ID");
 44
 145            if (!string.IsNullOrEmpty(existingFirebaseJson) && !string.IsNullOrEmpty(existingProjectId))
 46            {
 147                logger.LogInformation("Firebase credentials already set via environment variables");
 148                return;
 49            }
 50
 51            // Try to load from firebase.json file
 152            var firebaseJsonPath = PathUtility.GetFirebaseJsonPath();
 53
 154            if (File.Exists(firebaseJsonPath))
 55            {
 156                var firebaseJson = File.ReadAllText(firebaseJsonPath);
 57
 58                // Parse the JSON to extract project_id
 59                try
 60                {
 161                    using var document = JsonDocument.Parse(firebaseJson);
 162                    var root = document.RootElement;
 63
 164                    if (root.TryGetProperty("project_id", out var projectIdElement))
 65                    {
 166                        var projectId = projectIdElement.GetString();
 67
 168                        if (!string.IsNullOrEmpty(projectId))
 69                        {
 70                            // Set both environment variables
 171                            Environment.SetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON", firebaseJson);
 172                            Environment.SetEnvironmentVariable("FIREBASE_PROJECT_ID", projectId);
 73
 174                            logger.LogInformation("Loaded Firebase credentials from: {FirebasePath}", firebaseJsonPath);
 175                            logger.LogInformation("Firebase project ID: {ProjectId}", projectId);
 76                        }
 77                        else
 78                        {
 079                            logger.LogWarning("Firebase JSON file is missing or has empty project_id field");
 80                        }
 81                    }
 82                    else
 83                    {
 084                        logger.LogWarning("Firebase JSON file is missing project_id field");
 85                    }
 186                }
 187                catch (JsonException ex)
 88                {
 189                    logger.LogError(ex, "Failed to parse Firebase JSON file: {Message}", ex.Message);
 190                }
 91            }
 92            else
 93            {
 194                logger.LogInformation("No Firebase credentials file found at: {FirebasePath}", firebaseJsonPath);
 195                logger.LogInformation("Firebase integration will be disabled unless FIREBASE_PROJECT_ID and FIREBASE_SER
 96            }
 197        }
 098        catch (Exception ex)
 99        {
 0100            logger.LogWarning(ex, "Could not load Firebase credentials: {Message}", ex.Message);
 0101        }
 1102    }
 103}