github.com/jaylevin/jenkins-library@v1.230.4/pkg/piperutils/stepResults.go (about)

     1  package piperutils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/log"
     8  	"github.com/SAP/jenkins-library/pkg/piperenv"
     9  )
    10  
    11  // Path - struct to serialize paths and some metadata back to the invoker
    12  type Path struct {
    13  	Name      string `json:"name"`
    14  	Target    string `json:"target"`
    15  	Mandatory bool   `json:"mandatory"`
    16  	Scope     string `json:"scope"`
    17  }
    18  
    19  // PersistReportsAndLinks stores the report paths and links in JSON format in the workspace for processing outside
    20  func PersistReportsAndLinks(stepName, workspace string, reports, links []Path) {
    21  	if reports == nil {
    22  		reports = []Path{}
    23  	}
    24  	if links == nil {
    25  		links = []Path{}
    26  	}
    27  
    28  	hasMandatoryReport := false
    29  	for _, report := range reports {
    30  		if report.Mandatory {
    31  			hasMandatoryReport = true
    32  			break
    33  		}
    34  	}
    35  	reportList, err := json.Marshal(&reports)
    36  	if err != nil {
    37  		if hasMandatoryReport {
    38  			log.Entry().Fatalln("Failed to marshall reports.json data for archiving")
    39  		}
    40  		log.Entry().Errorln("Failed to marshall reports.json data for archiving")
    41  	}
    42  	piperenv.SetParameter(workspace, fmt.Sprintf("%v_reports.json", stepName), string(reportList))
    43  
    44  	linkList, err := json.Marshal(&links)
    45  	if err != nil {
    46  		log.Entry().Errorln("Failed to marshall links.json data for archiving")
    47  	} else {
    48  		piperenv.SetParameter(workspace, fmt.Sprintf("%v_links.json", stepName), string(linkList))
    49  	}
    50  }