Skip to content

Commit

Permalink
TS-40269 Cherry-pick Artifactory Upload fix from TIA branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael-N committed Sep 11, 2024
1 parent 7e2d3f5 commit d9ff1f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
38 changes: 29 additions & 9 deletions UploadDaemon/Upload/ArtifactoryUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Web;
using UploadDaemon.SymbolAnalysis;
using UploadDaemon.Configuration;
using System.IO.Compression;
using System.Reflection;

namespace UploadDaemon.Upload
{
Expand Down Expand Up @@ -54,23 +56,23 @@ public async Task<bool> UploadLineCoverageAsync(string originalTraceFilePath, st
}
string[] branchAndTimestamp = revisionOrTimestamp.Value.Split(':');
string url = $"{artifactory.Url}/uploads/{branchAndTimestamp[0]}/{branchAndTimestamp[1]}";
url = $"{url}/{artifactory.Partition}/simple";
if (artifactory.PathSuffix != null)
{
string encodedPathSuffix = HttpUtility.UrlEncode(artifactory.PathSuffix);
url = $"{url}/{encodedPathSuffix}";
}
url = $"{url}/report.simple";
string fileName = $"{artifactory.Partition}/report.simple";

logger.Debug("Uploading line coverage from {trace} to {artifactory} ({url})", originalTraceFilePath, artifactory.ToString(), url);

try
{
byte[] reportBytes = Encoding.UTF8.GetBytes(lineCoverageReport);
using (MemoryStream stream = new MemoryStream(reportBytes))
{
return await PerformLineCoverageUpload(originalTraceFilePath, revisionOrTimestamp.Value, url, stream);
}
byte[] reportBytes = CreateZipFile(lineCoverageReport, fileName);

string reportName = $"report.zip";
string reportUrl = $"{url}/{reportName}";

return await PerformLineCoverageUpload(originalTraceFilePath, revisionOrTimestamp.Value, reportUrl, reportBytes);
}
catch (Exception e)
{
Expand All @@ -80,9 +82,9 @@ public async Task<bool> UploadLineCoverageAsync(string originalTraceFilePath, st
}
}

private async Task<bool> PerformLineCoverageUpload(string originalTraceFilePath, string timestampValue, string url, MemoryStream stream)
private async Task<bool> PerformLineCoverageUpload(string originalTraceFilePath, string timestampValue, string url, byte[] stream)
{
using (HttpResponseMessage response = await HttpClientUtils.UploadMultiPartPut(client, url, "report", stream, "report.simple"))
using (HttpResponseMessage response = await HttpClientUtils.UploadPut(client, url, stream))
{
if (response.IsSuccessStatusCode)
{
Expand All @@ -100,6 +102,24 @@ private async Task<bool> PerformLineCoverageUpload(string originalTraceFilePath,
}
}

private static byte[] CreateZipFile(string lineCoverageReport, string entryName)
{
byte[] compressedBytes;
byte[] reportBytes = Encoding.UTF8.GetBytes(lineCoverageReport);
using (var outStream = new MemoryStream())
using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
{
var fileInArchive = archive.CreateEntry(entryName);
using (var entryStream = fileInArchive.Open())
using (var fileToCompressStream = new MemoryStream(reportBytes))
{
fileToCompressStream.CopyTo(entryStream);
}
compressedBytes = outStream.ToArray();
}
return compressedBytes;
}

/// <inheritdoc/>
public object GetTargetId()
{
Expand Down
14 changes: 14 additions & 0 deletions UploadDaemon/Upload/HttpClientUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,19 @@ public static async Task<HttpResponseMessage> UploadMultiPartPut(HttpClient clie
return await client.PutAsync(url, content);
}
}

/// <summary>
/// Uploads the given file in a put request.
/// </summary>
/// <returns>The HTTP response. The caller must dispose of it.</returns>
/// <exception cref="IOException">In case there are network or file system errors.</exception>
/// <exception cref="HttpRequestException">In case there are network errors.</exception>
public static async Task<HttpResponseMessage> UploadPut(HttpClient client, string url, byte[] stream)
{
using (ByteArrayContent content = new ByteArrayContent(stream))
{
return await client.PutAsync(url, content);
}
}
}
}

0 comments on commit d9ff1f1

Please sign in to comment.