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

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"net/http"
     8  	"net/http/cookiejar"
     9  
    10  	gabs "github.com/Jeffail/gabs/v2"
    11  	"github.com/SAP/jenkins-library/pkg/command"
    12  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    13  	"github.com/SAP/jenkins-library/pkg/log"
    14  	"github.com/SAP/jenkins-library/pkg/telemetry"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func gctsCreateRepository(config gctsCreateRepositoryOptions, telemetryData *telemetry.CustomData) {
    19  	// for command execution use Command
    20  	c := command.Command{}
    21  	// reroute command output to logging framework
    22  	c.Stdout(log.Writer())
    23  	c.Stderr(log.Writer())
    24  
    25  	// for http calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    26  	// and use a  &piperhttp.Client{} in a custom system
    27  	// Example: step checkmarxExecuteScan.go
    28  	httpClient := &piperhttp.Client{}
    29  
    30  	// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
    31  	err := createRepository(&config, telemetryData, &c, httpClient)
    32  	if err != nil {
    33  		log.Entry().WithError(err).Fatal("step execution failed")
    34  	}
    35  }
    36  
    37  func createRepository(config *gctsCreateRepositoryOptions, telemetryData *telemetry.CustomData, command command.ExecRunner, httpClient piperhttp.Sender) error {
    38  
    39  	cookieJar, cookieErr := cookiejar.New(nil)
    40  	if cookieErr != nil {
    41  		return errors.Wrapf(cookieErr, "creating repository on the ABAP system %v failed", config.Host)
    42  	}
    43  	clientOptions := piperhttp.ClientOptions{
    44  		CookieJar: cookieJar,
    45  		Username:  config.Username,
    46  		Password:  config.Password,
    47  	}
    48  	httpClient.SetOptions(clientOptions)
    49  
    50  	type repoData struct {
    51  		RID                 string `json:"rid"`
    52  		Name                string `json:"name"`
    53  		Role                string `json:"role"`
    54  		Type                string `json:"type"`
    55  		VSID                string `json:"vsid"`
    56  		RemoteRepositoryURL string `json:"url"`
    57  	}
    58  
    59  	type createRequestBody struct {
    60  		Repository string   `json:"repository"`
    61  		Data       repoData `json:"data"`
    62  	}
    63  
    64  	reqBody := createRequestBody{
    65  		Repository: config.Repository,
    66  		Data: repoData{
    67  			RID:                 config.Repository,
    68  			Name:                config.Repository,
    69  			Role:                config.Role,
    70  			Type:                config.Type,
    71  			VSID:                config.VSID,
    72  			RemoteRepositoryURL: config.RemoteRepositoryURL,
    73  		},
    74  	}
    75  	jsonBody, marshalErr := json.Marshal(reqBody)
    76  
    77  	if marshalErr != nil {
    78  		return errors.Wrapf(marshalErr, "creating repository on the ABAP system %v failed", config.Host)
    79  	}
    80  
    81  	header := make(http.Header)
    82  	header.Set("Content-Type", "application/json")
    83  	header.Add("Accept", "application/json")
    84  
    85  	url := config.Host + "/sap/bc/cts_abapvcs/repository?sap-client=" + config.Client
    86  
    87  	url, urlErr := addQueryToURL(url, config.QueryParameters)
    88  
    89  	if urlErr != nil {
    90  
    91  		return urlErr
    92  	}
    93  
    94  	resp, httpErr := httpClient.SendRequest("POST", url, bytes.NewBuffer(jsonBody), header, nil)
    95  
    96  	defer func() {
    97  		if resp != nil && resp.Body != nil {
    98  			resp.Body.Close()
    99  		}
   100  	}()
   101  
   102  	if resp == nil {
   103  		return errors.Errorf("creating repository on the ABAP system %v failed: %v", config.Host, httpErr)
   104  	}
   105  
   106  	bodyText, readErr := io.ReadAll(resp.Body)
   107  
   108  	if readErr != nil {
   109  		return errors.Wrapf(readErr, "creating repository on the ABAP system %v failed", config.Host)
   110  	}
   111  
   112  	response, parsingErr := gabs.ParseJSON([]byte(bodyText))
   113  
   114  	if parsingErr != nil {
   115  		return errors.Wrapf(parsingErr, "creating repository on the ABAP system %v failed", config.Host)
   116  	}
   117  
   118  	if httpErr != nil {
   119  		if resp.StatusCode == 500 {
   120  			if exception, ok := response.Path("exception").Data().(string); ok && exception == "Repository already exists" {
   121  				log.Entry().
   122  					WithField("repository", config.Repository).
   123  					Infof("the repository already exists on the ABAP system %v", config.Host)
   124  				return nil
   125  			}
   126  		}
   127  		log.Entry().Errorf("a HTTP error occurred! Response body: %v", response)
   128  		return errors.Wrapf(httpErr, "creating repository on the ABAP system %v failed", config.Host)
   129  	}
   130  
   131  	log.Entry().
   132  		WithField("repository", config.Repository).
   133  		Infof("successfully created the repository on ABAP system %v", config.Host)
   134  	return nil
   135  }