diff --git a/src/DevOpsMetrics.Service/Controllers/DORASummaryController.cs b/src/DevOpsMetrics.Service/Controllers/DORASummaryController.cs index e48995f7..f677b69d 100644 --- a/src/DevOpsMetrics.Service/Controllers/DORASummaryController.cs +++ b/src/DevOpsMetrics.Service/Controllers/DORASummaryController.cs @@ -74,8 +74,7 @@ public async Task UpdateDORASummaryItem( clientSecret = Configuration[clientSecretName]; if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret)) { - errorMessage = $"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault"; - throw new Exception(errorMessage); + errorMessage = $"clientId '{clientIdName}' or clientSecret '{clientSecretName}' not found in key vault"; } } else @@ -85,11 +84,23 @@ public async Task UpdateDORASummaryItem( if (string.IsNullOrEmpty(patToken)) { errorMessage = $"patToken '{patTokenName}' not found in key vault"; - throw new Exception(errorMessage); } } ProcessingResult result = new(); + //If the settings are missing, skip the processing and return an empty result + if (string.IsNullOrEmpty(errorMessage) == false) + { + if (log == null) + { + Console.WriteLine(errorMessage); + } + else + { + log.LogInformation(errorMessage); + } + return result; + } DeploymentFrequencyModel deploymentFrequencyModel = new(); LeadTimeForChangesModel leadTimeForChangesModel = new(); MeanTimeToRestoreModel meanTimeToRestoreModel = new(); @@ -108,12 +119,10 @@ public async Task UpdateDORASummaryItem( //TODO: fix this - should be using a common interface, not this null hack if (log == null) { - Console.WriteLine(errorMessage); Console.WriteLine(message); } else { - log.LogInformation(errorMessage); log.LogInformation(message); } if (useParallelProcessing) diff --git a/src/DevOpsMetrics.Service/Controllers/SettingsController.cs b/src/DevOpsMetrics.Service/Controllers/SettingsController.cs index d4429fb6..ebbfefbb 100644 --- a/src/DevOpsMetrics.Service/Controllers/SettingsController.cs +++ b/src/DevOpsMetrics.Service/Controllers/SettingsController.cs @@ -31,6 +31,11 @@ public SettingsController(IConfiguration configuration, IAzureTableStorageDA azu public async Task> GetAzureDevOpsSettings(string rowKey = null) { TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + //If the storage settings are missing, return an empty list instead of throwing an error + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableAzureDevOpsSettings) == false) + { + return new List(); + } List settings = await AzureTableStorageDA.GetAzureDevOpsSettingsFromStorage(tableStorageConfig, tableStorageConfig.TableAzureDevOpsSettings, rowKey); return settings; } @@ -39,6 +44,11 @@ public async Task> GetAzureDevOpsSettings(string rowKe public async Task> GetGitHubSettings(string rowKey = null) { TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + //If the storage settings are missing, return an empty list instead of throwing an error + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableGitHubSettings) == false) + { + return new List(); + } List settings = await AzureTableStorageDA.GetGitHubSettingsFromStorage(tableStorageConfig, tableStorageConfig.TableGitHubSettings, rowKey); return settings; } @@ -59,6 +69,10 @@ public async Task UpdateAzureDevOpsSetting(string patToken, //Save everything else to table storage TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableAzureDevOpsSettings) == false) + { + return false; + } return await AzureTableStorageDA.UpdateAzureDevOpsSettingInStorage(tableStorageConfig, tableStorageConfig.TableAzureDevOpsSettings, organization, project, repository, branch, buildName, buildId, resourceGroup, itemOrder, showSetting); } @@ -85,6 +99,10 @@ public async Task UpdateGitHubSetting(string clientId, string clientSecret //Save everything else to table storage TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableGitHubSettings) == false) + { + return false; + } return await AzureTableStorageDA.UpdateGitHubSettingInStorage(tableStorageConfig, tableStorageConfig.TableGitHubSettings, owner, repo, branch, workflowName, workflowId, resourceGroup, itemOrder, showSetting); } @@ -93,6 +111,10 @@ public async Task UpdateGitHubSetting(string clientId, string clientSecret public async Task UpdateDevOpsMonitoringEvent([FromBody] MonitoringEvent monitoringEvent) { TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableLog) == false) + { + return false; + } return await AzureTableStorageDA.UpdateDevOpsMonitoringEventInStorage(tableStorageConfig, monitoringEvent); } @@ -102,6 +124,10 @@ public async Task> GetAzureDevOpsProjectLog(string organization string partitionKey = PartitionKeys.CreateAzureDevOpsSettingsPartitionKey(organization, project, repository); TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableLog) == false) + { + return new List(); + } return await AzureTableStorageDA.GetProjectLogsFromStorage(tableStorageConfig, partitionKey); } @@ -115,6 +141,10 @@ public async Task UpdateAzureDevOpsProjectLog(string organization, string buildsUpdated, prsUpdated, HttpUtility.UrlDecode(buildUrl), HttpUtility.UrlDecode(prUrl), exceptionMessage, exceptionStackTrace); TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableLog) == false) + { + return false; + } return await AzureTableStorageDA.UpdateProjectLogInStorage(tableStorageConfig, log); } @@ -124,6 +154,10 @@ public async Task> GetGitHubProjectLog(string owner, string rep string partitionKey = PartitionKeys.CreateGitHubSettingsPartitionKey(owner, repo); TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableLog) == false) + { + return new List(); + } return await AzureTableStorageDA.GetProjectLogsFromStorage(tableStorageConfig, partitionKey); } @@ -137,12 +171,29 @@ public async Task UpdateGitHubProjectLog(string owner, string repo, buildsUpdated, prsUpdated, HttpUtility.UrlDecode(buildUrl), HttpUtility.UrlDecode(prUrl), exceptionMessage, exceptionStackTrace); TableStorageConfiguration tableStorageConfig = Common.GenerateTableStorageConfiguration(Configuration); + if (IsTableStorageConfigured(tableStorageConfig, tableStorageConfig.TableLog) == false) + { + return false; + } return await AzureTableStorageDA.UpdateProjectLogInStorage(tableStorageConfig, log); } + //If the table storage settings aren't available, we skip the call rather than throwing an error + private static bool IsTableStorageConfigured(TableStorageConfiguration tableStorageConfig, string tableName) + { + return tableStorageConfig != null && + string.IsNullOrEmpty(tableStorageConfig.StorageAccountConnectionString) == false && + string.IsNullOrEmpty(tableName) == false; + } + private async Task CreateKeyVaultSecret(string secretName, string secretValue) { string keyVaultURI = Configuration["AppSettings:KeyVaultURL"]; + //If the key vault isn't setup, or the secret is empty, skip saving the secret rather than throwing an error + if (string.IsNullOrEmpty(keyVaultURI) || string.IsNullOrEmpty(secretValue)) + { + return null; + } SecretClient secretClient = new(new Uri(keyVaultURI), new DefaultAzureCredential()); return await secretClient.SetSecretAsync(secretName, secretValue); } diff --git a/src/DevOpsMetrics.Tests/Service/TableStorageControllerDATests.cs b/src/DevOpsMetrics.Tests/Service/TableStorageControllerDATests.cs index 978b836c..9fcb6298 100644 --- a/src/DevOpsMetrics.Tests/Service/TableStorageControllerDATests.cs +++ b/src/DevOpsMetrics.Tests/Service/TableStorageControllerDATests.cs @@ -231,7 +231,7 @@ public async Task UpdateAzureDevOpsSettingTest() bool result = await controller.UpdateAzureDevOpsSetting(patToken, organization, project, repository, branch, buildName, buildId, resourceGroup, itemOrder, showSetting); //Assert - Assert.AreEqual(true, result); + Assert.AreEqual(false, result); } [TestMethod] @@ -256,7 +256,7 @@ public async Task UpdateGitHubSettingTest() bool result = await controller.UpdateGitHubSetting(clientId, clientSecret, owner, repo, branch, workflowName, workflowId, resourceGroup, itemOrder, showSetting); //Assert - Assert.AreEqual(true, result); + Assert.AreEqual(false, result); } [TestMethod] @@ -272,7 +272,7 @@ public async Task UpdateDevOpsMonitoringEventTest() bool result = await controller.UpdateDevOpsMonitoringEvent(newEvent); //Assert - Assert.AreEqual(true, result); + Assert.AreEqual(false, result); } [TestMethod] @@ -317,7 +317,7 @@ public async Task UpdateAzureDevOpsLogTest() buildsUpdated, prsUpdated, buildUrl, prUrl, exceptionMessage, exceptionStackTrace); //Assert - Assert.AreEqual(true, result); + Assert.AreEqual(false, result); } [TestMethod] @@ -361,7 +361,7 @@ public async Task UpdateGitHubLogTest() buildsUpdated, prsUpdated, buildUrl, prUrl, exceptionMessage, exceptionStackTrace); //Assert - Assert.AreEqual(true, result); + Assert.AreEqual(false, result); } private static int GetSampleUpdateData()