Skip to content

Commit

Permalink
feat(codeqlExecuteScan): added logging codeql version (SAP#4271)
Browse files Browse the repository at this point in the history
* added printing codeql version

* refactored duplicated code

* added else for logging codeql version
  • Loading branch information
daskuznetsova authored Mar 14, 2023
1 parent 9774eaa commit d6d1265
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions cmd/codeqlExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ func uploadResults(config *codeqlExecuteScanOptions, utils codeqlExecuteScanUtil
}

func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telemetry.CustomData, utils codeqlExecuteScanUtils) error {
codeqlVersion, err := os.ReadFile("/etc/image-version")
if err != nil {
log.Entry().Infof("CodeQL image version: unknown")
} else {
log.Entry().Infof("CodeQL image version: %s", string(codeqlVersion))
}

var reports []piperutils.Path
cmd := []string{"database", "create", config.Database, "--overwrite", "--source-root", config.ModulePath}

Expand All @@ -184,27 +191,20 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem
return fmt.Errorf("the step could not recognize the specified buildTool %s. please specify valid buildtool", config.BuildTool)
}
}

if len(language) > 0 {
cmd = append(cmd, "--language="+language)
} else {
cmd = append(cmd, "--language="+config.Language)
}

if len(config.Threads) > 0 {
cmd = append(cmd, "--threads="+config.Threads)
}

if len(config.Ram) > 0 {
cmd = append(cmd, "--ram="+config.Ram)
}
cmd = append(cmd, getRamAndThreadsFromConfig(config)...)

//codeql has an autobuilder which tries to build the project based on specified programming language
if len(config.BuildCommand) > 0 {
cmd = append(cmd, "--command="+config.BuildCommand)
}

err := execute(utils, cmd, GeneralConfig.Verbose)
err = execute(utils, cmd, GeneralConfig.Verbose)
if err != nil {
log.Entry().Error("failed running command codeql database create")
return err
Expand All @@ -217,12 +217,7 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem

cmd = nil
cmd = append(cmd, "database", "analyze", "--format=sarif-latest", fmt.Sprintf("--output=%vtarget/codeqlReport.sarif", config.ModulePath), config.Database)
if len(config.Threads) > 0 {
cmd = append(cmd, "--threads="+config.Threads)
}
if len(config.Ram) > 0 {
cmd = append(cmd, "--ram="+config.Ram)
}
cmd = append(cmd, getRamAndThreadsFromConfig(config)...)
cmd = codeqlQuery(cmd, config.QuerySuite)
err = execute(utils, cmd, GeneralConfig.Verbose)
if err != nil {
Expand All @@ -234,12 +229,7 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem

cmd = nil
cmd = append(cmd, "database", "analyze", "--format=csv", fmt.Sprintf("--output=%vtarget/codeqlReport.csv", config.ModulePath), config.Database)
if len(config.Threads) > 0 {
cmd = append(cmd, "--threads="+config.Threads)
}
if len(config.Ram) > 0 {
cmd = append(cmd, "--ram="+config.Ram)
}
cmd = append(cmd, getRamAndThreadsFromConfig(config)...)
cmd = codeqlQuery(cmd, config.QuerySuite)
err = execute(utils, cmd, GeneralConfig.Verbose)
if err != nil {
Expand Down Expand Up @@ -353,3 +343,14 @@ func buildRepoReference(repository, analyzedRef string) (string, error) {
}
return fmt.Sprintf("%s/tree/%s", repository, ref[2]), nil
}

func getRamAndThreadsFromConfig(config *codeqlExecuteScanOptions) []string {
params := make([]string, 0, 2)
if len(config.Threads) > 0 {
params = append(params, "--threads="+config.Threads)
}
if len(config.Ram) > 0 {
params = append(params, "--ram="+config.Ram)
}
return params
}

0 comments on commit d6d1265

Please sign in to comment.