Skip to content

Commit

Permalink
fix(sonarqube): issue collector failed when issue status updated (apa…
Browse files Browse the repository at this point in the history
…che#5544) (apache#5548)

* fix(sonarqube):  issue collector failed when issue status updated (apache#5544)

cherry-pick

* fix: fix golangci-lint error
  • Loading branch information
leric authored Jun 26, 2023
1 parent 9d49a9f commit 8b6a1ba
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func Test_topologicalSort(t *testing.T) {
{
name: "cycle error",
args: args{[]*plugin.SubTaskMeta{
&plugin.SubTaskMeta{
{
Name: "D",
Dependencies: []*plugin.SubTaskMeta{&plugin.SubTaskMeta{
Dependencies: []*plugin.SubTaskMeta{{
Name: "E",
}},
},
&plugin.SubTaskMeta{
{
Name: "E",
Dependencies: []*plugin.SubTaskMeta{&plugin.SubTaskMeta{
Dependencies: []*plugin.SubTaskMeta{{
Name: "D",
}},
},
Expand Down
6 changes: 3 additions & 3 deletions backend/plugins/dora/impl/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func TestMakeMetricPluginPipelinePlanV200(t *testing.T) {
doraOutputPlan := plugin.PipelinePlan{
plugin.PipelineStage{
{
Plugin: "dora",
Plugin: "dora",
Subtasks: []string{
"generateDeploymentCommits",
"enrichPrevSuccessDeploymentCommits",
},
Options: map[string]interface{}{"projectName": projectName},
Options: map[string]interface{}{"projectName": projectName},
},
},
plugin.PipelineStage{
Expand All @@ -56,7 +56,7 @@ func TestMakeMetricPluginPipelinePlanV200(t *testing.T) {
},
plugin.PipelineStage{
{
Plugin: "dora",
Plugin: "dora",
Subtasks: []string{
"calculateChangeLeadTime",
"ConnectIncidentToDeployment",
Expand Down
3 changes: 3 additions & 0 deletions backend/plugins/sonarqube/e2e/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package e2e

import (
"testing"
"time"

"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/models/domainlayer/crossdomain"
Expand All @@ -37,6 +38,7 @@ func TestSonarqubeAccountDataFlow(t *testing.T) {
Options: &tasks.SonarqubeOptions{
ConnectionId: 1,
},
TaskStartTime: time.Now(),
}

// import raw data table
Expand All @@ -51,6 +53,7 @@ func TestSonarqubeAccountDataFlow(t *testing.T) {
Options: &tasks.SonarqubeOptions{
ConnectionId: 2,
},
TaskStartTime: time.Now(),
}

dataflowTester.Subtask(tasks.ExtractAccountsMeta, taskData2)
Expand Down
3 changes: 3 additions & 0 deletions backend/plugins/sonarqube/e2e/filemetrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e

import (
"testing"
"time"

"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/models/domainlayer/codequality"
Expand All @@ -43,13 +44,15 @@ func TestSonarqubeFileMetricsDataFlow(t *testing.T) {
ConnectionId: 2,
ProjectKey: "testDevLake",
},
TaskStartTime: time.Now(),
}
// Interfered data
taskData2 := &tasks.SonarqubeTaskData{
Options: &tasks.SonarqubeOptions{
ConnectionId: 1,
ProjectKey: "testNone",
},
TaskStartTime: time.Now(),
}

// verify extraction
Expand Down
3 changes: 3 additions & 0 deletions backend/plugins/sonarqube/e2e/hotspot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e

import (
"testing"
"time"

"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/models/domainlayer/codequality"
Expand All @@ -41,13 +42,15 @@ func TestSonarqubeHotspotDataFlow(t *testing.T) {
ConnectionId: 1,
ProjectKey: "f5a50c63-2e8f-4107-9014-853f6f467757",
},
TaskStartTime: time.Now(),
}
// Interfered data
taskData2 := &tasks.SonarqubeTaskData{
Options: &tasks.SonarqubeOptions{
ConnectionId: 2,
ProjectKey: "testWarrenEtcd",
},
TaskStartTime: time.Now(),
}

// verify extraction
Expand Down
3 changes: 3 additions & 0 deletions backend/plugins/sonarqube/e2e/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e

import (
"testing"
"time"

"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/models/domainlayer/codequality"
Expand All @@ -41,13 +42,15 @@ func TestSonarqubeIssueDataFlow(t *testing.T) {
ConnectionId: 1,
ProjectKey: "f5a50c63-2e8f-4107-9014-853f6f467757",
},
TaskStartTime: time.Now(),
}
// Interfered data
taskData2 := &tasks.SonarqubeTaskData{
Options: &tasks.SonarqubeOptions{
ConnectionId: 2,
ProjectKey: "testWarrenEtcd",
},
TaskStartTime: time.Now(),
}

// verify extraction
Expand Down
2 changes: 2 additions & 0 deletions backend/plugins/sonarqube/e2e/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package e2e

import (
"testing"
"time"

"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/models/domainlayer/codequality"
Expand All @@ -42,6 +43,7 @@ func TestSonarqubeProjectDataFlow(t *testing.T) {
ConnectionId: 2,
ProjectKey: "e2c6d5e9-a321-4e8c-b322-03d9599ef962",
},
TaskStartTime: time.Now(),
}

dataflowTester.FlushTabler(&codequality.CqProject{})
Expand Down
6 changes: 4 additions & 2 deletions backend/plugins/sonarqube/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package impl

import (
"fmt"
"time"

"github.com/apache/incubator-devlake/core/dal"

Expand Down Expand Up @@ -127,8 +128,9 @@ func (p Sonarqube) PrepareTaskData(taskCtx plugin.TaskContext, options map[strin
return nil, errors.Default.Wrap(err, "unable to get Sonarqube API client instance")
}
taskData := &tasks.SonarqubeTaskData{
Options: op,
ApiClient: apiClient,
Options: op,
ApiClient: apiClient,
TaskStartTime: time.Now(),
}
// even we have project in _tool_sonaqube_projects, we still need to collect project to update LastAnalysisDate
var scope models.SonarqubeProject
Expand Down
16 changes: 16 additions & 0 deletions backend/plugins/sonarqube/tasks/hotspots_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ func CollectHotspots(taskCtx plugin.SubTaskContext) errors.Error {
Data []json.RawMessage `json:"hotspots"`
}
err := helper.UnmarshalResponse(res, &resData)

// check if sonar report updated during collecting
var issue struct {
UpdateDate *helper.Iso8601Time `json:"updateDate"`
}
for _, v := range resData.Data {
err = errors.Convert(json.Unmarshal(v, &issue))
if err != nil {
return nil, err
}
if issue.UpdateDate.ToTime().After(data.TaskStartTime) {
return nil, errors.Default.New(fmt.Sprintf(`Your data is affected by the latest analysis\n
Please recollect this project: %s`, data.Options.ProjectKey))
}
}

return resData.Data, err
},
})
Expand Down
11 changes: 6 additions & 5 deletions backend/plugins/sonarqube/tasks/issues_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type SonarqubeIssueTimeIteratorNode struct {
func CollectIssues(taskCtx plugin.SubTaskContext) (err errors.Error) {
logger := taskCtx.GetLogger()
logger.Info("collect issues")

iterator := helper.NewQueueIterator()
iterator.Push(
&SonarqubeIssueTimeIteratorNode{
Expand Down Expand Up @@ -154,19 +153,21 @@ func CollectIssues(taskCtx plugin.SubTaskContext) (err errors.Error) {
var resData struct {
Data []json.RawMessage `json:"issues"`
}
var issue struct {
UpdateDate *helper.Iso8601Time `json:"updateDate"`
}
err = helper.UnmarshalResponse(res, &resData)
if err != nil {
return nil, err
}

// check if sonar report updated during collecting
var issue struct {
UpdateDate *helper.Iso8601Time `json:"updateDate"`
}
for _, v := range resData.Data {
err = errors.Convert(json.Unmarshal(v, &issue))
if err != nil {
return nil, err
}
if issue.UpdateDate.ToTime().After(*data.LastAnalysisDate) {
if issue.UpdateDate.ToTime().After(data.TaskStartTime) {
return nil, errors.Default.New(fmt.Sprintf(`Your data is affected by the latest analysis\n
Please recollect this project: %s`, data.Options.ProjectKey))
}
Expand Down
1 change: 1 addition & 0 deletions backend/plugins/sonarqube/tasks/task_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SonarqubeTaskData struct {
Options *SonarqubeOptions
ApiClient *api.ApiAsyncClient
LastAnalysisDate *time.Time
TaskStartTime time.Time
}

func DecodeAndValidateTaskOptions(options map[string]interface{}) (*SonarqubeOptions, errors.Error) {
Expand Down

0 comments on commit 8b6a1ba

Please sign in to comment.