github.com/jaylevin/jenkins-library@v1.230.4/cmd/gctsCloneRepository.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 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 resp, httpErr := httpClient.SendRequest("POST", url, nil, header, nil) 51 52 defer func() { 53 if resp != nil && resp.Body != nil { 54 resp.Body.Close() 55 } 56 }() 57 58 if resp == nil { 59 return errors.Errorf("did not retrieve a HTTP response: %v", httpErr) 60 } 61 62 bodyText, readErr := ioutil.ReadAll(resp.Body) 63 64 if readErr != nil { 65 return errors.Wrap(readErr, "HTTP response body could not be read") 66 } 67 68 response, parsingErr := gabs.ParseJSON([]byte(bodyText)) 69 70 if parsingErr != nil { 71 return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText)) 72 } 73 74 if httpErr != nil { 75 if resp.StatusCode == 500 { 76 if exception, ok := response.Path("errorLog.1.code").Data().(string); ok && exception == "GCTS.CLIENT.1420" { 77 log.Entry(). 78 WithField("repository", config.Repository). 79 Info("the repository has already been cloned") 80 return nil 81 } else if exception, ok := response.Path("errorLog.1.code").Data().(string); ok && exception == "GCTS.CLIENT.3302" { 82 log.Entry().Errorf("%v", response.Path("errorLog.1.message").Data().(string)) 83 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.") 84 return errors.Wrap(httpErr, "cloning the repository failed") 85 } 86 } 87 log.Entry().Errorf("a HTTP error occurred! Response body: %v", response) 88 return errors.Wrap(httpErr, "cloning the repository failed") 89 } 90 91 log.Entry(). 92 WithField("repository", config.Repository). 93 Info("successfully cloned the Git repository to the local repository") 94 return nil 95 }