github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsCreateRepository.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     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  	resp, httpErr := httpClient.SendRequest("POST", url, bytes.NewBuffer(jsonBody), header, nil)
    88  
    89  	defer func() {
    90  		if resp != nil && resp.Body != nil {
    91  			resp.Body.Close()
    92  		}
    93  	}()
    94  
    95  	if resp == nil {
    96  		return errors.Errorf("creating repository on the ABAP system %v failed: %v", config.Host, httpErr)
    97  	}
    98  
    99  	bodyText, readErr := ioutil.ReadAll(resp.Body)
   100  
   101  	if readErr != nil {
   102  		return errors.Wrapf(readErr, "creating repository on the ABAP system %v failed", config.Host)
   103  	}
   104  
   105  	response, parsingErr := gabs.ParseJSON([]byte(bodyText))
   106  
   107  	if parsingErr != nil {
   108  		return errors.Wrapf(parsingErr, "creating repository on the ABAP system %v failed", config.Host)
   109  	}
   110  
   111  	if httpErr != nil {
   112  		if resp.StatusCode == 500 {
   113  			if exception, ok := response.Path("exception").Data().(string); ok && exception == "Repository already exists" {
   114  				log.Entry().
   115  					WithField("repository", config.Repository).
   116  					Infof("the repository already exists on the ABAP system %v", config.Host)
   117  				return nil
   118  			}
   119  		}
   120  		log.Entry().Errorf("a HTTP error occurred! Response body: %v", response)
   121  		return errors.Wrapf(httpErr, "creating repository on the ABAP system %v failed", config.Host)
   122  	}
   123  
   124  	log.Entry().
   125  		WithField("repository", config.Repository).
   126  		Infof("successfully created the repository on ABAP system %v", config.Host)
   127  	return nil
   128  }