github.com/jaylevin/jenkins-library@v1.230.4/pkg/gcs/reporting.go (about) 1 package gcs 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 ) 8 9 type ReportOutputParam struct { 10 FilePattern string 11 ParamRef string 12 StepResultType string 13 } 14 15 type Task struct { 16 SourcePath string 17 TargetPath string 18 } 19 20 func PersistReportsToGCS(gcsClient Client, outputParams []ReportOutputParam, inputParams map[string]string, gcsFolderPath string, gcsBucketID string, gcsSubFolder string, searchFilesFunc func(string) ([]string, error), fileInfo func(string) (os.FileInfo, error)) error { 21 tasks := []Task{} 22 for _, param := range outputParams { 23 targetFolder := GetTargetFolder(gcsFolderPath, param.StepResultType, gcsSubFolder) 24 if param.ParamRef != "" { 25 paramValue, ok := inputParams[param.ParamRef] 26 if !ok { 27 return fmt.Errorf("there is no such input parameter as %s", param.ParamRef) 28 } 29 if paramValue == "" { 30 return fmt.Errorf("the value of the parameter %s must not be empty", param.ParamRef) 31 } 32 tasks = append(tasks, Task{SourcePath: paramValue, TargetPath: filepath.Join(targetFolder, paramValue)}) 33 } else { 34 foundFiles, err := searchFilesFunc(param.FilePattern) 35 if err != nil { 36 return fmt.Errorf("failed to persist reports: %v", err) 37 } 38 for _, sourcePath := range foundFiles { 39 fileInfo, err := fileInfo(sourcePath) 40 if err != nil { 41 return fmt.Errorf("failed to persist reports: %v", err) 42 } 43 if fileInfo.IsDir() { 44 continue 45 } 46 tasks = append(tasks, Task{SourcePath: sourcePath, TargetPath: filepath.Join(targetFolder, sourcePath)}) 47 } 48 } 49 } 50 for _, task := range tasks { 51 if err := gcsClient.UploadFile(gcsBucketID, task.SourcePath, task.TargetPath); err != nil { 52 return fmt.Errorf("failed to persist reports: %v", err) 53 } 54 } 55 return nil 56 }