Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VCI-951: Fix empty error messages in Cloud targets #152

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/VirtoCommerce.Build.Tests/ArtifactPackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace VirtoCommerce.Build.Tests
{
public class ArtifactPackerTests
public partial class ArtifactPackerTests
{
[Fact]
public void SkipFileByList_Returns_False_When_Not_In_IgnoreList()
Expand All @@ -36,7 +36,7 @@ public void SkipFileByList_Returns_True_When_In_IgnoreList()
[Fact]
public void SkipFileByRegex_Returns_True_When_Matching()
{
var regex = new Regex(@".+Module\..*", RegexOptions.IgnoreCase);
var regex = SkipFilesRegex();
var fileName = "TestModule.dll";

var result = ArtifactPacker.SkipFileByRegex(fileName, regex);
Expand All @@ -47,7 +47,7 @@ public void SkipFileByRegex_Returns_True_When_Matching()
[Fact]
public void SkipFileByRegex_Returns_False_When_Not_Matching()
{
var ignoreRegex = new Regex(@".+Module\..*", RegexOptions.IgnoreCase);
var ignoreRegex = SkipFilesRegex();
var fileName = "AnotherLibrary.dll";

var result = ArtifactPacker.SkipFileByRegex(fileName, ignoreRegex);
Expand Down Expand Up @@ -80,7 +80,7 @@ public void KeepFileByList_Returns_False_When_Not_In_KeepList()
[Fact]
public void KeepFileByRegex_Returns_True_When_Matching()
{
var regex = new Regex(@$".*SampleModule(Module)?\..*", RegexOptions.IgnoreCase);
var regex = KeepFilesRegex();
var fileName = "SampleModule.dll";

var result = ArtifactPacker.KeepFileByRegex(fileName, regex);
Expand All @@ -91,12 +91,18 @@ public void KeepFileByRegex_Returns_True_When_Matching()
[Fact]
public void KeepFileByRegex_Returns_False_When_Not_Matching()
{
var regex = new Regex(@$".*SampleModule(Module)?\..*", RegexOptions.IgnoreCase);
var regex = KeepFilesRegex();
var fileName = "NotSample.dll";

var result = ArtifactPacker.KeepFileByRegex(fileName, regex);

Assert.False(result);
}

[GeneratedRegex(@".+Module\..*", RegexOptions.IgnoreCase, "en-GB")]
private static partial Regex SkipFilesRegex();

[GeneratedRegex(@".*SampleModule(Module)?\..*", RegexOptions.IgnoreCase, "en-GB")]
private static partial Regex KeepFilesRegex();
}
}
194 changes: 109 additions & 85 deletions src/VirtoCommerce.Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,63 +607,67 @@ private static void WebPackBuildMethod(Project webProject)
.After(GetManifestGit)
.Executes(() =>
{
var manifest = ModuleManifest;
manifest.PackageUrl = ModulePackageUrl;
UpdateManifestBody(ModuleManifest, ModulePackageUrl, ModulesLocalDirectory, ModulesJsonName, CustomVersionSuffix);
});

private static void UpdateManifestBody(ModuleManifest manifest, string modulePackageUrl, AbsolutePath modulesLocalDirectory, string modulesJsonName, string customVersionSuffix)
{
manifest.PackageUrl = modulePackageUrl;

var modulesJsonFilePath = ModulesLocalDirectory / ModulesJsonName;
var externalManifests =
JsonConvert.DeserializeObject<List<ExternalModuleManifest>>(modulesJsonFilePath.ReadAllText());
var externalManifest = externalManifests?.Find(x => x.Id == manifest.Id);
var modulesJsonFilePath = modulesLocalDirectory / modulesJsonName;
var externalManifests =
JsonConvert.DeserializeObject<List<ExternalModuleManifest>>(modulesJsonFilePath.ReadAllText());
var externalManifest = externalManifests?.Find(x => x.Id == manifest.Id);

if (externalManifest != null)
if (externalManifest != null)
{
if (!manifest.VersionTag.IsNullOrEmpty() || !customVersionSuffix.IsNullOrEmpty())
{
if (!manifest.VersionTag.IsNullOrEmpty() || !CustomVersionSuffix.IsNullOrEmpty())
{
manifest.VersionTag = manifest.VersionTag.EmptyToNull() ?? CustomVersionSuffix;
manifest.VersionTag = manifest.VersionTag.EmptyToNull() ?? CustomVersionSuffix;

var externalPrereleaseVersion =
externalManifest.Versions.FirstOrDefault(v => !v.VersionTag.IsNullOrEmpty());
var externalPrereleaseVersion =
externalManifest.Versions.FirstOrDefault(v => !v.VersionTag.IsNullOrEmpty());

if (externalPrereleaseVersion != null)
{
externalPrereleaseVersion.Dependencies = manifest.Dependencies;
externalPrereleaseVersion.Incompatibilities = manifest.Incompatibilities;
externalPrereleaseVersion.PlatformVersion = manifest.PlatformVersion;
externalPrereleaseVersion.ReleaseNotes = manifest.ReleaseNotes;
externalPrereleaseVersion.Version = manifest.Version;
externalPrereleaseVersion.VersionTag = manifest.VersionTag;
externalPrereleaseVersion.PackageUrl = manifest.PackageUrl;
}
else
{
externalManifest.Versions.Add(ExternalModuleManifestVersion.FromManifest(manifest));
}
if (externalPrereleaseVersion != null)
{
externalPrereleaseVersion.Dependencies = manifest.Dependencies;
externalPrereleaseVersion.Incompatibilities = manifest.Incompatibilities;
externalPrereleaseVersion.PlatformVersion = manifest.PlatformVersion;
externalPrereleaseVersion.ReleaseNotes = manifest.ReleaseNotes;
externalPrereleaseVersion.Version = manifest.Version;
externalPrereleaseVersion.VersionTag = manifest.VersionTag;
externalPrereleaseVersion.PackageUrl = manifest.PackageUrl;
}
else
{
externalManifest.PublishNewVersion(manifest);
externalManifest.Versions.Add(ExternalModuleManifestVersion.FromManifest(manifest));
}

externalManifest.Title = manifest.Title;
externalManifest.Description = manifest.Description;
externalManifest.Authors = manifest.Authors;
externalManifest.Copyright = manifest.Copyright;
externalManifest.Groups = manifest.Groups;
externalManifest.IconUrl = manifest.IconUrl;
externalManifest.Id = manifest.Id;
externalManifest.LicenseUrl = manifest.LicenseUrl;
externalManifest.Owners = manifest.Owners;
externalManifest.ProjectUrl = manifest.ProjectUrl;
externalManifest.RequireLicenseAcceptance = manifest.RequireLicenseAcceptance;
externalManifest.Tags = manifest.Tags;
}
else
{
externalManifests?.Add(ExternalModuleManifest.FromManifest(manifest));
externalManifest.PublishNewVersion(manifest);
}

modulesJsonFilePath.WriteAllText(JsonConvert.SerializeObject(externalManifests, Formatting.Indented));
});
externalManifest.Title = manifest.Title;
externalManifest.Description = manifest.Description;
externalManifest.Authors = manifest.Authors;
externalManifest.Copyright = manifest.Copyright;
externalManifest.Groups = manifest.Groups;
externalManifest.IconUrl = manifest.IconUrl;
externalManifest.Id = manifest.Id;
externalManifest.LicenseUrl = manifest.LicenseUrl;
externalManifest.Owners = manifest.Owners;
externalManifest.ProjectUrl = manifest.ProjectUrl;
externalManifest.RequireLicenseAcceptance = manifest.RequireLicenseAcceptance;
externalManifest.Tags = manifest.Tags;
}
else
{
externalManifests?.Add(ExternalModuleManifest.FromManifest(manifest));
}

modulesJsonFilePath.WriteAllText(JsonConvert.SerializeObject(externalManifests, Formatting.Indented));
}

public Target PublishManifestGit => _ => _
.After(UpdateManifest)
Expand Down Expand Up @@ -757,33 +761,48 @@ private static void WebPackBuildMethod(Project webProject)
.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 @@ -854,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
8 changes: 7 additions & 1 deletion src/VirtoCommerce.Build/Cloud/Client/VirtoCloudClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public async Task<string> UpdateEnvironmentAsync(string manifest, string appProj
if (!response.IsSuccessStatusCode)
{
var error = VirtoCloudError.FromStringResponse(responseContent);
Assert.Fail(error.GetErrorMessage());
var errorMessage = error.GetErrorMessage();
if (string.IsNullOrWhiteSpace(errorMessage))
{
errorMessage = response.ReasonPhrase;
}

Assert.Fail(errorMessage);
}

Serilog.Log.Information(responseContent);
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
Loading