You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Split git commit message scan from solman upload (#2773)
* Split git commit message scan from solman upload
This commit is contained in:
		| @@ -71,6 +71,8 @@ func GetAllStepMetadata() map[string]config.StepData { | ||||
| 		"containerSaveImage":                      containerSaveImageMetadata(), | ||||
| 		"sonarExecuteScan":                        sonarExecuteScanMetadata(), | ||||
| 		"terraformExecute":                        terraformExecuteMetadata(), | ||||
| 		"transportRequestDocIDFromGit":            transportRequestDocIDFromGitMetadata(), | ||||
| 		"transportRequestReqIDFromGit":            transportRequestReqIDFromGitMetadata(), | ||||
| 		"transportRequestUploadCTS":               transportRequestUploadCTSMetadata(), | ||||
| 		"transportRequestUploadSOLMAN":            transportRequestUploadSOLMANMetadata(), | ||||
| 		"uiVeri5ExecuteTests":                     uiVeri5ExecuteTestsMetadata(), | ||||
|   | ||||
| @@ -149,6 +149,8 @@ func Execute() { | ||||
| 	rootCmd.AddCommand(ContainerExecuteStructureTestsCommand()) | ||||
| 	rootCmd.AddCommand(BatsExecuteTestsCommand()) | ||||
| 	rootCmd.AddCommand(PipelineCreateScanSummaryCommand()) | ||||
| 	rootCmd.AddCommand(TransportRequestDocIDFromGitCommand()) | ||||
| 	rootCmd.AddCommand(TransportRequestReqIDFromGitCommand()) | ||||
|  | ||||
| 	addRootFlags(rootCmd) | ||||
| 	if err := rootCmd.Execute(); err != nil { | ||||
|   | ||||
							
								
								
									
										39
									
								
								cmd/transportRequestDocIDFromGit.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								cmd/transportRequestDocIDFromGit.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| ) | ||||
|  | ||||
| func transportRequestDocIDFromGit(config transportRequestDocIDFromGitOptions, | ||||
| 	telemetryData *telemetry.CustomData, | ||||
| 	commonPipelineEnvironment *transportRequestDocIDFromGitCommonPipelineEnvironment) { | ||||
|  | ||||
| 	err := runTransportRequestDocIDFromGit(&config, telemetryData, &gitIDInRange{}, commonPipelineEnvironment) | ||||
| 	if err != nil { | ||||
| 		log.Entry().WithError(err).Fatal("step execution failed") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func runTransportRequestDocIDFromGit(config *transportRequestDocIDFromGitOptions, | ||||
| 	telemetryData *telemetry.CustomData, | ||||
| 	trUtils gitIDInRangeFinder, | ||||
| 	commonPipelineEnvironment *transportRequestDocIDFromGitCommonPipelineEnvironment) error { | ||||
|  | ||||
| 	cdID, err := getChangeDocumentID(config, trUtils) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	commonPipelineEnvironment.custom.changeDocumentID = cdID | ||||
|  | ||||
| 	log.Entry().Infof("Retrieved change document ID '%s' from Git.", cdID) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func getChangeDocumentID(config *transportRequestDocIDFromGitOptions, | ||||
| 	trUtils gitIDInRangeFinder) (string, error) { | ||||
|  | ||||
| 	return trUtils.FindIDInRange(config.ChangeDocumentLabel, config.GitFrom, config.GitTo) | ||||
| } | ||||
							
								
								
									
										185
									
								
								cmd/transportRequestDocIDFromGit_generated.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								cmd/transportRequestDocIDFromGit_generated.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,185 @@ | ||||
| // Code generated by piper's step-generator. DO NOT EDIT. | ||||
|  | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/config" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperenv" | ||||
| 	"github.com/SAP/jenkins-library/pkg/splunk" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type transportRequestDocIDFromGitOptions struct { | ||||
| 	GitFrom             string `json:"gitFrom,omitempty"` | ||||
| 	GitTo               string `json:"gitTo,omitempty"` | ||||
| 	ChangeDocumentLabel string `json:"changeDocumentLabel,omitempty"` | ||||
| } | ||||
|  | ||||
| type transportRequestDocIDFromGitCommonPipelineEnvironment struct { | ||||
| 	custom struct { | ||||
| 		changeDocumentID string | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *transportRequestDocIDFromGitCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| 	content := []struct { | ||||
| 		category string | ||||
| 		name     string | ||||
| 		value    interface{} | ||||
| 	}{ | ||||
| 		{category: "custom", name: "changeDocumentId", value: p.custom.changeDocumentID}, | ||||
| 	} | ||||
|  | ||||
| 	errCount := 0 | ||||
| 	for _, param := range content { | ||||
| 		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) | ||||
| 		if err != nil { | ||||
| 			log.Entry().WithError(err).Error("Error persisting piper environment.") | ||||
| 			errCount++ | ||||
| 		} | ||||
| 	} | ||||
| 	if errCount > 0 { | ||||
| 		log.Entry().Fatal("failed to persist Piper environment") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TransportRequestDocIDFromGitCommand Retrieves change document ID from Git repository | ||||
| func TransportRequestDocIDFromGitCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "transportRequestDocIDFromGit" | ||||
|  | ||||
| 	metadata := transportRequestDocIDFromGitMetadata() | ||||
| 	var stepConfig transportRequestDocIDFromGitOptions | ||||
| 	var startTime time.Time | ||||
| 	var commonPipelineEnvironment transportRequestDocIDFromGitCommonPipelineEnvironment | ||||
| 	var logCollector *log.CollectorHook | ||||
|  | ||||
| 	var createTransportRequestDocIDFromGitCmd = &cobra.Command{ | ||||
| 		Use:   STEP_NAME, | ||||
| 		Short: "Retrieves change document ID from Git repository", | ||||
| 		Long:  `Scans the commit messages of the Git repository for a pattern to retrieve the change document ID.`, | ||||
| 		PreRunE: func(cmd *cobra.Command, _ []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			path, _ := os.Getwd() | ||||
| 			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} | ||||
| 			log.RegisterHook(fatalHook) | ||||
|  | ||||
| 			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) | ||||
| 			if err != nil { | ||||
| 				log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} | ||||
| 				log.RegisterHook(logCollector) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Run: func(_ *cobra.Command, _ []string) { | ||||
| 			telemetryData := telemetry.CustomData{} | ||||
| 			telemetryData.ErrorCode = "1" | ||||
| 			handler := func() { | ||||
| 				config.RemoveVaultSecretFiles() | ||||
| 				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") | ||||
| 				telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) | ||||
| 				telemetryData.ErrorCategory = log.GetErrorCategory().String() | ||||
| 				telemetry.Send(&telemetryData) | ||||
| 				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 					splunk.Send(&telemetryData, logCollector) | ||||
| 				} | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				splunk.Initialize(GeneralConfig.CorrelationID, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Dsn, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Token, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Index, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.SendLogs) | ||||
| 			} | ||||
| 			transportRequestDocIDFromGit(stepConfig, &telemetryData, &commonPipelineEnvironment) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addTransportRequestDocIDFromGitFlags(createTransportRequestDocIDFromGitCmd, &stepConfig) | ||||
| 	return createTransportRequestDocIDFromGitCmd | ||||
| } | ||||
|  | ||||
| func addTransportRequestDocIDFromGitFlags(cmd *cobra.Command, stepConfig *transportRequestDocIDFromGitOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitFrom, "gitFrom", `origin/master`, "GIT starting point for retrieving the change document and transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitTo, "gitTo", `HEAD`, "GIT ending point for retrieving the change document and transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.ChangeDocumentLabel, "changeDocumentLabel", `ChangeDocument`, "Pattern used for identifying lines holding the change document ID. The GIT commit log messages are scanned for this label") | ||||
|  | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func transportRequestDocIDFromGitMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:        "transportRequestDocIDFromGit", | ||||
| 			Aliases:     []config.Alias{}, | ||||
| 			Description: "Retrieves change document ID from Git repository", | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Parameters: []config.StepParameters{ | ||||
| 					{ | ||||
| 						Name:        "gitFrom", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/from"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "gitTo", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/to"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "changeDocumentLabel", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/changeDocumentLabel"}}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Outputs: config.StepOutputs{ | ||||
| 				Resources: []config.StepResources{ | ||||
| 					{ | ||||
| 						Name: "commonPipelineEnvironment", | ||||
| 						Type: "piperEnvironment", | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "custom/changeDocumentId"}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
							
								
								
									
										17
									
								
								cmd/transportRequestDocIDFromGit_generated_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								cmd/transportRequestDocIDFromGit_generated_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestTransportRequestDocIDFromGitCommand(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	testCmd := TransportRequestDocIDFromGitCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procedure | ||||
| 	assert.Equal(t, "transportRequestDocIDFromGit", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
							
								
								
									
										63
									
								
								cmd/transportRequestDocIDFromGit_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								cmd/transportRequestDocIDFromGit_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestTrGitGetChangeDocumentID(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("good", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("getChangeDocumentID", func(t *testing.T) { | ||||
| 			configMock := newCdIDConfigMock() | ||||
|  | ||||
| 			id, err := getChangeDocumentID(configMock.config, &transportRequestUtilsMock{cdID: "56781234"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "56781234") | ||||
| 			} | ||||
| 		}) | ||||
| 		t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { | ||||
| 			configMock := newCdIDConfigMock() | ||||
| 			cpe := &transportRequestDocIDFromGitCommonPipelineEnvironment{} | ||||
|  | ||||
| 			err := runTransportRequestDocIDFromGit(configMock.config, nil, &transportRequestUtilsMock{cdID: "56781234"}, cpe) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, cpe.custom.changeDocumentID, "56781234") | ||||
| 			} | ||||
| 		}) | ||||
|  | ||||
| 	}) | ||||
| 	t.Run("bad", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { | ||||
| 			configMock := newCdIDConfigMock() | ||||
| 			cpe := &transportRequestDocIDFromGitCommonPipelineEnvironment{} | ||||
|  | ||||
| 			err := runTransportRequestDocIDFromGit(configMock.config, nil, &transportRequestUtilsMock{err: errors.New("fail")}, cpe) | ||||
|  | ||||
| 			assert.EqualError(t, err, "fail") | ||||
| 		}) | ||||
|  | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type cdIDConfigMock struct { | ||||
| 	config *transportRequestDocIDFromGitOptions | ||||
| } | ||||
|  | ||||
| func newCdIDConfigMock() *cdIDConfigMock { | ||||
| 	return &cdIDConfigMock{ | ||||
| 		config: &transportRequestDocIDFromGitOptions{ | ||||
| 			GitFrom:             "origin/master", | ||||
| 			GitTo:               "HEAD", | ||||
| 			ChangeDocumentLabel: "ChangeDocument", | ||||
| 		}, | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										52
									
								
								cmd/transportRequestReqIDFromGit.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								cmd/transportRequestReqIDFromGit.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/transportrequest" | ||||
| ) | ||||
|  | ||||
| // mocking framework. Allows to redirect the containing methods | ||||
| type gitIDInRangeFinder interface { | ||||
| 	FindIDInRange(label, from, to string) (string, error) | ||||
| } | ||||
|  | ||||
| type gitIDInRange struct { | ||||
| } | ||||
|  | ||||
| func (*gitIDInRange) FindIDInRange(label, from, to string) (string, error) { | ||||
| 	return transportrequest.FindIDInRange(label, from, to) | ||||
| } | ||||
|  | ||||
| func transportRequestReqIDFromGit(config transportRequestReqIDFromGitOptions, | ||||
| 	telemetryData *telemetry.CustomData, | ||||
| 	commonPipelineEnvironment *transportRequestReqIDFromGitCommonPipelineEnvironment) { | ||||
|  | ||||
| 	err := runTransportRequestReqIDFromGit(&config, telemetryData, &gitIDInRange{}, commonPipelineEnvironment) | ||||
| 	if err != nil { | ||||
| 		log.Entry().WithError(err).Fatal("step execution failed") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func runTransportRequestReqIDFromGit(config *transportRequestReqIDFromGitOptions, | ||||
| 	telemetryData *telemetry.CustomData, | ||||
| 	trUtils gitIDInRangeFinder, | ||||
| 	commonPipelineEnvironment *transportRequestReqIDFromGitCommonPipelineEnvironment) error { | ||||
|  | ||||
| 	trID, err := getTransportRequestID(config, trUtils) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	commonPipelineEnvironment.custom.transportRequestID = trID | ||||
|  | ||||
| 	log.Entry().Infof("Retrieved transport request ID '%s' from Git.", trID) | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func getTransportRequestID(config *transportRequestReqIDFromGitOptions, | ||||
| 	trUtils gitIDInRangeFinder) (string, error) { | ||||
|  | ||||
| 	return trUtils.FindIDInRange(config.TransportRequestLabel, config.GitFrom, config.GitTo) | ||||
| } | ||||
							
								
								
									
										185
									
								
								cmd/transportRequestReqIDFromGit_generated.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										185
									
								
								cmd/transportRequestReqIDFromGit_generated.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,185 @@ | ||||
| // Code generated by piper's step-generator. DO NOT EDIT. | ||||
|  | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/config" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperenv" | ||||
| 	"github.com/SAP/jenkins-library/pkg/splunk" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| type transportRequestReqIDFromGitOptions struct { | ||||
| 	GitFrom               string `json:"gitFrom,omitempty"` | ||||
| 	GitTo                 string `json:"gitTo,omitempty"` | ||||
| 	TransportRequestLabel string `json:"transportRequestLabel,omitempty"` | ||||
| } | ||||
|  | ||||
| type transportRequestReqIDFromGitCommonPipelineEnvironment struct { | ||||
| 	custom struct { | ||||
| 		transportRequestID string | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (p *transportRequestReqIDFromGitCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| 	content := []struct { | ||||
| 		category string | ||||
| 		name     string | ||||
| 		value    interface{} | ||||
| 	}{ | ||||
| 		{category: "custom", name: "transportRequestId", value: p.custom.transportRequestID}, | ||||
| 	} | ||||
|  | ||||
| 	errCount := 0 | ||||
| 	for _, param := range content { | ||||
| 		err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(param.category, param.name), param.value) | ||||
| 		if err != nil { | ||||
| 			log.Entry().WithError(err).Error("Error persisting piper environment.") | ||||
| 			errCount++ | ||||
| 		} | ||||
| 	} | ||||
| 	if errCount > 0 { | ||||
| 		log.Entry().Fatal("failed to persist Piper environment") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // TransportRequestReqIDFromGitCommand Retrieves the transport request ID from Git repository | ||||
| func TransportRequestReqIDFromGitCommand() *cobra.Command { | ||||
| 	const STEP_NAME = "transportRequestReqIDFromGit" | ||||
|  | ||||
| 	metadata := transportRequestReqIDFromGitMetadata() | ||||
| 	var stepConfig transportRequestReqIDFromGitOptions | ||||
| 	var startTime time.Time | ||||
| 	var commonPipelineEnvironment transportRequestReqIDFromGitCommonPipelineEnvironment | ||||
| 	var logCollector *log.CollectorHook | ||||
|  | ||||
| 	var createTransportRequestReqIDFromGitCmd = &cobra.Command{ | ||||
| 		Use:   STEP_NAME, | ||||
| 		Short: "Retrieves the transport request ID from Git repository", | ||||
| 		Long:  `Scans the commit messages of the Git repository for a pattern to retrieve the transport request ID.`, | ||||
| 		PreRunE: func(cmd *cobra.Command, _ []string) error { | ||||
| 			startTime = time.Now() | ||||
| 			log.SetStepName(STEP_NAME) | ||||
| 			log.SetVerbose(GeneralConfig.Verbose) | ||||
|  | ||||
| 			path, _ := os.Getwd() | ||||
| 			fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} | ||||
| 			log.RegisterHook(fatalHook) | ||||
|  | ||||
| 			err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) | ||||
| 			if err != nil { | ||||
| 				log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 				return err | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { | ||||
| 				sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) | ||||
| 				log.RegisterHook(&sentryHook) | ||||
| 			} | ||||
|  | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} | ||||
| 				log.RegisterHook(logCollector) | ||||
| 			} | ||||
|  | ||||
| 			return nil | ||||
| 		}, | ||||
| 		Run: func(_ *cobra.Command, _ []string) { | ||||
| 			telemetryData := telemetry.CustomData{} | ||||
| 			telemetryData.ErrorCode = "1" | ||||
| 			handler := func() { | ||||
| 				config.RemoveVaultSecretFiles() | ||||
| 				commonPipelineEnvironment.persist(GeneralConfig.EnvRootPath, "commonPipelineEnvironment") | ||||
| 				telemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) | ||||
| 				telemetryData.ErrorCategory = log.GetErrorCategory().String() | ||||
| 				telemetry.Send(&telemetryData) | ||||
| 				if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 					splunk.Send(&telemetryData, logCollector) | ||||
| 				} | ||||
| 			} | ||||
| 			log.DeferExitHandler(handler) | ||||
| 			defer handler() | ||||
| 			telemetry.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) | ||||
| 			if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { | ||||
| 				splunk.Initialize(GeneralConfig.CorrelationID, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Dsn, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Token, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.Index, | ||||
| 					GeneralConfig.HookConfig.SplunkConfig.SendLogs) | ||||
| 			} | ||||
| 			transportRequestReqIDFromGit(stepConfig, &telemetryData, &commonPipelineEnvironment) | ||||
| 			telemetryData.ErrorCode = "0" | ||||
| 			log.Entry().Info("SUCCESS") | ||||
| 		}, | ||||
| 	} | ||||
|  | ||||
| 	addTransportRequestReqIDFromGitFlags(createTransportRequestReqIDFromGitCmd, &stepConfig) | ||||
| 	return createTransportRequestReqIDFromGitCmd | ||||
| } | ||||
|  | ||||
| func addTransportRequestReqIDFromGitFlags(cmd *cobra.Command, stepConfig *transportRequestReqIDFromGitOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitFrom, "gitFrom", `origin/master`, "GIT starting point for retrieving the transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitTo, "gitTo", `HEAD`, "GIT ending point for retrieving the transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.TransportRequestLabel, "transportRequestLabel", `TransportRequest`, "Pattern used for identifying lines holding the transport request ID. The GIT commit log messages are scanned for this label") | ||||
|  | ||||
| } | ||||
|  | ||||
| // retrieve step metadata | ||||
| func transportRequestReqIDFromGitMetadata() config.StepData { | ||||
| 	var theMetaData = config.StepData{ | ||||
| 		Metadata: config.StepMetadata{ | ||||
| 			Name:        "transportRequestReqIDFromGit", | ||||
| 			Aliases:     []config.Alias{}, | ||||
| 			Description: "Retrieves the transport request ID from Git repository", | ||||
| 		}, | ||||
| 		Spec: config.StepSpec{ | ||||
| 			Inputs: config.StepInputs{ | ||||
| 				Parameters: []config.StepParameters{ | ||||
| 					{ | ||||
| 						Name:        "gitFrom", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/from"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "gitTo", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/to"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "transportRequestLabel", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/transportRequestLabel"}}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Outputs: config.StepOutputs{ | ||||
| 				Resources: []config.StepResources{ | ||||
| 					{ | ||||
| 						Name: "commonPipelineEnvironment", | ||||
| 						Type: "piperEnvironment", | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "custom/transportRequestId"}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 		}, | ||||
| 	} | ||||
| 	return theMetaData | ||||
| } | ||||
							
								
								
									
										17
									
								
								cmd/transportRequestReqIDFromGit_generated_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								cmd/transportRequestReqIDFromGit_generated_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
|  | ||||
| func TestTransportRequestReqIDFromGitCommand(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	testCmd := TransportRequestReqIDFromGitCommand() | ||||
|  | ||||
| 	// only high level testing performed - details are tested in step generation procedure | ||||
| 	assert.Equal(t, "transportRequestReqIDFromGit", testCmd.Use, "command name incorrect") | ||||
|  | ||||
| } | ||||
							
								
								
									
										84
									
								
								cmd/transportRequestReqIDFromGit_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								cmd/transportRequestReqIDFromGit_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| type transportRequestUtilsMock struct { | ||||
| 	err  error | ||||
| 	trID string | ||||
| 	cdID string | ||||
| } | ||||
|  | ||||
| func (m *transportRequestUtilsMock) FindIDInRange(label, from, to string) (string, error) { | ||||
| 	if m.err != nil { | ||||
| 		return "", m.err | ||||
| 	} | ||||
| 	if strings.HasPrefix(label, "TransportRequest") { | ||||
| 		return m.trID, nil | ||||
| 	} | ||||
| 	if strings.HasPrefix(label, "ChangeDocument") { | ||||
| 		return m.cdID, nil | ||||
| 	} | ||||
|  | ||||
| 	return "invalid", fmt.Errorf("invalid label passed: %s", label) | ||||
| } | ||||
|  | ||||
| func TestTrGitGetTransportRequestID(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("good", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("getTransportRequestID", func(t *testing.T) { | ||||
| 			configMock := newTrIDConfigMock() | ||||
|  | ||||
| 			id, err := getTransportRequestID(configMock.config, &transportRequestUtilsMock{trID: "43218765"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "43218765") | ||||
| 			} | ||||
| 		}) | ||||
| 		t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { | ||||
| 			configMock := newTrIDConfigMock() | ||||
| 			cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{} | ||||
|  | ||||
| 			err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{trID: "43218765"}, cpe) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, cpe.custom.transportRequestID, "43218765") | ||||
| 			} | ||||
| 		}) | ||||
| 	}) | ||||
| 	t.Run("bad", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("runTransportRequestDocIDFromGit", func(t *testing.T) { | ||||
| 			configMock := newTrIDConfigMock() | ||||
| 			cpe := &transportRequestReqIDFromGitCommonPipelineEnvironment{} | ||||
|  | ||||
| 			err := runTransportRequestReqIDFromGit(configMock.config, nil, &transportRequestUtilsMock{err: errors.New("fail")}, cpe) | ||||
|  | ||||
| 			assert.EqualError(t, err, "fail") | ||||
| 		}) | ||||
|  | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| type trIDConfigMock struct { | ||||
| 	config *transportRequestReqIDFromGitOptions | ||||
| } | ||||
|  | ||||
| func newTrIDConfigMock() *trIDConfigMock { | ||||
| 	return &trIDConfigMock{ | ||||
| 		config: &transportRequestReqIDFromGitOptions{ | ||||
| 			GitFrom:               "origin/master", | ||||
| 			GitTo:                 "HEAD", | ||||
| 			TransportRequestLabel: "TransportRequest", | ||||
| 		}, | ||||
| 	} | ||||
| } | ||||
| @@ -5,7 +5,6 @@ import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperutils" | ||||
| 	"github.com/SAP/jenkins-library/pkg/telemetry" | ||||
| 	"github.com/SAP/jenkins-library/pkg/transportrequest" | ||||
| 	"github.com/SAP/jenkins-library/pkg/transportrequest/solman" | ||||
| ) | ||||
|  | ||||
| @@ -59,17 +58,6 @@ func transportRequestUploadSOLMAN(config transportRequestUploadSOLMANOptions, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // mocking framework. Allows to redirect the containing methods | ||||
| type iTransportRequestUtils interface { | ||||
| 	FindIDInRange(label, from, to string) (string, error) | ||||
| } | ||||
| type transportRequestUtils struct { | ||||
| } | ||||
|  | ||||
| func (*transportRequestUtils) FindIDInRange(label, from, to string) (string, error) { | ||||
| 	return transportrequest.FindIDInRange(label, from, to) | ||||
| } | ||||
|  | ||||
| func runTransportRequestUploadSOLMAN(config *transportRequestUploadSOLMANOptions, | ||||
| 	action solman.Action, | ||||
| 	telemetryData *telemetry.CustomData, | ||||
| @@ -82,52 +70,23 @@ func runTransportRequestUploadSOLMAN(config *transportRequestUploadSOLMANOptions | ||||
| 		Password: config.Password, | ||||
| 	}) | ||||
|  | ||||
| 	cdID, err := getChangeDocumentID(config, &transportRequestUtils{}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	action.WithChangeDocumentID(cdID) | ||||
|  | ||||
| 	trID, err := getTransportRequestID(config, &transportRequestUtils{}) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	action.WithTransportRequestID(trID) | ||||
| 	action.WithTransportRequestID(config.TransportRequestID) | ||||
| 	action.WithChangeDocumentID(config.ChangeDocumentID) | ||||
| 	action.WithApplicationID(config.ApplicationID) | ||||
| 	action.WithFile(config.FilePath) | ||||
| 	action.WithCMOpts(config.CmClientOpts) | ||||
|  | ||||
| 	commonPipelineEnvironment.custom.changeDocumentID = cdID | ||||
| 	commonPipelineEnvironment.custom.transportRequestID = trID | ||||
| 	commonPipelineEnvironment.custom.transportRequestID = config.TransportRequestID | ||||
| 	commonPipelineEnvironment.custom.changeDocumentID = config.ChangeDocumentID | ||||
|  | ||||
| 	err = action.Perform(utils, utils) | ||||
| 	err := action.Perform(utils, utils) | ||||
|  | ||||
| 	if err == nil { | ||||
| 		log.Entry().Infof("Upload of artifact '%s' to SAP Solution Manager succeeded (ChangeDocumentId: '%s', TransportRequestId: '%s').", | ||||
| 			config.FilePath, | ||||
| 			cdID, | ||||
| 			trID, | ||||
| 			config.ChangeDocumentID, | ||||
| 			config.TransportRequestID, | ||||
| 		) | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func getTransportRequestID(config *transportRequestUploadSOLMANOptions, | ||||
| 	trUtils iTransportRequestUtils) (string, error) { | ||||
|  | ||||
| 	if len(config.TransportRequestID) > 0 { | ||||
| 		return config.TransportRequestID, nil | ||||
| 	} | ||||
|  | ||||
| 	return trUtils.FindIDInRange(config.TransportRequestLabel, config.GitFrom, config.GitTo) | ||||
| } | ||||
|  | ||||
| func getChangeDocumentID(config *transportRequestUploadSOLMANOptions, | ||||
| 	trUtils iTransportRequestUtils) (string, error) { | ||||
|  | ||||
| 	if len(config.ChangeDocumentID) > 0 { | ||||
| 		return config.ChangeDocumentID, nil | ||||
| 	} | ||||
|  | ||||
| 	return trUtils.FindIDInRange(config.ChangeDocumentLabel, config.GitFrom, config.GitTo) | ||||
| } | ||||
|   | ||||
| @@ -17,18 +17,14 @@ import ( | ||||
| ) | ||||
|  | ||||
| type transportRequestUploadSOLMANOptions struct { | ||||
| 	Endpoint              string   `json:"endpoint,omitempty"` | ||||
| 	Username              string   `json:"username,omitempty"` | ||||
| 	Password              string   `json:"password,omitempty"` | ||||
| 	ApplicationID         string   `json:"applicationId,omitempty"` | ||||
| 	ChangeDocumentID      string   `json:"changeDocumentId,omitempty"` | ||||
| 	TransportRequestID    string   `json:"transportRequestId,omitempty"` | ||||
| 	FilePath              string   `json:"filePath,omitempty"` | ||||
| 	CmClientOpts          []string `json:"cmClientOpts,omitempty"` | ||||
| 	GitFrom               string   `json:"gitFrom,omitempty"` | ||||
| 	GitTo                 string   `json:"gitTo,omitempty"` | ||||
| 	ChangeDocumentLabel   string   `json:"changeDocumentLabel,omitempty"` | ||||
| 	TransportRequestLabel string   `json:"transportRequestLabel,omitempty"` | ||||
| 	Endpoint           string   `json:"endpoint,omitempty"` | ||||
| 	Username           string   `json:"username,omitempty"` | ||||
| 	Password           string   `json:"password,omitempty"` | ||||
| 	ApplicationID      string   `json:"applicationId,omitempty"` | ||||
| 	ChangeDocumentID   string   `json:"changeDocumentId,omitempty"` | ||||
| 	TransportRequestID string   `json:"transportRequestId,omitempty"` | ||||
| 	FilePath           string   `json:"filePath,omitempty"` | ||||
| 	CmClientOpts       []string `json:"cmClientOpts,omitempty"` | ||||
| } | ||||
|  | ||||
| type transportRequestUploadSOLMANCommonPipelineEnvironment struct { | ||||
| @@ -148,15 +144,13 @@ func addTransportRequestUploadSOLMANFlags(cmd *cobra.Command, stepConfig *transp | ||||
| 	cmd.Flags().StringVar(&stepConfig.TransportRequestID, "transportRequestId", os.Getenv("PIPER_transportRequestId"), "ID of the transport request to which the file is uploaded") | ||||
| 	cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Name/Path of the file which should be uploaded") | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.CmClientOpts, "cmClientOpts", []string{}, "Additional options handed over to the cm client") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitFrom, "gitFrom", `origin/master`, "GIT starting point for retrieving the change document and transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.GitTo, "gitTo", `HEAD`, "GIT ending point for retrieving the change document and transport request ID") | ||||
| 	cmd.Flags().StringVar(&stepConfig.ChangeDocumentLabel, "changeDocumentLabel", `ChangeDocument`, "Pattern used for identifying lines holding the change document ID. The GIT commit log messages are scanned for this label") | ||||
| 	cmd.Flags().StringVar(&stepConfig.TransportRequestLabel, "transportRequestLabel", `TransportRequest`, "Pattern used for identifying lines holding the transport request ID. The GIT commit log messages are scanned for this label") | ||||
|  | ||||
| 	cmd.MarkFlagRequired("endpoint") | ||||
| 	cmd.MarkFlagRequired("username") | ||||
| 	cmd.MarkFlagRequired("password") | ||||
| 	cmd.MarkFlagRequired("applicationId") | ||||
| 	cmd.MarkFlagRequired("changeDocumentId") | ||||
| 	cmd.MarkFlagRequired("transportRequestId") | ||||
| 	cmd.MarkFlagRequired("filePath") | ||||
| 	cmd.MarkFlagRequired("cmClientOpts") | ||||
| } | ||||
| @@ -226,7 +220,7 @@ func transportRequestUploadSOLMANMetadata() config.StepData { | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| @@ -239,7 +233,7 @@ func transportRequestUploadSOLMANMetadata() config.StepData { | ||||
| 						}, | ||||
| 						Scope:     []string{"PARAMETERS"}, | ||||
| 						Type:      "string", | ||||
| 						Mandatory: false, | ||||
| 						Mandatory: true, | ||||
| 						Aliases:   []config.Alias{}, | ||||
| 					}, | ||||
| 					{ | ||||
| @@ -258,43 +252,11 @@ func transportRequestUploadSOLMANMetadata() config.StepData { | ||||
| 					{ | ||||
| 						Name:        "cmClientOpts", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEP", "GENERAL"}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEPS", "GENERAL"}, | ||||
| 						Type:        "[]string", | ||||
| 						Mandatory:   true, | ||||
| 						Aliases:     []config.Alias{{Name: "clientOpts"}, {Name: "changeManagement/clientOpts"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "gitFrom", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEP", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/from"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "gitTo", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEP", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/git/to"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "changeDocumentLabel", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEP", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/changeDocumentLabel"}}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Name:        "transportRequestLabel", | ||||
| 						ResourceRef: []config.ResourceReference{}, | ||||
| 						Scope:       []string{"PARAMETERS", "STAGES", "STEP", "GENERAL"}, | ||||
| 						Type:        "string", | ||||
| 						Mandatory:   false, | ||||
| 						Aliases:     []config.Alias{{Name: "changeManagement/transportRequestLabel"}}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			Containers: []config.Container{ | ||||
|   | ||||
| @@ -5,8 +5,6 @@ import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/SAP/jenkins-library/pkg/transportrequest/solman" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| @@ -56,37 +54,10 @@ type ConfigMock struct { | ||||
| 	config *transportRequestUploadSOLMANOptions | ||||
| } | ||||
|  | ||||
| func (m *ConfigMock) with(field string, value string) *ConfigMock { | ||||
| 	r := reflect.ValueOf(m.config) | ||||
| 	f := reflect.Indirect(r).FieldByName(field) | ||||
| 	f.SetString(value) | ||||
| 	return m | ||||
| } | ||||
|  | ||||
| func (m *ConfigMock) without(field string) *ConfigMock { | ||||
| 	return m.with(field, "") | ||||
| } | ||||
|  | ||||
| type transportRequestUtilsMock struct { | ||||
| 	trID string | ||||
| 	cdID string | ||||
| } | ||||
|  | ||||
| func (m *transportRequestUtilsMock) FindIDInRange(label, from, to string) (string, error) { | ||||
| 	if strings.HasPrefix(label, "TransportRequest") { | ||||
| 		return m.trID, nil | ||||
| 	} | ||||
| 	if strings.HasPrefix(label, "ChangeDocument") { | ||||
| 		return m.cdID, nil | ||||
| 	} | ||||
|  | ||||
| 	return "invalid", fmt.Errorf("invalid label passed: %s", label) | ||||
| } | ||||
|  | ||||
| func TestTrSolmanRunTransportRequestUpload(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("solmand upload", func(t *testing.T) { | ||||
| 	t.Run("good", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("straight forward", func(t *testing.T) { | ||||
| @@ -113,6 +84,10 @@ func TestTrSolmanRunTransportRequestUpload(t *testing.T) { | ||||
| 				assert.True(t, actionMock.performCalled) | ||||
| 			} | ||||
| 		}) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("bad", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("Error during deployment", func(t *testing.T) { | ||||
| 			utilsMock := newTransportRequestUploadSOLMANTestsUtils(0) | ||||
| @@ -124,79 +99,20 @@ func TestTrSolmanRunTransportRequestUpload(t *testing.T) { | ||||
|  | ||||
| 			assert.Error(t, err, "upload failed") | ||||
| 		}) | ||||
|  | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestTrSolmanGetTransportRequestID(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("get transport request id", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("TransportRequestID from config", func(t *testing.T) { | ||||
| 			configMock := newConfigMock() | ||||
|  | ||||
| 			id, err := getTransportRequestID(configMock.config, &transportRequestUtilsMock{trID: "43218765", cdID: "56781234"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "87654321") | ||||
| 			} | ||||
| 		}) | ||||
| 		t.Run("TransportRequestID from git commit", func(t *testing.T) { | ||||
| 			configMock := newConfigMock().without("TransportRequestID") | ||||
|  | ||||
| 			id, err := getTransportRequestID(configMock.config, &transportRequestUtilsMock{trID: "43218765", cdID: "56781234"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "43218765") | ||||
| 			} | ||||
| 		}) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func TestTrSolmanGetChangeDocumentID(t *testing.T) { | ||||
| 	t.Parallel() | ||||
|  | ||||
| 	t.Run("get change document id", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		t.Run("ChangeDocumentID from config", func(t *testing.T) { | ||||
| 			configMock := newConfigMock() | ||||
|  | ||||
| 			id, err := getChangeDocumentID(configMock.config, &transportRequestUtilsMock{trID: "43218765", cdID: "56781234"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "12345678") | ||||
| 			} | ||||
| 		}) | ||||
| 		t.Run("ChangeDocumentID from git commit", func(t *testing.T) { | ||||
| 			configMock := newConfigMock().without("ChangeDocumentID") | ||||
|  | ||||
| 			id, err := getChangeDocumentID(configMock.config, &transportRequestUtilsMock{trID: "43218765", cdID: "56781234"}) | ||||
|  | ||||
| 			if assert.NoError(t, err) { | ||||
| 				assert.Equal(t, id, "56781234") | ||||
| 			} | ||||
| 		}) | ||||
| 	}) | ||||
| } | ||||
|  | ||||
| func newConfigMock() *ConfigMock { | ||||
| 	return &ConfigMock{ | ||||
| 		config: &transportRequestUploadSOLMANOptions{ | ||||
| 			Endpoint:              "https://example.org/solman", | ||||
| 			Username:              "me", | ||||
| 			Password:              "********", | ||||
| 			ApplicationID:         "XYZ", | ||||
| 			ChangeDocumentID:      "12345678", | ||||
| 			TransportRequestID:    "87654321", | ||||
| 			FilePath:              "myApp.abc", | ||||
| 			CmClientOpts:          []string{"-Dtest=abc123"}, | ||||
| 			GitFrom:               "origin/master", | ||||
| 			GitTo:                 "HEAD", | ||||
| 			ChangeDocumentLabel:   "ChangeDocument", | ||||
| 			TransportRequestLabel: "TransportRequest", | ||||
| 			Endpoint:           "https://example.org/solman", | ||||
| 			Username:           "me", | ||||
| 			Password:           "********", | ||||
| 			ApplicationID:      "XYZ", | ||||
| 			ChangeDocumentID:   "12345678", | ||||
| 			TransportRequestID: "87654321", | ||||
| 			FilePath:           "myApp.abc", | ||||
| 			CmClientOpts:       []string{"-Dtest=abc123"}, | ||||
| 		}, | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										47
									
								
								resources/metadata/transportRequestDocIDFromGit.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								resources/metadata/transportRequestDocIDFromGit.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| metadata: | ||||
|   name: transportRequestDocIDFromGit | ||||
|   description: "Retrieves change document ID from Git repository" | ||||
|   longDescription: | | ||||
|     Scans the commit messages of the Git repository for a pattern to retrieve the change document ID. | ||||
| spec: | ||||
|   inputs: | ||||
|     params: | ||||
|       - name: gitFrom | ||||
|         aliases: | ||||
|           - name: changeManagement/git/from | ||||
|         type: "string" | ||||
|         description: "GIT starting point for retrieving the change document and transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "origin/master" | ||||
|       - name: gitTo | ||||
|         aliases: | ||||
|           - name: changeManagement/git/to | ||||
|         type: "string" | ||||
|         description: "GIT ending point for retrieving the change document and transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "HEAD" | ||||
|       - name: changeDocumentLabel | ||||
|         aliases: | ||||
|           - name: changeManagement/changeDocumentLabel | ||||
|         type: "string" | ||||
|         description: "Pattern used for identifying lines holding the change document ID. The GIT commit log messages are scanned for this label" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "ChangeDocument" | ||||
|   outputs: | ||||
|     resources: | ||||
|       - name: commonPipelineEnvironment | ||||
|         type: piperEnvironment | ||||
|         params: | ||||
|           - name: custom/changeDocumentId | ||||
							
								
								
									
										47
									
								
								resources/metadata/transportRequestReqIDFromGit.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								resources/metadata/transportRequestReqIDFromGit.yaml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| metadata: | ||||
|   name: transportRequestReqIDFromGit | ||||
|   description: "Retrieves the transport request ID from Git repository" | ||||
|   longDescription: | | ||||
|     Scans the commit messages of the Git repository for a pattern to retrieve the transport request ID. | ||||
| spec: | ||||
|   inputs: | ||||
|     params: | ||||
|       - name: gitFrom | ||||
|         aliases: | ||||
|           - name: changeManagement/git/from | ||||
|         type: "string" | ||||
|         description: "GIT starting point for retrieving the transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "origin/master" | ||||
|       - name: gitTo | ||||
|         aliases: | ||||
|           - name: changeManagement/git/to | ||||
|         type: "string" | ||||
|         description: "GIT ending point for retrieving the transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "HEAD" | ||||
|       - name: transportRequestLabel | ||||
|         aliases: | ||||
|           - name: changeManagement/transportRequestLabel | ||||
|         type: "string" | ||||
|         description: "Pattern used for identifying lines holding the transport request ID. The GIT commit log messages are scanned for this label" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|         default: "TransportRequest" | ||||
|   outputs: | ||||
|     resources: | ||||
|       - name: commonPipelineEnvironment | ||||
|         type: piperEnvironment | ||||
|         params: | ||||
|           - name: custom/transportRequestId | ||||
| @@ -66,6 +66,7 @@ spec: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/changeDocumentId | ||||
|         type: string | ||||
|         mandatory: true | ||||
|         description: "ID of the change document to which the file is uploaded" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
| @@ -74,6 +75,7 @@ spec: | ||||
|           - name: commonPipelineEnvironment | ||||
|             param: custom/transportRequestId | ||||
|         type: string | ||||
|         mandatory: true | ||||
|         description: "ID of the transport request to which the file is uploaded" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
| @@ -99,52 +101,8 @@ spec: | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEP | ||||
|           - STEPS | ||||
|           - GENERAL | ||||
|       - name: gitFrom | ||||
|         aliases: | ||||
|           - name: changeManagement/git/from | ||||
|         type: "string" | ||||
|         description: "GIT starting point for retrieving the change document and transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEP | ||||
|           - GENERAL | ||||
|         default: "origin/master" | ||||
|       - name: gitTo | ||||
|         aliases: | ||||
|           - name: changeManagement/git/to | ||||
|         type: "string" | ||||
|         description: "GIT ending point for retrieving the change document and transport request ID" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEP | ||||
|           - GENERAL | ||||
|         default: "HEAD" | ||||
|       - name: changeDocumentLabel | ||||
|         aliases: | ||||
|           - name: changeManagement/changeDocumentLabel | ||||
|         type: "string" | ||||
|         description: "Pattern used for identifying lines holding the change document ID. The GIT commit log messages are scanned for this label" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEP | ||||
|           - GENERAL | ||||
|         default: "ChangeDocument" | ||||
|       - name: transportRequestLabel | ||||
|         aliases: | ||||
|           - name: changeManagement/transportRequestLabel | ||||
|         type: "string" | ||||
|         description: "Pattern used for identifying lines holding the transport request ID. The GIT commit log messages are scanned for this label" | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEP | ||||
|           - GENERAL | ||||
|         default: "TransportRequest" | ||||
|   outputs: | ||||
|     resources: | ||||
|       - name: commonPipelineEnvironment | ||||
|   | ||||
		Reference in New Issue
	
	Block a user