github.com/SAP/jenkins-library@v1.362.0/pkg/contrast/reporting.go (about)

     1  package contrast
     2  
     3  import (
     4  	"encoding/json"
     5  	"path/filepath"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/piperutils"
     9  	"github.com/SAP/jenkins-library/pkg/toolrecord"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type ContrastAudit struct {
    14  	ToolName       string             `json:"toolName"`
    15  	ApplicationUrl string             `json:"applicationUrl"`
    16  	ScanResults    []ContrastFindings `json:"findings"`
    17  }
    18  
    19  type ContrastFindings struct {
    20  	ClassificationName string `json:"classificationName"`
    21  	Total              int    `json:"total"`
    22  	Audited            int    `json:"audited"`
    23  }
    24  
    25  type ApplicationInfo struct {
    26  	Url    string
    27  	Id     string
    28  	Name   string
    29  	Server string
    30  }
    31  
    32  func WriteJSONReport(jsonReport ContrastAudit, modulePath string) ([]piperutils.Path, error) {
    33  	utils := piperutils.Files{}
    34  	reportPaths := []piperutils.Path{}
    35  
    36  	reportsDirectory := filepath.Join(modulePath, "contrast")
    37  	jsonComplianceReportData := filepath.Join(reportsDirectory, "piper_contrast_report.json")
    38  	if err := utils.MkdirAll(reportsDirectory, 0777); err != nil {
    39  		return reportPaths, errors.Wrapf(err, "failed to create report directory")
    40  	}
    41  
    42  	file, _ := json.Marshal(jsonReport)
    43  	if err := utils.FileWrite(jsonComplianceReportData, file, 0666); err != nil {
    44  		log.SetErrorCategory(log.ErrorConfiguration)
    45  		return reportPaths, errors.Wrapf(err, "failed to write contrast json compliance report")
    46  	}
    47  
    48  	reportPaths = append(reportPaths, piperutils.Path{Name: "Contrast JSON Compliance Report", Target: jsonComplianceReportData})
    49  	return reportPaths, nil
    50  }
    51  
    52  func CreateAndPersistToolRecord(utils piperutils.FileUtils, appInfo *ApplicationInfo, modulePath string) (string, error) {
    53  	toolRecord, err := createToolRecordContrast(utils, appInfo, modulePath)
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  
    58  	toolRecordFileName, err := persistToolRecord(toolRecord)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  
    63  	return toolRecordFileName, nil
    64  }
    65  
    66  func createToolRecordContrast(utils piperutils.FileUtils, appInfo *ApplicationInfo, modulePath string) (*toolrecord.Toolrecord, error) {
    67  	record := toolrecord.New(utils, modulePath, "contrast", appInfo.Server)
    68  
    69  	record.DisplayName = appInfo.Name
    70  	record.DisplayURL = appInfo.Url
    71  
    72  	err := record.AddKeyData("application",
    73  		appInfo.Id,
    74  		appInfo.Name,
    75  		appInfo.Url)
    76  	if err != nil {
    77  		return record, err
    78  	}
    79  
    80  	return record, nil
    81  }
    82  
    83  func persistToolRecord(toolrecord *toolrecord.Toolrecord) (string, error) {
    84  	err := toolrecord.Persist()
    85  	if err != nil {
    86  		return "", err
    87  	}
    88  	return toolrecord.GetFileName(), nil
    89  }