Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codeqlExecuteScan): auto populate github api url #4369

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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