diff --git a/pkg/codeql/codeql.go b/pkg/codeql/codeql.go index 2c4f45c..d4fc100 100644 --- a/pkg/codeql/codeql.go +++ b/pkg/codeql/codeql.go @@ -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 { diff --git a/pkg/queue/types.go b/pkg/queue/types.go index 534ec6d..f00cd2f 100644 --- a/pkg/queue/types.go +++ b/pkg/queue/types.go @@ -5,13 +5,15 @@ import ( "mrvacommander/pkg/common" ) +type QueryLanguage string + // AnalyzeJob represents a job specifying a repository and a query pack to analyze it with. // This is the message format that the agent receives from the queue. // TODO: make query_pack_location query_pack_url with a presigned URL type AnalyzeJob struct { Spec common.JobSpec // json:"job_spec" QueryPackLocation artifactstore.ArtifactLocation // json:"query_pack_location" - QueryLanguage string // json:"query_language" + QueryLanguage QueryLanguage // json:"query_language" } // AnalyzeResult represents the result of an analysis job. diff --git a/pkg/server/server.go b/pkg/server/server.go index bd2627b..886f02c 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -27,7 +27,7 @@ func (c *CommanderSingle) startAnalyses( analysisRepos *map[common.NameWithOwner]qldbstore.CodeQLDatabaseLocation, queryPackLocation artifactstore.ArtifactLocation, sessionId int, - queryLanguage string) { + queryLanguage queue.QueryLanguage) { slog.Debug("Queueing analysis jobs", "count", len(*analysisRepos)) @@ -629,7 +629,7 @@ func (c *CommanderSingle) buildSessionInfoResponseJson(si SessionInfo) ([]byte, Actor: actor, ControllerRepo: controllerRepo, ID: si.ID, - QueryLanguage: si.Language, + QueryLanguage: string(si.Language), QueryPackURL: si.QueryPack, CreatedAt: time.Now().Format(time.RFC3339), UpdatedAt: time.Now().Format(time.RFC3339), @@ -649,7 +649,7 @@ func (c *CommanderSingle) buildSessionInfoResponseJson(si SessionInfo) ([]byte, SessionID: si.ID, NameWithOwner: job.Spec.NameWithOwner, }, common.JobInfo{ - QueryLanguage: si.Language, + QueryLanguage: string(si.Language), CreatedAt: response.CreatedAt, UpdatedAt: response.UpdatedAt, SkippedRepositories: skippedRepositories, @@ -667,7 +667,7 @@ func (c *CommanderSingle) buildSessionInfoResponseJson(si SessionInfo) ([]byte, } -func (c *CommanderSingle) collectRequestInfoAndSaveQueryPack(w http.ResponseWriter, r *http.Request, sessionId int) (string, []common.NameWithOwner, artifactstore.ArtifactLocation, error) { +func (c *CommanderSingle) collectRequestInfoAndSaveQueryPack(w http.ResponseWriter, r *http.Request, sessionId int) (queue.QueryLanguage, []common.NameWithOwner, artifactstore.ArtifactLocation, error) { slog.Debug("Collecting session info") if r.Body == nil { @@ -708,7 +708,7 @@ func (c *CommanderSingle) collectRequestInfoAndSaveQueryPack(w http.ResponseWrit } // 2. Save the language - sessionLanguage := msg.Language + sessionLanguage := queue.QueryLanguage(msg.Language) // 3. Save the repositories var sessionRepos []common.NameWithOwner diff --git a/pkg/server/types.go b/pkg/server/types.go index f65974e..852b060 100644 --- a/pkg/server/types.go +++ b/pkg/server/types.go @@ -11,7 +11,7 @@ import ( type SessionInfo struct { ID int QueryPack string - Language string + Language queue.QueryLanguage AccessMismatchRepos []common.NameWithOwner NotFoundRepos []common.NameWithOwner NoCodeqlDBRepos []common.NameWithOwner