github.com/SAP/jenkins-library@v1.362.0/cmd/abapEnvironmentPullGitRepo.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/SAP/jenkins-library/pkg/abaputils" 8 "github.com/SAP/jenkins-library/pkg/command" 9 piperhttp "github.com/SAP/jenkins-library/pkg/http" 10 "github.com/SAP/jenkins-library/pkg/log" 11 "github.com/SAP/jenkins-library/pkg/telemetry" 12 "github.com/pkg/errors" 13 ) 14 15 func abapEnvironmentPullGitRepo(options abapEnvironmentPullGitRepoOptions, _ *telemetry.CustomData) { 16 17 // for command execution use Command 18 c := command.Command{} 19 // reroute command output to logging framework 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 // error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end 33 err := runAbapEnvironmentPullGitRepo(&options, &autils, &apiManager) 34 if err != nil { 35 log.Entry().WithError(err).Fatal("step execution failed") 36 } 37 } 38 39 func runAbapEnvironmentPullGitRepo(options *abapEnvironmentPullGitRepoOptions, com abaputils.Communication, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) { 40 41 subOptions := convertPullConfig(options) 42 43 // Determine the host, user and password, either via the input parameters or via a cloud foundry service key 44 connectionDetails, err := com.GetAbapCommunicationArrangementInfo(subOptions, "") 45 if err != nil { 46 return errors.Wrap(err, "Parameters for the ABAP Connection not available") 47 } 48 connectionDetails.CertificateNames = options.CertificateNames 49 50 var repositories []abaputils.Repository 51 err = checkPullRepositoryConfiguration(*options) 52 if err != nil { 53 return err 54 } 55 repositories, err = abaputils.GetRepositories(&abaputils.RepositoriesConfig{RepositoryNames: options.RepositoryNames, Repositories: options.Repositories, RepositoryName: options.RepositoryName, CommitID: options.CommitID}, false) 56 handleIgnoreCommit(repositories, options.IgnoreCommit) 57 if err != nil { 58 return err 59 } 60 61 err = pullRepositories(repositories, connectionDetails, apiManager) 62 return err 63 64 } 65 66 func pullRepositories(repositories []abaputils.Repository, pullConnectionDetails abaputils.ConnectionDetailsHTTP, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) { 67 log.Entry().Infof("Start pulling %v repositories", len(repositories)) 68 for _, repo := range repositories { 69 err = handlePull(repo, pullConnectionDetails, apiManager) 70 if err != nil { 71 break 72 } 73 } 74 if err == nil { 75 finishPullLogs() 76 } 77 return err 78 } 79 80 func handlePull(repo abaputils.Repository, con abaputils.ConnectionDetailsHTTP, apiManager abaputils.SoftwareComponentApiManagerInterface) (err error) { 81 82 logString := repo.GetPullLogString() 83 errorString := "Pull of the " + logString + " failed on the ABAP system" 84 85 abaputils.AddDefaultDashedLine(1) 86 log.Entry().Info("Start pulling the " + logString) 87 abaputils.AddDefaultDashedLine(1) 88 89 api, errGetAPI := apiManager.GetAPI(con, repo) 90 if errGetAPI != nil { 91 return errors.Wrap(errGetAPI, "Could not initialize the connection to the system") 92 } 93 94 err = api.Pull() 95 if err != nil { 96 return errors.Wrapf(err, errorString) 97 } 98 99 // Polling the status of the repository import on the ABAP Environment system 100 status, errorPollEntity := abaputils.PollEntity(api, apiManager.GetPollIntervall()) 101 if errorPollEntity != nil { 102 return errors.Wrapf(errorPollEntity, errorString) 103 } 104 if status == "E" { 105 return errors.New(errorString) 106 } 107 log.Entry().Info(repo.Name + " was pulled successfully") 108 return err 109 } 110 111 func checkPullRepositoryConfiguration(options abapEnvironmentPullGitRepoOptions) error { 112 113 if (len(options.RepositoryNames) > 0 && options.Repositories != "") || (len(options.RepositoryNames) > 0 && options.RepositoryName != "") || (options.RepositoryName != "" && options.Repositories != "") { 114 return fmt.Errorf("Checking configuration failed: %w", errors.New("Only one of the paramters `RepositoryName`,`RepositoryNames` or `Repositories` may be configured at the same time")) 115 } 116 if len(options.RepositoryNames) == 0 && options.Repositories == "" && options.RepositoryName == "" { 117 return fmt.Errorf("Checking configuration failed: %w", errors.New("You have not specified any repository configuration to be pulled into the ABAP Environment System. Please make sure that you specified the repositories that should be pulled either in a dedicated file or via the parameter 'repositoryNames'. For more information please read the User documentation")) 118 } 119 return nil 120 } 121 122 func finishPullLogs() { 123 abaputils.AddDefaultDashedLine(1) 124 log.Entry().Info("All repositories were pulled successfully") 125 } 126 127 func convertPullConfig(config *abapEnvironmentPullGitRepoOptions) abaputils.AbapEnvironmentOptions { 128 subOptions := abaputils.AbapEnvironmentOptions{} 129 130 subOptions.CfAPIEndpoint = config.CfAPIEndpoint 131 subOptions.CfServiceInstance = config.CfServiceInstance 132 subOptions.CfServiceKeyName = config.CfServiceKeyName 133 subOptions.CfOrg = config.CfOrg 134 subOptions.CfSpace = config.CfSpace 135 subOptions.Host = config.Host 136 subOptions.Password = config.Password 137 subOptions.Username = config.Username 138 return subOptions 139 } 140 141 func handleIgnoreCommit(repositories []abaputils.Repository, ignoreCommit bool) { 142 for i := range repositories { 143 if ignoreCommit { 144 repositories[i].CommitID = "" 145 } 146 } 147 }