1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/gcs/reporting.go
Siarhei Pazdniakou cd243ee542
feat(gcs): allow upload to gcs from steps (#3034)
* Upload reports to Google Cloud Storage bucket

* Added tests. Made fixes

* Update step generation. GCS client was moved to GeneralConfig

* Code was refactored

* Fixed issues

* Fixed issues

* Code correction due to PR comments

* Improved gcs client and integration tests

* Integrated gcp config. Updated step metadata

* Fixed issues. Added tests

* Added cpe, vault, aliases resolving for reporting parameters

* Added tests

* Uncommented DeferExitHandler. Removed useless comments

* fixed cloning of config

* Added comments for exported functions. Removed unused mock

* minor fix

* Implemented setting of report name via paramRef

* some refactoring. Writing tests

* Update pkg/config/reporting.go

* Update cmd/sonarExecuteScan_generated.go

* Apply suggestions from code review

* Update pkg/config/reporting.go

* Update pkg/config/reporting.go

* fixed removing valut secret files

* Update pkg/config/reporting.go

* restore order

* restore order

* Apply suggestions from code review

* go generate

* fixed tests

* Update resources/metadata/sonarExecuteScan.yaml

* Update resources.go

* Fixed tests. Code was regenerated

* changed somewhere gcp to gcs. Fixed one test

* move gcsSubFolder to input parameters

* fixed removing valut secret files

* minor fix in integration tests

* fix integration tests

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2021-12-15 15:07:47 +01:00

57 lines
1.7 KiB
Go

package gcs
import (
"fmt"
"os"
"path/filepath"
)
type ReportOutputParam struct {
FilePattern string
ParamRef string
StepResultType string
}
type Task struct {
SourcePath string
TargetPath string
}
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 {
tasks := []Task{}
for _, param := range outputParams {
targetFolder := GetTargetFolder(gcsFolderPath, param.StepResultType, gcsSubFolder)
if param.ParamRef != "" {
paramValue, ok := inputParams[param.ParamRef]
if !ok {
return fmt.Errorf("there is no such input parameter as %s", param.ParamRef)
}
if paramValue == "" {
return fmt.Errorf("the value of the parameter %s must not be empty", param.ParamRef)
}
tasks = append(tasks, Task{SourcePath: paramValue, TargetPath: filepath.Join(targetFolder, paramValue)})
} else {
foundFiles, err := searchFilesFunc(param.FilePattern)
if err != nil {
return fmt.Errorf("failed to persist reports: %v", err)
}
for _, sourcePath := range foundFiles {
fileInfo, err := fileInfo(sourcePath)
if err != nil {
return fmt.Errorf("failed to persist reports: %v", err)
}
if fileInfo.IsDir() {
continue
}
tasks = append(tasks, Task{SourcePath: sourcePath, TargetPath: filepath.Join(targetFolder, sourcePath)})
}
}
}
for _, task := range tasks {
if err := gcsClient.UploadFile(gcsBucketID, task.SourcePath, task.TargetPath); err != nil {
return fmt.Errorf("failed to persist reports: %v", err)
}
}
return nil
}