github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapEnvironmentCheckoutBranch_generated.go (about)

     1  // Code generated by piper's step-generator. DO NOT EDIT.
     2  
     3  package cmd
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/config"
    11  	"github.com/SAP/jenkins-library/pkg/log"
    12  	"github.com/SAP/jenkins-library/pkg/splunk"
    13  	"github.com/SAP/jenkins-library/pkg/telemetry"
    14  	"github.com/SAP/jenkins-library/pkg/validation"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  type abapEnvironmentCheckoutBranchOptions struct {
    19  	Username          string `json:"username,omitempty"`
    20  	Password          string `json:"password,omitempty"`
    21  	RepositoryName    string `json:"repositoryName,omitempty"`
    22  	BranchName        string `json:"branchName,omitempty"`
    23  	Host              string `json:"host,omitempty"`
    24  	Repositories      string `json:"repositories,omitempty"`
    25  	CfAPIEndpoint     string `json:"cfApiEndpoint,omitempty"`
    26  	CfOrg             string `json:"cfOrg,omitempty"`
    27  	CfSpace           string `json:"cfSpace,omitempty"`
    28  	CfServiceInstance string `json:"cfServiceInstance,omitempty"`
    29  	CfServiceKeyName  string `json:"cfServiceKeyName,omitempty"`
    30  }
    31  
    32  // AbapEnvironmentCheckoutBranchCommand Switches between branches of a git repository on a SAP BTP ABAP Environment system
    33  func AbapEnvironmentCheckoutBranchCommand() *cobra.Command {
    34  	const STEP_NAME = "abapEnvironmentCheckoutBranch"
    35  
    36  	metadata := abapEnvironmentCheckoutBranchMetadata()
    37  	var stepConfig abapEnvironmentCheckoutBranchOptions
    38  	var startTime time.Time
    39  	var logCollector *log.CollectorHook
    40  	var splunkClient *splunk.Splunk
    41  	telemetryClient := &telemetry.Telemetry{}
    42  
    43  	var createAbapEnvironmentCheckoutBranchCmd = &cobra.Command{
    44  		Use:   STEP_NAME,
    45  		Short: "Switches between branches of a git repository on a SAP BTP ABAP Environment system",
    46  		Long: `This step switches between branches of a git repository (Software Component) on a SAP BTP ABAP Environment system.
    47  Please provide either of the following options:
    48  
    49  * The host and credentials the BTP ABAP Environment system itself. The credentials must be configured for the Communication Scenario [SAP_COM_0510](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/b04a9ae412894725a2fc539bfb1ca055.html).
    50  * The Cloud Foundry parameters (API endpoint, organization, space), credentials, the service instance for the ABAP service and the service key for the Communication Scenario SAP_COM_0510.
    51  * Only provide one of those options with the respective credentials. If all values are provided, the direct communication (via host) has priority.`,
    52  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    53  			startTime = time.Now()
    54  			log.SetStepName(STEP_NAME)
    55  			log.SetVerbose(GeneralConfig.Verbose)
    56  
    57  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    58  
    59  			path, _ := os.Getwd()
    60  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    61  			log.RegisterHook(fatalHook)
    62  
    63  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    64  			if err != nil {
    65  				log.SetErrorCategory(log.ErrorConfiguration)
    66  				return err
    67  			}
    68  			log.RegisterSecret(stepConfig.Username)
    69  			log.RegisterSecret(stepConfig.Password)
    70  
    71  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    72  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    73  				log.RegisterHook(&sentryHook)
    74  			}
    75  
    76  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    77  				splunkClient = &splunk.Splunk{}
    78  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    79  				log.RegisterHook(logCollector)
    80  			}
    81  
    82  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    83  			if err != nil {
    84  				return err
    85  			}
    86  			if err = validation.ValidateStruct(stepConfig); err != nil {
    87  				log.SetErrorCategory(log.ErrorConfiguration)
    88  				return err
    89  			}
    90  
    91  			return nil
    92  		},
    93  		Run: func(_ *cobra.Command, _ []string) {
    94  			stepTelemetryData := telemetry.CustomData{}
    95  			stepTelemetryData.ErrorCode = "1"
    96  			handler := func() {
    97  				config.RemoveVaultSecretFiles()
    98  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    99  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
   100  				stepTelemetryData.PiperCommitHash = GitCommit
   101  				telemetryClient.SetData(&stepTelemetryData)
   102  				telemetryClient.Send()
   103  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   104  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   105  				}
   106  			}
   107  			log.DeferExitHandler(handler)
   108  			defer handler()
   109  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   110  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   111  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   112  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   113  					GeneralConfig.HookConfig.SplunkConfig.Token,
   114  					GeneralConfig.HookConfig.SplunkConfig.Index,
   115  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   116  			}
   117  			abapEnvironmentCheckoutBranch(stepConfig, &stepTelemetryData)
   118  			stepTelemetryData.ErrorCode = "0"
   119  			log.Entry().Info("SUCCESS")
   120  		},
   121  	}
   122  
   123  	addAbapEnvironmentCheckoutBranchFlags(createAbapEnvironmentCheckoutBranchCmd, &stepConfig)
   124  	return createAbapEnvironmentCheckoutBranchCmd
   125  }
   126  
   127  func addAbapEnvironmentCheckoutBranchFlags(cmd *cobra.Command, stepConfig *abapEnvironmentCheckoutBranchOptions) {
   128  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
   129  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password for either the Cloud Foundry API or the Communication Arrangement for SAP_COM_0510")
   130  	cmd.Flags().StringVar(&stepConfig.RepositoryName, "repositoryName", os.Getenv("PIPER_repositoryName"), "Specifies a Repository (Software Component) on the SAP BTP ABAP Environment system")
   131  	cmd.Flags().StringVar(&stepConfig.BranchName, "branchName", os.Getenv("PIPER_branchName"), "Specifies a Branch of a Repository (Software Component) on the SAP BTP ABAP Environment system")
   132  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the host address of the SAP BTP ABAP Environment system")
   133  	cmd.Flags().StringVar(&stepConfig.Repositories, "repositories", os.Getenv("PIPER_repositories"), "Specifies a YAML file containing the repositories configuration")
   134  	cmd.Flags().StringVar(&stepConfig.CfAPIEndpoint, "cfApiEndpoint", os.Getenv("PIPER_cfApiEndpoint"), "Cloud Foundry API Enpoint")
   135  	cmd.Flags().StringVar(&stepConfig.CfOrg, "cfOrg", os.Getenv("PIPER_cfOrg"), "Cloud Foundry target organization")
   136  	cmd.Flags().StringVar(&stepConfig.CfSpace, "cfSpace", os.Getenv("PIPER_cfSpace"), "Cloud Foundry target space")
   137  	cmd.Flags().StringVar(&stepConfig.CfServiceInstance, "cfServiceInstance", os.Getenv("PIPER_cfServiceInstance"), "Cloud Foundry Service Instance")
   138  	cmd.Flags().StringVar(&stepConfig.CfServiceKeyName, "cfServiceKeyName", os.Getenv("PIPER_cfServiceKeyName"), "Cloud Foundry Service Key")
   139  
   140  	cmd.MarkFlagRequired("username")
   141  	cmd.MarkFlagRequired("password")
   142  }
   143  
   144  // retrieve step metadata
   145  func abapEnvironmentCheckoutBranchMetadata() config.StepData {
   146  	var theMetaData = config.StepData{
   147  		Metadata: config.StepMetadata{
   148  			Name:        "abapEnvironmentCheckoutBranch",
   149  			Aliases:     []config.Alias{},
   150  			Description: "Switches between branches of a git repository on a SAP BTP ABAP Environment system",
   151  		},
   152  		Spec: config.StepSpec{
   153  			Inputs: config.StepInputs{
   154  				Secrets: []config.StepSecrets{
   155  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing user and password to authenticate to the BTP ABAP Environment system or the Cloud Foundry API", Type: "jenkins", Aliases: []config.Alias{{Name: "cfCredentialsId", Deprecated: false}, {Name: "credentialsId", Deprecated: false}}},
   156  				},
   157  				Parameters: []config.StepParameters{
   158  					{
   159  						Name: "username",
   160  						ResourceRef: []config.ResourceReference{
   161  							{
   162  								Name:  "abapCredentialsId",
   163  								Param: "username",
   164  								Type:  "secret",
   165  							},
   166  						},
   167  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   168  						Type:      "string",
   169  						Mandatory: true,
   170  						Aliases:   []config.Alias{},
   171  						Default:   os.Getenv("PIPER_username"),
   172  					},
   173  					{
   174  						Name: "password",
   175  						ResourceRef: []config.ResourceReference{
   176  							{
   177  								Name:  "abapCredentialsId",
   178  								Param: "password",
   179  								Type:  "secret",
   180  							},
   181  						},
   182  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   183  						Type:      "string",
   184  						Mandatory: true,
   185  						Aliases:   []config.Alias{},
   186  						Default:   os.Getenv("PIPER_password"),
   187  					},
   188  					{
   189  						Name:        "repositoryName",
   190  						ResourceRef: []config.ResourceReference{},
   191  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   192  						Type:        "string",
   193  						Mandatory:   false,
   194  						Aliases:     []config.Alias{},
   195  						Default:     os.Getenv("PIPER_repositoryName"),
   196  					},
   197  					{
   198  						Name:        "branchName",
   199  						ResourceRef: []config.ResourceReference{},
   200  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   201  						Type:        "string",
   202  						Mandatory:   false,
   203  						Aliases:     []config.Alias{},
   204  						Default:     os.Getenv("PIPER_branchName"),
   205  					},
   206  					{
   207  						Name:        "host",
   208  						ResourceRef: []config.ResourceReference{},
   209  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   210  						Type:        "string",
   211  						Mandatory:   false,
   212  						Aliases:     []config.Alias{},
   213  						Default:     os.Getenv("PIPER_host"),
   214  					},
   215  					{
   216  						Name:        "repositories",
   217  						ResourceRef: []config.ResourceReference{},
   218  						Scope:       []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"},
   219  						Type:        "string",
   220  						Mandatory:   false,
   221  						Aliases:     []config.Alias{},
   222  						Default:     os.Getenv("PIPER_repositories"),
   223  					},
   224  					{
   225  						Name:        "cfApiEndpoint",
   226  						ResourceRef: []config.ResourceReference{},
   227  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   228  						Type:        "string",
   229  						Mandatory:   false,
   230  						Aliases:     []config.Alias{{Name: "cloudFoundry/apiEndpoint"}},
   231  						Default:     os.Getenv("PIPER_cfApiEndpoint"),
   232  					},
   233  					{
   234  						Name:        "cfOrg",
   235  						ResourceRef: []config.ResourceReference{},
   236  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   237  						Type:        "string",
   238  						Mandatory:   false,
   239  						Aliases:     []config.Alias{{Name: "cloudFoundry/org"}},
   240  						Default:     os.Getenv("PIPER_cfOrg"),
   241  					},
   242  					{
   243  						Name:        "cfSpace",
   244  						ResourceRef: []config.ResourceReference{},
   245  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   246  						Type:        "string",
   247  						Mandatory:   false,
   248  						Aliases:     []config.Alias{{Name: "cloudFoundry/space"}},
   249  						Default:     os.Getenv("PIPER_cfSpace"),
   250  					},
   251  					{
   252  						Name:        "cfServiceInstance",
   253  						ResourceRef: []config.ResourceReference{},
   254  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   255  						Type:        "string",
   256  						Mandatory:   false,
   257  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceInstance"}},
   258  						Default:     os.Getenv("PIPER_cfServiceInstance"),
   259  					},
   260  					{
   261  						Name:        "cfServiceKeyName",
   262  						ResourceRef: []config.ResourceReference{},
   263  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"},
   264  						Type:        "string",
   265  						Mandatory:   false,
   266  						Aliases:     []config.Alias{{Name: "cloudFoundry/serviceKey"}, {Name: "cloudFoundry/serviceKeyName"}, {Name: "cfServiceKeyName"}},
   267  						Default:     os.Getenv("PIPER_cfServiceKeyName"),
   268  					},
   269  				},
   270  			},
   271  			Containers: []config.Container{
   272  				{Name: "cf", Image: "ppiper/cf-cli:7"},
   273  			},
   274  		},
   275  	}
   276  	return theMetaData
   277  }