github.com/xgoffin/jenkins-library@v1.154.0/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  }
    29  
    30  // GctsCreateRepositoryCommand Creates a Git repository on an ABAP system
    31  func GctsCreateRepositoryCommand() *cobra.Command {
    32  	const STEP_NAME = "gctsCreateRepository"
    33  
    34  	metadata := gctsCreateRepositoryMetadata()
    35  	var stepConfig gctsCreateRepositoryOptions
    36  	var startTime time.Time
    37  	var logCollector *log.CollectorHook
    38  	var splunkClient *splunk.Splunk
    39  	telemetryClient := &telemetry.Telemetry{}
    40  
    41  	var createGctsCreateRepositoryCmd = &cobra.Command{
    42  		Use:   STEP_NAME,
    43  		Short: "Creates a Git repository on an ABAP system",
    44  		Long:  `Creates a local Git repository on an ABAP system if it does not already exist.`,
    45  		PreRunE: func(cmd *cobra.Command, _ []string) error {
    46  			startTime = time.Now()
    47  			log.SetStepName(STEP_NAME)
    48  			log.SetVerbose(GeneralConfig.Verbose)
    49  
    50  			GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
    51  
    52  			path, _ := os.Getwd()
    53  			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
    54  			log.RegisterHook(fatalHook)
    55  
    56  			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
    57  			if err != nil {
    58  				log.SetErrorCategory(log.ErrorConfiguration)
    59  				return err
    60  			}
    61  			log.RegisterSecret(stepConfig.Username)
    62  			log.RegisterSecret(stepConfig.Password)
    63  
    64  			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
    65  				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
    66  				log.RegisterHook(&sentryHook)
    67  			}
    68  
    69  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    70  				splunkClient = &splunk.Splunk{}
    71  				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
    72  				log.RegisterHook(logCollector)
    73  			}
    74  
    75  			validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
    76  			if err != nil {
    77  				return err
    78  			}
    79  			if err = validation.ValidateStruct(stepConfig); err != nil {
    80  				log.SetErrorCategory(log.ErrorConfiguration)
    81  				return err
    82  			}
    83  
    84  			return nil
    85  		},
    86  		Run: func(_ *cobra.Command, _ []string) {
    87  			stepTelemetryData := telemetry.CustomData{}
    88  			stepTelemetryData.ErrorCode = "1"
    89  			handler := func() {
    90  				config.RemoveVaultSecretFiles()
    91  				stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
    92  				stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
    93  				stepTelemetryData.PiperCommitHash = GitCommit
    94  				telemetryClient.SetData(&stepTelemetryData)
    95  				telemetryClient.Send()
    96  				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
    97  					splunkClient.Send(telemetryClient.GetData(), logCollector)
    98  				}
    99  			}
   100  			log.DeferExitHandler(handler)
   101  			defer handler()
   102  			telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
   103  			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
   104  				splunkClient.Initialize(GeneralConfig.CorrelationID,
   105  					GeneralConfig.HookConfig.SplunkConfig.Dsn,
   106  					GeneralConfig.HookConfig.SplunkConfig.Token,
   107  					GeneralConfig.HookConfig.SplunkConfig.Index,
   108  					GeneralConfig.HookConfig.SplunkConfig.SendLogs)
   109  			}
   110  			gctsCreateRepository(stepConfig, &stepTelemetryData)
   111  			stepTelemetryData.ErrorCode = "0"
   112  			log.Entry().Info("SUCCESS")
   113  		},
   114  	}
   115  
   116  	addGctsCreateRepositoryFlags(createGctsCreateRepositoryCmd, &stepConfig)
   117  	return createGctsCreateRepositoryCmd
   118  }
   119  
   120  func addGctsCreateRepositoryFlags(cmd *cobra.Command, stepConfig *gctsCreateRepositoryOptions) {
   121  	cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "Username to authenticate to the ABAP system")
   122  	cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password to authenticate to the ABAP system")
   123  	cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Specifies the name (ID) of the local repository on the ABAP system")
   124  	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`.")
   125  	cmd.Flags().StringVar(&stepConfig.Client, "client", os.Getenv("PIPER_client"), "Specifies the client of the ABAP system to be addressed")
   126  	cmd.Flags().StringVar(&stepConfig.RemoteRepositoryURL, "remoteRepositoryURL", os.Getenv("PIPER_remoteRepositoryURL"), "URL of the corresponding remote repository")
   127  	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")
   128  	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")
   129  	cmd.Flags().StringVar(&stepConfig.Type, "type", `GIT`, "Type of the used source code management tool")
   130  
   131  	cmd.MarkFlagRequired("username")
   132  	cmd.MarkFlagRequired("password")
   133  	cmd.MarkFlagRequired("repository")
   134  	cmd.MarkFlagRequired("host")
   135  	cmd.MarkFlagRequired("client")
   136  }
   137  
   138  // retrieve step metadata
   139  func gctsCreateRepositoryMetadata() config.StepData {
   140  	var theMetaData = config.StepData{
   141  		Metadata: config.StepMetadata{
   142  			Name:        "gctsCreateRepository",
   143  			Aliases:     []config.Alias{},
   144  			Description: "Creates a Git repository on an ABAP system",
   145  		},
   146  		Spec: config.StepSpec{
   147  			Inputs: config.StepInputs{
   148  				Secrets: []config.StepSecrets{
   149  					{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"},
   150  				},
   151  				Parameters: []config.StepParameters{
   152  					{
   153  						Name: "username",
   154  						ResourceRef: []config.ResourceReference{
   155  							{
   156  								Name:  "abapCredentialsId",
   157  								Param: "username",
   158  								Type:  "secret",
   159  							},
   160  						},
   161  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   162  						Type:      "string",
   163  						Mandatory: true,
   164  						Aliases:   []config.Alias{},
   165  						Default:   os.Getenv("PIPER_username"),
   166  					},
   167  					{
   168  						Name: "password",
   169  						ResourceRef: []config.ResourceReference{
   170  							{
   171  								Name:  "abapCredentialsId",
   172  								Param: "password",
   173  								Type:  "secret",
   174  							},
   175  						},
   176  						Scope:     []string{"PARAMETERS", "STAGES", "STEPS"},
   177  						Type:      "string",
   178  						Mandatory: true,
   179  						Aliases:   []config.Alias{},
   180  						Default:   os.Getenv("PIPER_password"),
   181  					},
   182  					{
   183  						Name:        "repository",
   184  						ResourceRef: []config.ResourceReference{},
   185  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   186  						Type:        "string",
   187  						Mandatory:   true,
   188  						Aliases:     []config.Alias{},
   189  						Default:     os.Getenv("PIPER_repository"),
   190  					},
   191  					{
   192  						Name:        "host",
   193  						ResourceRef: []config.ResourceReference{},
   194  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   195  						Type:        "string",
   196  						Mandatory:   true,
   197  						Aliases:     []config.Alias{},
   198  						Default:     os.Getenv("PIPER_host"),
   199  					},
   200  					{
   201  						Name:        "client",
   202  						ResourceRef: []config.ResourceReference{},
   203  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   204  						Type:        "string",
   205  						Mandatory:   true,
   206  						Aliases:     []config.Alias{},
   207  						Default:     os.Getenv("PIPER_client"),
   208  					},
   209  					{
   210  						Name:        "remoteRepositoryURL",
   211  						ResourceRef: []config.ResourceReference{},
   212  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   213  						Type:        "string",
   214  						Mandatory:   false,
   215  						Aliases:     []config.Alias{},
   216  						Default:     os.Getenv("PIPER_remoteRepositoryURL"),
   217  					},
   218  					{
   219  						Name:        "role",
   220  						ResourceRef: []config.ResourceReference{},
   221  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   222  						Type:        "string",
   223  						Mandatory:   false,
   224  						Aliases:     []config.Alias{},
   225  						Default:     `SOURCE`,
   226  					},
   227  					{
   228  						Name:        "vSID",
   229  						ResourceRef: []config.ResourceReference{},
   230  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   231  						Type:        "string",
   232  						Mandatory:   false,
   233  						Aliases:     []config.Alias{},
   234  						Default:     os.Getenv("PIPER_vSID"),
   235  					},
   236  					{
   237  						Name:        "type",
   238  						ResourceRef: []config.ResourceReference{},
   239  						Scope:       []string{"PARAMETERS", "STAGES", "STEPS"},
   240  						Type:        "string",
   241  						Mandatory:   false,
   242  						Aliases:     []config.Alias{},
   243  						Default:     `GIT`,
   244  					},
   245  				},
   246  			},
   247  		},
   248  	}
   249  	return theMetaData
   250  }