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

     1  package cmd
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/http/cookiejar"
     7  
     8  	"github.com/Jeffail/gabs/v2"
     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 gctsCloneRepository(config gctsCloneRepositoryOptions, telemetryData *telemetry.CustomData) {
    16  
    17  	// for http calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    18  	// and use a  &piperhttp.Client{} in a custom system
    19  	// Example: step checkmarxExecuteScan.go
    20  	httpClient := &piperhttp.Client{}
    21  
    22  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    23  	err := cloneRepository(&config, telemetryData, httpClient)
    24  	if err != nil {
    25  		log.Entry().WithError(err).Fatal("step execution failed")
    26  	}
    27  }
    28  
    29  func cloneRepository(config *gctsCloneRepositoryOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
    30  
    31  	cookieJar, cookieErr := cookiejar.New(nil)
    32  	if cookieErr != nil {
    33  		return errors.Wrap(cookieErr, "creating a cookie jar failed")
    34  	}
    35  	clientOptions := piperhttp.ClientOptions{
    36  		CookieJar: cookieJar,
    37  		Username:  config.Username,
    38  		Password:  config.Password,
    39  	}
    40  	httpClient.SetOptions(clientOptions)
    41  
    42  	header := make(http.Header)
    43  	header.Set("Content-Type", "application/json")
    44  	header.Add("Accept", "application/json")
    45  
    46  	url := config.Host +
    47  		"/sap/bc/cts_abapvcs/repository/" + config.Repository +
    48  		"/clone?sap-client=" + config.Client
    49  
    50  	url, urlErr := addQueryToURL(url, config.QueryParameters)
    51  
    52  	if urlErr != nil {
    53  
    54  		return urlErr
    55  	}
    56  	resp, httpErr := httpClient.SendRequest("POST", url, nil, header, nil)
    57  
    58  	defer func() {
    59  		if resp != nil && resp.Body != nil {
    60  			resp.Body.Close()
    61  		}
    62  	}()
    63  
    64  	if resp == nil {
    65  		return errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
    66  	}
    67  
    68  	bodyText, readErr := io.ReadAll(resp.Body)
    69  
    70  	if readErr != nil {
    71  		return errors.Wrap(readErr, "HTTP response body could not be read")
    72  	}
    73  
    74  	response, parsingErr := gabs.ParseJSON([]byte(bodyText))
    75  
    76  	if parsingErr != nil {
    77  		return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText))
    78  	}
    79  
    80  	if httpErr != nil {
    81  		if resp.StatusCode == 500 {
    82  			if exception, ok := response.Path("errorLog.1.code").Data().(string); ok && exception == "GCTS.CLIENT.1420" {
    83  				log.Entry().
    84  					WithField("repository", config.Repository).
    85  					Info("the repository has already been cloned")
    86  				return nil
    87  			} else if exception, ok := response.Path("errorLog.1.code").Data().(string); ok && exception == "GCTS.CLIENT.3302" {
    88  				log.Entry().Errorf("%v", response.Path("errorLog.1.message").Data().(string))
    89  				log.Entry().Error("possible reason: the remote repository is set to 'private'. you need to provide the local ABAP server repository with authentication credentials to the remote Git repository in order to clone it.")
    90  				return errors.Wrap(httpErr, "cloning the repository failed")
    91  			}
    92  		}
    93  		log.Entry().Errorf("a HTTP error occurred! Response body: %v", response)
    94  		return errors.Wrap(httpErr, "cloning the repository failed")
    95  	}
    96  
    97  	log.Entry().
    98  		WithField("repository", config.Repository).
    99  		Info("successfully cloned the Git repository to the local repository")
   100  	return nil
   101  }