1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

whitesource image scan removing the timestamp and commit id (#4842)

* whitesource image scan removing the timestamp and commit id to keep static project name

* moving the logic within whitesource step
This commit is contained in:
Vijayan T 2024-02-26 17:38:13 +05:30 committed by GitHub
parent ebf8e7d08d
commit 04028a647c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@ -1108,12 +1109,17 @@ func downloadDockerImageAsTarNew(config *ScanOptions, utils whitesourceUtils) er
dClientOptions := piperDocker.ClientOptions{ImageName: saveImageOptions.ContainerImage, RegistryURL: saveImageOptions.ContainerRegistryURL, LocalPath: "", ImageFormat: "legacy"}
dClient := &piperDocker.Client{}
dClient.SetOptions(dClientOptions)
if _, err := runContainerSaveImage(&saveImageOptions, &telemetry.CustomData{}, "./cache", "", dClient, utils); err != nil {
tarFilePath, err := runContainerSaveImage(&saveImageOptions, &telemetry.CustomData{}, "./cache", "", dClient, utils)
if err != nil {
if strings.Contains(fmt.Sprint(err), "no image found") {
log.SetErrorCategory(log.ErrorConfiguration)
}
return errors.Wrapf(err, "failed to download Docker image %v", config.ScanImage)
}
// to remove timestamp and artifact version
if err := renameTarfilePath(tarFilePath); err != nil {
return errors.Wrapf(err, "failed to rename image %v", err)
}
return nil
}
@ -1141,3 +1147,20 @@ func downloadDockerImageAsTar(config *ScanOptions, utils whitesourceUtils) error
return nil
}
func renameTarfilePath(tarFilepath string) error {
if _, err := os.Stat(tarFilepath); os.IsNotExist(err) {
return fmt.Errorf("file %s does not exist", tarFilepath)
}
pattern := `-\d{14}_[a-f0-9]{40}\.tar$` //format is -<timestamp>_<commitHash>.tar
regex := regexp.MustCompile(pattern)
if regex.MatchString(tarFilepath) {
newName := regex.ReplaceAllString(tarFilepath, ".tar")
err := os.Rename(tarFilepath, newName)
if err != nil {
return fmt.Errorf("error renaming file %s to %s: %v", tarFilepath, newName, err)
}
log.Entry().Infof("Renamed file %s to %s\n", tarFilepath, newName)
}
return nil
}