Skip to content

Commit

Permalink
Remove prefixes from the sonar-scanner cli output
Browse files Browse the repository at this point in the history
Fix typo
  • Loading branch information
LowCostCustoms committed Jan 18, 2021
1 parent 035bce5 commit 4beb848
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Dockerfile
bin
action-entrypoint.sh
action.yml
run.sh
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.vscode
bin
run.sh
2 changes: 1 addition & 1 deletion action-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ echo "::group::Building docker image"
docker build --build-arg BASE_IMAGE=$IMAGE -t $image_name .
echo "::endgroup::"

echo "::group::Runing sonar-scanner"
echo "::group::Running sonar-scanner"
docker run \
--rm \
-t \
Expand Down
2 changes: 1 addition & 1 deletion action.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
log.Infof("Log level set to %s", env.LogLevel)

if env.TlsSkipVerify {
log.Warn("Sonar host certificate verification has beed disabled")
log.Warn("Sonar host certificate verification was disabled")
}

// Create a new sonar-scanner run.
Expand Down
44 changes: 22 additions & 22 deletions internal/sonarscanner/sonar_scanner_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/sirupsen/logrus"
)

var messagePrefixRegex = regexp.MustCompile("^(\\d+:\\d+:\\d+\\.\\d+\\s+)?(DEBUG|WARN|INFO|ERROR):\\s*")

func runSonarScanner(log *logrus.Entry, cmd *exec.Cmd) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
Expand All @@ -31,30 +33,28 @@ func runSonarScanner(log *logrus.Entry, cmd *exec.Cmd) error {
}

func redirectOutput(log *logrus.Entry, defaultLevel logrus.Level, reader io.Reader) {
rx := regexp.MustCompile("^(INFO|WARN|ERRO|DEBU)")

scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
matches := rx.FindStringSubmatch(line)

level := defaultLevel
if matches != nil {
switch matches[1] {
case "WARN":
level = logrus.WarnLevel
break
case "INFO":
level = logrus.InfoLevel
break
case "DEBU":
level = logrus.DebugLevel
break
case "ERRO":
level = logrus.ErrorLevel
break
}
level, message := getLevelAndMessage(defaultLevel, scanner.Text())
log.Logln(level, message)
}
}

func getLevelAndMessage(defaultLevel logrus.Level, line string) (logrus.Level, string) {
indices := messagePrefixRegex.FindStringSubmatchIndex(line)
if indices != nil {
message := line[indices[1]:]
switch line[indices[4]:indices[5]] {
case "DEBUG":
return logrus.DebugLevel, message
case "INFO":
return logrus.InfoLevel, message
case "WARN":
return logrus.WarnLevel, message
case "ERROR":
return logrus.ErrorLevel, message
}
log.Logln(level, line)
}

return defaultLevel, line
}
66 changes: 66 additions & 0 deletions internal/sonarscanner/sonar_scanner_command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sonarscanner

import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestGetLevelAndMessage(t *testing.T) {
assertLevelAndMessage(
t,
logrus.InfoLevel,
"message with no level",
logrus.InfoLevel,
"message with no level",
)
assertLevelAndMessage(
t,
logrus.InfoLevel,
"DEBUG: debug message",
logrus.DebugLevel,
"debug message",
)
assertLevelAndMessage(
t,
logrus.DebugLevel,
"INFO: info message",
logrus.InfoLevel,
"info message",
)
assertLevelAndMessage(
t,
logrus.DebugLevel,
"WARN: warning message",
logrus.WarnLevel,
"warning message",
)
assertLevelAndMessage(
t,
logrus.DebugLevel,
"ERROR: error message",
logrus.ErrorLevel,
"error message",
)
assertLevelAndMessage(
t,
logrus.DebugLevel,
"17:05:12.779 ERROR: error message with the timestamp",
logrus.ErrorLevel,
"error message with the timestamp",
)
}

func assertLevelAndMessage(
t *testing.T,
level logrus.Level,
message string,
expectedLevel logrus.Level,
expectedMessage string,
) {
actualLevel, actualMessage := getLevelAndMessage(level, message)

assert.Equal(t, expectedLevel, actualLevel)
assert.Equal(t, expectedMessage, actualMessage)
}

0 comments on commit 4beb848

Please sign in to comment.