github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/piperutils/stepResults.go (about) 1 package piperutils 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/SAP/jenkins-library/pkg/log" 10 ) 11 12 // Path - struct to serialize paths and some metadata back to the invoker 13 type Path struct { 14 Name string `json:"name"` 15 Target string `json:"target"` 16 Mandatory bool `json:"mandatory"` 17 Scope string `json:"scope"` 18 } 19 20 type fileWriter interface { 21 WriteFile(filename string, data []byte, perm os.FileMode) error 22 } 23 24 // PersistReportsAndLinks stores the report paths and links in JSON format in the workspace for processing outside 25 func PersistReportsAndLinks(stepName, workspace string, files fileWriter, reports, links []Path) error { 26 if reports == nil { 27 reports = []Path{} 28 } 29 if links == nil { 30 links = []Path{} 31 } 32 33 hasMandatoryReport := false 34 for _, report := range reports { 35 if report.Mandatory { 36 hasMandatoryReport = true 37 break 38 } 39 } 40 41 reportList, err := json.Marshal(&reports) 42 if err != nil { 43 if hasMandatoryReport { 44 return fmt.Errorf("failed to marshall reports.json data for archiving: %w", err) 45 } 46 log.Entry().Errorln("Failed to marshall reports.json data for archiving") 47 } 48 49 if err := files.WriteFile(filepath.Join(workspace, fmt.Sprintf("%v_reports.json", stepName)), reportList, 0666); err != nil { 50 return fmt.Errorf("failed to write reports.json: %w", err) 51 } 52 53 linkList, err := json.Marshal(&links) 54 if err != nil { 55 return fmt.Errorf("failed to marshall links.json data for archiving: %w", err) 56 } 57 if err := files.WriteFile(filepath.Join(workspace, fmt.Sprintf("%v_links.json", stepName)), linkList, 0666); err != nil { 58 return fmt.Errorf("failed to write links.json: %w", err) 59 } 60 return nil 61 }