|
| 1 | +package diagnostics |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type sourceStruct struct { |
| 13 | + Id string `json:"id"` |
| 14 | + Name string `json:"name"` |
| 15 | + ExtractorName string `json:"extractorName"` |
| 16 | +} |
| 17 | + |
| 18 | +type diagnosticSeverity string |
| 19 | + |
| 20 | +const ( |
| 21 | + severityError diagnosticSeverity = "error" |
| 22 | + severityWarning diagnosticSeverity = "warning" |
| 23 | + severityNote diagnosticSeverity = "note" |
| 24 | +) |
| 25 | + |
| 26 | +type visibilityStruct struct { |
| 27 | + StatusPage bool `json:"statusPage"` // True if the message should be displayed on the status page (defaults to false) |
| 28 | + CliSummaryTable bool `json:"cliSummaryTable"` // True if the message should be counted in the diagnostics summary table printed by `codeql database analyze` (defaults to false) |
| 29 | + Telemetry bool `json:"telemetry"` // True if the message should be sent to telemetry (defaults to false) |
| 30 | +} |
| 31 | + |
| 32 | +var fullVisibility *visibilityStruct = &visibilityStruct{true, true, true} |
| 33 | + |
| 34 | +type locationStruct struct { |
| 35 | + File string `json:"file,omitempty"` |
| 36 | + StartLine int `json:"startLine,omitempty"` |
| 37 | + StartColumn int `json:"startColumn,omitempty"` |
| 38 | + EndLine int `json:"endLine,omitempty"` |
| 39 | + EndColumn int `json:"endColumn,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +var noLocation *locationStruct = nil |
| 43 | + |
| 44 | +type diagnostic struct { |
| 45 | + Timestamp string `json:"timestamp"` |
| 46 | + Source sourceStruct `json:"source"` |
| 47 | + MarkdownMessage string `json:"markdownMessage"` |
| 48 | + Severity string `json:"severity"` |
| 49 | + Visibility *visibilityStruct `json:"visibility,omitempty"` // Use a pointer so that it is omitted if nil |
| 50 | + Location *locationStruct `json:"location,omitempty"` // Use a pointer so that it is omitted if nil |
| 51 | +} |
| 52 | + |
| 53 | +var diagnosticsEmitted, diagnosticsLimit uint = 0, 100 |
| 54 | +var noDiagnosticDirPrinted bool = false |
| 55 | + |
| 56 | +func emitDiagnostic(sourceid, sourcename, markdownMessage string, severity diagnosticSeverity, visibility *visibilityStruct, location *locationStruct) { |
| 57 | + if diagnosticsEmitted < diagnosticsLimit { |
| 58 | + diagnosticsEmitted += 1 |
| 59 | + |
| 60 | + diagnosticDir := os.Getenv("CODEQL_EXTRACTOR_GO_DIAGNOSTIC_DIR") |
| 61 | + if diagnosticDir == "" { |
| 62 | + if !noDiagnosticDirPrinted { |
| 63 | + log.Println("No diagnostic directory set, so not emitting diagnostic") |
| 64 | + noDiagnosticDirPrinted = true |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000") + "Z" |
| 70 | + |
| 71 | + var d diagnostic |
| 72 | + |
| 73 | + if diagnosticsEmitted < diagnosticsLimit { |
| 74 | + d = diagnostic{ |
| 75 | + timestamp, |
| 76 | + sourceStruct{sourceid, sourcename, "go"}, |
| 77 | + markdownMessage, |
| 78 | + string(severity), |
| 79 | + visibility, |
| 80 | + location, |
| 81 | + } |
| 82 | + } else { |
| 83 | + d = diagnostic{ |
| 84 | + timestamp, |
| 85 | + sourceStruct{"go/autobuilder/diagnostic-limit-reached", "Diagnostics limit exceeded", "go"}, |
| 86 | + fmt.Sprintf("CodeQL has produced more than the maximum number of diagnostics. Only the first %d have been reported.", diagnosticsLimit), |
| 87 | + string(severityWarning), |
| 88 | + fullVisibility, |
| 89 | + noLocation, |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + content, err := json.Marshal(d) |
| 94 | + if err != nil { |
| 95 | + log.Println(err) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + targetFile, err := os.CreateTemp(diagnosticDir, "go-extractor.*.json") |
| 100 | + if err != nil { |
| 101 | + log.Println("Failed to create diagnostic file: ") |
| 102 | + log.Println(err) |
| 103 | + return |
| 104 | + } |
| 105 | + defer func() { |
| 106 | + if err := targetFile.Close(); err != nil { |
| 107 | + log.Println("Failed to close diagnostic file:") |
| 108 | + log.Println(err) |
| 109 | + } |
| 110 | + }() |
| 111 | + |
| 112 | + _, err = targetFile.Write(content) |
| 113 | + if err != nil { |
| 114 | + log.Println("Failed to write to diagnostic file: ") |
| 115 | + log.Println(err) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func EmitPackageDifferentOSArchitecture(pkgPath string) { |
| 121 | + emitDiagnostic( |
| 122 | + "go/autobuilder/package-different-os-architecture", |
| 123 | + "Package "+pkgPath+" is intended for a different OS or architecture", |
| 124 | + "Make sure the `GOOS` and `GOARCH` [environment variables are correctly set](https://docs.github.com/en/actions/learn-github-actions/variables#defining-environment-variables-for-a-single-workflow). Alternatively, [change your OS and architecture](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#using-a-github-hosted-runner).", |
| 125 | + severityWarning, |
| 126 | + fullVisibility, |
| 127 | + noLocation, |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +const maxNumPkgPaths = 5 |
| 132 | + |
| 133 | +func EmitCannotFindPackages(pkgPaths []string) { |
| 134 | + numPkgPaths := len(pkgPaths) |
| 135 | + |
| 136 | + ending := "s" |
| 137 | + if numPkgPaths == 1 { |
| 138 | + ending = "" |
| 139 | + } |
| 140 | + |
| 141 | + numPrinted := numPkgPaths |
| 142 | + truncated := false |
| 143 | + if numPrinted > maxNumPkgPaths { |
| 144 | + numPrinted = maxNumPkgPaths |
| 145 | + truncated = true |
| 146 | + } |
| 147 | + |
| 148 | + secondLine := "`" + strings.Join(pkgPaths[0:numPrinted], "`, `") + "`" |
| 149 | + if truncated { |
| 150 | + secondLine += fmt.Sprintf(" and %d more", numPkgPaths-maxNumPkgPaths) |
| 151 | + } |
| 152 | + |
| 153 | + emitDiagnostic( |
| 154 | + "go/autobuilder/package-not-found", |
| 155 | + fmt.Sprintf("%d package%s could not be found", numPkgPaths, ending), |
| 156 | + "The following packages could not be found.\n\n"+secondLine+"\n\nCheck that the paths are correct and make sure any private packages can be accessed. If any of the packages are present in the repository then you may need a [custom build command](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages).", |
| 157 | + severityError, |
| 158 | + fullVisibility, |
| 159 | + noLocation, |
| 160 | + ) |
| 161 | +} |
| 162 | + |
| 163 | +func EmitNewerGoVersionNeeded() { |
| 164 | + emitDiagnostic( |
| 165 | + "go/autobuilder/newer-go-version-needed", |
| 166 | + "Newer Go version needed", |
| 167 | + "The detected version of Go is lower than the version specified in `go.mod`. [Install a newer version](https://github.com/actions/setup-go#basic).", |
| 168 | + severityError, |
| 169 | + fullVisibility, |
| 170 | + noLocation, |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +func EmitGoFilesFoundButNotProcessed() { |
| 175 | + emitDiagnostic( |
| 176 | + "go/autobuilder/go-files-found-but-not-processed", |
| 177 | + "Go files were found but not processed", |
| 178 | + "[Specify a custom build command](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages) that includes one or more `go build` commands to build the `.go` files to be analyzed.", |
| 179 | + severityError, |
| 180 | + fullVisibility, |
| 181 | + noLocation, |
| 182 | + ) |
| 183 | +} |
0 commit comments