github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/gctsCreateRepository_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 gctsCreateRepositoryOptions struct {
    19  	Username            string                 `json:"username,omitempty"`
    20  	Password            string                 `json:"password,omitempty"`
    21  	Repository          string                 `json:"repository,omitempty"`
    22  	Host                string                 `json:"host,omitempty"`
    23  	Client              string                 `json:"client,omitempty"`
    24  	RemoteRepositoryURL string                 `json:"remoteRepositoryURL,omitempty"`
    25  	Role                string                 `json:"role,omitempty" validate:"possible-values=SOURCE TARGET"`
    26  	VSID                string                 `json:"vSID,omitempty"`
    27  	Type                string                 `json:"type,omitempty" validate:"possible-values=GIT"`
    28  	QueryParameters     map[string]interface{} `json:"queryParameters,omitempty"`
    29  	SkipSSLVerification bool                   `json:"skipSSLVerification,omitempty"`
    30  }
    31  
    32  // GctsCreateRepositoryCommand Creates a Git repository on an ABAP system
    33  func GctsCreateRepositoryCommand() *cobra.Command {
    34  	const STEP_NAME = "gctsCreateRepository"
    35  
    36  	metadata := gctsCreateRepositoryMetadata()
    37  	var stepConfig gctsCreateRepositoryOptions
    38  	var startTime time.Time
    39  	var logCollector *log.CollectorHook
    40  	var splunkClient *splunk.Splunk
    41  	telemetryClient := &telemetry.Telemetry{}
    42  
    43  	var createGctsCreateRepositoryCmd = &cobra.Command{
    44  		Use:   STEP_NAME,
    45  		Short: "Creates a Git repository on an ABAP system",
    46  		Long:  `Creates a local Git repository on an ABAP system if it does not already exist.`,
    47  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    48  			startTime = time.Now()
    49  			log.SetStepName(STEP_NAME)
    50  			log.SetVerbose(GeneralConfig.Verbose)
    51  
    52  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    53  
    54  			path, _ := os.Getwd()
    55  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    56  			log.RegisterHook(fatalHook)
    57  
    58  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    59  			if err != nil {
    60  				log.SetErrorCategory(log.ErrorConfiguration)
    61  				return err
    62  			}
    63  			log.RegisterSecret(stepConfig.Username)
    64  			log.RegisterSecret(stepConfig.Password)
    65  
    66  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    67  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    68  				log.RegisterHook(&sentryHook)
    69  			}
    70  
    71  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    72  				splunkClient = &splunk.Splunk{}
    73  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    74  				log.RegisterHook(logCollector)
    75  			}
    76  
    77  			if err = log.RegisterANSHookIfConfigured(GeneralConfig.CorrelationID); err != nil {
    78  				log.Entry().WithError(err).Warn("failed to set up SAP Alert Notification Service log hook")
    79  			}
    80  
    81  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    82  			if err != nil {
    83  				return err
    84  			}
    85  			if err = validation.ValidateStruct(stepConfig); err != nil {
    86  				log.SetErrorCategory(log.ErrorConfiguration)
    87  				return err
    88  			}
    89  
    90  			return nil
    91  		},
    92  		Run: func(_ *cobra.Command, _ []string) {
    93  			stepTelemetryData := telemetry.CustomData{}
    94  			stepTelemetryData.ErrorCode = "1"
    95  			handler := func() {
    96  				config.RemoveVaultSecretFiles()
    97  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    98  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    99  				stepTelemetryData.PiperCommitHash = GitCommit
   100  				telemetryClient.SetData(&stepTelemetryData)
   101  				telemetryClient.Send()
   102  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   103  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   104  						GeneralConfig.HookConfig.SplunkConfig.Dsn,
   105  						GeneralConfig.HookConfig.SplunkConfig.Token,
   106  						GeneralConfig.HookConfig.SplunkConfig.Index,
   107  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   108  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   109  				}
   110  				if len(GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint) > 0 {
   111  					splunkClient.Initialize(GeneralConfig.CorrelationID,
   112  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblEndpoint,
   113  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblToken,
   114  						GeneralConfig.HookConfig.SplunkConfig.ProdCriblIndex,
   115  						GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   116  					splunkClient.Send(telemetryClient.GetData(), logCollector)
   117  				}
   118  			}
   119  			log.DeferExitHandler(handler)
   120  			defer handler()
   121  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   122  			gctsCreateRepository(stepConfig, &stepTelemetryData)
   123  			stepTelemetryData.ErrorCode = "0"
   124  			log.Entry().Info("SUCCESS")
   125  		},
   126  	}
   127  
   128  	addGctsCreateRepositoryFlags(createGctsCreateRepositoryCmd, &stepConfig)
   129  	return createGctsCreateRepositoryCmd
   130  }
   131  
   132  func addGctsCreateRepositoryFlags(cmd *cobra.Command, stepConfig *gctsCreateRepositoryOptions) {
   133  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "Username to authenticate to the ABAP system")
   134  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password to authenticate to the ABAP system")
   135  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repository on the ABAP system")
   136  	cmd.Flags().StringVar(&stepConfig.Host, "host", os.Getenv("PIPER_host"), "Specifies the protocol and host address, including the port. Please provide in the format `<protocol>://<host>:<port>`. Supported protocols are `http` and `https`.")
   137  	cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Specifies the client of the ABAP system to be addressed")
   138  	cmd.Flags().StringVar(&stepConfig.RemoteRepositoryURL, "remoteRepositoryURL", os.Getenv("PIPER_remoteRepositoryURL"), "URL of the corresponding remote repository")
   139  	cmd.Flags().StringVar(&stepConfig.Role, "role", `SOURCE`, "Role of the local repository. Choose between 'TARGET' and 'SOURCE'. Local repositories with a TARGET role will NOT be able to be the source of code changes")
   140  	cmd.Flags().StringVar(&stepConfig.VSID, "vSID", os.Getenv("PIPER_vSID"), "Virtual SID of the local repository. The vSID corresponds to the transport route that delivers content to the remote Git repository")
   141  	cmd.Flags().StringVar(&stepConfig.Type, "type", `GIT`, "Type of the used source code management tool")
   142  
   143  	cmd.Flags().BoolVar(&stepConfig.SkipSSLVerification, "skipSSLVerification", false, "Skip the verification of SSL (Secure Socket Layer) certificates when using HTTPS. This parameter is **not recommended** for productive environments.")
   144  
   145  	cmd.MarkFlagRequired("username")
   146  	cmd.MarkFlagRequired("password")
   147  	cmd.MarkFlagRequired("repository")
   148  	cmd.MarkFlagRequired("host")
   149  	cmd.MarkFlagRequired("client")
   150  }
   151  
   152  // retrieve step metadata
   153  func gctsCreateRepositoryMetadata() config.StepData {
   154  	var theMetaData = config.StepData{
   155  		Metadata: config.StepMetadata{
   156  			Name:        "gctsCreateRepository",
   157  			Aliases:     []config.Alias{},
   158  			Description: "Creates a Git repository on an ABAP system",
   159  		},
   160  		Spec: config.StepSpec{
   161  			Inputs: config.StepInputs{
   162  				Secrets: []config.StepSecrets{
   163  					{Name: "abapCredentialsId", Description: "Jenkins credentials ID containing username and password for authentication to the ABAP system on which you want to create the repository", Type: "jenkins"},
   164  				},
   165  				Parameters: []config.StepParameters{
   166  					{
   167  						Name: "username",
   168  						ResourceRef: []config.ResourceReference{
   169  							{
   170  								Name:  "abapCredentialsId",
   171  								Param: "username",
   172  								Type:  "secret",
   173  							},
   174  						},
   175  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   176  						Type:      "string",
   177  						Mandatory: true,
   178  						Aliases:   []config.Alias{},
   179  						Default:   os.Getenv("PIPER_username"),
   180  					},
   181  					{
   182  						Name: "password",
   183  						ResourceRef: []config.ResourceReference{
   184  							{
   185  								Name:  "abapCredentialsId",
   186  								Param: "password",
   187  								Type:  "secret",
   188  							},
   189  						},
   190  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   191  						Type:      "string",
   192  						Mandatory: true,
   193  						Aliases:   []config.Alias{},
   194  						Default:   os.Getenv("PIPER_password"),
   195  					},
   196  					{
   197  						Name:        "repository",
   198  						ResourceRef: []config.ResourceReference{},
   199  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   200  						Type:        "string",
   201  						Mandatory:   true,
   202  						Aliases:     []config.Alias{},
   203  						Default:     os.Getenv("PIPER_repository"),
   204  					},
   205  					{
   206  						Name:        "host",
   207  						ResourceRef: []config.ResourceReference{},
   208  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   209  						Type:        "string",
   210  						Mandatory:   true,
   211  						Aliases:     []config.Alias{},
   212  						Default:     os.Getenv("PIPER_host"),
   213  					},
   214  					{
   215  						Name:        "client",
   216  						ResourceRef: []config.ResourceReference{},
   217  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   218  						Type:        "string",
   219  						Mandatory:   true,
   220  						Aliases:     []config.Alias{},
   221  						Default:     os.Getenv("PIPER_client"),
   222  					},
   223  					{
   224  						Name:        "remoteRepositoryURL",
   225  						ResourceRef: []config.ResourceReference{},
   226  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   227  						Type:        "string",
   228  						Mandatory:   false,
   229  						Aliases:     []config.Alias{},
   230  						Default:     os.Getenv("PIPER_remoteRepositoryURL"),
   231  					},
   232  					{
   233  						Name:        "role",
   234  						ResourceRef: []config.ResourceReference{},
   235  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   236  						Type:        "string",
   237  						Mandatory:   false,
   238  						Aliases:     []config.Alias{},
   239  						Default:     `SOURCE`,
   240  					},
   241  					{
   242  						Name:        "vSID",
   243  						ResourceRef: []config.ResourceReference{},
   244  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   245  						Type:        "string",
   246  						Mandatory:   false,
   247  						Aliases:     []config.Alias{},
   248  						Default:     os.Getenv("PIPER_vSID"),
   249  					},
   250  					{
   251  						Name:        "type",
   252  						ResourceRef: []config.ResourceReference{},
   253  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   254  						Type:        "string",
   255  						Mandatory:   false,
   256  						Aliases:     []config.Alias{},
   257  						Default:     `GIT`,
   258  					},
   259  					{
   260  						Name:        "queryParameters",
   261  						ResourceRef: []config.ResourceReference{},
   262  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   263  						Type:        "map[string]interface{}",
   264  						Mandatory:   false,
   265  						Aliases:     []config.Alias{},
   266  					},
   267  					{
   268  						Name:        "skipSSLVerification",
   269  						ResourceRef: []config.ResourceReference{},
   270  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   271  						Type:        "bool",
   272  						Mandatory:   false,
   273  						Aliases:     []config.Alias{},
   274  						Default:     false,
   275  					},
   276  				},
   277  			},
   278  		},
   279  	}
   280  	return theMetaData
   281  }