Skip to content

Commit fa4449c

Browse files
committed
🚸 Format CI messages for more visibility
1 parent 0d3b4d1 commit fa4449c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

platform/env.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const (
5555

5656
// ExtractQodanaEnvironment extracts Qodana environment variables from the current environment.
5757
func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
58-
ci := cienvironment.DetectCIEnvironment()
5958
if license := os.Getenv(QodanaLicense); license != "" {
6059
setEnvironmentFunc(QodanaLicense, license)
6160
}
@@ -68,9 +67,10 @@ func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
6867
if revision := os.Getenv(QodanaRevision); revision != "" {
6968
setEnvironmentFunc(QodanaRevision, revision)
7069
}
70+
ci := cienvironment.DetectCIEnvironment()
7171
qEnv := "cli"
7272
if ci != nil {
73-
qEnv = strings.ReplaceAll(strings.ToLower(ci.Name), " ", "-")
73+
qEnv = getCIName(ci)
7474
setEnvironmentFunc(qodanaJobUrl, validateJobUrl(ci.URL, qEnv))
7575
if ci.Git != nil {
7676
setEnvironmentFunc(QodanaRemoteUrl, validateRemoteUrl(ci.Git.Remote, qEnv))
@@ -94,6 +94,10 @@ func ExtractQodanaEnvironment(setEnvironmentFunc func(string, string)) {
9494
setEnvironmentFunc(qodanaEnv, fmt.Sprintf("%s:%s", qEnv, Version))
9595
}
9696

97+
func getCIName(ci *cienvironment.CiEnvironment) string {
98+
return strings.ReplaceAll(strings.ToLower(ci.Name), " ", "-")
99+
}
100+
97101
func validateRemoteUrl(remote string, qEnv string) string {
98102
if strings.HasPrefix(qEnv, "space") {
99103
return getSpaceRemoteUrl()

platform/output.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package platform
1919
import (
2020
"fmt"
2121
"github.com/JetBrains/qodana-cli/v2024/sarif"
22+
cienvironment "github.com/cucumber/ci-environment/go"
2223
"os"
2324
"strings"
2425

@@ -108,14 +109,14 @@ func SuccessMessage(message string, a ...interface{}) {
108109
func WarningMessage(message string, a ...interface{}) {
109110
message = fmt.Sprintf(message, a...)
110111
icon := warningStyle.Sprint("\n! ")
111-
pterm.Println(icon, Primary(message))
112+
pterm.Println(formatMessageForCI(icon, Primary(message)))
112113
}
113114

114115
// ErrorMessage prints an error message with the icon.
115116
func ErrorMessage(message string, a ...interface{}) {
116117
message = fmt.Sprintf(message, a...)
117118
icon := errorStyle.Sprint("✗ ")
118-
pterm.Println(icon, errorStyle.Sprint(message))
119+
pterm.Println(formatMessageForCI(icon, errorStyle.Sprint(message)))
119120
}
120121

121122
// PrintLinterLog prints the linter logs with color, when needed.
@@ -277,3 +278,18 @@ func getProblemsFoundMessage(newProblems int) string {
277278
return fmt.Sprintf("Found %d new problems according to the checks applied", newProblems)
278279
}
279280
}
281+
282+
// formatMessageForCI formats the message for the CI environment.
283+
func formatMessageForCI(level, format string, a ...interface{}) string {
284+
message := fmt.Sprintf(format, a...)
285+
ci := cienvironment.DetectCIEnvironment()
286+
name := getCIName(ci)
287+
if name == "github-actions" {
288+
return fmt.Sprintf("::%s::%s", level, message)
289+
} else if strings.HasPrefix(name, "azure") {
290+
return fmt.Sprintf("##vso[task.logissue type=%s]%s", level, message)
291+
} else if strings.HasPrefix(name, "circleci") {
292+
return fmt.Sprintf("echo '%s: %s'", level, message)
293+
}
294+
return message
295+
}

0 commit comments

Comments
 (0)