< Summary

Information
Class: Orchestrator.EnvironmentHelper
Assembly: Orchestrator
File(s): /home/runner/work/KicktippAi/KicktippAi/src/Orchestrator/EnvironmentHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 103
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LoadEnvironmentVariables(...)0%620%
LoadFirebaseCredentials(...)0%110100%

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
 014            var envPath = PathUtility.GetEnvFilePath("Orchestrator");
 15
 016            if (File.Exists(envPath))
 17            {
 018                Env.Load(envPath);
 019                logger.LogInformation("Loaded .env file from: {EnvPath}", envPath);
 20            }
 21            else
 22            {
 023                logger.LogWarning("No .env file found at: {EnvPath}", envPath);
 024                logger.LogInformation("Please create a .env file in the secrets directory based on .env.example");
 025                logger.LogInformation("Alternatively, set environment variables directly");
 26            }
 27
 28            // Load Firebase credentials if available
 029            LoadFirebaseCredentials(logger);
 030        }
 031        catch (Exception ex)
 32        {
 033            logger.LogWarning(ex, "Could not load .env file: {Message}", ex.Message);
 034        }
 035    }
 36
 37    private static void LoadFirebaseCredentials(ILogger logger)
 38    {
 39        try
 40        {
 41            // Check if Firebase credentials are already set via environment variables
 042            var existingFirebaseJson = Environment.GetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON");
 043            var existingProjectId = Environment.GetEnvironmentVariable("FIREBASE_PROJECT_ID");
 44
 045            if (!string.IsNullOrEmpty(existingFirebaseJson) && !string.IsNullOrEmpty(existingProjectId))
 46            {
 047                logger.LogInformation("Firebase credentials already set via environment variables");
 048                return;
 49            }
 50
 51            // Try to load from firebase.json file
 052            var firebaseJsonPath = PathUtility.GetFirebaseJsonPath();
 53
 054            if (File.Exists(firebaseJsonPath))
 55            {
 056                var firebaseJson = File.ReadAllText(firebaseJsonPath);
 57
 58                // Parse the JSON to extract project_id
 59                try
 60                {
 061                    using var document = JsonDocument.Parse(firebaseJson);
 062                    var root = document.RootElement;
 63
 064                    if (root.TryGetProperty("project_id", out var projectIdElement))
 65                    {
 066                        var projectId = projectIdElement.GetString();
 67
 068                        if (!string.IsNullOrEmpty(projectId))
 69                        {
 70                            // Set both environment variables
 071                            Environment.SetEnvironmentVariable("FIREBASE_SERVICE_ACCOUNT_JSON", firebaseJson);
 072                            Environment.SetEnvironmentVariable("FIREBASE_PROJECT_ID", projectId);
 73
 074                            logger.LogInformation("Loaded Firebase credentials from: {FirebasePath}", firebaseJsonPath);
 075                            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                    }
 086                }
 087                catch (JsonException ex)
 88                {
 089                    logger.LogError(ex, "Failed to parse Firebase JSON file: {Message}", ex.Message);
 090                }
 91            }
 92            else
 93            {
 094                logger.LogInformation("No Firebase credentials file found at: {FirebasePath}", firebaseJsonPath);
 095                logger.LogInformation("Firebase integration will be disabled unless FIREBASE_PROJECT_ID and FIREBASE_SER
 96            }
 097        }
 098        catch (Exception ex)
 99        {
 0100            logger.LogWarning(ex, "Could not load Firebase credentials: {Message}", ex.Message);
 0101        }
 0102    }
 103}