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(fortifyExecuteScan): set python version (#3960)
* set python version
This commit is contained in:
		| @@ -971,6 +971,18 @@ func triggerFortifyScan(config fortifyExecuteScanOptions, utils fortifyUtils, bu | ||||
| 	return scanProject(&config, utils, buildID, buildLabel, buildProject) | ||||
| } | ||||
|  | ||||
| func appendPythonVersionToTranslate(translateOptions map[string]interface{}, pythonVersion string) error { | ||||
| 	if pythonVersion == "python2" { | ||||
| 		translateOptions["pythonVersion"] = "2" | ||||
| 	} else if pythonVersion == "python3" { | ||||
| 		translateOptions["pythonVersion"] = "3" | ||||
| 	} else { | ||||
| 		return fmt.Errorf("Invalid pythonVersion '%s'. Possible values for pythonVersion are 'python2' and 'python3'. ", pythonVersion) | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func populatePipTranslate(config *fortifyExecuteScanOptions, classpath string) (string, error) { | ||||
| 	if len(config.Translate) > 0 { | ||||
| 		return config.Translate, nil | ||||
| @@ -978,9 +990,13 @@ func populatePipTranslate(config *fortifyExecuteScanOptions, classpath string) ( | ||||
|  | ||||
| 	var translateList []map[string]interface{} | ||||
| 	translateList = append(translateList, make(map[string]interface{})) | ||||
|  | ||||
| 	separator := getSeparator() | ||||
|  | ||||
| 	err := appendPythonVersionToTranslate(translateList[0], config.PythonVersion) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
|  | ||||
| 	translateList[0]["pythonPath"] = classpath + separator + | ||||
| 		getSuppliedOrDefaultListAsString(config.PythonAdditionalPath, []string{}, separator) | ||||
| 	translateList[0]["src"] = getSuppliedOrDefaultListAsString( | ||||
| @@ -1167,6 +1183,9 @@ func appendToOptions(config *fortifyExecuteScanOptions, options []string, t map[ | ||||
| 		if len(t["djangoTemplatDirs"]) > 0 { | ||||
| 			options = append(options, "-django-template-dirs", t["djangoTemplatDirs"]) | ||||
| 		} | ||||
| 		if len(t["pythonVersion"]) > 0 { | ||||
| 			options = append(options, "-python-version", t["pythonVersion"]) | ||||
| 		} | ||||
|  | ||||
| 	default: | ||||
| 		return options | ||||
|   | ||||
| @@ -35,7 +35,7 @@ type fortifyExecuteScanOptions struct { | ||||
| 	MustAuditIssueGroups            string   `json:"mustAuditIssueGroups,omitempty"` | ||||
| 	SpotAuditIssueGroups            string   `json:"spotAuditIssueGroups,omitempty"` | ||||
| 	PythonRequirementsInstallSuffix string   `json:"pythonRequirementsInstallSuffix,omitempty"` | ||||
| 	PythonVersion                   string   `json:"pythonVersion,omitempty"` | ||||
| 	PythonVersion                   string   `json:"pythonVersion,omitempty" validate:"possible-values=python3 python2"` | ||||
| 	UploadResults                   bool     `json:"uploadResults,omitempty"` | ||||
| 	Version                         string   `json:"version,omitempty"` | ||||
| 	BuildDescriptorFile             string   `json:"buildDescriptorFile,omitempty"` | ||||
|   | ||||
| @@ -701,7 +701,7 @@ func TestTriggerFortifyScan(t *testing.T) { | ||||
| 		assert.Equal(t, []string{"install", "--user"}, utils.executions[2].parameters) | ||||
|  | ||||
| 		assert.Equal(t, "sourceanalyzer", utils.executions[3].executable) | ||||
| 		assert.Equal(t, []string{"-verbose", "-64", "-b", "test", "-Xmx4G", "-Xms2G", "-python-path", "/usr/lib/python35.zip;/usr/lib/python3.5;/usr/lib/python3.5/plat-x86_64-linux-gnu;/usr/lib/python3.5/lib-dynload;/home/piper/.local/lib/python3.5/site-packages;/usr/local/lib/python3.5/dist-packages;/usr/lib/python3/dist-packages;./lib", "-exclude", fmt.Sprintf("./**/tests/**/*%s./**/setup.py", separator), "./**/*"}, utils.executions[3].parameters) | ||||
| 		assert.Equal(t, []string{"-verbose", "-64", "-b", "test", "-Xmx4G", "-Xms2G", "-python-path", "/usr/lib/python35.zip;/usr/lib/python3.5;/usr/lib/python3.5/plat-x86_64-linux-gnu;/usr/lib/python3.5/lib-dynload;/home/piper/.local/lib/python3.5/site-packages;/usr/local/lib/python3.5/dist-packages;/usr/lib/python3/dist-packages;./lib", "-python-version", "2", "-exclude", fmt.Sprintf("./**/tests/**/*%s./**/setup.py", separator), "./**/*"}, utils.executions[3].parameters) | ||||
|  | ||||
| 		assert.Equal(t, "sourceanalyzer", utils.executions[4].executable) | ||||
| 		assert.Equal(t, []string{"-verbose", "-64", "-b", "test", "-scan", "-Xmx4G", "-Xms2G", "-build-label", "testLabel", "-logfile", "target/fortify-scan.log", "-f", "target/result.fpr"}, utils.executions[4].parameters) | ||||
| @@ -1046,32 +1046,38 @@ func TestPopulateMavenTranslate(t *testing.T) { | ||||
|  | ||||
| func TestPopulatePipTranslate(t *testing.T) { | ||||
| 	t.Run("PythonAdditionalPath without translate", func(t *testing.T) { | ||||
| 		config := fortifyExecuteScanOptions{PythonAdditionalPath: []string{"./lib", "."}} | ||||
| 		config := fortifyExecuteScanOptions{PythonVersion: "python2", PythonAdditionalPath: []string{"./lib", "."}} | ||||
| 		translate, err := populatePipTranslate(&config, "") | ||||
| 		separator := getSeparator() | ||||
| 		expected := fmt.Sprintf(`[{"exclude":"./**/tests/**/*%v./**/setup.py","pythonPath":"%v./lib%v.","src":"./**/*"}]`, | ||||
| 		expected := fmt.Sprintf(`[{"exclude":"./**/tests/**/*%v./**/setup.py","pythonPath":"%v./lib%v.","pythonVersion":"2","src":"./**/*"}]`, | ||||
| 			separator, separator, separator) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, expected, translate) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Invalid python version", func(t *testing.T) { | ||||
| 		config := fortifyExecuteScanOptions{PythonVersion: "python4", PythonAdditionalPath: []string{"./lib", "."}} | ||||
| 		_, err := populatePipTranslate(&config, "") | ||||
| 		assert.Error(t, err) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Src without translate", func(t *testing.T) { | ||||
| 		config := fortifyExecuteScanOptions{Src: []string{"./**/*.py"}} | ||||
| 		config := fortifyExecuteScanOptions{PythonVersion: "python3", Src: []string{"./**/*.py"}} | ||||
| 		translate, err := populatePipTranslate(&config, "") | ||||
| 		separator := getSeparator() | ||||
| 		expected := fmt.Sprintf( | ||||
| 			`[{"exclude":"./**/tests/**/*%v./**/setup.py","pythonPath":"%v","src":"./**/*.py"}]`, | ||||
| 			`[{"exclude":"./**/tests/**/*%v./**/setup.py","pythonPath":"%v","pythonVersion":"3","src":"./**/*.py"}]`, | ||||
| 			separator, separator) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, expected, translate) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("Exclude without translate", func(t *testing.T) { | ||||
| 		config := fortifyExecuteScanOptions{Exclude: []string{"./**/tests/**/*"}} | ||||
| 		config := fortifyExecuteScanOptions{PythonVersion: "python3", Exclude: []string{"./**/tests/**/*"}} | ||||
| 		translate, err := populatePipTranslate(&config, "") | ||||
| 		separator := getSeparator() | ||||
| 		expected := fmt.Sprintf( | ||||
| 			`[{"exclude":"./**/tests/**/*","pythonPath":"%v","src":"./**/*"}]`, | ||||
| 			`[{"exclude":"./**/tests/**/*","pythonPath":"%v","pythonVersion":"3","src":"./**/*"}]`, | ||||
| 			separator) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, expected, translate) | ||||
|   | ||||
| @@ -175,6 +175,9 @@ spec: | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         default: python3 | ||||
|         possibleValues: | ||||
|           - python3 | ||||
|           - python2 | ||||
|       - name: uploadResults | ||||
|         type: bool | ||||
|         description: "Whether results shall be uploaded or not" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user