1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/gctsDeploy.go
Chris Bo 25decaa256
Introducing new step 'gctsRollback' (#1526)
* added new step gctsDeployCommit

* suggested PR fixes applied

* fixed test

* Remove unused imports

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* added URL encoding for 'request' parameter

* regenerate after change

* add new step gctsRollbackCommit

* fixed typo in docu

* enhanced error messages

* minor changes

* renamed step to 'gctsDeploy'

* changed name

* remove space

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* changed step name to gctsRollback

* changed function name

* fix conflict

* fixed gctsDeploy step name

* fix typo

* fixed error handling

* added Jenkins credentials for github token

* regenerated

* newly generated

* removed calling piper binary with go function call

* removed unused execRunner parameter

* cleaned up

* fixed merge conflict

* added docu page

* cleaned up

* provide Jenkins creds also in config.yaml

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2020-07-23 20:20:07 +02:00

84 lines
2.4 KiB
Go

package cmd
import (
"io/ioutil"
"net/http/cookiejar"
"net/url"
"github.com/Jeffail/gabs/v2"
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
func gctsDeploy(config gctsDeployOptions, telemetryData *telemetry.CustomData) {
// for http calls import piperhttp "github.com/SAP/jenkins-library/pkg/http"
// and use a &piperhttp.Client{} in a custom system
// Example: step checkmarxExecuteScan.go
httpClient := &piperhttp.Client{}
// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := deployCommit(&config, nil, httpClient)
if err != nil {
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func deployCommit(config *gctsDeployOptions, telemetryData *telemetry.CustomData, httpClient piperhttp.Sender) error {
cookieJar, cookieErr := cookiejar.New(nil)
if cookieErr != nil {
return errors.Wrap(cookieErr, "creating a cookie jar failed")
}
clientOptions := piperhttp.ClientOptions{
CookieJar: cookieJar,
Username: config.Username,
Password: config.Password,
}
httpClient.SetOptions(clientOptions)
requestURL := config.Host +
"/sap/bc/cts_abapvcs/repository/" + config.Repository +
"/pullByCommit?sap-client=" + config.Client
if config.Commit != "" {
log.Entry().Infof("preparing to deploy specified commit %v", config.Commit)
params := url.Values{}
params.Add("request", config.Commit)
requestURL = requestURL + "&" + params.Encode()
}
resp, httpErr := httpClient.SendRequest("GET", requestURL, nil, nil, nil)
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
if httpErr != nil {
return httpErr
} else if resp == nil {
return errors.New("did not retrieve a HTTP response")
}
bodyText, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
return errors.Wrapf(readErr, "HTTP response body could not be read")
}
response, parsingErr := gabs.ParseJSON([]byte(bodyText))
if parsingErr != nil {
return errors.Wrapf(parsingErr, "HTTP response body could not be parsed as JSON: %v", string(bodyText))
}
log.Entry().
WithField("repository", config.Repository).
Infof("successfully deployed commit %v (previous commit was %v)", response.Path("toCommit").Data().(string), response.Path("fromCommit").Data().(string))
return nil
}