1010import com .atlassian .util .concurrent .NotNull ;
1111import com .codedx .bambooplugin .utils .Archiver ;
1212import com .codedx .bambooplugin .utils .CodeDxBuildStatistics ;
13- import com .codedx .bambooplugin .utils .CodeDxConstants ;
14- import com .codedx .bambooplugin .utils .CodeDxReportWriter ;
1513import com .codedx .client .ApiClient ;
1614import com .codedx .client .ApiException ;
1715import com .codedx .client .api .*;
@@ -45,7 +43,6 @@ private static class ScanTaskState {
4543 String includePaths ;
4644 String excludePaths ;
4745 String toolOutputFiles ;
48- String reportArchiveName ;
4946 boolean waitForResults ;
5047 String failureSeverity ;
5148 boolean onlyConsiderNewFindings ;
@@ -60,8 +57,7 @@ private static class ScanTaskState {
6057 List <File > filesToUpload ;
6158 String analysisPrepId ;
6259 String analysisJobId ;
63- CodeDxBuildStatistics statsBeforeAnalysis ;
64- CodeDxBuildStatistics statsAfterAnalysis ;
60+ CodeDxBuildStatistics buildStatistics ;
6561 List <GroupedCount > groupedCounts ;
6662 }
6763
@@ -81,7 +77,6 @@ public TaskResult execute(final TaskContext taskContext) throws TaskException
8177
8278 stateMachine .add (CodeDxScanTask ::loadUserPreferences );
8379 stateMachine .add (CodeDxScanTask ::setupApiClient );
84- stateMachine .add (CodeDxScanTask ::getBuildStatsBeforeAnalysis );
8580 stateMachine .add (CodeDxScanTask ::collectFilesToUpload );
8681 stateMachine .add (CodeDxScanTask ::uploadFiles );
8782 stateMachine .add (CodeDxScanTask ::waitForCodeDxToBeReadyForAnalysis );
@@ -126,7 +121,6 @@ private static Boolean loadUserPreferences(ScanTaskState state) {
126121 return false ;
127122 }
128123 state .toolOutputFiles = config .get ("toolOutputFiles" );
129- state .reportArchiveName = config .get ("reportArchiveName" );
130124 state .waitForResults = config .getAsBoolean ("waitForResults" );
131125 state .failureSeverity = config .get ("selectedFailureSeverity" );
132126 // Don't wait if the failure severity is "None". Is this what we want?
@@ -309,26 +303,6 @@ private static Boolean waitForCodeDxToBeReadyForAnalysis(ScanTaskState state) {
309303 return true ;
310304 }
311305
312- private static Boolean getBuildStatsBeforeAnalysis (ScanTaskState state ) {
313-
314- if (!state .waitForResults || state .reportArchiveName == null || state .reportArchiveName .isEmpty ()) {
315- // We only need the pre-analysis stats if we are writing the report later
316- return true ;
317- }
318-
319- log (state , "Querying Code Dx for pre-analysis statistics" );
320-
321- CodeDxBuildStatistics stats = getBuildStats (state );
322-
323- if (stats == null ) {
324- return false ; // Error already logged in helper method
325- }
326-
327- state .statsBeforeAnalysis = stats ;
328-
329- return true ;
330- }
331-
332306 private static Boolean startAnalysis (ScanTaskState state ) {
333307 log (state , "Querying Code Dx to start analysis" );
334308
@@ -364,9 +338,8 @@ private static Boolean handleAnalysisResults(ScanTaskState state) {
364338 List <Function <ScanTaskState , Boolean >> stateMachine = new ArrayList <Function <ScanTaskState , Boolean >>();
365339
366340 stateMachine .add (CodeDxScanTask ::waitForAnalysisToFinish );
367- stateMachine .add (CodeDxScanTask ::getBuildStatsAfterAnalysis );
341+ stateMachine .add (CodeDxScanTask ::getBuildStatistics );
368342 stateMachine .add (CodeDxScanTask ::getGroupedCounts );
369- stateMachine .add (CodeDxScanTask ::writeReportArchive );
370343 stateMachine .add (CodeDxScanTask ::checkFailureSeverity );
371344
372345 return runStateMachine (stateMachine , state );
@@ -417,16 +390,30 @@ private static Boolean waitForAnalysisToFinish(ScanTaskState state) {
417390 return true ;
418391 }
419392
420- private static Boolean getBuildStatsAfterAnalysis (ScanTaskState state ) {
393+ private static Boolean getBuildStatistics (ScanTaskState state ) {
421394 log (state , "Querying Code Dx for post-analysis statistics" );
422395
423- CodeDxBuildStatistics stats = getBuildStats (state );
396+ Filter filter = new Filter ();
397+ filter .put ("~status" , "gone" );
424398
425- if (stats == null ) {
426- return false ; // Error already logged in helper method
399+ GroupedCountsRequest bySeverity = new GroupedCountsRequest ();
400+ bySeverity .setFilter (filter );
401+ bySeverity .setCountBy ("severity" );
402+
403+ GroupedCountsRequest byStatus = new GroupedCountsRequest ();
404+ byStatus .setFilter (filter );
405+ byStatus .setCountBy ("status" );
406+
407+ CodeDxBuildStatistics stats = null ;
408+ try {
409+ List <GroupedCount > severityGroupedCounts = state .findingDataApi .getFindingsGroupCount (state .projectId , bySeverity );
410+ List <GroupedCount > statusGroupedCounts = state .findingDataApi .getFindingsGroupCount (state .projectId , byStatus );
411+ stats = new CodeDxBuildStatistics (severityGroupedCounts , statusGroupedCounts );
412+ } catch (ApiException e ) {
413+ logApiException (state , e );
427414 }
428415
429- state .statsAfterAnalysis = stats ;
416+ state .buildStatistics = stats ;
430417
431418 return true ;
432419 }
@@ -450,33 +437,8 @@ private static Boolean getGroupedCounts(ScanTaskState state) {
450437 return false ;
451438 }
452439 } else {
453- state .groupedCounts = state .statsAfterAnalysis .getGroupedSeverityCounts ();
454- }
455-
456- return true ;
457- }
458-
459- private static Boolean writeReportArchive (ScanTaskState state ) {
460-
461- // TODO: Remove if not needed after implementing Build Results / Tables
462- /*
463- // If user provides an archive name, create a zip containing an simple html file
464- if(state.reportArchiveName != null && !state.reportArchiveName.isEmpty()) {
465- log(state, "Creating archived report file. Name: %s", state.reportArchiveName);
466-
467- CodeDxReportWriter reportWriter = new CodeDxReportWriter(state.reportArchiveName,
468- state.statsBeforeAnalysis,
469- state.statsAfterAnalysis,
470- state.apiUrl,
471- state.projectId);
472- try {
473- reportWriter.writeReport(state.taskContext.getRootDirectory());
474- } catch (IOException e) {
475- logError(state, "An error occurred when trying to create the report archive file.");
476- return false;
477- }
440+ state .groupedCounts = state .buildStatistics .getGroupedSeverityCounts ();
478441 }
479- */
480442
481443 return true ;
482444 }
@@ -501,32 +463,6 @@ private static Boolean checkFailureSeverity(ScanTaskState state) {
501463 return true ;
502464 }
503465
504- // Helper methods
505-
506- private static CodeDxBuildStatistics getBuildStats (ScanTaskState state ) {
507- Filter filter = new Filter ();
508- filter .put ("~status" , "gone" );
509-
510- GroupedCountsRequest bySeverity = new GroupedCountsRequest ();
511- bySeverity .setFilter (filter );
512- bySeverity .setCountBy ("severity" );
513-
514- GroupedCountsRequest byStatus = new GroupedCountsRequest ();
515- byStatus .setFilter (filter );
516- byStatus .setCountBy ("status" );
517-
518- CodeDxBuildStatistics stats = null ;
519- try {
520- List <GroupedCount > severityGroupedCounts = state .findingDataApi .getFindingsGroupCount (state .projectId , bySeverity );
521- List <GroupedCount > statusGroupedCounts = state .findingDataApi .getFindingsGroupCount (state .projectId , byStatus );
522- stats = new CodeDxBuildStatistics (severityGroupedCounts , statusGroupedCounts );
523- } catch (ApiException e ) {
524- logApiException (state , e );
525- }
526-
527- return stats ;
528- }
529-
530466 private static void log (ScanTaskState state , String format , Object ... args ) {
531467 state .buildLogger .addBuildLogEntry (String .format (format , args ));
532468 }
0 commit comments