Skip to content

Commit

Permalink
Merge pull request #872 from DeveloperMetrics/ImprovementsToHandleInv…
Browse files Browse the repository at this point in the history
…alidRequests

Improvements to handle invalid requests
  • Loading branch information
samsmithnz authored Jun 8, 2023
2 parents 2526067 + 3ed708d commit 8d7fef7
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/DevOpsMetrics.Cmd/ServiceApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<bool> UpdateDevOpsMonitoringEvent(MonitoringEvent monitoringEv
return await PostResponse(Client, url, monitoringEvent);
}

private async Task<T> GetResponse<T>(HttpClient client, string url)
private static async Task<T> GetResponse<T>(HttpClient client, string url)
{
T obj = default;
if (client != null && url != null)
Expand All @@ -74,12 +74,12 @@ private async Task<T> GetResponse<T>(HttpClient client, string url)
return obj;
}

private async Task<bool> PostResponse(HttpClient client, string url, MonitoringEvent monitoringEvent)
private static async Task<bool> PostResponse(HttpClient client, string url, MonitoringEvent monitoringEvent)
{

if (client != null && url != null)
{
StringContent content = new StringContent(JsonConvert.SerializeObject(monitoringEvent), Encoding.UTF8, "application/json");
StringContent content = new(JsonConvert.SerializeObject(monitoringEvent), Encoding.UTF8, "application/json");

Debug.WriteLine("Running url: " + client.BaseAddress.ToString() + url);
using (HttpResponseMessage response = await client.PostAsync(url, content))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async static Task<string> GetGitHubMessage(string url, string clientId, s
using (HttpResponseMessage response = await client.GetAsync(url))
{
//Throw a response exception
response.EnsureSuccessStatusCode();
//response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
responseBody = await response.Content.ReadAsStringAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ public async Task<int> UpdateGitHubActionRunsInStorage(string clientId, string c
int numberOfDays, int maxNumberOfItems)
{
JArray items = await GitHubAPIAccess.GetGitHubActionRunsJArray(clientId, clientSecret, owner, repo, workflowId);
if (items == null)
{
items = new();
}
Debug.WriteLine($"{items.Count} builds found for {owner}/{repo}/{workflowName}");

int itemsAdded = 0;
Expand Down Expand Up @@ -269,7 +273,10 @@ public async Task<int> UpdateGitHubActionPullRequestsInStorage(string clientId,
int numberOfDays, int maxNumberOfItems)
{
JArray items = await GitHubAPIAccess.GetGitHubPullRequestsJArray(clientId, clientSecret, owner, repo, branch);

if (items == null)
{
items = new();
}
int itemsAdded = 0;
TableStorageCommonDA tableDA = new(tableStorageConfig.StorageAccountConnectionString, tableStorageConfig.TableGitHubPRs);
//Check each build to see if it's in storage, adding the items not in storage
Expand Down Expand Up @@ -307,7 +314,10 @@ public async Task<int> UpdateGitHubActionPullRequestCommitsInStorage(string clie
string owner, string repo, string pull_number)
{
JArray items = await GitHubAPIAccess.GetGitHubPullRequestCommitsJArray(clientId, clientSecret, owner, repo, pull_number);

if (items == null)
{
items = new();
}
int itemsAdded = 0;
TableStorageCommonDA tableDA = new(tableStorageConfig.StorageAccountConnectionString, tableStorageConfig.TableGitHubPRCommits);
//Check each build to see if it's in storage, adding the items not in storage
Expand Down Expand Up @@ -400,7 +410,7 @@ public static async Task<bool> UpdateDORASummaryItem(TableStorageConfiguration t
string owner, string project, string repo, DORASummaryItem DORASummaryItem)
{
string partitionKey = owner;
string rowKey = "";
string rowKey;
if (string.IsNullOrEmpty(project))
{
rowKey = repo;
Expand Down
7 changes: 6 additions & 1 deletion src/DevOpsMetrics.Core/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
//[assembly: SuppressMessage("Style", "IDE0066:Convert switch statement to expression", Justification = "This is a 'silly' rule. The switch is clearer.", Scope = "member", Target = "~M:DevOpsMetrics.Core.Models.Common.Badges.BadgeColor(System.String)~System.String")]
[assembly: SuppressMessage("Style", "IDE0066:Convert switch statement to expression", Justification = "This is a 'silly' rule. The switch is clearer.", Scope = "member", Target = "~M:DevOpsMetrics.Core.Models.Common.Badges.BadgeURL(System.String,System.String)~System.String")]
[assembly: SuppressMessage("CodeQuality", "IDE0077:Avoid legacy format target in 'SuppressMessageAttribute'", Justification = "<Pending>")]
[assembly: SuppressMessage("Style", "IDE0074:Use compound assignment", Justification = "<Pending>", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.MeanTimeToRestoreDA.GetAzureMeanTimeToRestore(System.Boolean,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,DevOpsMetrics.Core.Models.Common.DevOpsPlatform,System.String,System.Int32,System.Int32)~DevOpsMetrics.Core.Models.Common.MeanTimeToRestoreModel")]
[assembly: SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionPullRequestCommitsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Style", "IDE0074:Use compound assignment", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionPullRequestCommitsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionPullRequestsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String,System.Int32,System.Int32)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Style", "IDE0074:Use compound assignment", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionPullRequestsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String,System.Int32,System.Int32)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Style", "IDE0270:Use coalesce expression", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionRunsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String,System.String,System.String,System.Int32,System.Int32)~System.Threading.Tasks.Task{System.Int32}")]
[assembly: SuppressMessage("Style", "IDE0074:Use compound assignment", Justification = "Does not improve readability", Scope = "member", Target = "~M:DevOpsMetrics.Core.DataAccess.TableStorage.AzureTableStorageDA.UpdateGitHubActionRunsInStorage(System.String,System.String,DevOpsMetrics.Core.Models.Common.TableStorageConfiguration,System.String,System.String,System.String,System.String,System.String,System.Int32,System.Int32)~System.Threading.Tasks.Task{System.Int32}")]
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Selenium.WebDriver" Version="4.9.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.10.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="114.0.5735.9000" />
</ItemGroup>

Expand Down
16 changes: 8 additions & 8 deletions src/DevOpsMetrics.Service/Controllers/BuildsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public async Task<int> UpdateAzureDevOpsBuilds(
//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken))
{
throw new Exception($"patToken '{patTokenName}' not found in key vault");
}
//if (string.IsNullOrEmpty(patToken))
//{
// throw new Exception($"patToken '{patTokenName}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateAzureDevOpsBuildsInStorage(patToken, tableStorageConfig, organization, project, branch, buildName, buildId, numberOfDays, maxNumberOfItems);
}
Expand Down Expand Up @@ -72,10 +72,10 @@ public async Task<int> UpdateGitHubActionRuns(
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
{
throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
}
//if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
//{
// throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateGitHubActionRunsInStorage(clientId, clientSecret, tableStorageConfig,
owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<ProcessingResult> UpdateDORASummaryItem(
Microsoft.Extensions.Logging.ILogger log = null,
bool useCache = true,
bool isGitHub = true,
bool useParallelProcessing = true)
bool useParallelProcessing = false)
{
//Start timer
DateTime startTime = DateTime.Now;
Expand All @@ -68,7 +68,8 @@ public async Task<ProcessingResult> UpdateDORASummaryItem(
//Instead of throwing exceptions when there are no secrets, add the error message to the overall processing message
string errorMessage = null;
if (isGitHub == true)
{ //Get the client id and secret from the settings
{
//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientId = Configuration[clientIdName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public async Task<DeploymentFrequencyModel> GetAzureDevOpsDeploymentFrequency(bo
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken))
{
throw new Exception($"patToken '{patTokenName}' not found in key vault");
}
//if (string.IsNullOrEmpty(patToken))
//{
// throw new Exception($"patToken '{patTokenName}' not found in key vault");
//}

DeploymentFrequencyDA da = new();
model = await DeploymentFrequencyDA.GetAzureDevOpsDeploymentFrequency(getSampleData, patToken, tableStorageConfig, organization, project, branch, buildName, numberOfDays, maxNumberOfItems, useCache);
Expand Down Expand Up @@ -76,10 +76,10 @@ public async Task<DeploymentFrequencyModel> GetGitHubDeploymentFrequency(bool ge
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
{
throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
}
//if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
//{
// throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
//}

DeploymentFrequencyDA da = new();
model = await DeploymentFrequencyDA.GetGitHubDeploymentFrequency(getSampleData, clientId, clientSecret, tableStorageConfig, owner, repo, branch, workflowName, workflowId, numberOfDays, maxNumberOfItems, useCache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public async Task<LeadTimeForChangesModel> GetAzureDevOpsLeadTimeForChanges(bool
//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken))
{
throw new Exception($"patToken '{patTokenName}' not found in key vault");
}
//if (string.IsNullOrEmpty(patToken))
//{
// throw new Exception($"patToken '{patTokenName}' not found in key vault");
//}

LeadTimeForChangesDA da = new();
model = await LeadTimeForChangesDA.GetAzureDevOpsLeadTimesForChanges(getSampleData, patToken, tableStorageConfig,
Expand Down Expand Up @@ -76,10 +76,10 @@ public async Task<LeadTimeForChangesModel> GetGitHubLeadTimeForChanges(bool getS
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
{
throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
}
//if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
//{
// throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
//}

LeadTimeForChangesDA da = new();
model = await LeadTimeForChangesDA.GetGitHubLeadTimesForChanges(getSampleData, clientId, clientSecret, tableStorageConfig,
Expand Down
32 changes: 16 additions & 16 deletions src/DevOpsMetrics.Service/Controllers/PullRequestsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public async Task<int> UpdateAzureDevOpsPullRequests(
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken))
{
throw new Exception($"patToken '{patTokenName}' not found in key vault");
}
//if (string.IsNullOrEmpty(patToken))
//{
// throw new Exception($"patToken '{patTokenName}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateAzureDevOpsPullRequestsInStorage(patToken, tableStorageConfig,
organization, project, repository, numberOfDays, maxNumberOfItems);
Expand Down Expand Up @@ -74,10 +74,10 @@ public async Task<int> UpdateGitHubActionPullRequests(
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
{
throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
}
//if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
//{
// throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateGitHubActionPullRequestsInStorage(clientId, clientSecret, tableStorageConfig,
owner, repo, branch, numberOfDays, maxNumberOfItems);
Expand Down Expand Up @@ -110,10 +110,10 @@ public async Task<int> UpdateAzureDevOpsPullRequestCommits(
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken))
{
throw new Exception($"patToken '{patTokenName}' not found in key vault");
}
//if (string.IsNullOrEmpty(patToken))
//{
// throw new Exception($"patToken '{patTokenName}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateAzureDevOpsPullRequestCommitsInStorage(patToken, tableStorageConfig,
organization, project, repository, pullRequestId, numberOfDays, maxNumberOfItems);
Expand Down Expand Up @@ -148,10 +148,10 @@ public async Task<int> UpdateGitHubActionPullRequestCommits(
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
{
throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
}
//if (string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientSecret))
//{
// throw new Exception($"clientId '{clientId}' or clientSecret '{clientSecret}' not found in key vault");
//}

numberOfRecordsSaved = await AzureTableStorageDA.UpdateGitHubActionPullRequestCommitsInStorage(clientId, clientSecret, tableStorageConfig,
owner, repo, pull_number);
Expand Down
Loading

0 comments on commit 8d7fef7

Please sign in to comment.