From 592e6515b4eed1c105fc4c0cc413845516351c2c Mon Sep 17 00:00:00 2001 From: Cata Date: Wed, 12 Jun 2024 16:27:26 +0100 Subject: [PATCH] refactor: replace tabs with arrows [IDE-294] (#540) --- infrastructure/code/code_html.go | 127 ++++++++- infrastructure/code/code_html_test.go | 9 +- infrastructure/code/template/details.html | 300 +++++++++++++++------- 3 files changed, 320 insertions(+), 116 deletions(-) diff --git a/infrastructure/code/code_html.go b/infrastructure/code/code_html.go index e4f42b84d..94fc62d36 100644 --- a/infrastructure/code/code_html.go +++ b/infrastructure/code/code_html.go @@ -19,6 +19,7 @@ package code import ( "bytes" _ "embed" + "encoding/json" "fmt" "html/template" "path/filepath" @@ -52,7 +53,6 @@ type DataFlowItem struct { type ExampleCommit struct { CommitURL string - IconSVG template.HTML RepoName string RepoLink string ExampleLines []ExampleLines @@ -65,7 +65,6 @@ var globalTemplate *template.Template func init() { funcMap := template.FuncMap{ - "vendorIconSvg": getVendorIconSvg, "repoName": getRepoName, "trimCWEPrefix": trimCWEPrefix, "idxMinusOne": idxMinusOne, @@ -86,6 +85,9 @@ func getCodeDetailsHtml(issue snyk.Issue) string { return "" } + exampleCommits := prepareExampleCommits(additionalData.ExampleCommitFixes) + commitFixes := parseExampleCommitsToTemplateJS(exampleCommits) + data := map[string]interface{}{ "IssueTitle": additionalData.Title, "IssueMessage": additionalData.Message, @@ -98,7 +100,8 @@ func getCodeDetailsHtml(issue snyk.Issue) string { "DataFlowTable": prepareDataFlowTable(additionalData), "RepoCount": additionalData.RepoDatasetSize, "ExampleCount": len(additionalData.ExampleCommitFixes), - "ExampleCommitFixes": prepareExampleCommitFixes(additionalData.ExampleCommitFixes), + "ExampleCommitFixes": exampleCommits, + "CommitFixes": commitFixes, "PriorityScore": additionalData.PriorityScore, "SnykWebUrl": config.CurrentConfig().SnykUi(), "LessonUrl": issue.LessonUrl, @@ -106,6 +109,12 @@ func getCodeDetailsHtml(issue snyk.Issue) string { "IgnoreLineAction": getLineToIgnoreAction(issue), "HasAIFix": additionalData.HasAIFix, "ExternalIcon": getExternalIconSvg(), + "ScanAnimation": getScanAnimationSvg(), + "GitHubIcon": getGitHubIconSvg(), + "ArrowLeftDark": getArrowLeftDarkSvg(), + "ArrowLeftLight": getArrowLeftLightSvg(), + "ArrowRightDark": getArrowRightDarkSvg(), + "ArrowRightLight": getArrowRightLightSvg(), } if issue.IsIgnored { @@ -199,20 +208,28 @@ func prepareExampleLines(lines []snyk.CommitChangeLine) []ExampleLines { return exampleLines } -func prepareExampleCommitFixes(fixes []snyk.ExampleCommitFix) []ExampleCommit { +func prepareExampleCommits(fixes []snyk.ExampleCommitFix) []ExampleCommit { var fixData []ExampleCommit for _, fix := range fixes { fixData = append(fixData, ExampleCommit{ CommitURL: fix.CommitURL, - IconSVG: getVendorIconSvg(), RepoName: getRepoName(fix.CommitURL), - ExampleLines: prepareExampleLines(fix.Lines), RepoLink: fix.CommitURL, + ExampleLines: prepareExampleLines(fix.Lines), }) } return fixData } +func parseExampleCommitsToTemplateJS(fixes []ExampleCommit) template.JS { + jsonFixes, err := json.Marshal(fixes) + if err != nil { + config.CurrentConfig().Logger().Error().Msgf("Failed to marshal example commit fixes: %v", err) + return "" + } + return template.JS(jsonFixes) +} + func getIssueType(additionalData snyk.CodeIssueData) string { if additionalData.IsSecurityType { return "Vulnerability" @@ -244,11 +261,10 @@ func formatDate(date time.Time) string { } func getExternalIconSvg() template.HTML { - return template.HTML(` - - - `) + return template.HTML(` + + + `) } func getSeverityIconSvg(issue snyk.Issue) template.HTML { @@ -278,7 +294,7 @@ func getSeverityIconSvg(issue snyk.Issue) template.HTML { } } -func getVendorIconSvg() template.HTML { +func getGitHubIconSvg() template.HTML { return template.HTML(` `) } + +func getScanAnimationSvg() template.HTML { + return template.HTML(` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + `) +} + +func getArrowLeftDarkSvg() template.HTML { + return template.HTML(` + + `) +} + +func getArrowLeftLightSvg() template.HTML { + return template.HTML(` + + `) +} + +func getArrowRightDarkSvg() template.HTML { + return template.HTML(` + + + `) +} + +func getArrowRightLightSvg() template.HTML { + return template.HTML(` + + `) +} diff --git a/infrastructure/code/code_html_test.go b/infrastructure/code/code_html_test.go index 37fd693dd..b3ce5cb67 100644 --- a/infrastructure/code/code_html_test.go +++ b/infrastructure/code/code_html_test.go @@ -78,12 +78,9 @@ func Test_Code_Html_getCodeDetailsHtml(t *testing.T) { // 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)) + expectedFixesDescription := fmt.Sprintf(`This type of vulnerability was fixed in %d open source projects.`, repoCount) assert.Regexp(t, regexp.MustCompile(expectedFixesDescription), codePanelHtml) - assert.Contains(t, codePanelHtml, ``, "Two tabs, first is selected") - assert.Contains(t, codePanelHtml, ` `, "Second tab is present") - assert.Contains(t, codePanelHtml, `href="https://github.com/apache/tomcat/commit/0fa9d5547c5300cf8162b8f31a40aea6847a5c32?diff=split#diff-7e23eb1aa3b7b4d5db89bfd2860277e5L75"`, "Repo name has GitHub link") + assert.Contains(t, codePanelHtml, ``, "GitHub icon preceding the repo name is present") // assert Footer assert.Contains(t, codePanelHtml, `id="action-ignore-line">68`) @@ -119,7 +116,7 @@ func Test_Code_Html_getCodeDetailsHtml_withAIfix(t *testing.T) { // assert Fixes section assert.Contains(t, codePanelHtml, ` id="ai-fix-wrapper" class="">`) - assert.Contains(t, codePanelHtml, `Generate fix using Snyk DeepCode AI`) + assert.Contains(t, codePanelHtml, `Generate fix using Snyk`) assert.Contains(t, codePanelHtml, ` id="no-ai-fix-wrapper" class="hidden">`) } diff --git a/infrastructure/code/template/details.html b/infrastructure/code/template/details.html index ba97d86b8..346f9cc7d 100644 --- a/infrastructure/code/template/details.html +++ b/infrastructure/code/template/details.html @@ -19,9 +19,9 @@ - + + content="default-src 'none'; style-src 'self' 'nonce-${nonce}'; script-src 'nonce-${nonce}';" />