Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add issue range to scan issues #443

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions application/server/notification/scan_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"strconv"

"github.com/snyk/snyk-ls/domain/ide/converter"
"github.com/snyk/snyk-ls/domain/ide/notification"
"github.com/snyk/snyk-ls/domain/snyk"
"github.com/snyk/snyk-ls/internal/lsp"
Expand Down Expand Up @@ -105,6 +106,7 @@ func (n *scanNotifier) appendOssIssues(scanIssues []lsp.ScanIssue, folderPath st
Title: additionalData.Title,
Severity: issue.Severity.String(),
FilePath: issue.AffectedFilePath,
Range: converter.ToRange(issue.Range),
AdditionalData: lsp.OssIssueData{
License: additionalData.License,
Identifiers: lsp.OssIdentifiers{
Expand Down Expand Up @@ -147,6 +149,7 @@ func (n *scanNotifier) appendIacIssues(scanIssues []lsp.ScanIssue, folderPath st
Title: additionalData.Title,
Severity: issue.Severity.String(),
FilePath: issue.AffectedFilePath,
Range: converter.ToRange(issue.Range),
AdditionalData: lsp.IacIssueData{
PublicId: additionalData.PublicId,
Documentation: additionalData.Documentation,
Expand Down Expand Up @@ -209,6 +212,7 @@ func (n *scanNotifier) appendCodeIssues(scanIssues []lsp.ScanIssue, folderPath s
Title: issue.Message,
Severity: issue.Severity.String(),
FilePath: issue.AffectedFilePath,
Range: converter.ToRange(issue.Range),
AdditionalData: lsp.CodeIssueData{
Message: additionalData.Message,
Rule: additionalData.Rule,
Expand Down
138 changes: 72 additions & 66 deletions application/server/notification/scan_notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"

notification2 "github.com/snyk/snyk-ls/application/server/notification"
"github.com/snyk/snyk-ls/domain/ide/converter"
"github.com/snyk/snyk-ls/domain/snyk"
lsp2 "github.com/snyk/snyk-ls/internal/lsp"
"github.com/snyk/snyk-ls/internal/notification"
Expand Down Expand Up @@ -77,13 +78,25 @@ func Test_SendSuccess_SendsForAllEnabledProducts(t *testing.T) {

const folderPath = "/test/iac/folderPath"

// expected message uses lsp2.ScanIssue && lsp2.CodeIssueData
testRange := snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
}
lspTestRange := converter.ToRange(testRange)

expectedIacIssue := []lsp2.ScanIssue{
{
Id: "098f6bcd4621d373cade4e832627b4f6",
Title: "iacTitle",
Severity: "critical",
FilePath: "iacAffectedFilePath",
Range: lspTestRange,
AdditionalData: lsp2.IacIssueData{
PublicId: "iacID",
Documentation: "iacDocumentation",
Expand All @@ -101,6 +114,7 @@ func Test_SendSuccess_SendsForAllEnabledProducts(t *testing.T) {
Title: "codeMessage",
Severity: "low",
FilePath: "codeAffectedFilePath",
Range: lspTestRange,
AdditionalData: lsp2.CodeIssueData{
Message: "codeMessage",
Rule: "codeRule",
Expand All @@ -120,19 +134,10 @@ func Test_SendSuccess_SendsForAllEnabledProducts(t *testing.T) {

scanIssues := []snyk.Issue{
{ // IaC issue
ID: "iacID",
Severity: snyk.Critical,
IssueType: 1,
Range: snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
},
ID: "iacID",
Severity: snyk.Critical,
IssueType: 1,
Range: testRange,
Message: "iacMessage",
FormattedMessage: "iacFormattedMessage",
AffectedFilePath: "iacAffectedFilePath",
Expand All @@ -153,19 +158,10 @@ func Test_SendSuccess_SendsForAllEnabledProducts(t *testing.T) {
},
},
{ // Code issue
ID: "codeID",
Severity: snyk.Low,
IssueType: 1,
Range: snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
},
ID: "codeID",
Severity: snyk.Low,
IssueType: 1,
Range: testRange,
Message: "codeMessage",
FormattedMessage: "codeFormattedMessage",
AffectedFilePath: "codeAffectedFilePath",
Expand Down Expand Up @@ -218,12 +214,25 @@ func Test_SendSuccess_SendsForOpenSource(t *testing.T) {

const folderPath = "/test/oss/folderPath"

r := snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
}
lspTestRange := converter.ToRange(r)

expectedUIScanIssue := []lsp2.ScanIssue{
{
Id: "OSS Key",
Title: "OSS Title",
Severity: "critical",
FilePath: "ossAffectedFilePath",
Range: lspTestRange,
AdditionalData: lsp2.OssIssueData{
License: "OSS License",
Identifiers: lsp2.OssIdentifiers{
Expand Down Expand Up @@ -256,19 +265,10 @@ func Test_SendSuccess_SendsForOpenSource(t *testing.T) {

issues := []snyk.Issue{
{ // OSS issue
ID: "SNYK-JS-BABELTRAVERSE-5962463",
Severity: snyk.Critical,
IssueType: 1,
Range: snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
},
ID: "SNYK-JS-BABELTRAVERSE-5962463",
Severity: snyk.Critical,
IssueType: 1,
Range: r,
Message: "Incomplete List of Disallowed Inputs",
FormattedMessage: "Incomplete List of Disallowed Inputs",
AffectedFilePath: "ossAffectedFilePath",
Expand Down Expand Up @@ -331,13 +331,25 @@ func Test_SendSuccess_SendsForSnykCode(t *testing.T) {
scanNotifier, _ := notification2.NewScanNotifier(mockNotifier)

const folderPath = "/test/iac/folderPath"
r := snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
}
lspTestRange := converter.ToRange(r)

expectedCodeIssue := []lsp2.ScanIssue{
{
Id: "5a105e8b9d40e1329780d62ea2265d8a",
Title: "codeMessage",
Severity: "low",
FilePath: "codeAffectedFilePath",
Range: lspTestRange,
AdditionalData: lsp2.CodeIssueData{
Message: "codeMessage",
Rule: "codeRule",
Expand All @@ -356,19 +368,10 @@ func Test_SendSuccess_SendsForSnykCode(t *testing.T) {

scanIssues := []snyk.Issue{
{ // Code issue
ID: "codeID",
Severity: snyk.Low,
IssueType: 1,
Range: snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
},
ID: "codeID",
Severity: snyk.Low,
IssueType: 1,
Range: r,
Message: "codeMessage",
FormattedMessage: "codeFormattedMessage",
AffectedFilePath: "codeAffectedFilePath",
Expand Down Expand Up @@ -412,6 +415,17 @@ func Test_SendSuccess_SendsForAllSnykIac(t *testing.T) {
scanNotifier, _ := notification2.NewScanNotifier(mockNotifier)

const folderPath = "/test/iac/folderPath"
r := snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
}
lspTestRange := converter.ToRange(r)

// expected message uses lsp2.ScanIssue && lsp2.CodeIssueData
expectedIacIssue := []lsp2.ScanIssue{
Expand All @@ -420,6 +434,7 @@ func Test_SendSuccess_SendsForAllSnykIac(t *testing.T) {
Title: "iacTitle",
Severity: "critical",
FilePath: "iacAffectedFilePath",
Range: lspTestRange,
AdditionalData: lsp2.IacIssueData{
PublicId: "iacID",
Documentation: "iacDocumentation",
Expand All @@ -433,19 +448,10 @@ func Test_SendSuccess_SendsForAllSnykIac(t *testing.T) {

scanIssues := []snyk.Issue{
{ // IaC issue
ID: "iacID",
Severity: snyk.Critical,
IssueType: 1,
Range: snyk.Range{
Start: snyk.Position{
Line: 1,
Character: 1,
},
End: snyk.Position{
Line: 1,
Character: 2,
},
},
ID: "iacID",
Severity: snyk.Critical,
IssueType: 1,
Range: r,
Message: "iacMessage",
FormattedMessage: "iacFormattedMessage",
AffectedFilePath: "iacAffectedFilePath",
Expand Down
11 changes: 6 additions & 5 deletions internal/lsp/message_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1025,11 +1025,12 @@ type SnykScanParams struct {

type ScanIssue struct { // TODO - convert this to a generic type
// Unique key identifying an issue in the whole result set. Not the same as the Snyk issue ID.
Id string `json:"id"`
Title string `json:"title"`
Severity string `json:"severity"`
FilePath string `json:"filePath"`
AdditionalData any `json:"additionalData,omitempty"`
Id string `json:"id"`
Title string `json:"title"`
Severity string `json:"severity"`
FilePath string `json:"filePath"`
Range sglsp.Range `json:"range"`
AdditionalData any `json:"additionalData,omitempty"`
}

// Snyk Open Source
Expand Down
Loading