Skip to content

Commit

Permalink
whitesource image scan removing the timestamp and commit id (#4842)
Browse files Browse the repository at this point in the history
* whitesource image scan removing the timestamp and commit id to keep static project name

* moving the logic within whitesource step
  • Loading branch information
vijayanjay authored Feb 26, 2024
1 parent ebf8e7d commit 04028a6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/whitesourceExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 04028a6

Please sign in to comment.