public static async Task <TestSuiteOutputData> GetResults([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "results/{projectName}/{testSuiteName}/{buildNumber}")] HttpRequest req, string projectName, string testSuiteName, string buildNumber, TraceWriter log) { var table = await GetTableForProject(projectName); var query = new TableQuery <TestRun>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, TestRun.CreatePartitionKey(testSuiteName, buildNumber))); // prepare the result var result = new TestSuiteOutputData() { ProjectName = projectName, TestSuiteName = testSuiteName, BuildNumber = buildNumber }; // get all test results var testRuns = new List <TestRunOutputData>(); TableContinuationToken continuationToken = null; do { var queryResults = await table.ExecuteQuerySegmentedAsync(query, continuationToken); continuationToken = queryResults.ContinuationToken; var data = queryResults.Results .Select(r => new TestRunOutputData() { CreatedDate = r.Timestamp.UtcDateTime, TestResult = r.TestResult, TestFullName = r.TestFullName, TestOutput = r.TestOutput, IsFullTestOutput = r.TestOutputUrl != null, Attachments = r.Attachments .Select(a => new TestRunAttachmentOutputData() { FileName = a.FileName, Url = $"{BlobBaseUrl}/{projectName}/{a.BlobUrl}" }) .ToList() }); testRuns.AddRange(data); } while (continuationToken != null); // sort by creation date result.TestRuns = testRuns.OrderBy(r => r.CreatedDate).ToList(); return(result); }
public static async Task <IActionResult> PublishResult([HttpTrigger(AuthorizationLevel.Function, "post", Route = "publish")] TestRunInputData input, TraceWriter log) { try { // remove special characters input.ProjectName = Helpers.RemoveUnsafeChars(input.ProjectName); input.TestSuiteName = Helpers.RemoveUnsafeChars(input.TestSuiteName); input.BuildNumber = Helpers.RemoveUnsafeChars(input.BuildNumber); input.TestFullName = Helpers.RemoveUnsafeChars(input.TestFullName, allowUrlChars: true); // save test run results var entity = new TestRun() { PartitionKey = TestRun.CreatePartitionKey(input.TestSuiteName, input.BuildNumber), RowKey = TestRun.CreateRowKey(input.TestFullName), Attachments = new List <TestRunAttachmentLink>(), Timestamp = DateTimeOffset.UtcNow, TestResult = input.TestResult }; // store test output - if it is short, put it directly in the table; if it is too long, put it in blob storage if (input.TestOutput.Length <= MaxFieldLength) { entity.TestOutput = input.TestOutput; } else { // prepare URL var testOutputUrl = GetTestOutputBlobUrl(input.TestSuiteName, input.BuildNumber, input.TestFullName); // upload test output var container = await GetBlobContainerForProject(input.ProjectName); var testOutputBlob = container.GetBlockBlobReference(testOutputUrl); await testOutputBlob.UploadTextAsync(input.TestOutput); entity.TestOutput = input.TestOutput.Substring(0, MaxFieldLength); entity.TestOutputUrl = testOutputUrl; } // save attachments to blob storage if (input.Attachments != null) { foreach (var attachment in input.Attachments) { // prepare URL var attachmentUrl = GetTestAttachmentBlobUrl(input.TestSuiteName, input.BuildNumber, input.TestFullName, attachment.FileName); // upload blob var container = await GetBlobContainerForProject(input.ProjectName); var attachmentBlob = container.GetBlockBlobReference(attachmentUrl); var attachmentData = Convert.FromBase64String(attachment.ContentBase64); await attachmentBlob.UploadFromByteArrayAsync(attachmentData, 0, attachmentData.Length); entity.Attachments.Add(new TestRunAttachmentLink() { FileName = attachment.FileName, BlobUrl = attachmentUrl }); } } // store entity var table = await GetTableForProject(input.ProjectName, createIfNotExists : true); await table.ExecuteAsync(TableOperation.Insert(entity)); // store index entity var indexTable = await GetIndexTable(createIfNotExists : true); await indexTable.ExecuteAsync(TableOperation.InsertOrReplace(new TestSuiteInfo() { PartitionKey = TestSuiteInfo.CreatePartitionKey(input.ProjectName), RowKey = TestSuiteInfo.CreateRowKey(input.TestSuiteName, input.BuildNumber), Timestamp = DateTimeOffset.UtcNow })); return(new OkObjectResult(new TestRunInputResult() { TestSuiteUrl = $"{AppBaseUrl}/results/{input.ProjectName}/{input.TestSuiteName}/{input.BuildNumber}", TestResultUrl = $"{AppBaseUrl}/results/{input.ProjectName}/{input.TestSuiteName}/{input.BuildNumber}#{input.TestFullName}" })); } catch (Exception ex) { log.Error(ex.ToString()); return(new BadRequestErrorMessageResult(ex.Message)); } }