Exemple #1
0
        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));
            }
        }