2727import org .conqat .lib .commons .string .StringUtils ;
2828
2929import java .io .IOException ;
30- import java .util .ArrayList ;
31- import java .util .HashMap ;
32- import java .util .List ;
33- import java .util .Map ;
34- import java .util .Objects ;
35- import java .util .Optional ;
30+ import java .util .*;
3631import java .util .function .Supplier ;
3732import java .util .regex .Matcher ;
3833import java .util .regex .Pattern ;
@@ -70,7 +65,7 @@ public TeamscaleClient(OkHttpClient client, HttpUrl teamscaleServerUrl, String u
7065 * @throws HttpStatusCodeException if an HTTP error code was returned by Teamscale
7166 * @throws ParserException if there was an error parsing Teamscale's response
7267 */
73- public Pair <List <Finding >, List <Finding >> fetchFindingsUsingCommitDetails (String branchAndTimestamp ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
68+ public Pair <List <Finding >, List <Finding >> fetchFindingsUsingCommitDetails (String branchAndTimestamp , String uniformPath ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
7469 HttpUrl .Builder builder =
7570 teamscaleServerUrl .newBuilder ()
7671 .addPathSegment ("api" )
@@ -82,7 +77,7 @@ public Pair<List<Finding>, List<Finding>> fetchFindingsUsingCommitDetails(String
8277 HttpUrl url = builder .build ();
8378 Request request = createAuthenticatedGetRequest (url );
8479 String response = sendRequest (request );
85- return parseCommitFindingResponse (response );
80+ return parseCommitFindingResponse (response , uniformPath );
8681 }
8782
8883 /**
@@ -92,13 +87,13 @@ public Pair<List<Finding>, List<Finding>> fetchFindingsUsingCommitDetails(String
9287 * @throws ParserException if there was an error parsing Teamscale's response
9388 * @implNote The API we use here is not yet a public API (<a href="https://cqse.atlassian.net/browse/TS-44014">TS-44014</a>), so we don't use a versioned endpoint.
9489 */
95- public Pair <List <Finding >, List <Finding >> fetchFindingsUsingLinearDelta (String startBranchAndTimestamp , String endBranchAndTimestamp ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
90+ public Pair <List <Finding >, List <Finding >> fetchFindingsUsingLinearDelta (String startBranchAndTimestamp , String endBranchAndTimestamp , String uniformPath ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
9691 HttpUrl .Builder builder =
9792 teamscaleServerUrl .newBuilder ().addPathSegments ("api/projects" ).addPathSegment (project )
9893 .addPathSegments ("findings/delta" )
9994 .addQueryParameter ("t2" , endBranchAndTimestamp )
10095 .addQueryParameter ("t1" , startBranchAndTimestamp )
101- .addQueryParameter ("uniform-path" , "" );
96+ .addQueryParameter ("uniform-path" , uniformPath );
10297
10398 HttpUrl url = builder .build ();
10499 Request request = createAuthenticatedGetRequest (url );
@@ -113,7 +108,7 @@ public Pair<List<Finding>, List<Finding>> fetchFindingsUsingLinearDelta(String s
113108 * @throws ParserException if there was an error parsing Teamscale's response
114109 * @implNote The API we use here is not yet a public API (<a href="https://cqse.atlassian.net/browse/TS-44014">TS-44014</a>), so we don't use a versioned endpoint.
115110 */
116- public Pair <List <Finding >, List <Finding >> fetchFindingsUsingBranchMergeDelta (String sourceBranchAndTimestamp , String targetBranchAndTimestamp ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
111+ public Pair <List <Finding >, List <Finding >> fetchFindingsUsingBranchMergeDelta (String sourceBranchAndTimestamp , String targetBranchAndTimestamp , String uniformPath ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
117112
118113 HttpUrl .Builder builder =
119114 teamscaleServerUrl .newBuilder ()
@@ -122,6 +117,9 @@ public Pair<List<Finding>, List<Finding>> fetchFindingsUsingBranchMergeDelta(Str
122117 .addPathSegments ("merge-requests/finding-churn" )
123118 .addQueryParameter ("source" , sourceBranchAndTimestamp )
124119 .addQueryParameter ("target" , targetBranchAndTimestamp );
120+ if (!uniformPath .isEmpty ()) {
121+ builder .addQueryParameter ("included-paths" , uniformPath );
122+ }
125123
126124 HttpUrl url = builder .build ();
127125 Request request = createAuthenticatedGetRequest (url );
@@ -135,13 +133,13 @@ public Pair<List<Finding>, List<Finding>> fetchFindingsUsingBranchMergeDelta(Str
135133 * @throws HttpStatusCodeException if an HTTP error code was returned by Teamscale
136134 * @throws ParserException if there was an error parsing Teamscale's response
137135 */
138- public List <MetricViolation > fetchMetricAssessments (String branchAndTimestamp , String thresholdConfig ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
136+ public List <MetricViolation > fetchMetricAssessments (String branchAndTimestamp , String thresholdConfig , String uniformPath ) throws IOException , HttpRedirectException , HttpStatusCodeException , ParserException {
139137 HttpUrl .Builder builder =
140138 teamscaleServerUrl .newBuilder ()
141139 .addPathSegments ("api/projects" )
142140 .addPathSegment (project )
143141 .addPathSegment ("metric-assessments" )
144- .addQueryParameter ("uniform-path" , "" )
142+ .addQueryParameter ("uniform-path" , uniformPath )
145143 .addQueryParameter ("t" , branchAndTimestamp )
146144 .addQueryParameter ("configuration-name" , thresholdConfig );
147145 HttpUrl url = builder .build ();
@@ -251,14 +249,19 @@ private boolean isAnalysisFinished(String branch, long timestamp) throws IOExcep
251249 }
252250 }
253251
254- private Pair <List <Finding >, List <Finding >> parseCommitFindingResponse (String response ) throws ParserException {
252+ private Pair <List <Finding >, List <Finding >> parseCommitFindingResponse (String response , String uniformPath ) throws ParserException {
255253 try {
256- return tryParseFindingsResponse (response , "$.addedFindings.*" , "$.findingsInChangedCode.*" );
254+ Pair <List <Finding >, List <Finding >> result = tryParseFindingsResponse (response , "$.addedFindings.*" , "$.findingsInChangedCode.*" );
255+ return Pair .createPair (filterFindingLocations (result .getFirst (), uniformPath ), filterFindingLocations (result .getSecond (), uniformPath ));
257256 } catch (ParserException | PathNotFoundException e ) {
258257 throw new ParserException ("Could not parse findings JSON response:\n " + response + "\n \n Please contact CQSE with an error report." , e );
259258 }
260259 }
261260
261+ private List <Finding > filterFindingLocations (List <Finding > findings , String uniformPath ) {
262+ return findings .stream ().filter (finding -> finding .uniformPath .startsWith (uniformPath )).collect (Collectors .toList ());
263+ }
264+
262265 /**
263266 * @implNote The APIs we use in {@link #fetchFindingsUsingBranchMergeDelta} and {@link #fetchFindingsUsingLinearDelta}
264267 * are not yet public APIs (<a href="https://cqse.atlassian.net/browse/TS-44014">TS-44014</a>).
@@ -353,6 +356,8 @@ private List<MetricViolation> parseMetricResponse(String response) throws Parser
353356 assessment .add (ETrafficLightColor .values ()[i ], mapping [i ]);
354357 }
355358 formattedTextValue = assessment .toFormattedColorDistributionString ();
359+ } else if ("NUMERIC" .equals (schemaEntry .get ("valueType" ))) {
360+ formattedTextValue = String .format (Locale .ENGLISH , "%.4f" , metricViolation .get ("value" ));
356361 } else {
357362 formattedTextValue = metricViolation .get ("value" ).toString ();
358363 }
0 commit comments