@@ -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