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 
			
		
		
		
	checkmarxExecuteScan: adapt to 9.2 api (#2363)
* Update checkmarxExecuteScan.go * api mods * Switch default * Fix decode * mod marshalling * Fix unmarshalling * Code fmt and small fix * Optimize preset handling * Integer handling * Fix test * cleanup * go fmt * Improve test
This commit is contained in:
		| @@ -13,6 +13,7 @@ import ( | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"encoding/json" | ||||
| 	"encoding/xml" | ||||
|  | ||||
| 	"github.com/SAP/jenkins-library/pkg/checkmarx" | ||||
| @@ -38,26 +39,40 @@ func checkmarxExecuteScan(config checkmarxExecuteScanOptions, telemetryData *tel | ||||
| } | ||||
|  | ||||
| func runScan(config checkmarxExecuteScanOptions, sys checkmarx.System, workspace string, influx *checkmarxExecuteScanInflux) error { | ||||
|  | ||||
| 	team, err := loadTeam(sys, config.TeamName, config.TeamID) | ||||
| 	if err != nil { | ||||
| 		return errors.Wrap(err, "failed to load team") | ||||
| 	teamID := config.TeamID | ||||
| 	if len(teamID) == 0 { | ||||
| 		team, err := loadTeam(sys, config.TeamName) | ||||
| 		if err != nil { | ||||
| 			return errors.Wrap(err, "failed to load team") | ||||
| 		} | ||||
| 		teamIDBytes, _ := team.ID.MarshalJSON() | ||||
| 		err = json.Unmarshal(teamIDBytes, &teamID) | ||||
| 		if err != nil { | ||||
| 			var teamIDInt int | ||||
| 			err = json.Unmarshal(teamIDBytes, &teamIDInt) | ||||
| 			if err != nil { | ||||
| 				return errors.Wrap(err, "failed to unmarshall team.ID") | ||||
| 			} | ||||
| 			teamID = strconv.Itoa(teamIDInt) | ||||
| 		} | ||||
| 	} | ||||
| 	project, projectName, err := loadExistingProject(sys, config.ProjectName, config.PullRequestName, team.ID) | ||||
| 	project, projectName, err := loadExistingProject(sys, config.ProjectName, config.PullRequestName, teamID) | ||||
| 	if err != nil { | ||||
| 		return errors.Wrap(err, "error when trying to load project") | ||||
| 	} | ||||
| 	if project.Name == projectName { | ||||
| 		log.Entry().Infof("Project %v exists...", projectName) | ||||
| 		if len(config.Preset) > 0 { | ||||
| 			err := setPresetForProject(sys, project.ID, projectName, config.Preset, config.SourceEncoding) | ||||
| 			presetID, _ := strconv.Atoi(config.Preset) | ||||
| 			err := setPresetForProject(sys, project.ID, presetID, projectName, config.Preset, config.SourceEncoding) | ||||
| 			if err != nil { | ||||
| 				return errors.Wrapf(err, "failed to set preset %v for project %v", config.Preset, projectName) | ||||
| 			} | ||||
| 		} | ||||
| 	} else { | ||||
| 		log.Entry().Infof("Project %v does not exist, starting to create it...", projectName) | ||||
| 		project, err = createAndConfigureNewProject(sys, projectName, team.ID, config.Preset, config.SourceEncoding) | ||||
| 		presetID, _ := strconv.Atoi(config.Preset) | ||||
| 		project, err = createAndConfigureNewProject(sys, projectName, teamID, presetID, config.Preset, config.SourceEncoding) | ||||
| 		if err != nil { | ||||
| 			return errors.Wrapf(err, "failed to create and configure new project %v", projectName) | ||||
| 		} | ||||
| @@ -70,20 +85,13 @@ func runScan(config checkmarxExecuteScanOptions, sys checkmarx.System, workspace | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func loadTeam(sys checkmarx.System, teamName, teamID string) (checkmarx.Team, error) { | ||||
| func loadTeam(sys checkmarx.System, teamName string) (checkmarx.Team, error) { | ||||
| 	teams := sys.GetTeams() | ||||
| 	team := checkmarx.Team{} | ||||
| 	if len(teams) > 0 { | ||||
| 		if len(teamName) > 0 { | ||||
| 			team = sys.FilterTeamByName(teams, teamName) | ||||
| 		} else { | ||||
| 			team = sys.FilterTeamByID(teams, teamID) | ||||
| 		} | ||||
| 	if len(teams) > 0 && len(teamName) > 0 { | ||||
| 		return sys.FilterTeamByName(teams, teamName), nil | ||||
| 	} | ||||
| 	if len(team.ID) == 0 { | ||||
| 		return team, fmt.Errorf("failed to identify team by teamName %v as well as by checkmarxGroupId %v", teamName, teamID) | ||||
| 	} | ||||
| 	return team, nil | ||||
| 	return team, fmt.Errorf("failed to identify team by teamName %v", teamName) | ||||
| } | ||||
|  | ||||
| func loadExistingProject(sys checkmarx.System, initialProjectName, pullRequestName, teamID string) (checkmarx.Project, string, error) { | ||||
| @@ -411,7 +419,7 @@ func enforceThresholds(config checkmarxExecuteScanOptions, results map[string]in | ||||
| 	return insecure | ||||
| } | ||||
|  | ||||
| func createAndConfigureNewProject(sys checkmarx.System, projectName, teamID, presetValue, engineConfiguration string) (checkmarx.Project, error) { | ||||
| func createAndConfigureNewProject(sys checkmarx.System, projectName, teamID string, presetIDValue int, presetValue, engineConfiguration string) (checkmarx.Project, error) { | ||||
| 	if len(presetValue) == 0 { | ||||
| 		log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 		return checkmarx.Project{}, fmt.Errorf("preset not specified, creation of project %v failed", projectName) | ||||
| @@ -422,7 +430,7 @@ func createAndConfigureNewProject(sys checkmarx.System, projectName, teamID, pre | ||||
| 		return checkmarx.Project{}, errors.Wrapf(err, "cannot create project %v", projectName) | ||||
| 	} | ||||
|  | ||||
| 	if err := setPresetForProject(sys, projectCreateResult.ID, projectName, presetValue, engineConfiguration); err != nil { | ||||
| 	if err := setPresetForProject(sys, projectCreateResult.ID, presetIDValue, projectName, presetValue, engineConfiguration); err != nil { | ||||
| 		return checkmarx.Project{}, errors.Wrapf(err, "failed to set preset %v for project", presetValue) | ||||
| 	} | ||||
|  | ||||
| @@ -440,22 +448,13 @@ func createAndConfigureNewProject(sys checkmarx.System, projectName, teamID, pre | ||||
| func loadPreset(sys checkmarx.System, presetValue string) (checkmarx.Preset, error) { | ||||
| 	presets := sys.GetPresets() | ||||
| 	var preset checkmarx.Preset | ||||
| 	presetID, err := strconv.Atoi(presetValue) | ||||
| 	var configuredPresetID int | ||||
| 	var configuredPresetName string | ||||
| 	if err != nil { | ||||
| 		preset = sys.FilterPresetByName(presets, presetValue) | ||||
| 		configuredPresetName = presetValue | ||||
| 	} else { | ||||
| 		preset = sys.FilterPresetByID(presets, presetID) | ||||
| 		configuredPresetID = presetID | ||||
| 	} | ||||
|  | ||||
| 	if configuredPresetID > 0 && preset.ID == configuredPresetID || len(configuredPresetName) > 0 && preset.Name == configuredPresetName { | ||||
| 	preset = sys.FilterPresetByName(presets, presetValue) | ||||
| 	configuredPresetName = presetValue | ||||
| 	if len(configuredPresetName) > 0 && preset.Name == configuredPresetName { | ||||
| 		log.Entry().Infof("Loaded preset %v", preset.Name) | ||||
| 		return preset, nil | ||||
| 	} | ||||
|  | ||||
| 	log.Entry().Infof("Preset '%s' not found. Available presets are:", presetValue) | ||||
| 	for _, prs := range presets { | ||||
| 		log.Entry().Infof("preset id: %v, name: '%v'", prs.ID, prs.Name) | ||||
| @@ -465,13 +464,16 @@ func loadPreset(sys checkmarx.System, presetValue string) (checkmarx.Preset, err | ||||
|  | ||||
| // setPresetForProject is only called when it has already been established that the preset needs to be set. | ||||
| // It will exit via the logging framework in case the preset could be found, or the project could not be updated. | ||||
| func setPresetForProject(sys checkmarx.System, projectID int, projectName, presetValue, engineConfiguration string) error { | ||||
| 	preset, err := loadPreset(sys, presetValue) | ||||
| 	if err != nil { | ||||
| 		return errors.Wrapf(err, "preset %v not found, configuration of project %v failed", presetValue, projectName) | ||||
| func setPresetForProject(sys checkmarx.System, projectID, presetIDValue int, projectName, presetValue, engineConfiguration string) error { | ||||
| 	presetID := presetIDValue | ||||
| 	if presetID <= 0 { | ||||
| 		preset, err := loadPreset(sys, presetValue) | ||||
| 		if err != nil { | ||||
| 			return errors.Wrapf(err, "preset %v not found, configuration of project %v failed", presetValue, projectName) | ||||
| 		} | ||||
| 		presetID = preset.ID | ||||
| 	} | ||||
|  | ||||
| 	err = sys.UpdateProjectConfiguration(projectID, preset.ID, engineConfiguration) | ||||
| 	err := sys.UpdateProjectConfiguration(projectID, presetID, engineConfiguration) | ||||
| 	if err != nil { | ||||
| 		return errors.Wrapf(err, "updating configuration of project %v failed", projectName) | ||||
| 	} | ||||
|   | ||||
| @@ -231,7 +231,7 @@ thresholds instead of ` + "`" + `percentage` + "`" + ` whereas we strongly recom | ||||
| } | ||||
|  | ||||
| func addCheckmarxExecuteScanFlags(cmd *cobra.Command, stepConfig *checkmarxExecuteScanOptions) { | ||||
| 	cmd.Flags().BoolVar(&stepConfig.AvoidDuplicateProjectScans, "avoidDuplicateProjectScans", false, "Whether duplicate scans of the same project state shall be avoided or not") | ||||
| 	cmd.Flags().BoolVar(&stepConfig.AvoidDuplicateProjectScans, "avoidDuplicateProjectScans", true, "Whether duplicate scans of the same project state shall be avoided or not") | ||||
| 	cmd.Flags().StringVar(&stepConfig.FilterPattern, "filterPattern", `!**/node_modules/**, !**/.xmake/**, !**/*_test.go, !**/vendor/**/*.go, **/*.html, **/*.xml, **/*.go, **/*.py, **/*.js, **/*.scala, **/*.ts`, "The filter pattern used to zip the files relevant for scanning, patterns can be negated by setting an exclamation mark in front i.e. `!test/*.js` would avoid adding any javascript files located in the test directory") | ||||
| 	cmd.Flags().StringVar(&stepConfig.FullScanCycle, "fullScanCycle", `5`, "Indicates how often a full scan should happen between the incremental scans when activated") | ||||
| 	cmd.Flags().BoolVar(&stepConfig.FullScansScheduled, "fullScansScheduled", true, "Whether full scans are to be scheduled or not. Should be used in relation with `incremental` and `fullScanCycle`") | ||||
|   | ||||
| @@ -2,10 +2,12 @@ package cmd | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| @@ -43,15 +45,20 @@ func (fi fileInfo) Sys() interface{} { | ||||
| } | ||||
|  | ||||
| type systemMock struct { | ||||
| 	response      interface{} | ||||
| 	isIncremental bool | ||||
| 	isPublic      bool | ||||
| 	forceScan     bool | ||||
| 	createProject bool | ||||
| 	previousPName string | ||||
| 	response                         interface{} | ||||
| 	isIncremental                    bool | ||||
| 	isPublic                         bool | ||||
| 	forceScan                        bool | ||||
| 	createProject                    bool | ||||
| 	previousPName                    string | ||||
| 	getPresetsCalled                 bool | ||||
| 	updateProjectConfigurationCalled bool | ||||
| } | ||||
|  | ||||
| func (sys *systemMock) FilterPresetByName(presets []checkmarx.Preset, presetName string) checkmarx.Preset { | ||||
| 	if presetName == "CX_Default" { | ||||
| 		return checkmarx.Preset{ID: 16, Name: "CX_Default", OwnerName: "16"} | ||||
| 	} | ||||
| 	return checkmarx.Preset{ID: 10050, Name: "SAP_JS_Default", OwnerName: "16"} | ||||
| } | ||||
| func (sys *systemMock) FilterPresetByID(presets []checkmarx.Preset, presetID int) checkmarx.Preset { | ||||
| @@ -81,10 +88,17 @@ func (sys *systemMock) GetProjectsByNameAndTeam(projectName, teamID string) ([]c | ||||
| 	return []checkmarx.Project{}, fmt.Errorf("no project error") | ||||
| } | ||||
| func (sys *systemMock) FilterTeamByName(teams []checkmarx.Team, teamName string) checkmarx.Team { | ||||
| 	return checkmarx.Team{ID: "16", FullName: "OpenSource/Cracks/16"} | ||||
| 	if teamName == "OpenSource/Cracks/16" { | ||||
| 		return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"} | ||||
| 	} | ||||
| 	return checkmarx.Team{ID: json.RawMessage(`15`), FullName: "OpenSource/Cracks/15"} | ||||
| } | ||||
| func (sys *systemMock) FilterTeamByID(teams []checkmarx.Team, teamID string) checkmarx.Team { | ||||
| 	return checkmarx.Team{ID: "15", FullName: "OpenSource/Cracks/15"} | ||||
| func (sys *systemMock) FilterTeamByID(teams []checkmarx.Team, teamID json.RawMessage) checkmarx.Team { | ||||
| 	teamIDBytes, _ := teamID.MarshalJSON() | ||||
| 	if bytes.Compare(teamIDBytes, []byte(`"16"`)) == 0 { | ||||
| 		return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"} | ||||
| 	} | ||||
| 	return checkmarx.Team{ID: json.RawMessage(`15`), FullName: "OpenSource/Cracks/15"} | ||||
| } | ||||
| func (sys *systemMock) DownloadReport(reportID int) ([]byte, error) { | ||||
| 	return sys.response.([]byte), nil | ||||
| @@ -111,6 +125,7 @@ func (sys *systemMock) ScanProject(projectID int, isIncrementalV, isPublicV, for | ||||
| 	return checkmarx.Scan{ID: 16}, nil | ||||
| } | ||||
| func (sys *systemMock) UpdateProjectConfiguration(projectID int, presetID int, engineConfigurationID string) error { | ||||
| 	sys.updateProjectConfigurationCalled = true | ||||
| 	return nil | ||||
| } | ||||
| func (sys *systemMock) UpdateProjectExcludeSettings(projectID int, excludeFolders string, excludeFiles string) error { | ||||
| @@ -126,13 +141,14 @@ func (sys *systemMock) CreateBranch(projectID int, branchName string) int { | ||||
| 	return 18 | ||||
| } | ||||
| func (sys *systemMock) GetPresets() []checkmarx.Preset { | ||||
| 	return []checkmarx.Preset{{ID: 10078, Name: "SAP Java Default", OwnerName: "16"}, {ID: 10048, Name: "SAP JS Default", OwnerName: "16"}} | ||||
| 	sys.getPresetsCalled = true | ||||
| 	return []checkmarx.Preset{{ID: 10078, Name: "SAP Java Default", OwnerName: "16"}, {ID: 10048, Name: "SAP JS Default", OwnerName: "16"}, {ID: 16, Name: "CX_Default", OwnerName: "16"}} | ||||
| } | ||||
| func (sys *systemMock) GetProjects() ([]checkmarx.Project, error) { | ||||
| 	return []checkmarx.Project{{ID: 15, Name: "OtherTest", TeamID: "16"}, {ID: 1, Name: "Test", TeamID: "16"}}, nil | ||||
| } | ||||
| func (sys *systemMock) GetTeams() []checkmarx.Team { | ||||
| 	return []checkmarx.Team{{ID: "16", FullName: "OpenSource/Cracks/16"}, {ID: "15", FullName: "OpenSource/Cracks/15"}} | ||||
| 	return []checkmarx.Team{{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}, {ID: json.RawMessage(`15`), FullName: "OpenSource/Cracks/15"}} | ||||
| } | ||||
|  | ||||
| type systemMockForExistingProject struct { | ||||
| @@ -159,10 +175,10 @@ func (sys *systemMockForExistingProject) GetProjectsByNameAndTeam(projectName, t | ||||
| 	return []checkmarx.Project{{ID: 19, Name: projectName, TeamID: teamID, IsPublic: true}}, nil | ||||
| } | ||||
| func (sys *systemMockForExistingProject) FilterTeamByName(teams []checkmarx.Team, teamName string) checkmarx.Team { | ||||
| 	return checkmarx.Team{ID: "16", FullName: "OpenSource/Cracks/16"} | ||||
| 	return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"} | ||||
| } | ||||
| func (sys *systemMockForExistingProject) FilterTeamByID(teams []checkmarx.Team, teamID string) checkmarx.Team { | ||||
| 	return checkmarx.Team{ID: "15", FullName: "OpenSource/Cracks/15"} | ||||
| func (sys *systemMockForExistingProject) FilterTeamByID(teams []checkmarx.Team, teamID json.RawMessage) checkmarx.Team { | ||||
| 	return checkmarx.Team{ID: json.RawMessage(`"15"`), FullName: "OpenSource/Cracks/15"} | ||||
| } | ||||
| func (sys *systemMockForExistingProject) DownloadReport(reportID int) ([]byte, error) { | ||||
| 	return sys.response.([]byte), nil | ||||
| @@ -205,13 +221,13 @@ func (sys *systemMockForExistingProject) CreateBranch(projectID int, branchName | ||||
| 	return 0 | ||||
| } | ||||
| func (sys *systemMockForExistingProject) GetPresets() []checkmarx.Preset { | ||||
| 	return []checkmarx.Preset{{ID: 10078, Name: "SAP Java Default", OwnerName: "16"}, {ID: 10048, Name: "SAP JS Default", OwnerName: "16"}} | ||||
| 	return []checkmarx.Preset{{ID: 10078, Name: "SAP_Java_Default", OwnerName: "16"}, {ID: 10048, Name: "SAP_JS_Default", OwnerName: "16"}} | ||||
| } | ||||
| func (sys *systemMockForExistingProject) GetProjects() ([]checkmarx.Project, error) { | ||||
| 	return []checkmarx.Project{{ID: 1, Name: "TestExisting", TeamID: "16"}}, nil | ||||
| } | ||||
| func (sys *systemMockForExistingProject) GetTeams() []checkmarx.Team { | ||||
| 	return []checkmarx.Team{{ID: "16", FullName: "OpenSource/Cracks/16"}, {ID: "15", FullName: "OpenSource/Cracks/15"}} | ||||
| 	return []checkmarx.Team{{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}, {ID: json.RawMessage(`"15"`), FullName: "OpenSource/Cracks/15"}} | ||||
| } | ||||
|  | ||||
| func TestFilterFileGlob(t *testing.T) { | ||||
| @@ -321,9 +337,26 @@ func TestRunScan(t *testing.T) { | ||||
| 	assert.Equal(t, true, sys.scanProjectCalled, "ScanProject was not invoked") | ||||
| } | ||||
|  | ||||
| func TestSetPresetForProjectWithIDProvided(t *testing.T) { | ||||
| 	sys := &systemMock{} | ||||
| 	err := setPresetForProject(sys, 12345, 16, "testProject", "CX_Default", "") | ||||
| 	assert.NoError(t, err, "error occured but none expected") | ||||
| 	assert.Equal(t, false, sys.getPresetsCalled, "GetPresets was called") | ||||
| 	assert.Equal(t, true, sys.updateProjectConfigurationCalled, "UpdateProjectConfiguration was not called") | ||||
| } | ||||
|  | ||||
| func TestSetPresetForProjectWithNameProvided(t *testing.T) { | ||||
| 	sys := &systemMock{} | ||||
| 	presetID, _ := strconv.Atoi("CX_Default") | ||||
| 	err := setPresetForProject(sys, 12345, presetID, "testProject", "CX_Default", "") | ||||
| 	assert.NoError(t, err, "error occured but none expected") | ||||
| 	assert.Equal(t, true, sys.getPresetsCalled, "GetPresets was not called") | ||||
| 	assert.Equal(t, true, sys.updateProjectConfigurationCalled, "UpdateProjectConfiguration was not called") | ||||
| } | ||||
|  | ||||
| func TestVerifyOnly(t *testing.T) { | ||||
| 	sys := &systemMockForExistingProject{response: []byte(`<?xml version="1.0" encoding="utf-8"?><CxXMLResults />`)} | ||||
| 	options := checkmarxExecuteScanOptions{VerifyOnly: true, ProjectName: "TestExisting", VulnerabilityThresholdUnit: "absolute", FullScanCycle: "2", Incremental: true, FullScansScheduled: true, Preset: "10048", TeamID: "16", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true} | ||||
| 	options := checkmarxExecuteScanOptions{VerifyOnly: true, ProjectName: "TestExisting", VulnerabilityThresholdUnit: "absolute", FullScanCycle: "2", Incremental: true, FullScansScheduled: true, Preset: "10048", TeamName: "OpenSource/Cracks/15", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true} | ||||
| 	workspace, err := ioutil.TempDir("", "workspace1") | ||||
| 	if err != nil { | ||||
| 		t.Fatal("Failed to create temporary workspace directory") | ||||
| @@ -377,7 +410,7 @@ func TestRunScanForPullRequest(t *testing.T) { | ||||
|  | ||||
| func TestRunScanForPullRequestProjectNew(t *testing.T) { | ||||
| 	sys := &systemMock{response: []byte(`<?xml version="1.0" encoding="utf-8"?><CxXMLResults />`), createProject: true} | ||||
| 	options := checkmarxExecuteScanOptions{PullRequestName: "PR-17", ProjectName: "Test", VulnerabilityThresholdUnit: "percentage", FullScanCycle: "3", Incremental: true, FullScansScheduled: true, Preset: "10048", TeamName: "OpenSource/Cracks/15", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true} | ||||
| 	options := checkmarxExecuteScanOptions{PullRequestName: "PR-17", ProjectName: "Test", AvoidDuplicateProjectScans: true, VulnerabilityThresholdUnit: "percentage", FullScanCycle: "3", Incremental: true, FullScansScheduled: true, Preset: "10048", TeamName: "OpenSource/Cracks/15", VulnerabilityThresholdEnabled: true, GeneratePdfReport: true} | ||||
| 	workspace, err := ioutil.TempDir("", "workspace4") | ||||
| 	if err != nil { | ||||
| 		t.Fatal("Failed to create temporary workspace directory") | ||||
| @@ -388,9 +421,10 @@ func TestRunScanForPullRequestProjectNew(t *testing.T) { | ||||
| 	influx := checkmarxExecuteScanInflux{} | ||||
|  | ||||
| 	err = runScan(options, sys, workspace, &influx) | ||||
| 	assert.NoError(t, err, "Unexpected error caught") | ||||
| 	assert.Equal(t, true, sys.isIncremental, "isIncremental has wrong value") | ||||
| 	assert.Equal(t, true, sys.isPublic, "isPublic has wrong value") | ||||
| 	assert.Equal(t, true, sys.forceScan, "forceScan has wrong value") | ||||
| 	assert.Equal(t, false, sys.forceScan, "forceScan has wrong value") | ||||
| } | ||||
|  | ||||
| func TestRunScanHighViolationPercentage(t *testing.T) { | ||||
| @@ -538,11 +572,6 @@ func TestEnforceThresholds(t *testing.T) { | ||||
|  | ||||
| func TestLoadPreset(t *testing.T) { | ||||
| 	sys := &systemMock{} | ||||
| 	t.Run("resolve via code", func(t *testing.T) { | ||||
| 		preset, err := loadPreset(sys, "10048") | ||||
| 		assert.NoError(t, err, "Expected success but failed") | ||||
| 		assert.Equal(t, 10048, preset.ID, "Expected result but got none") | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("resolve via name", func(t *testing.T) { | ||||
| 		preset, err := loadPreset(sys, "SAP_JS_Default") | ||||
|   | ||||
| @@ -435,7 +435,7 @@ func getMtaID(mtaYamlFile string, utils mtaBuildUtils) (string, error) { | ||||
|  | ||||
| 	id, ok := result["ID"].(string) | ||||
| 	if !ok || len(id) == 0 { | ||||
| 		fmt.Errorf("Id not found in mta yaml file (or wrong type)") | ||||
| 		return "", fmt.Errorf("Id not found in mta yaml file (or wrong type)") | ||||
| 	} | ||||
|  | ||||
| 	return id, nil | ||||
|   | ||||
| @@ -110,8 +110,8 @@ type Project struct { | ||||
|  | ||||
| // Team - Team Structure | ||||
| type Team struct { | ||||
| 	ID       string `json:"id"` | ||||
| 	FullName string `json:"fullName"` | ||||
| 	ID       json.RawMessage `json:"id"` | ||||
| 	FullName string          `json:"fullName"` | ||||
| } | ||||
|  | ||||
| // Links - Links Structure | ||||
| @@ -186,7 +186,7 @@ type System interface { | ||||
| 	FilterPresetByID(presets []Preset, presetID int) Preset | ||||
| 	FilterProjectByName(projects []Project, projectName string) Project | ||||
| 	FilterTeamByName(teams []Team, teamName string) Team | ||||
| 	FilterTeamByID(teams []Team, teamID string) Team | ||||
| 	FilterTeamByID(teams []Team, teamID json.RawMessage) Team | ||||
| 	DownloadReport(reportID int) ([]byte, error) | ||||
| 	GetReportStatus(reportID int) (ReportStatusResponse, error) | ||||
| 	RequestNewReport(scanID int, reportType string) (Report, error) | ||||
| @@ -197,7 +197,7 @@ type System interface { | ||||
| 	UpdateProjectConfiguration(projectID int, presetID int, engineConfigurationID string) error | ||||
| 	UpdateProjectExcludeSettings(projectID int, excludeFolders string, excludeFiles string) error | ||||
| 	UploadProjectSourceCode(projectID int, zipFile string) error | ||||
| 	CreateProject(projectName string, teamID string) (ProjectCreateResult, error) | ||||
| 	CreateProject(projectName, teamID string) (ProjectCreateResult, error) | ||||
| 	CreateBranch(projectID int, branchName string) int | ||||
| 	GetPresets() []Preset | ||||
| 	GetProjectByID(projectID int) (Project, error) | ||||
| @@ -353,7 +353,7 @@ func (sys *SystemInstance) GetProjectsByNameAndTeam(projectName, teamID string) | ||||
| } | ||||
|  | ||||
| // CreateProject creates a new project in the Checkmarx backend | ||||
| func (sys *SystemInstance) CreateProject(projectName string, teamID string) (ProjectCreateResult, error) { | ||||
| func (sys *SystemInstance) CreateProject(projectName, teamID string) (ProjectCreateResult, error) { | ||||
| 	var result ProjectCreateResult | ||||
| 	jsonData := map[string]interface{}{ | ||||
| 		"name":       projectName, | ||||
| @@ -630,9 +630,11 @@ func (sys *SystemInstance) FilterTeamByName(teams []Team, teamName string) Team | ||||
| } | ||||
|  | ||||
| // FilterTeamByID filters a team by its ID | ||||
| func (sys *SystemInstance) FilterTeamByID(teams []Team, teamID string) Team { | ||||
| func (sys *SystemInstance) FilterTeamByID(teams []Team, teamID json.RawMessage) Team { | ||||
| 	teamIDBytes1, _ := teamID.MarshalJSON() | ||||
| 	for _, team := range teams { | ||||
| 		if team.ID == teamID { | ||||
| 		teamIDBytes2, _ := team.ID.MarshalJSON() | ||||
| 		if bytes.Compare(teamIDBytes1, teamIDBytes2) == 0 { | ||||
| 			return team | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -2,11 +2,13 @@ package checkmarx | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"encoding/json" | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
|  | ||||
| @@ -165,7 +167,7 @@ func TestGetTeams(t *testing.T) { | ||||
| 	logger := log.Entry().WithField("package", "SAP/jenkins-library/pkg/checkmarx_test") | ||||
| 	opts := piperHttp.ClientOptions{} | ||||
| 	t.Run("test success", func(t *testing.T) { | ||||
| 		myTestClient := senderMock{responseBody: `[{"id":"1", "fullName":"Team1"}, {"id":"2", "fullName":"Team2"}, {"id":"3", "fullName":"Team3"}]`, httpStatusCode: 200} | ||||
| 		myTestClient := senderMock{responseBody: `[{"id":"1", "fullName":"Team1"}, {"id":2, "fullName":"Team2"}, {"id":3, "fullName":"Team3"}]`, httpStatusCode: 200} | ||||
| 		sys := SystemInstance{serverURL: "https://cx.server.com", client: &myTestClient, logger: logger} | ||||
| 		myTestClient.SetOptions(opts) | ||||
|  | ||||
| @@ -180,13 +182,19 @@ func TestGetTeams(t *testing.T) { | ||||
| 		t.Run("test filter teams by name", func(t *testing.T) { | ||||
| 			team2 := sys.FilterTeamByName(teams, "Team2") | ||||
| 			assert.Equal(t, "Team2", team2.FullName, "Team name incorrect") | ||||
| 			assert.Equal(t, "2", team2.ID, "Team id incorrect") | ||||
| 			assert.Equal(t, json.RawMessage([]byte(strconv.Itoa(2))), team2.ID, "Team id incorrect") | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("test Filter teams by ID", func(t *testing.T) { | ||||
| 			team1 := sys.FilterTeamByID(teams, "1") | ||||
| 			team1 := sys.FilterTeamByID(teams, json.RawMessage(`"1"`)) | ||||
| 			assert.Equal(t, "Team1", team1.FullName, "Team name incorrect") | ||||
| 			assert.Equal(t, "1", team1.ID, "Team id incorrect") | ||||
| 			assert.Equal(t, json.RawMessage(`"1"`), team1.ID, "Team id incorrect") | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("test Filter teams by numeric ID", func(t *testing.T) { | ||||
| 			team3 := sys.FilterTeamByID(teams, json.RawMessage(`3`)) | ||||
| 			assert.Equal(t, "Team3", team3.FullName, "Team name incorrect") | ||||
| 			assert.Equal(t, json.RawMessage(`3`), team3.ID, "Team id incorrect") | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("test fail Filter teams by name", func(t *testing.T) { | ||||
|   | ||||
| @@ -30,7 +30,7 @@ spec: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|           - STEPS | ||||
|         default: false | ||||
|         default: true | ||||
|       - name: filterPattern | ||||
|         type: string | ||||
|         description: The filter pattern used to zip the files relevant for scanning, patterns can be negated by setting an exclamation mark in front i.e. `!test/*.js` would avoid adding any javascript files located in the test directory | ||||
|   | ||||
		Reference in New Issue
	
	Block a user