github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/cnbutils/project/metadata/metadata.go (about)

     1  // Package metadata handles generation of the project-metadata.toml
     2  package metadata
     3  
     4  import (
     5  	"bytes"
     6  	"path/filepath"
     7  
     8  	"github.com/BurntSushi/toml"
     9  	"github.com/SAP/jenkins-library/pkg/cnbutils"
    10  	"github.com/SAP/jenkins-library/pkg/log"
    11  	"github.com/SAP/jenkins-library/pkg/piperenv"
    12  	"github.com/buildpacks/lifecycle/platform"
    13  )
    14  
    15  var metadataFilePath = "/layers/project-metadata.toml"
    16  
    17  func writeProjectMetadata(metadata platform.ProjectMetadata, path string, utils cnbutils.BuildUtils) error {
    18  	var buf bytes.Buffer
    19  
    20  	err := toml.NewEncoder(&buf).Encode(metadata)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	err = utils.FileWrite(path, buf.Bytes(), 0644)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	return nil
    31  }
    32  
    33  func extractMetadataFromCPE(piperEnvRoot string, utils cnbutils.BuildUtils) platform.ProjectMetadata {
    34  	cpePath := filepath.Join(piperEnvRoot, "commonPipelineEnvironment")
    35  	return platform.ProjectMetadata{
    36  		Source: &platform.ProjectSource{
    37  			Type: "git",
    38  			Version: map[string]interface{}{
    39  				"commit":   piperenv.GetResourceParameter(cpePath, "git", "headCommitId"),
    40  				"describe": piperenv.GetResourceParameter(cpePath, "git", "commitMessage"),
    41  			},
    42  			Metadata: map[string]interface{}{
    43  				"refs": []string{
    44  					piperenv.GetResourceParameter(cpePath, "git", "branch"),
    45  				},
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func WriteProjectMetadata(piperEnvRoot string, utils cnbutils.BuildUtils) {
    52  	projectMetadata := extractMetadataFromCPE(piperEnvRoot, utils)
    53  
    54  	err := writeProjectMetadata(projectMetadata, metadataFilePath, utils)
    55  	if err != nil {
    56  		log.Entry().Warnf("failed write 'project-metadata.toml', error: %s", err.Error())
    57  		return
    58  	}
    59  }