github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentCreateTag.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  
     8  	"github.com/SAP/jenkins-library/pkg/abaputils"
     9  	"github.com/SAP/jenkins-library/pkg/command"
    10  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/telemetry"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  func abapEnvironmentCreateTag(config abapEnvironmentCreateTagOptions, _ *telemetry.CustomData) {
    17  
    18  	c := command.Command{}
    19  
    20  	c.Stdout(log.Writer())
    21  	c.Stderr(log.Writer())
    22  
    23  	var autils = abaputils.AbapUtils{
    24  		Exec: &c,
    25  	}
    26  
    27  	apiManager := abaputils.SoftwareComponentApiManager{
    28  		Client:        &piperhttp.Client{},
    29  		PollIntervall: 5 * time.Second,
    30  	}
    31  
    32  	if err := runAbapEnvironmentCreateTag(&config, &autils, &apiManager); err != nil {
    33  		log.Entry().WithError(err).Fatal("step execution failed")
    34  	}
    35  }
    36  
    37  func runAbapEnvironmentCreateTag(config *abapEnvironmentCreateTagOptions, com abaputils.Communication, apiManager abaputils.SoftwareComponentApiManagerInterface) error {
    38  
    39  	connectionDetails, errorGetInfo := com.GetAbapCommunicationArrangementInfo(convertTagConfig(config), "")
    40  	if errorGetInfo != nil {
    41  		return errors.Wrap(errorGetInfo, "Parameters for the ABAP Connection not available")
    42  	}
    43  	connectionDetails.CertificateNames = config.CertificateNames
    44  
    45  	backlog, errorPrepare := prepareBacklog(config)
    46  	if errorPrepare != nil {
    47  		return fmt.Errorf("Something failed during the tag creation: %w", errorPrepare)
    48  	}
    49  
    50  	return createTags(backlog, connectionDetails, apiManager)
    51  }
    52  
    53  func createTags(backlog []abaputils.CreateTagBacklog, con abaputils.ConnectionDetailsHTTP, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) {
    54  
    55  	errorOccurred := false
    56  	for _, item := range backlog {
    57  		err = createTagsForSingleItem(item, con, apiManager)
    58  		if err != nil {
    59  			errorOccurred = true
    60  		}
    61  	}
    62  
    63  	if errorOccurred {
    64  		message := "At least one tag has not been created"
    65  		log.Entry().Errorf(message)
    66  		return errors.New(message)
    67  	}
    68  	return nil
    69  
    70  }
    71  
    72  func createTagsForSingleItem(item abaputils.CreateTagBacklog, con abaputils.ConnectionDetailsHTTP, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) {
    73  
    74  	errorOccurred := false
    75  	for index := range item.Tags {
    76  		err = createSingleTag(item, index, con, apiManager)
    77  		if err != nil {
    78  			errorOccurred = true
    79  		}
    80  	}
    81  	if errorOccurred {
    82  		message := "At least one tag has not been created"
    83  		err = errors.New(message)
    84  	}
    85  	return err
    86  }
    87  
    88  func createSingleTag(item abaputils.CreateTagBacklog, index int, con abaputils.ConnectionDetailsHTTP, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) {
    89  
    90  	api, errGetAPI := apiManager.GetAPI(con, abaputils.Repository{Name: item.RepositoryName, CommitID: item.CommitID})
    91  	if errGetAPI != nil {
    92  		return errors.Wrap(errGetAPI, "Could not initialize the connection to the system")
    93  	}
    94  
    95  	createTagError := api.CreateTag(item.Tags[index])
    96  	if createTagError != nil {
    97  		return errors.Wrapf(err, "Creation of Tag failed on the ABAP system")
    98  	}
    99  
   100  	status, errorPollEntity := abaputils.PollEntity(api, apiManager.GetPollIntervall())
   101  
   102  	if errorPollEntity == nil && status == "S" {
   103  		log.Entry().Info("Created tag " + item.Tags[index].TagName + " for repository " + item.RepositoryName + " with commitID " + item.CommitID)
   104  	} else {
   105  		log.Entry().Error("NOT created: Tag " + item.Tags[index].TagName + " for repository " + item.RepositoryName + " with commitID " + item.CommitID)
   106  		err = errors.New("Creation of Tag failed on the ABAP system")
   107  	}
   108  
   109  	return err
   110  }
   111  
   112  func prepareBacklog(config *abapEnvironmentCreateTagOptions) (backlog []abaputils.CreateTagBacklog, err error) {
   113  
   114  	if config.Repositories != "" && config.RepositoryName != "" {
   115  		return nil, errors.New("Configuring the parameter repositories and the parameter repositoryName at the same time is not allowed")
   116  	}
   117  
   118  	if config.RepositoryName != "" && config.CommitID != "" {
   119  		backlog = append(backlog, abaputils.CreateTagBacklog{RepositoryName: config.RepositoryName, CommitID: config.CommitID})
   120  	}
   121  
   122  	if config.Repositories != "" {
   123  		descriptor, err := abaputils.ReadAddonDescriptor(config.Repositories) //config.Repositories should contain a file name
   124  		if err != nil {
   125  			return nil, err
   126  		}
   127  		for _, repo := range descriptor.Repositories {
   128  			backlogInstance := abaputils.CreateTagBacklog{RepositoryName: repo.Name, CommitID: repo.CommitID}
   129  			if config.GenerateTagForAddonComponentVersion && repo.VersionYAML != "" {
   130  				tag := abaputils.Tag{TagName: "v" + repo.VersionYAML, TagDescription: "Generated by the ABAP Environment Pipeline"}
   131  				backlogInstance.Tags = append(backlogInstance.Tags, tag)
   132  			}
   133  			backlog = append(backlog, backlogInstance)
   134  		}
   135  		if config.GenerateTagForAddonProductVersion {
   136  			if descriptor.AddonProduct != "" && descriptor.AddonVersionYAML != "" {
   137  				addonProductDash := strings.Replace(descriptor.AddonProduct, "/", "-", 2)
   138  				backlog = addTagToList(backlog, addonProductDash+"-"+descriptor.AddonVersionYAML, "Generated by the ABAP Environment Pipeline")
   139  			} else {
   140  				log.Entry().WithField("generateTagForAddonProductVersion", config.GenerateTagForAddonProductVersion).WithField("AddonProduct", descriptor.AddonProduct).WithField("AddonVersion", descriptor.AddonVersionYAML).Infof("Not all required values are provided to create an addon product version tag")
   141  			}
   142  		}
   143  	}
   144  	if config.TagName != "" {
   145  		backlog = addTagToList(backlog, config.TagName, config.TagDescription)
   146  	}
   147  	return backlog, nil
   148  }
   149  
   150  func addTagToList(backlog []abaputils.CreateTagBacklog, tag string, description string) []abaputils.CreateTagBacklog {
   151  
   152  	for i, item := range backlog {
   153  		tag := abaputils.Tag{TagName: tag, TagDescription: description}
   154  		backlog[i].Tags = append(item.Tags, tag)
   155  	}
   156  	return backlog
   157  }
   158  
   159  func convertTagConfig(config *abapEnvironmentCreateTagOptions) abaputils.AbapEnvironmentOptions {
   160  	subOptions := abaputils.AbapEnvironmentOptions{}
   161  
   162  	subOptions.CfAPIEndpoint = config.CfAPIEndpoint
   163  	subOptions.CfServiceInstance = config.CfServiceInstance
   164  	subOptions.CfServiceKeyName = config.CfServiceKeyName
   165  	subOptions.CfOrg = config.CfOrg
   166  	subOptions.CfSpace = config.CfSpace
   167  	subOptions.Host = config.Host
   168  	subOptions.Password = config.Password
   169  	subOptions.Username = config.Username
   170  
   171  	return subOptions
   172  }