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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"mime"
     7  	"net/http"
     8  	"os"
     9  	"path/filepath"
    10  
    11  	"github.com/SAP/jenkins-library/pkg/cpi"
    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 integrationArtifactDownload(config integrationArtifactDownloadOptions, telemetryData *telemetry.CustomData) {
    19  	// Utils can be used wherever the command.ExecRunner interface is expected.
    20  	// It can also be used for example as a mavenExecRunner.
    21  	httpClient := &piperhttp.Client{}
    22  
    23  	// For HTTP calls import  piperhttp "github.com/SAP/jenkins-library/pkg/http"
    24  	// and use a  &piperhttp.Client{} in a custom system
    25  	// Example: step checkmarxExecuteScan.go
    26  
    27  	// Error situations should be bubbled up until they reach the line below which will then stop execution
    28  	// through the log.Entry().Fatal() call leading to an os.Exit(1) in the end.
    29  	err := runIntegrationArtifactDownload(&config, telemetryData, httpClient)
    30  	if err != nil {
    31  		log.Entry().WithError(err).Fatal("step execution failed")
    32  	}
    33  }
    34  
    35  func runIntegrationArtifactDownload(config *integrationArtifactDownloadOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
    36  	clientOptions := piperhttp.ClientOptions{}
    37  	header := make(http.Header)
    38  	header.Add("Accept", "application/zip")
    39  	serviceKey, err := cpi.ReadCpiServiceKey(config.APIServiceKey)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	downloadArtifactURL := fmt.Sprintf("%s/api/v1/IntegrationDesigntimeArtifacts(Id='%s',Version='%s')/$value", serviceKey.OAuth.Host, config.IntegrationFlowID, config.IntegrationFlowVersion)
    44  	tokenParameters := cpi.TokenParameters{TokenURL: serviceKey.OAuth.OAuthTokenProviderURL, Username: serviceKey.OAuth.ClientID, Password: serviceKey.OAuth.ClientSecret, Client: httpClient}
    45  	token, err := cpi.CommonUtils.GetBearerToken(tokenParameters)
    46  	if err != nil {
    47  		return errors.Wrap(err, "failed to fetch Bearer Token")
    48  	}
    49  	clientOptions.Token = fmt.Sprintf("Bearer %s", token)
    50  	httpClient.SetOptions(clientOptions)
    51  	httpMethod := "GET"
    52  	downloadResp, httpErr := httpClient.SendRequest(httpMethod, downloadArtifactURL, nil, header, nil)
    53  	if httpErr != nil {
    54  		return errors.Wrapf(httpErr, "HTTP %v request to %v failed with error", httpMethod, downloadArtifactURL)
    55  	}
    56  	if downloadResp == nil {
    57  		return errors.Errorf("did not retrieve a HTTP response: %v", httpErr)
    58  	}
    59  	contentDisposition := downloadResp.Header.Get("Content-Disposition")
    60  	disposition, params, err := mime.ParseMediaType(contentDisposition)
    61  	if err != nil {
    62  		return errors.Wrapf(err, "failed to read filename from http response headers, Content-Disposition "+disposition)
    63  	}
    64  	filename := params["filename"]
    65  
    66  	if downloadResp != nil && downloadResp.Body != nil {
    67  		defer downloadResp.Body.Close()
    68  	}
    69  
    70  	if downloadResp.StatusCode == 200 {
    71  		workspaceRelativePath := config.DownloadPath
    72  		err = os.MkdirAll(workspaceRelativePath, 0755)
    73  		if err != nil {
    74  			return errors.Wrapf(err, "Failed to create workspace directory")
    75  		}
    76  		zipFileName := filepath.Join(workspaceRelativePath, filename)
    77  		file, err := os.Create(zipFileName)
    78  		if err != nil {
    79  			return errors.Wrapf(err, "Failed to create integration flow artifact file")
    80  		}
    81  		if _, err := io.Copy(file, downloadResp.Body); err != nil {
    82  			return err
    83  		}
    84  		return nil
    85  	}
    86  	responseBody, readErr := io.ReadAll(downloadResp.Body)
    87  
    88  	if readErr != nil {
    89  		return errors.Wrapf(readErr, "HTTP response body could not be read, Response status code : %v", downloadResp.StatusCode)
    90  	}
    91  
    92  	log.Entry().Errorf("a HTTP error occurred! Response body: %v, Response status code : %v", string(responseBody), downloadResp.StatusCode)
    93  	return errors.Errorf("Integration Flow artifact download failed, Response Status code: %v", downloadResp.StatusCode)
    94  }