|<!-- Insert this Sequence as one of the last children of the Sequence with DisplayName="Compile and Test" -->|
|<Variable x:TypeArguments="mtbc:IBuildDetail" Name="LastBuildDetail" />|
|<Variable x:TypeArguments="x:Int32" Name="LastWarningCount" />|
|<Variable x:TypeArguments="x:Int32" Name="WarningCount" />|
|<Assign x:TypeArguments="mtbc:IBuildDetail" To="[LastBuildDetail]" Value="[BuildDetail.BuildServer.GetBuild(BuildDetail.BuildDefinition.LastBuildUri)]" />|
|<Assign x:TypeArguments="x:Int32" To="[LastWarningCount]" Value="[Microsoft.TeamFoundation.Build.Client.InformationNodeConverters.GetBuildWarnings(LastBuildDetail).Count]" />|
|<Assign x:TypeArguments="x:Int32" To="[WarningCount]" Value="[Microsoft.TeamFoundation.Build.Client.InformationNodeConverters.GetBuildWarnings(BuildDetail).Count]" />|
|<If Condition="[WarningCount > LastWarningCount]">|
|<mtbwa:WriteBuildError Message="This build has more warnings than the last build. Focus on fixing more build warnings than are introduced." />|
|<!-- The follow SetBuildProperties line will mark the build as Failed instead of just Partially Successful if the warning count has increased. -->|
|<mtbwa:SetBuildProperties PropertiesToSet="Status" Status="Failed" />|
I would recommend one change which is to use the last good build instead of just the last build. Because when a build fails or is cancelled, it will have less warnings, and the next build will have more.
There are a few things that need to be changed for this to work as expected.
First, this doesn't account for any warnings that appear in the "Other Warnings & Errors" section in the build summary. The "LastWarningCount" is the total number of warnings, and the "WarningCount" is only the number of warnings generated from MSBuild.
Second, since a "good build" is one where both "CompilationStatus" and "TestStatus" are "Succeeded", then one of these values needs to be set to "Failed". Otherwise, the next time you queue a build, it will continue to falsely recognize builds that failed due to warnings as the "LastGoodBuild".
So, I would add the following in addition to using "LastGoodBuild":
<mtbwa:SetBuildProperties PropertiesToSet="CompilationStatus" Status="Failed" />