Skip to content

Commit 8924b97

Browse files
feat(codeqlExecuteScan): added a feature to execute custom command (#4912)
* added running custom command * go mod tidy * added log with parsed command info * fixed log format --------- Co-authored-by: sumeet patil <[email protected]>
1 parent f7543be commit 8924b97

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

cmd/codeqlExecuteScan.go

+26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/SAP/jenkins-library/pkg/maven"
1616
"github.com/SAP/jenkins-library/pkg/piperutils"
1717
"github.com/SAP/jenkins-library/pkg/telemetry"
18+
"github.com/google/shlex"
1819
"github.com/pkg/errors"
1920
)
2021

@@ -139,6 +140,13 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem
139140
}
140141
reports = append(reports, scanReports...)
141142

143+
if len(config.CustomCommand) > 0 {
144+
err = runCustomCommand(utils, config.CustomCommand)
145+
if err != nil {
146+
return reports, err
147+
}
148+
}
149+
142150
repoInfo, err := codeql.GetRepoInfo(config.Repository, config.AnalyzedRef, config.CommitID,
143151
config.TargetGithubRepoURL, config.TargetGithubBranchName)
144152
if err != nil {
@@ -394,6 +402,24 @@ func uploadProjectToGitHub(config *codeqlExecuteScanOptions, repoInfo *codeql.Re
394402
return nil
395403
}
396404

405+
func runCustomCommand(utils codeqlExecuteScanUtils, command string) error {
406+
log.Entry().Infof("custom command will be run: %s", command)
407+
cmd, err := shlex.Split(command)
408+
if err != nil {
409+
log.Entry().WithError(err).Errorf("failed to parse custom command %s", command)
410+
return err
411+
}
412+
log.Entry().Infof("Parsed command '%s' with %d arguments: ['%s']", cmd[0], len(cmd[1:]), strings.Join(cmd[1:], "', '"))
413+
414+
err = utils.RunExecutable(cmd[0], cmd[1:]...)
415+
if err != nil {
416+
log.Entry().WithError(err).Errorf("failed to run command %s", command)
417+
return err
418+
}
419+
log.Entry().Info("Success.")
420+
return nil
421+
}
422+
397423
func checkForCompliance(scanResults []codeql.CodeqlFindings, config *codeqlExecuteScanOptions, repoInfo *codeql.RepoInfo) error {
398424
for _, scanResult := range scanResults {
399425
if scanResult.ClassificationName == codeql.AuditAll {

cmd/codeqlExecuteScan_generated.go

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ require (
177177
github.com/google/btree v1.0.1 // indirect
178178
github.com/google/go-querystring v1.1.0 // indirect
179179
github.com/google/gofuzz v1.2.0 // indirect
180-
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
180+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
181181
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
182182
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
183183
github.com/gorilla/mux v1.8.0 // indirect

resources/metadata/codeqlExecuteScan.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ spec:
233233
- STEPS
234234
- STAGES
235235
- PARAMETERS
236+
- name: customCommand
237+
type: string
238+
description: "A custom user-defined command to run between codeql analysis and results upload."
239+
scope:
240+
- STEPS
241+
- STAGES
242+
- PARAMETERS
236243
containers:
237244
- image: ""
238245
outputs:

0 commit comments

Comments
 (0)