Introduce explicit type QueryLanguage = string and update code to clarify

Previously:
- There is confusion between nameWithOwner and queryLanguage.  Both are strings.
  Between

        runResult, err := codeql.RunQuery(databasePath, job.QueryLanguage, queryPackPath, tempDir)
    (agent.go l205)

  and

        func RunQuery(database string, nwo string, queryPackPath string, tempDir string) (*RunQueryResult, error)

  QueryLanguage is suddenly name with owner in the code.

  Added some debugging, the value is the query language in the two places it gets used:

        server         | 2024/07/03 18:30:15 DEBUG Processed request info location="{Data:map[bucket:packs key:1]}" language=cpp
        ...
        agent          | 2024/07/03 18:30:15 DEBUG XX: is nwo a name/owner, or the original callers' queryLanguage? nwo=cpp
        ...
        agent          | 2024/07/03 18:30:19 DEBUG XX: 2: is nwo a name/owner, or the original callers' queryLanguage? nwo=cpp

Changes:
- Introduce explicit type QueryLanguage = string and update code to clarify
- inline trivial function
This commit is contained in:
Michael Hohn
2024-07-03 13:30:02 -07:00
committed by =Michael Hohn
parent 07f93f3d27
commit b3cf7a4f65
4 changed files with 38 additions and 19 deletions

View File

@@ -8,6 +8,7 @@ import (
"io"
"log"
"log/slog"
"mrvacommander/pkg/queue"
"mrvacommander/utils"
"os"
"os/exec"
@@ -81,8 +82,11 @@ func addFileToZip(zipWriter *zip.Writer, filePath, zipPath string) error {
return nil
}
func RunQuery(database string, nwo string, queryPackPath string, tempDir string) (*RunQueryResult, error) {
func RunQuery(database string, language queue.QueryLanguage, queryPackPath string, tempDir string) (*RunQueryResult, error) {
path, err := getCodeQLCLIPath()
// XX: is nwo a name/owner, or the original callers' queryLanguage?
slog.Debug("XX: is nwo a name/owner, or the original callers' queryLanguage?",
"language", language)
if err != nil {
return nil, fmt.Errorf("failed to get codeql cli path: %v", err)
@@ -142,7 +146,7 @@ func RunQuery(database string, nwo string, queryPackPath string, tempDir string)
var sarifFilePath string
if shouldGenerateSarif {
sarif, err := generateSarif(codeql, nwo, databasePath, queryPackPath, databaseSHA, resultsDir)
sarif, err := generateSarif(codeql, language, databasePath, queryPackPath, databaseSHA, resultsDir)
if err != nil {
return nil, fmt.Errorf("failed to generate SARIF: %v", err)
}
@@ -347,7 +351,7 @@ func getSarifOutputType(queryMetadata QueryMetadata, compatibleQueryKinds []stri
return ""
}
func generateSarif(codeql CodeqlCli, nwo, databasePath, queryPackPath, databaseSHA string, resultsDir string) ([]byte, error) {
func generateSarif(codeql CodeqlCli, language queue.QueryLanguage, databasePath, queryPackPath, databaseSHA string, resultsDir string) ([]byte, error) {
sarifFile := filepath.Join(resultsDir, "results.sarif")
cmd := exec.Command(codeql.Path, "database", "interpret-results", "--format=sarif-latest", "--output="+sarifFile, "--sarif-add-snippets", "--no-group-results", databasePath, queryPackPath)
if output, err := cmd.CombinedOutput(); err != nil {
@@ -359,12 +363,21 @@ func generateSarif(codeql CodeqlCli, nwo, databasePath, queryPackPath, databaseS
return nil, fmt.Errorf("failed to read SARIF file: %v", err)
}
// Modify the sarif: start by extracting
var sarif Sarif
if err := json.Unmarshal(sarifData, &sarif); err != nil {
return nil, fmt.Errorf("failed to unmarshal SARIF: %v", err)
}
injectVersionControlInfo(&sarif, nwo, databaseSHA)
// now inject version control info
for _, run := range sarif.Runs {
run.VersionControlProvenance = append(run.VersionControlProvenance, map[string]interface{}{
"repositoryUri": fmt.Sprintf("%s/%s", os.Getenv("GITHUB_SERVER_URL"), language),
"revisionId": databaseSHA,
})
}
// and write it back
sarifBytes, err := json.Marshal(sarif)
if err != nil {
return nil, fmt.Errorf("failed to marshal SARIF: %v", err)
@@ -373,14 +386,18 @@ func generateSarif(codeql CodeqlCli, nwo, databasePath, queryPackPath, databaseS
return sarifBytes, nil
}
func injectVersionControlInfo(sarif *Sarif, nwo, databaseSHA string) {
for _, run := range sarif.Runs {
run.VersionControlProvenance = append(run.VersionControlProvenance, map[string]interface{}{
"repositoryUri": fmt.Sprintf("%s/%s", os.Getenv("GITHUB_SERVER_URL"), nwo),
"revisionId": databaseSHA,
})
}
}
// XX: inlined this function
// func injectVersionControlInfo(sarif *Sarif, nwo, databaseSHA string) {
// // XX: is nwo name/owner or language?
// slog.Debug("XX: 2: is nwo a name/owner, or the original callers' queryLanguage?",
// "nwo", nwo)
// for _, run := range sarif.Runs {
// run.VersionControlProvenance = append(run.VersionControlProvenance, map[string]interface{}{
// "repositoryUri": fmt.Sprintf("%s/%s", os.Getenv("GITHUB_SERVER_URL"), nwo),
// "revisionId": databaseSHA,
// })
// }
// }
// getSarifResultCount returns the number of results in the SARIF file.
func getSarifResultCount(sarif []byte) int {