Skip to content

Commit

Permalink
feat(codeqlExecuteScan) : auto fill api url (SAP#4369)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeetpatil authored and maxatsap committed Jul 23, 2024
1 parent 3ab26ff commit 6aec9ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/codeqlExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func runCodeqlExecuteScan(config *codeqlExecuteScanOptions, telemetryData *telem
}

if config.CheckForCompliance {
codeqlScanAuditInstance := codeql.NewCodeqlScanAuditInstance(config.GithubAPIURL, repoInfo.owner, repoInfo.repo, token, []string{})
codeqlScanAuditInstance := codeql.NewCodeqlScanAuditInstance(repoInfo.serverUrl, repoInfo.owner, repoInfo.repo, token, []string{})
scanResults, err := codeqlScanAuditInstance.GetVulnerabilities(repoInfo.ref)
if err != nil {
return reports, errors.Wrap(err, "failed to get scan results")
Expand Down
11 changes: 0 additions & 11 deletions cmd/codeqlExecuteScan_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions pkg/codeql/codeql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ type githubCodeqlScanningService interface {

const auditStateOpen = "open"

func NewCodeqlScanAuditInstance(apiURL, owner, repository, token string, trustedCerts []string) CodeqlScanAuditInstance {
return CodeqlScanAuditInstance{apiURL: apiURL, owner: owner, repository: repository, token: token, trustedCerts: trustedCerts}
func NewCodeqlScanAuditInstance(serverUrl, owner, repository, token string, trustedCerts []string) CodeqlScanAuditInstance {
return CodeqlScanAuditInstance{serverUrl: serverUrl, owner: owner, repository: repository, token: token, trustedCerts: trustedCerts}
}

type CodeqlScanAuditInstance struct {
apiURL string
serverUrl string
owner string
repository string
token string
Expand All @@ -31,7 +31,8 @@ type CodeqlScanAuditInstance struct {
}

func (codeqlScanAudit *CodeqlScanAuditInstance) GetVulnerabilities(analyzedRef string) (CodeqlScanning, error) {
ctx, client, err := sapgithub.NewClient(codeqlScanAudit.token, codeqlScanAudit.apiURL, "", codeqlScanAudit.trustedCerts)
apiUrl := getApiUrl(codeqlScanAudit.serverUrl)
ctx, client, err := sapgithub.NewClient(codeqlScanAudit.token, apiUrl, "", codeqlScanAudit.trustedCerts)
if err != nil {
return CodeqlScanning{}, err
}
Expand Down Expand Up @@ -63,3 +64,11 @@ func getVulnerabilitiesFromClient(ctx context.Context, codeScanning githubCodeql
codeqlScanning.Audited = (codeqlScanning.Total - openStateCount)
return codeqlScanning, nil
}

func getApiUrl(serverUrl string) string {
if serverUrl == "https://github.com" {
return "https://api.github.com"
}

return (serverUrl + "/api/v3")
}
10 changes: 10 additions & 0 deletions pkg/codeql/codeql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ func TestGetVulnerabilitiesFromClient(t *testing.T) {
assert.Error(t, err)
})
}

func TestGetApiUrl(t *testing.T) {
t.Run("public url", func(t *testing.T) {
assert.Equal(t, "https://api.github.com", getApiUrl("https://github.com"))
})

t.Run("enterprise github url", func(t *testing.T) {
assert.Equal(t, "https://github.test.org/api/v3", getApiUrl("https://github.test.org"))
})
}
9 changes: 0 additions & 9 deletions resources/metadata/codeqlExecuteScan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ spec:
- type: vaultSecret
default: github
name: githubVaultSecretName
- name: githubApiUrl
description: "Set the GitHub API URL."
scope:
- GENERAL
- PARAMETERS
- STAGES
- STEPS
type: string
default: "https://api.github.com"
- name: buildTool
type: string
description: Defines the build tool which is used for building the project.
Expand Down

0 comments on commit 6aec9ab

Please sign in to comment.