Skip to content

Commit

Permalink
VCI-951: Add fixes of maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro committed Nov 12, 2024
1 parent d5ef25b commit 1577399
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 62 deletions.
104 changes: 62 additions & 42 deletions src/VirtoCommerce.Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,33 +761,48 @@ private static void UpdateManifestBody(ModuleManifest manifest, string modulePac
.SetPullRequestKey(SonarPRNumber ?? Environment.GetEnvironmentVariable("CHANGE_ID"))
.SetProcessArgumentConfigurator(args =>
{
if (!string.IsNullOrEmpty(SonarPRProvider))
{
args = args.Add($"/d:sonar.pullrequest.provider={SonarPRProvider}");
}

if (!string.IsNullOrEmpty(SonarGithubRepo))
{
args = args.Add("/d:sonar.pullrequest.github.repository={value}", SonarGithubRepo);
}
args = AddSonarPRProvider(args);
args = AddSonarPRGithubRepo(args);

return args;
}))
.When(!PullRequest, cc => cc
.SetBranchName(branchName)
.SetProcessArgumentConfigurator(args =>
{
if (!_sonarLongLiveBranches.Contains(branchName))
{
args = args.Add($"/d:\"sonar.branch.target={branchNameTarget}\"");
}

return args;
})
.SetProcessArgumentConfigurator(args => AddSonarBranchTarget(args, branchName, branchNameTarget))
)
);
});

private static Arguments AddSonarBranchTarget(Arguments args, string branchName, string branchNameTarget)
{
if (!_sonarLongLiveBranches.Contains(branchName))
{
args = args.Add($"/d:\"sonar.branch.target={branchNameTarget}\"");
}

return args;
}

private static Arguments AddSonarPRGithubRepo(Arguments args)
{
if (!string.IsNullOrEmpty(SonarGithubRepo))
{
args = args.Add("/d:sonar.pullrequest.github.repository={value}", SonarGithubRepo);
}

return args;
}

private static Arguments AddSonarPRProvider(Arguments args)
{
if (!string.IsNullOrEmpty(SonarPRProvider))
{
args = args.Add($"/d:sonar.pullrequest.provider={SonarPRProvider}");
}

return args;
}

public Target SonarQubeEnd => _ => _
.After(SonarQubeStart)
.DependsOn(Compile)
Expand Down Expand Up @@ -858,35 +873,40 @@ await PublishRelease(GitHubUser, GitRepositoryName, GitHubToken, tag, descriptio
}
catch (AggregateException ex)
{
foreach (var innerException in ex.Flatten().InnerExceptions.OfType<ApiValidationException>())
{
var responseString = innerException.HttpResponse?.Body.ToString() ?? string.Empty;
var responseDocument = JsonDocument.Parse(responseString);
var alreadyExistsError = false;

if (responseDocument.RootElement.TryGetProperty("errors", out var errors))
{
var errorCount = errors.GetArrayLength();

if (errorCount > 0)
{
alreadyExistsError = errors.EnumerateArray().Any(e =>
e.GetProperty("code").GetString() == "already_exists");
}
}

if (alreadyExistsError)
{
ExitCode = (int)ExitCodes.GithubReleaseAlreadyExists;
}

Log.Error($"Api Validation Error: {responseString}");
}
IterateAgregatedErrors(ex);

Assert.Fail("Publish Release Failed", ex);
}
});

private void IterateAgregatedErrors(AggregateException ex)
{
foreach (var innerException in ex.Flatten().InnerExceptions.OfType<ApiValidationException>())
{
var responseString = innerException.HttpResponse?.Body.ToString() ?? string.Empty;
var responseDocument = JsonDocument.Parse(responseString);
var alreadyExistsError = false;

if (responseDocument.RootElement.TryGetProperty("errors", out var errors))
{
var errorCount = errors.GetArrayLength();

if (errorCount > 0)
{
alreadyExistsError = errors.EnumerateArray().Any(e =>
e.GetProperty("code").GetString() == "already_exists");
}
}

if (alreadyExistsError)
{
ExitCode = (int)ExitCodes.GithubReleaseAlreadyExists;
}

Log.Error($"Api Validation Error: {responseString}");
}
}

public Target ClearTemp => _ => _
.Executes(() => ClearTempBeforeExit = true);

Expand Down
48 changes: 28 additions & 20 deletions src/VirtoCommerce.Build/PlatformTools/Build.PackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,7 @@ private static bool IsPlatformInstallationNeeded(string version)
}
}

var progress = new Progress<ProgressMessage>(m =>
{
if (m.Level == ProgressMessageLevel.Error)
{
ExitCode = 1;
Log.Error(m.Message);

}
else
{
Log.Information(m.Message);
}
});
var progress = PlatformProgressHandler();

if (!SkipDependencySolving)
{
Expand All @@ -447,26 +435,46 @@ private static bool IsPlatformInstallationNeeded(string version)

modulesToInstall.AddRange(missingModules);
}

modulesToInstall.ForEach(module => module.DependsOn.Clear());
moduleInstaller.Install(modulesToInstall, progress);

if (ExitCode > 0)
{
Assert.Fail("Errors occurred while installing modules.");
}
Assert.False(ExitCode > 0, "Errors occurred while installing modules.");

foreach (var moduleSource in moduleSources)
{
var installer = GetModuleInstaller(moduleSource);

await installer.Install(moduleSource, progress);
}
AbsolutePath absoluteDiscoveryPath = Path.GetFullPath(discoveryPath);
var zipFiles = absoluteDiscoveryPath.GlobFiles("*/*.zip");
zipFiles.ForEach(f => f.DeleteFile());
CleanZipArtifacts(discoveryPath);
localModuleCatalog.Reload();
});

private Progress<ProgressMessage> PlatformProgressHandler()
{
return new Progress<ProgressMessage>(m =>
{
if (m.Level == ProgressMessageLevel.Error)
{
ExitCode = 1;
Log.Error(m.Message);

}
else
{
Log.Information(m.Message);
}
});
}

private static void CleanZipArtifacts(string discoveryPath)
{
AbsolutePath absoluteDiscoveryPath = Path.GetFullPath(discoveryPath);
var zipFiles = absoluteDiscoveryPath.GlobFiles("*/*.zip");
zipFiles.ForEach(f => f.DeleteFile());
}

private static ManifestModuleInfo LoadModuleInfo(ModuleItem module, ManifestModuleInfo externalModule)
{
if (!externalModule.Ref.Contains(externalModule.Version.ToString()))
Expand Down

0 comments on commit 1577399

Please sign in to comment.