github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/codeql/reporting.go (about)

     1  package codeql
     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/pkg/errors"
    10  )
    11  
    12  type CodeqlAudit struct {
    13  	ToolName               string           `json:"toolName"`
    14  	RepositoryUrl          string           `json:"repositoryUrl"`
    15  	RepositoryReferenceUrl string           `json:"repositoryReferenceUrl"` //URL of PR or Branch where scan was performed
    16  	CodeScanningLink       string           `json:"codeScanningLink"`
    17  	QuerySuite             string           `json:"querySuite"`
    18  	ScanResults            []CodeqlFindings `json:"findings"`
    19  }
    20  
    21  type CodeqlFindings struct {
    22  	ClassificationName string `json:"classificationName"`
    23  	Total              int    `json:"total"`
    24  	Audited            int    `json:"audited"`
    25  }
    26  
    27  func WriteJSONReport(jsonReport CodeqlAudit, modulePath string) ([]piperutils.Path, error) {
    28  	utils := piperutils.Files{}
    29  	reportPaths := []piperutils.Path{}
    30  
    31  	reportsDirectory := filepath.Join(modulePath, "codeql")
    32  	jsonComplianceReportPath := filepath.Join(reportsDirectory, "piper_codeql_report.json")
    33  	if err := utils.MkdirAll(reportsDirectory, 0777); err != nil {
    34  		return reportPaths, errors.Wrapf(err, "failed to create report directory")
    35  	}
    36  
    37  	file, _ := json.Marshal(jsonReport)
    38  	if err := utils.FileWrite(jsonComplianceReportPath, file, 0666); err != nil {
    39  		log.SetErrorCategory(log.ErrorConfiguration)
    40  		return reportPaths, errors.Wrapf(err, "failed to write codeql json compliance report")
    41  	}
    42  
    43  	reportPaths = append(reportPaths, piperutils.Path{Name: "Codeql JSON Compliance Report", Target: jsonComplianceReportPath})
    44  
    45  	return reportPaths, nil
    46  }