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: ai fix html [IDE-375] #527

Merged
merged 3 commits into from
Jun 10, 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
10 changes: 10 additions & 0 deletions infrastructure/code/code_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func getCodeDetailsHtml(issue snyk.Issue) string {

data := map[string]interface{}{
"IssueTitle": additionalData.Title,
"IssueMessage": additionalData.Message,
"IssueType": getIssueType(additionalData),
"SeverityIcon": getSeverityIconSvg(issue),
"CWEs": issue.CWEs,
Expand All @@ -104,6 +105,7 @@ func getCodeDetailsHtml(issue snyk.Issue) string {
"LessonIcon": getLessonIconSvg(),
"IgnoreLineAction": getLineToIgnoreAction(issue),
"HasAIFix": additionalData.HasAIFix,
"ExternalIcon": getExternalIconSvg(),
}

if issue.IsIgnored {
Expand Down Expand Up @@ -228,6 +230,14 @@ func formatDate(date time.Time) string {
return fmt.Sprintf("%s %02d, %d", month, date.Day(), date.Year())
}

func getExternalIconSvg() template.HTML {
return template.HTML(` <svg class="is-external-icon" width="9" height="9" viewBox="0 0 9 9" xmlns="http://www.w3.
org/2000/svg" fill="none">
<path d="M4.99998 0L6.64648 1.6465L3.14648 5.1465L3.85348 5.8535L7.35348 2.3535L8.99998 4V0H4.99998Z" fill="#888"/>
<path d="M8 8H1V1H4.5L3.5 0H1C0.4485 0 0 0.4485 0 1V8C0 8.5515 0.4485 9 1 9H8C8.5515 9 9 8.5515 9 8V5.5L8 4.5V8Z" fill="#888"/>
</svg>`)
}

func getSeverityIconSvg(issue snyk.Issue) template.HTML {
switch issue.Severity {
case snyk.Critical:
Expand Down
35 changes: 35 additions & 0 deletions infrastructure/code/code_html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func Test_Code_Html_getCodeDetailsHtml(t *testing.T) {
assert.NotContains(t, codePanelHtml, `class="ignore-details-section"`)

// assert Fixes section
assert.Contains(t, codePanelHtml, ` id="ai-fix-wrapper" class="hidden">`)
assert.Contains(t, codePanelHtml, ` id="no-ai-fix-wrapper" class="">`)
expectedFixesDescription := fmt.Sprintf(`\s*This issue was fixed by %d projects. Here are %d example fixe.\s*`, repoCount, len(fixes))
assert.Regexp(t, regexp.MustCompile(expectedFixesDescription), codePanelHtml)
assert.Contains(t, codePanelHtml, `<span class="tab-item is-selected" id="tab-link-0">`, "Two tabs, first is selected")
Expand All @@ -88,6 +90,39 @@ func Test_Code_Html_getCodeDetailsHtml(t *testing.T) {
assert.Contains(t, codePanelHtml, `class="ignore-button secondary">Ignore in this file</button>`)
}

func Test_Code_Html_getCodeDetailsHtml_withAIfix(t *testing.T) {
_ = testutil.UnitTest(t)

dataFlow := getDataFlowElements()
fixes := getFixes()
repoCount := 54387
issue := snyk.Issue{
Range: getIssueRange(),
CWEs: []string{"CWE-123", "CWE-456"},
ID: "go/NoHardcodedCredentials/test",
Severity: 2,
LessonUrl: "https://learn.snyk.io/lesson/no-rate-limiting/?loc=ide",
AdditionalData: snyk.CodeIssueData{
Title: "Allocation of Resources Without Limits or Throttling",
DataFlow: dataFlow,
ExampleCommitFixes: fixes,
RepoDatasetSize: repoCount,
IsSecurityType: true,
Text: getVulnerabilityOverviewText(),
PriorityScore: 890,
HasAIFix: true,
},
}

// invoke method under test
codePanelHtml := getCodeDetailsHtml(issue)

// assert Fixes section
assert.Contains(t, codePanelHtml, ` id="ai-fix-wrapper" class="">`)
assert.Contains(t, codePanelHtml, `Generate fix <span class="wide">using Snyk DeepCode AI</span>`)
assert.Contains(t, codePanelHtml, ` id="no-ai-fix-wrapper" class="hidden">`)
}

func Test_Code_Html_getCodeDetailsHtml_ignored(t *testing.T) {
_ = testutil.UnitTest(t)

Expand Down
Loading
Loading