Skip to content

Commit

Permalink
Fixing deployment status response parsing (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav committed May 25, 2022
1 parent 5f09483 commit 81748f5
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 9 deletions.
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Newtonsoft.Json" Version="[11.0.2]" />
<PackageReference Include="System.Runtime.Handles" Version="4.3.0" />
<PackageReference Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.1" />
<PackageReference Include="Mono.Cecil" Version="0.11.3" />
</ItemGroup>

</Project>
Expand Up @@ -10,6 +10,7 @@
using Microsoft.NET.Sdk.Functions.Http;
using Microsoft.NET.Sdk.Functions.MSBuild.Properties;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Task = System.Threading.Tasks.Task;

namespace Microsoft.NET.Sdk.Functions.MSBuild.Tasks
Expand Down Expand Up @@ -65,14 +66,10 @@ public async Task<DeployStatus> PollDeploymentStatusAsync(string deploymentUrl,

private async Task<DeployStatus> GetDeploymentStatusAsync(string deploymentUrl, string userName, string password, int retryCount, TimeSpan retryDelay, CancellationTokenSource cts)
{
Dictionary<string, string> json = await InvokeGetRequestWithRetryAsync<Dictionary<string, string>>(deploymentUrl, userName, password, retryCount, retryDelay, cts);
string statusString = null;
if (json!= null && !json.TryGetValue("status", out statusString))
{
return DeployStatus.Unknown;
}

if (Enum.TryParse(statusString, out DeployStatus result))
var json = await InvokeGetRequestWithRetryAsync<JObject>(deploymentUrl, userName, password, retryCount, retryDelay, cts);
if (json != null
&& json.TryGetValue("status", out JToken statusString)
&& Enum.TryParse(statusString.Value<string>(), out DeployStatus result))
{
return result;
}
Expand Down
Expand Up @@ -15,6 +15,21 @@
<ProjectReference Include="..\..\src\Microsoft.NET.Sdk.Functions.MSBuild\Microsoft.NET.Sdk.Functions.MSBuild.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Update="Resources\TestPublishContents.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="DeploymentResponse" xml:space="preserve">
<value>{
"id": "7010fa61-d5df-46b5-a22e-98cfc81f1637",
"status": 3,
"status_text": "",
"author_email": "N/A",
"author": "N/A",
"deployer": "Push-Deployer",
"message": "Created via a push deployment",
"progress": "",
"received_time": "2022-05-25T04:40:36.0994691Z",
"start_time": "2022-05-25T04:40:37.1272389Z",
"end_time": "2022-05-25T04:40:39.4733696Z",
"last_success_end_time": null,
"complete": true,
"active": false,
"is_temp": false,
"is_readonly": true,
"url": "https://facaval-lc.scm.azurewebsites.net/api/deployments/latest",
"log_url": "https://facaval-lc.scm.azurewebsites.net/api/deployments/latest/log",
"site_name": "facaval-lc",
"build_summary": {
"errors": [],
"warnings": []
}
}</value>
</data>
</root>
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.NET.Sdk.Functions.Http;
using Microsoft.NET.Sdk.Functions.MSBuild.Properties;
using Microsoft.NET.Sdk.Functions.MSBuild.Tasks;
using Moq;
using Newtonsoft.Json;
Expand Down Expand Up @@ -97,5 +98,40 @@ public async Task PollDeploymentStatusTest_ForValidResponses(HttpStatusCode resp
// Assert
verifyStep(client, expectedDeployStatus == actualdeployStatus);
}

[Fact]
public async Task PollDeploymentStatusTest_WithDeploymentSummary_Succeeds()
{
// Arrange
string deployUrl = "https://sitename.scm.azurewebsites.net/DeploymentStatus?Id=knownId";
Action<Mock<IHttpClient>, DeployStatus> verifyStep = (client, status) =>
{
client.Verify(c => c.GetAsync(
It.Is<Uri>(uri => string.Equals(uri.AbsoluteUri, deployUrl, StringComparison.Ordinal)), It.IsAny<CancellationToken>()));
Assert.Equal($"{UserAgentName}/{UserAgentVersion}", client.Object.DefaultRequestHeaders.GetValues("User-Agent").FirstOrDefault());
Assert.Equal(DeployStatus.Failed, status);
};

Mock<IHttpClient> client = new Mock<IHttpClient>();
HttpRequestMessage requestMessage = new HttpRequestMessage();
client.Setup(x => x.DefaultRequestHeaders).Returns(requestMessage.Headers);
client.Setup(c => c.GetAsync(new Uri(deployUrl, UriKind.RelativeOrAbsolute), It.IsAny<CancellationToken>())).Returns(() =>
{
HttpContent httpContent = new StringContent(Properties.Resources.DeploymentResponse, Encoding.UTF8, "application/json");
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = httpContent
};
return Task.FromResult(responseMessage);
});

ZipDeploymentStatus deploymentStatus = new ZipDeploymentStatus(client.Object, $"{UserAgentName}/{UserAgentVersion}", null, false);

// Act
var actualdeployStatus = await deploymentStatus.PollDeploymentStatusAsync(deployUrl, userName, password);

// Assert
verifyStep(client, actualdeployStatus);
}
}
}

0 comments on commit 81748f5

Please sign in to comment.