This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch update-sonarqube-issue-collector
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to 
refs/heads/update-sonarqube-issue-collector by this push:
     new 8e3be1c41 fix: update sonarqube issue collector by type/status/severity
8e3be1c41 is described below

commit 8e3be1c41fbbda06bdfe59902934bc34bced7390
Author: abeizn <[email protected]>
AuthorDate: Tue Oct 24 16:03:09 2023 +0800

    fix: update sonarqube issue collector by type/status/severity
---
 .../plugins/sonarqube/tasks/issues_collector.go    | 146 ++++-----------------
 1 file changed, 25 insertions(+), 121 deletions(-)

diff --git a/backend/plugins/sonarqube/tasks/issues_collector.go 
b/backend/plugins/sonarqube/tasks/issues_collector.go
index dce957f04..3393a6833 100644
--- a/backend/plugins/sonarqube/tasks/issues_collector.go
+++ b/backend/plugins/sonarqube/tasks/issues_collector.go
@@ -22,7 +22,6 @@ import (
        "fmt"
        "net/http"
        "net/url"
-       "time"
 
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/models/common"
@@ -34,22 +33,33 @@ const RAW_ISSUES_TABLE = "sonarqube_api_issues"
 
 var _ plugin.SubTaskEntryPoint = CollectIssues
 
-type SonarqubeIssueTimeIteratorNode struct {
-       CreatedAfter  *time.Time
-       CreatedBefore *time.Time
+type SonarqubeIssueIteratorNode struct {
+       Severity string
+       Status   string
+       Type     string
 }
 
 func CollectIssues(taskCtx plugin.SubTaskContext) (err errors.Error) {
        logger := taskCtx.GetLogger()
        logger.Info("collect issues")
-       iterator := helper.NewQueueIterator()
-       iterator.Push(
-               &SonarqubeIssueTimeIteratorNode{
-                       CreatedAfter:  nil,
-                       CreatedBefore: nil,
-               },
-       )
 
+       iterator := helper.NewQueueIterator()
+       severities := []string{"BLOCKER", "CRITICAL", "MAJOR", "MINOR", "INFO"}
+       statuses := []string{"OPEN", "CONFIRMED", "REOPENED", "RESOLVED", 
"CLOSED"}
+       types := []string{"BUG", "VULNERABILITY", "CODE_SMELL"}
+       for _, severity := range severities {
+               for _, status := range statuses {
+                       for _, typ := range types {
+                               iterator.Push(
+                                       &SonarqubeIssueIteratorNode{
+                                               Severity: severity,
+                                               Status:   status,
+                                               Type:     typ,
+                                       },
+                               )
+                       }
+               }
+       }
        rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, 
RAW_ISSUES_TABLE)
        collector, err := helper.NewApiCollector(helper.ApiCollectorArgs{
                RawDataSubTaskArgs: *rawDataSubTaskArgs,
@@ -60,96 +70,15 @@ func CollectIssues(taskCtx plugin.SubTaskContext) (err 
errors.Error) {
                Query: func(reqData *helper.RequestData) (url.Values, 
errors.Error) {
                        query := url.Values{}
                        query.Set("componentKeys", fmt.Sprintf("%v", 
data.Options.ProjectKey))
-                       input, ok := 
reqData.Input.(*SonarqubeIssueTimeIteratorNode)
-                       if !ok {
-                               return nil, 
errors.Default.New(fmt.Sprintf("Input to SonarqubeIssueTimeIteratorNode 
failed:%+v", reqData.Input))
-                       }
-
-                       if input.CreatedAfter != nil {
-                               query.Set("createdAfter", 
GetFormatTime(input.CreatedAfter))
-                       }
-
-                       if input.CreatedBefore != nil {
-                               query.Set("createdBefore", 
GetFormatTime(input.CreatedBefore))
-                       }
-
+                       query.Set("severities", 
reqData.Input.(*SonarqubeIssueIteratorNode).Severity)
+            query.Set("statuses", 
reqData.Input.(*SonarqubeIssueIteratorNode).Status)
+            query.Set("types", 
reqData.Input.(*SonarqubeIssueIteratorNode).Type)
                        query.Set("p", fmt.Sprintf("%v", reqData.Pager.Page))
                        query.Set("ps", fmt.Sprintf("%v", reqData.Pager.Size))
                        query.Encode()
                        return query, nil
                },
-               GetTotalPages: func(res *http.Response, args 
*helper.ApiCollectorArgs) (int, errors.Error) {
-                       body := &SonarqubePagination{}
-                       err := helper.UnmarshalResponse(res, body)
-                       if err != nil {
-                               return 0, err
-                       }
-
-                       pages := body.Paging.Total / args.PageSize
-                       if body.Paging.Total%args.PageSize > 0 {
-                               pages++
-                       }
-
-                       query := res.Request.URL.Query()
-
-                       // if get more than 10000 data, that need split it
-                       if pages > 100 {
-                               var createdAfterUnix int64
-                               var createdBeforeUnix int64
-
-                               createdAfter, err := 
getTimeFromFormatTime(query.Get("createdAfter"))
-                               if err != nil {
-                                       return 0, err
-                               }
-                               createdBefore, err := 
getTimeFromFormatTime(query.Get("createdBefore"))
-                               if err != nil {
-                                       return 0, err
-                               }
-
-                               if createdAfter == nil {
-                                       createdAfterUnix = 0
-                               } else {
-                                       createdAfterUnix = createdAfter.Unix()
-                               }
-
-                               if createdBefore == nil {
-                                       createdBeforeUnix = time.Now().Unix()
-                               } else {
-                                       createdBeforeUnix = createdBefore.Unix()
-                               }
-
-                               // can not split it any more
-                               if createdBeforeUnix-createdAfterUnix <= 10 {
-                                       return 100, nil
-                               }
-
-                               // split it
-                               MidTime := 
time.Unix((createdAfterUnix+createdBeforeUnix)/2+1, 0)
-
-                               // left part
-                               iterator.Push(&SonarqubeIssueTimeIteratorNode{
-                                       CreatedAfter:  createdAfter,
-                                       CreatedBefore: &MidTime,
-                               })
-
-                               // right part
-                               iterator.Push(&SonarqubeIssueTimeIteratorNode{
-                                       CreatedAfter:  &MidTime,
-                                       CreatedBefore: createdBefore,
-                               })
-
-                               logger.Info("split [%s][%s] by mid [%s] for it 
has pages:[%d] and total:[%d]",
-                                       query.Get("createdAfter"), 
query.Get("createdBefore"), GetFormatTime(&MidTime), pages, body.Paging.Total)
-
-                               return 0, nil
-                       } else {
-                               logger.Info("[%s][%s] has pages:[%d] and 
total:[%d]",
-                                       query.Get("createdAfter"), 
query.Get("createdBefore"), pages, body.Paging.Total)
-
-                               return pages, nil
-                       }
-               },
-
+               GetTotalPages: GetTotalPagesFromResponse,
                ResponseParser: func(res *http.Response) ([]json.RawMessage, 
errors.Error) {
                        var resData struct {
                                Data []json.RawMessage `json:"issues"`
@@ -191,28 +120,3 @@ var CollectIssuesMeta = plugin.SubTaskMeta{
        Description:      "Collect issues data from Sonarqube api",
        DomainTypes:      []string{plugin.DOMAIN_TYPE_CODE_QUALITY},
 }
-
-func GetFormatTime(t *time.Time) string {
-       if t == nil {
-               return ""
-       }
-       return t.Format("2006-01-02T15:04:05-0700")
-}
-
-func getTimeFromFormatTime(formatTime string) (*time.Time, errors.Error) {
-       if formatTime == "" {
-               return nil, nil
-       }
-
-       if len(formatTime) < 20 {
-               return nil, errors.Default.New(fmt.Sprintf("formatTime [%s] is 
too short ", formatTime))
-       }
-
-       t, err := time.Parse("2006-01-02T15:04:05-0700", formatTime)
-
-       if err != nil {
-               return nil, errors.Default.New(fmt.Sprintf("Failed to Parse the 
time from [%s]:%s", formatTime, err.Error()))
-       }
-
-       return &t, nil
-}

Reply via email to