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 
			
		
		
		
	feat(splunk): Adds head commitID of current build to commonPipelineEnvironment (#2844)
				
					
				
			* Adds headCommitId, which stores the head commit has of the current build, includes tests and generated files * Adds headCommitId, which stores the head commit has of the current build, includes tests and generated files * Go fmt fix * Fixes artifactoryPrepareVersion test * Removes xMake CommitId
This commit is contained in:
		| @@ -143,6 +143,7 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD | ||||
| 	} | ||||
| 	gitCommitID := gitCommit.String() | ||||
|  | ||||
| 	commonPipelineEnvironment.git.headCommitID = gitCommitID | ||||
| 	newVersion := version | ||||
|  | ||||
| 	if versioningType == "cloud" || versioningType == "cloud_noTag" { | ||||
| @@ -198,7 +199,7 @@ func runArtifactPrepareVersion(config *artifactPrepareVersionOptions, telemetryD | ||||
|  | ||||
| 	log.Entry().Infof("New version: '%v'", newVersion) | ||||
|  | ||||
| 	commonPipelineEnvironment.git.commitID = gitCommitID | ||||
| 	commonPipelineEnvironment.git.commitID = gitCommitID // this commitID changes and is not necessarily the HEAD commitID | ||||
| 	commonPipelineEnvironment.artifactVersion = newVersion | ||||
| 	commonPipelineEnvironment.originalArtifactVersion = version | ||||
| 	commonPipelineEnvironment.git.commitMessage = gitCommitMessage | ||||
|   | ||||
| @@ -42,6 +42,7 @@ type artifactPrepareVersionCommonPipelineEnvironment struct { | ||||
| 	originalArtifactVersion string | ||||
| 	git                     struct { | ||||
| 		commitID      string | ||||
| 		headCommitID  string | ||||
| 		commitMessage string | ||||
| 	} | ||||
| } | ||||
| @@ -55,6 +56,7 @@ func (p *artifactPrepareVersionCommonPipelineEnvironment) persist(path, resource | ||||
| 		{category: "", name: "artifactVersion", value: p.artifactVersion}, | ||||
| 		{category: "", name: "originalArtifactVersion", value: p.originalArtifactVersion}, | ||||
| 		{category: "git", name: "commitId", value: p.git.commitID}, | ||||
| 		{category: "git", name: "headCommitId", value: p.git.headCommitID}, | ||||
| 		{category: "git", name: "commitMessage", value: p.git.commitMessage}, | ||||
| 	} | ||||
|  | ||||
| @@ -425,6 +427,7 @@ func artifactPrepareVersionMetadata() config.StepData { | ||||
| 							{"Name": "artifactVersion"}, | ||||
| 							{"Name": "originalArtifactVersion"}, | ||||
| 							{"Name": "git/commitId"}, | ||||
| 							{"Name": "git/headCommitId"}, | ||||
| 							{"Name": "git/commitMessage"}, | ||||
| 						}, | ||||
| 					}, | ||||
|   | ||||
| @@ -303,7 +303,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
| 		assert.Equal(t, repo.revisionHash.String(), cpe.git.commitID) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("error - failed to retrive version", func(t *testing.T) { | ||||
| 	t.Run("error - failed to retrieve version", func(t *testing.T) { | ||||
| 		config := artifactPrepareVersionOptions{} | ||||
|  | ||||
| 		versioningMock := artifactVersioningMock{ | ||||
| @@ -315,7 +315,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
|  | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("error - failed to retrive git commit ID", func(t *testing.T) { | ||||
| 	t.Run("error - failed to retrieve git commit ID", func(t *testing.T) { | ||||
| 		config := artifactPrepareVersionOptions{} | ||||
|  | ||||
| 		versioningMock := artifactVersioningMock{ | ||||
| @@ -341,7 +341,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
|  | ||||
| 		repo := gitRepositoryMock{} | ||||
|  | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, nil, &versioningMock, nil, &repo, nil) | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, nil) | ||||
| 		assert.Contains(t, fmt.Sprint(err), "failed to get versioning template for scheme 'notSupported'") | ||||
| 	}) | ||||
|  | ||||
| @@ -357,7 +357,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
|  | ||||
| 		repo := gitRepositoryMock{} | ||||
|  | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, nil, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return nil, fmt.Errorf("worktree error") }) | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return nil, fmt.Errorf("worktree error") }) | ||||
| 		assert.EqualError(t, err, "failed to retrieve git worktree: worktree error") | ||||
| 	}) | ||||
|  | ||||
| @@ -374,7 +374,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
| 		worktree := gitWorktreeMock{checkoutError: "checkout error"} | ||||
| 		repo := gitRepositoryMock{} | ||||
|  | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, nil, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		assert.EqualError(t, err, "failed to initialize worktree: checkout error") | ||||
| 	}) | ||||
|  | ||||
| @@ -392,7 +392,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
| 		worktree := gitWorktreeMock{} | ||||
| 		repo := gitRepositoryMock{} | ||||
|  | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, nil, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		assert.EqualError(t, err, "failed to write version: setVersion error") | ||||
| 	}) | ||||
|  | ||||
| @@ -409,7 +409,7 @@ func TestRunArtifactPrepareVersion(t *testing.T) { | ||||
| 		worktree := gitWorktreeMock{} | ||||
| 		repo := gitRepositoryMock{} | ||||
|  | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, nil, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		err := runArtifactPrepareVersion(&config, &telemetry.CustomData{}, &artifactPrepareVersionCommonPipelineEnvironment{}, &versioningMock, nil, &repo, func(r gitRepository) (gitWorktree, error) { return &worktree, nil }) | ||||
| 		assert.Contains(t, fmt.Sprint(err), "failed to push changes for version '1.2.3") | ||||
| 	}) | ||||
| } | ||||
|   | ||||
| @@ -78,7 +78,7 @@ The following data will be sent to the endpoint if activated: | ||||
| * ErrorCode | ||||
| * ErrorCategory | ||||
| * CorrelationID (not hashed) | ||||
| * CommitHash | ||||
| * CommitHash (Head commit hash of current build.) | ||||
| * Branch | ||||
| * GitOwner | ||||
| * GitRepository | ||||
|   | ||||
| @@ -30,6 +30,7 @@ spec: | ||||
|         params: | ||||
|           - name: artifactVersion | ||||
|           - name: git/commitId | ||||
|           - name: git/headCommitId | ||||
|           - name: git/branch | ||||
|           - name: custom/customList | ||||
|             type: "[]string" | ||||
|   | ||||
| @@ -28,6 +28,7 @@ type testStepCommonPipelineEnvironment struct { | ||||
| 	artifactVersion string | ||||
| 	git struct { | ||||
| 		commitID string | ||||
| 		headCommitID string | ||||
| 		branch string | ||||
| 	} | ||||
| 	custom struct { | ||||
| @@ -43,6 +44,7 @@ func (p *testStepCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| 	}{ | ||||
| 		{category: "", name: "artifactVersion", value: p.artifactVersion}, | ||||
| 		{category: "git", name: "commitId", value: p.git.commitID}, | ||||
| 		{category: "git", name: "headCommitId", value: p.git.headCommitID}, | ||||
| 		{category: "git", name: "branch", value: p.git.branch}, | ||||
| 		{category: "custom", name: "customList", value: p.custom.customList}, | ||||
| 	} | ||||
| @@ -230,6 +232,7 @@ func testStepMetadata() config.StepData { | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "artifactVersion"}, | ||||
| 							{"Name": "git/commitId"}, | ||||
| 							{"Name": "git/headCommitId"}, | ||||
| 							{"Name": "git/branch"}, | ||||
| 							{"Name": "custom/customList"}, | ||||
| 						}, | ||||
|   | ||||
| @@ -27,6 +27,7 @@ type testStepCommonPipelineEnvironment struct { | ||||
| 	artifactVersion string | ||||
| 	git struct { | ||||
| 		commitID string | ||||
| 		headCommitID string | ||||
| 		branch string | ||||
| 	} | ||||
| 	custom struct { | ||||
| @@ -42,6 +43,7 @@ func (p *testStepCommonPipelineEnvironment) persist(path, resourceName string) { | ||||
| 	}{ | ||||
| 		{category: "", name: "artifactVersion", value: p.artifactVersion}, | ||||
| 		{category: "git", name: "commitId", value: p.git.commitID}, | ||||
| 		{category: "git", name: "headCommitId", value: p.git.headCommitID}, | ||||
| 		{category: "git", name: "branch", value: p.git.branch}, | ||||
| 		{category: "custom", name: "customList", value: p.custom.customList}, | ||||
| 	} | ||||
| @@ -229,6 +231,7 @@ func testStepMetadata() config.StepData { | ||||
| 						Parameters: []map[string]interface{}{ | ||||
| 							{"Name": "artifactVersion"}, | ||||
| 							{"Name": "git/commitId"}, | ||||
| 							{"Name": "git/headCommitId"}, | ||||
| 							{"Name": "git/branch"}, | ||||
| 							{"Name": "custom/customList"}, | ||||
| 						}, | ||||
|   | ||||
| @@ -133,7 +133,7 @@ func prepareTelemetry(customTelemetryData telemetry.CustomData) MonitoringData { | ||||
| 		ErrorCode:       tData.CustomData.ErrorCode, | ||||
| 		ErrorCategory:   tData.CustomData.ErrorCategory, | ||||
| 		CorrelationID:   SplunkClient.correlationID, | ||||
| 		CommitHash:      readCommonPipelineEnvironment("git/commitId"), | ||||
| 		CommitHash:      readCommonPipelineEnvironment("git/headCommitId"), | ||||
| 		Branch:          readCommonPipelineEnvironment("git/branch"), | ||||
| 		GitOwner:        readCommonPipelineEnvironment("github/owner"), | ||||
| 		GitRepository:   readCommonPipelineEnvironment("github/repository"), | ||||
| @@ -173,7 +173,7 @@ func tryPostMessages(telemetryData MonitoringData, messages []log.Message) error | ||||
| 	resp, err := SplunkClient.splunkClient.SendRequest(http.MethodPost, SplunkClient.splunkDsn, bytes.NewBuffer(payload), nil, nil) | ||||
|  | ||||
| 	if err != nil { | ||||
| 		return errors.Wrap(err, "error sending the requets to Splunk") | ||||
| 		return errors.Wrap(err, "error sending the requests to Splunk") | ||||
| 	} | ||||
|  | ||||
| 	defer func() { | ||||
|   | ||||
| @@ -270,6 +270,7 @@ spec: | ||||
|           - name: artifactVersion | ||||
|           - name: originalArtifactVersion | ||||
|           - name: git/commitId | ||||
|           - name: git/headCommitId | ||||
|           - name: git/commitMessage | ||||
|   containers: | ||||
|     - image: maven:3.6-jdk-8 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user