1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

checkmarxExecuteScan fixes (#3540)

* Fix FilterByTeamName and LoadExistingProject

* Fix project name loop

Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
This commit is contained in:
Adrien 2022-02-28 14:22:47 +01:00 committed by GitHub
parent 421168a169
commit a73951909b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 15 deletions

View File

@ -105,6 +105,9 @@ func runScan(config checkmarxExecuteScanOptions, sys checkmarx.System, influx *c
return err
}
} else {
if len(teamID) == 0 {
return errors.Wrap(err, "TeamName or TeamID is required to create a new project")
}
project, err = createNewProject(config, sys, projectName, teamID)
if err != nil {
return err
@ -161,10 +164,15 @@ func presetExistingProject(config checkmarxExecuteScanOptions, sys checkmarx.Sys
func loadTeam(sys checkmarx.System, teamName string) (checkmarx.Team, error) {
teams := sys.GetTeams()
team := checkmarx.Team{}
var err error
if len(teams) > 0 && len(teamName) > 0 {
return sys.FilterTeamByName(teams, teamName), nil
team, err = sys.FilterTeamByName(teams, teamName)
}
if err != nil {
return team, fmt.Errorf("failed to identify team by teamName %v", teamName)
} else {
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) {
@ -198,7 +206,19 @@ func loadExistingProject(sys checkmarx.System, initialProjectName, pullRequestNa
if len(projects) == 0 {
return checkmarx.Project{}, projectName, nil
}
project = projects[0]
if len(projects) == 1 {
project = projects[0]
} else {
for _, current_project := range projects {
if projectName == current_project.Name {
project = current_project
break
}
}
if len(project.Name) == 0 {
return project, projectName, errors.New("Cannot find project " + projectName + ". You need to provide the teamName parameter if you want a new project to be created.")
}
}
log.Entry().Debugf("Loaded project with name %v", project.Name)
}
return project, projectName, nil

View File

@ -91,11 +91,11 @@ func (sys *systemMock) GetProjectsByNameAndTeam(projectName, teamID string) ([]c
sys.previousPName = projectName
return []checkmarx.Project{}, fmt.Errorf("no project error")
}
func (sys *systemMock) FilterTeamByName(_ []checkmarx.Team, teamName string) checkmarx.Team {
func (sys *systemMock) FilterTeamByName(_ []checkmarx.Team, teamName string) (checkmarx.Team, error) {
if teamName == "OpenSource/Cracks/16" {
return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}
return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}, nil
}
return checkmarx.Team{ID: json.RawMessage(`15`), FullName: "OpenSource/Cracks/15"}
return checkmarx.Team{ID: json.RawMessage(`15`), FullName: "OpenSource/Cracks/15"}, nil
}
func (sys *systemMock) FilterTeamByID(_ []checkmarx.Team, teamID json.RawMessage) checkmarx.Team {
teamIDBytes, _ := teamID.MarshalJSON()
@ -178,8 +178,8 @@ func (sys *systemMockForExistingProject) GetProjectByID(int) (checkmarx.Project,
func (sys *systemMockForExistingProject) GetProjectsByNameAndTeam(projectName, teamID string) ([]checkmarx.Project, error) {
return []checkmarx.Project{{ID: 19, Name: projectName, TeamID: teamID, IsPublic: true}}, nil
}
func (sys *systemMockForExistingProject) FilterTeamByName([]checkmarx.Team, string) checkmarx.Team {
return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}
func (sys *systemMockForExistingProject) FilterTeamByName([]checkmarx.Team, string) (checkmarx.Team, error) {
return checkmarx.Team{ID: json.RawMessage(`"16"`), FullName: "OpenSource/Cracks/16"}, nil
}
func (sys *systemMockForExistingProject) FilterTeamByID([]checkmarx.Team, json.RawMessage) checkmarx.Team {
return checkmarx.Team{ID: json.RawMessage(`"15"`), FullName: "OpenSource/Cracks/15"}

View File

@ -188,7 +188,7 @@ type System interface {
FilterPresetByName(presets []Preset, presetName string) Preset
FilterPresetByID(presets []Preset, presetID int) Preset
FilterProjectByName(projects []Project, projectName string) Project
FilterTeamByName(teams []Team, teamName string) Team
FilterTeamByName(teams []Team, teamName string) (Team, error)
FilterTeamByID(teams []Team, teamID json.RawMessage) Team
DownloadReport(reportID int) ([]byte, error)
GetReportStatus(reportID int) (ReportStatusResponse, error)
@ -623,13 +623,13 @@ func (sys *SystemInstance) DownloadReport(reportID int) ([]byte, error) {
}
// FilterTeamByName filters a team by its name
func (sys *SystemInstance) FilterTeamByName(teams []Team, teamName string) Team {
func (sys *SystemInstance) FilterTeamByName(teams []Team, teamName string) (Team, error) {
for _, team := range teams {
if team.FullName == teamName || team.FullName == strings.ReplaceAll(teamName, `\`, `/`) {
return team
return team, nil
}
}
return Team{}
return Team{}, errors.New("Failed to find team with name " + teamName)
}
// FilterTeamByID filters a team by its ID

View File

@ -181,13 +181,13 @@ func TestGetTeams(t *testing.T) {
assert.Equal(t, "/Team/4", teams[3].FullName, "Team name 4 incorrect")
t.Run("test filter teams by name", func(t *testing.T) {
team2 := sys.FilterTeamByName(teams, "Team2")
team2, _ := sys.FilterTeamByName(teams, "Team2")
assert.Equal(t, "Team2", team2.FullName, "Team name incorrect")
assert.Equal(t, json.RawMessage([]byte(strconv.Itoa(2))), team2.ID, "Team id incorrect")
})
t.Run("test filter teams by name with backslash/forward slash", func(t *testing.T) {
team4 := sys.FilterTeamByName(teams, "\\Team\\4")
team4, _ := sys.FilterTeamByName(teams, "\\Team\\4")
assert.Equal(t, "/Team/4", team4.FullName, "Team name incorrect")
assert.Equal(t, json.RawMessage([]byte(strconv.Itoa(4))), team4.ID, "Team id incorrect")
})
@ -205,8 +205,9 @@ func TestGetTeams(t *testing.T) {
})
t.Run("test fail Filter teams by name", func(t *testing.T) {
team := sys.FilterTeamByName(teams, "Team")
team, err := sys.FilterTeamByName(teams, "Team")
assert.Equal(t, "", team.FullName, "Team name incorrect")
assert.Contains(t, fmt.Sprint(err), "Failed to find team")
})
})