github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/abap/aakaas/componentVersion.go (about)

     1  package aakaas
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/url"
     6  
     7  	abapbuild "github.com/SAP/jenkins-library/pkg/abap/build"
     8  	"github.com/SAP/jenkins-library/pkg/abaputils"
     9  	"github.com/SAP/jenkins-library/pkg/log"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  const cvQueryURL string = "/odata/aas_ocs_package/xSSDAxC_Component_Version"
    14  const cvValidateURL string = "/odata/aas_ocs_package/ValidateComponentVersion"
    15  
    16  type ComponentVersion struct {
    17  	versionable
    18  }
    19  
    20  func (c *ComponentVersion) ConstructComponentVersion(repo abaputils.Repository, conn abapbuild.Connector) error {
    21  	if err := c.constructVersionable(repo.Name, repo.VersionYAML, conn, cvQueryURL); err != nil {
    22  		return err
    23  	}
    24  	if err := c.resolveNext(statusFilterCV); err != nil {
    25  		return err
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  func (c *ComponentVersion) CopyVersionFieldsToRepo(repo *abaputils.Repository) {
    32  	repo.Version = c.TechRelease
    33  	repo.SpLevel = c.TechSpLevel
    34  	repo.PatchLevel = c.TechPatchLevel
    35  	repo.VersionYAML = c.Version
    36  }
    37  
    38  func (c *ComponentVersion) Validate() error {
    39  	log.Entry().Infof("Validate component %s version %s and resolve version", c.Name, c.Version)
    40  
    41  	values := url.Values{}
    42  	values.Set("Name", "'"+c.Name+"'")
    43  	values.Set("Version", "'"+c.Version+"'")
    44  	requestUrl := cvValidateURL + "?" + values.Encode()
    45  
    46  	body, err := c.connector.Get(requestUrl)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	var response jsonComponentVersionValidationResponse
    51  	if err := json.Unmarshal(body, &response); err != nil {
    52  		return errors.Wrap(err, "Unexpected AAKaaS response for Validate Component Version: "+string(body))
    53  	}
    54  	c.Name = response.Wrapper.Name
    55  	c.TechRelease = response.Wrapper.TechRelease
    56  	c.TechSpLevel = response.Wrapper.TechSpLevel
    57  	c.TechPatchLevel = response.Wrapper.TechPatchLevel
    58  	log.Entry().Infof("Resolved version %s, splevel %s, patchlevel %s", c.TechRelease, c.TechSpLevel, c.TechPatchLevel)
    59  
    60  	return nil
    61  }
    62  
    63  type jsonComponentVersionValidationResponse struct {
    64  	Wrapper struct {
    65  		Name           string `json:"Name"`
    66  		TechRelease    string `json:"Version"`
    67  		TechSpLevel    string `json:"SpLevel"`
    68  		TechPatchLevel string `json:"PatchLevel"`
    69  	} `json:"d"`
    70  }