Skip to content

Commit eeb10dd

Browse files
committed
Fixing issue in pulling dependencies from TC.
1 parent 5a15079 commit eeb10dd

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

src/PostSharp.Engineering.BuildTools/Dependencies/DependenciesHelper.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,7 @@ private static bool GetLatestBuildId(
269269
string ciBuildType,
270270
string branch,
271271
[NotNullWhen( true )] out CiBuildId? latestBuildId )
272-
{
273-
latestBuildId = teamCity.GetLatestBuildId(
274-
console,
275-
ciBuildType,
276-
branch );
277-
278-
if ( latestBuildId == null )
279-
{
280-
console.WriteError( $"No successful build for '{dependencyName}' dependency on '{branch}' branch, BuildTypeId='{ciBuildType}'." );
281-
282-
return false;
283-
}
284-
285-
return true;
286-
}
272+
=> teamCity.TryGetLatestBuildId( console, ciBuildType, branch, out latestBuildId );
287273

288274
private static bool ResolveBuildNumbersFromBranches(
289275
BuildContext context,

src/PostSharp.Engineering.BuildTools/Tools/TeamCity/TeamCityClient.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ private bool TryGet( string path, ConsoleHelper? console, out HttpResponseMessag
5353
ReportHttpErrorIfAny( response, console );
5454
}
5555

56+
if ( !response.IsSuccessStatusCode )
57+
{
58+
console?.WriteWarning( $"HTTP GET {path} failed with code {response.StatusCode}." );
59+
}
60+
5661
return response.IsSuccessStatusCode;
5762
}
5863

@@ -106,25 +111,63 @@ public bool TryGetBranchFromBuildNumber( ConsoleHelper console, CiBuildId buildI
106111
return true;
107112
}
108113

109-
public CiBuildId? GetLatestBuildId( ConsoleHelper console, string buildTypeId, string branchName )
114+
public bool TryGetLatestBuildId( ConsoleHelper console, string buildTypeId, string branchName, out CiBuildId? buildId )
115+
{
116+
var prefix = "ref/heads/";
117+
118+
string nakedBranchName;
119+
120+
if ( branchName.StartsWith( prefix, StringComparison.Ordinal ) )
121+
{
122+
nakedBranchName = branchName.Substring( prefix.Length );
123+
}
124+
else
125+
{
126+
nakedBranchName = branchName;
127+
}
128+
129+
if ( this.TryGetLatestBuildIdCore( console, buildTypeId, nakedBranchName, out buildId ) )
130+
{
131+
return true;
132+
}
133+
134+
if ( this.TryGetLatestBuildIdCore( console, buildTypeId, prefix + nakedBranchName, out buildId ) )
135+
{
136+
return true;
137+
}
138+
139+
console.WriteError( $"Cannot get the last build for build type '{buildTypeId}', branch '{branchName}': No build available." );
140+
141+
return false;
142+
}
143+
144+
private bool TryGetLatestBuildIdCore( ConsoleHelper console, string buildTypeId, string branchName, out CiBuildId? buildId )
110145
{
111146
var path = $"/app/rest/builds?locator=defaultFilter:false,state:finished,status:SUCCESS,buildType:{buildTypeId},branch:{branchName}";
112147

113148
if ( !this.TryGet( path, console, out var response ) )
114149
{
115-
return null;
150+
console.WriteError( $"Cannot get the last build for build type '{buildTypeId}', branch '{branchName}': HTTP GET failed." );
151+
152+
buildId = null;
153+
154+
return false;
116155
}
117156

118157
var document = response.Content.ReadAsXDocument();
119158
var build = document.Root?.Elements( "build" ).FirstOrDefault();
120159

121160
if ( build == null )
122161
{
123-
return null;
162+
buildId = null;
163+
164+
return false;
124165
}
125166
else
126167
{
127-
return new CiBuildId( int.Parse( build.Attribute( "number" )!.Value, CultureInfo.InvariantCulture ), buildTypeId );
168+
buildId = new CiBuildId( int.Parse( build.Attribute( "number" )!.Value, CultureInfo.InvariantCulture ), buildTypeId );
169+
170+
return true;
128171
}
129172
}
130173

0 commit comments

Comments
 (0)