github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/utils/result.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"os"
     7  
     8  	"github.com/jfrog/jfrog-cli-core/v2/common/project"
     9  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
    10  	clientutils "github.com/jfrog/jfrog-client-go/utils"
    11  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    12  	"github.com/jfrog/jfrog-client-go/utils/io/content"
    13  )
    14  
    15  const (
    16  	configDeployerPrefix = "deployer"
    17  	gradleConfigRepo     = "repo"
    18  	configServerId       = "serverid"
    19  )
    20  
    21  type Result struct {
    22  	successCount int
    23  	failCount    int
    24  	reader       *content.ContentReader
    25  }
    26  
    27  func (r *Result) SuccessCount() int {
    28  	return r.successCount
    29  }
    30  
    31  func (r *Result) FailCount() int {
    32  	return r.failCount
    33  }
    34  
    35  func (r *Result) Reader() *content.ContentReader {
    36  	return r.reader
    37  }
    38  
    39  func (r *Result) SetSuccessCount(successCount int) {
    40  	r.successCount = successCount
    41  }
    42  
    43  func (r *Result) SetFailCount(failCount int) {
    44  	r.failCount = failCount
    45  }
    46  
    47  func (r *Result) SetReader(reader *content.ContentReader) {
    48  	r.reader = reader
    49  }
    50  
    51  // UnmarshalDeployableArtifacts reads and parses the deployed artifacts details from the provided file.
    52  // The details were written by Build-info project while deploying artifacts to maven and gradle repositories.
    53  // deployableArtifactsFilePath - path to deployableArtifacts file written by Build-info project.
    54  // projectConfigPath - path to gradle/maven config yaml path.
    55  // lateDeploy - boolean indicates if the artifacts was expected to be deployed.
    56  func UnmarshalDeployableArtifacts(deployableArtifactsFilePath, projectConfigPath string, lateDeploy bool) (*Result, error) {
    57  	modulesMap, err := unmarshalDeployableArtifactsJson(deployableArtifactsFilePath)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	url, repo, err := getDeployerUrlAndRepo(modulesMap, projectConfigPath)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	// Iterate over the modules map, counting successes/failures & save artifact's SourcePath, TargetPath, and Sha256.
    66  	succeeded, failed := 0, 0
    67  	var artifactsArray []clientutils.FileTransferDetails
    68  	for _, module := range *modulesMap {
    69  		for _, artifact := range module {
    70  			if lateDeploy || artifact.DeploySucceeded {
    71  				artifactDetails, err := artifact.CreateFileTransferDetails(url, repo)
    72  				if err != nil {
    73  					return nil, err
    74  				}
    75  				artifactsArray = append(artifactsArray, artifactDetails)
    76  				succeeded++
    77  			} else {
    78  				failed++
    79  			}
    80  		}
    81  	}
    82  	err = clientutils.SaveFileTransferDetailsInFile(deployableArtifactsFilePath, &artifactsArray)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	// Return result
    87  	result := new(Result)
    88  	result.SetSuccessCount(succeeded)
    89  	result.SetFailCount(failed)
    90  	result.SetReader(content.NewContentReader(deployableArtifactsFilePath, "files"))
    91  	return result, nil
    92  }
    93  
    94  // getDeployerUrlAndRepo returns the deployer url and the target repository for maven and gradle.
    95  // Url is being read from the project's local configuration file.
    96  // Repository is being read from the modulesMap.
    97  // modulesMap - map of the DeployableArtifactDetails.
    98  // configPath -  path to the project's local configuration file.
    99  func getDeployerUrlAndRepo(modulesMap *map[string][]clientutils.DeployableArtifactDetails, configPath string) (string, string, error) {
   100  	repo := getTargetRepoFromMap(modulesMap)
   101  	vConfig, err := project.ReadConfigFile(configPath, project.YAML)
   102  	if err != nil {
   103  		return "", "", err
   104  	}
   105  	// The relevant deployment repository will be written by the build-info project to the deployableArtifacts file starting from version 2.24.12 of build-info-extractor-gradle.
   106  	// In case of a gradle project with a configuration of 'usePlugin=true' it's possible that an old build-info-extractor-gradle version is being used.
   107  	// In this case, the value of "repo" will be empty, and the deployment repository will be therefore read from the local project configuration file.
   108  	if repo == "" {
   109  		repo = vConfig.GetString(configDeployerPrefix + "." + gradleConfigRepo)
   110  	}
   111  	artDetails, err := config.GetSpecificConfig(vConfig.GetString(configDeployerPrefix+"."+configServerId), true, true)
   112  	if err != nil {
   113  		return "", "", err
   114  	}
   115  	url := artDetails.ArtifactoryUrl
   116  	return url, repo, nil
   117  }
   118  
   119  func getTargetRepoFromMap(modulesMap *map[string][]clientutils.DeployableArtifactDetails) string {
   120  	for _, module := range *modulesMap {
   121  		for _, artifact := range module {
   122  			return artifact.TargetRepository
   123  		}
   124  	}
   125  	return ""
   126  }
   127  
   128  func unmarshalDeployableArtifactsJson(filesPath string) (*map[string][]clientutils.DeployableArtifactDetails, error) {
   129  	// Open the file
   130  	jsonFile, err := os.Open(filesPath)
   131  	defer func() {
   132  		e := jsonFile.Close()
   133  		if err == nil {
   134  			err = errorutils.CheckError(e)
   135  		}
   136  	}()
   137  	if err != nil {
   138  		return nil, errorutils.CheckError(err)
   139  	}
   140  	// Read and convert json file to a modules map
   141  	byteValue, err := io.ReadAll(jsonFile)
   142  	if err != nil {
   143  		return nil, errorutils.CheckError(err)
   144  	}
   145  	var modulesMap map[string][]clientutils.DeployableArtifactDetails
   146  	if len(byteValue) == 0 {
   147  		return &modulesMap, nil
   148  	}
   149  	err = json.Unmarshal(byteValue, &modulesMap)
   150  	if err != nil {
   151  		return nil, errorutils.CheckError(err)
   152  	}
   153  	return &modulesMap, nil
   154  }