Skip to content

Commit

Permalink
feat: added cq_issue_impacts table
Browse files Browse the repository at this point in the history
  • Loading branch information
klesh committed Oct 10, 2024
1 parent 9a87ad9 commit 2a78f30
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 0 deletions.
11 changes: 11 additions & 0 deletions backend/core/models/domainlayer/codequality/cq_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ type CqIssue struct {
func (CqIssue) TableName() string {
return "cq_issues"
}

type CqIssueImpact struct {
common.NoPKModel
CqIssueId string `gorm:"primaryKey;type:varchar(255)"`
SoftwareQuality string `gorm:"primaryKey;type:varchar(255)"`
Severity string `gorm:"type:varchar(100)"`
}

func (CqIssueImpact) TableName() string {
return "cq_issue_impacts"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migrationscripts

import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
"github.com/apache/incubator-devlake/core/plugin"
)

var _ plugin.MigrationScript = (*addCqIssueImpacts)(nil)

type cqIssueImpacts struct {
archived.NoPKModel
CqIssueId string `gorm:"primaryKey;type:varchar(255)"`
SoftwareQuality string `gorm:"primaryKey;type:varchar(255)"`
Severity string `gorm:"type:varchar(100)"`
}

type addCqIssueImpacts struct {
}

func (script *addCqIssueImpacts) Up(basicRes context.BasicRes) errors.Error {
return basicRes.GetDal().AutoMigrate(&cqIssueImpacts{})
}

func (*addCqIssueImpacts) Version() uint64 {
return 20241010162658
}

func (*addCqIssueImpacts) Name() string {
return "add cq_issue_impacts table"
}
1 change: 1 addition & 0 deletions backend/core/models/migrationscripts/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,6 @@ func All() []plugin.MigrationScript {
new(addIsSubtaskToIssue),
new(addIsChildToCicdPipeline),
new(increaseCqIssueComponentLength),
new(addCqIssueImpacts),
}
}
2 changes: 2 additions & 0 deletions backend/plugins/sonarqube/impl/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (p Sonarqube) GetTablesInfo() []dal.Tabler {
&models.SonarqubeConnection{},
&models.SonarqubeProject{},
&models.SonarqubeIssue{},
&models.SonarqubeIssueImpact{},
&models.SonarqubeIssueCodeBlock{},
&models.SonarqubeHotspot{},
&models.SonarqubeFileMetrics{},
Expand All @@ -102,6 +103,7 @@ func (p Sonarqube) SubTaskMetas() []plugin.SubTaskMeta {
tasks.ExtractAccountsMeta,
tasks.ConvertProjectsMeta,
tasks.ConvertIssuesMeta,
tasks.ConvertIssueImpactsMeta,
tasks.ConvertIssueCodeBlocksMeta,
tasks.ConvertHotspotsMeta,
tasks.ConvertFileMetricsMeta,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migrationscripts

import (
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/migrationhelper"
)

var _ plugin.MigrationScript = (*addIssueImpacts)(nil)

type issueImpacts20241010 struct {
ConnectionId uint64 `gorm:"primaryKey"`
IssueKey string `gorm:"primaryKey;type:varchar(100)"`
SoftwareQuality string `gorm:"primaryKey;type:varchar(255)"`
Severity string `gorm:"type:varchar(100)"`
archived.NoPKModel
}

func (issueImpacts20241010) TableName() string {
return "_tool_sonarqube_issue_impacts"
}

type addIssueImpacts struct {
}

func (script *addIssueImpacts) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(basicRes, &issueImpacts20241010{})
}

func (*addIssueImpacts) Version() uint64 {
return 20241010162943
}

func (*addIssueImpacts) Name() string {
return "add issue_impacts table for sonarcloud"
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func All() []plugin.MigrationScript {
new(changeIssueComponentType),
new(increaseProjectKeyLength),
new(addOrgToConn),
new(addIssueImpacts),
}
}
12 changes: 12 additions & 0 deletions backend/plugins/sonarqube/models/sonarqube_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ type SonarqubeIssue struct {
func (SonarqubeIssue) TableName() string {
return "_tool_sonarqube_issues"
}

type SonarqubeIssueImpact struct {
ConnectionId uint64 `gorm:"primaryKey"`
IssueKey string `gorm:"primaryKey;type:varchar(100)"`
SoftwareQuality string `gorm:"primaryKey;type:varchar(255)"`
Severity string `gorm:"type:varchar(100)"`
common.NoPKModel
}

func (SonarqubeIssueImpact) TableName() string {
return "_tool_sonarqube_issue_impacts"
}
75 changes: 75 additions & 0 deletions backend/plugins/sonarqube/tasks/issue_impacts_convertor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tasks

import (
"reflect"

"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/domainlayer/codequality"
"github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
sonarqubeModels "github.com/apache/incubator-devlake/plugins/sonarqube/models"
)

var ConvertIssueImpactsMeta = plugin.SubTaskMeta{
Name: "convertIssueImpacts",
EntryPoint: ConvertIssueImpacts,
EnabledByDefault: true,
Description: "Convert tool layer table sonarqube_issue_impacts into domain layer table cq_issue_impacts",
DomainTypes: []string{plugin.DOMAIN_TYPE_CODE_QUALITY},
}

func ConvertIssueImpacts(taskCtx plugin.SubTaskContext) errors.Error {
db := taskCtx.GetDal()
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_ISSUES_TABLE)
cursor, err := db.Cursor(
dal.From("_tool_sonarqube_issue_impacts p"),
dal.Join("LEFT JOIN _tool_sonarqube_issues i ON (i.connection_id = p.connection_id AND i.issue_key = p.issue_key)"),
dal.Where("i.connection_id = ? AND i.project_key = ?", data.Options.ConnectionId, data.Options.ProjectKey))
if err != nil {
return err
}
defer cursor.Close()

issueIdGen := didgen.NewDomainIdGenerator(&sonarqubeModels.SonarqubeIssue{})
converter, err := api.NewDataConverter(api.DataConverterArgs{
InputRowType: reflect.TypeOf(sonarqubeModels.SonarqubeIssueImpact{}),
Input: cursor,
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Convert: func(inputRow interface{}) ([]interface{}, errors.Error) {
impact := inputRow.(*sonarqubeModels.SonarqubeIssueImpact)
domainIssueImpact := &codequality.CqIssueImpact{
CqIssueId: issueIdGen.Generate(data.Options.ConnectionId, impact.IssueKey),
SoftwareQuality: impact.SoftwareQuality,
Severity: impact.Severity,
}
return []interface{}{
domainIssueImpact,
}, nil
},
})

if err != nil {
return err
}

return converter.Execute()
}
14 changes: 14 additions & 0 deletions backend/plugins/sonarqube/tasks/issues_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func ExtractIssues(taskCtx plugin.SubTaskContext) errors.Error {
results = append(results, codeBlock)
}
}

for _, v := range body.Impacts {
impact := &models.SonarqubeIssueImpact{
ConnectionId: data.Options.ConnectionId,
IssueKey: sonarqubeIssue.IssueKey,
SoftwareQuality: v.SoftwareQuality,
Severity: v.Severity,
}
results = append(results, impact)
}
return results, nil
},
})
Expand Down Expand Up @@ -151,6 +161,10 @@ type IssuesResponse struct {
Type string `json:"type"`
Scope string `json:"scope"`
QuickFixAvailable bool `json:"quickFixAvailable"`
Impacts []struct {
SoftwareQuality string `json:"softwareQuality"`
Severity string `json:"severity"`
} `json:"impacts"`
}

type flow struct {
Expand Down

0 comments on commit 2a78f30

Please sign in to comment.