Skip to content

Commit ba179df

Browse files
author
Klesh Wong
authored
fix: Data too long for _tool_sonarqube_issue_code_blocks.component (#8492)
1 parent ae31420 commit ba179df

4 files changed

Lines changed: 27 additions & 3 deletions

File tree

backend/core/utils/strings.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package utils
1919

2020
import (
2121
"crypto/rand"
22-
"github.com/apache/incubator-devlake/core/errors"
2322
"math/big"
2423
"strings"
24+
25+
"github.com/apache/incubator-devlake/core/errors"
2526
)
2627

2728
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
@@ -81,3 +82,18 @@ func SanitizeString(s string) string {
8182
}
8283
return strings.Replace(s, s[prefixLen:strLen-suffixLen], strings.Repeat("*", strLen-prefixLen-suffixLen), -1)
8384
}
85+
86+
// from https://stackoverflow.com/questions/12311033/extracting-substrings-in-go
87+
func Substr(input string, start int, length int) string {
88+
asRunes := []rune(input)
89+
90+
if start >= len(asRunes) {
91+
return ""
92+
}
93+
94+
if start+length > len(asRunes) {
95+
length = len(asRunes) - start
96+
}
97+
98+
return string(asRunes[start : start+length])
99+
}

backend/core/utils/strings_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,10 @@ func TestSanitizeString(t *testing.T) {
119119
})
120120
}
121121
}
122+
123+
func TestSubstr(t *testing.T) {
124+
assert.Equalf(t, "😂😂😂", Substr("😂😂😂a", 0, 3), "substr on utf8")
125+
assert.Equalf(t, "c", Substr("😂😂c😂", 2, 1), "substr on lattin + unicode")
126+
assert.Equalf(t, "abcde", Substr("abcde", 0, 100), "specified length greater than actual length")
127+
assert.Equalf(t, "", Substr("abcde", 100, 100), "specified start greater than actual length")
128+
}

backend/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,6 @@ require (
129129
golang.org/x/mod v0.17.0
130130
)
131131

132-
replace github.com/chenzhuoyu/iasm => github.com/cloudwego/iasm v0.2.0
132+
// replace github.com/chenzhuoyu/iasm => github.com/cloudwego/iasm v0.2.0
133133

134134
//replace github.com/apache/incubator-devlake => ./

backend/plugins/sonarqube/tasks/issues_extractor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/apache/incubator-devlake/core/errors"
2626
"github.com/apache/incubator-devlake/core/models/common"
2727
"github.com/apache/incubator-devlake/core/plugin"
28+
"github.com/apache/incubator-devlake/core/utils"
2829
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
2930
"github.com/apache/incubator-devlake/plugins/sonarqube/models"
3031
)
@@ -81,7 +82,7 @@ func ExtractIssues(taskCtx plugin.SubTaskContext) errors.Error {
8182
codeBlockInIssue := &models.SonarqubeIssueCodeBlock{
8283
ConnectionId: data.Options.ConnectionId,
8384
IssueKey: sonarqubeIssue.IssueKey,
84-
Component: sonarqubeIssue.Component,
85+
Component: utils.Substr(sonarqubeIssue.Component, 0, 500),
8586
Msg: sonarqubeIssue.Message,
8687
StartLine: sonarqubeIssue.StartLine,
8788
EndLine: sonarqubeIssue.EndLine,

0 commit comments

Comments
 (0)