diff --git a/infrastructure/code/snyk_code_http_client.go b/infrastructure/code/snyk_code_http_client.go index 37d1b7482..d11f3af4c 100644 --- a/infrastructure/code/snyk_code_http_client.go +++ b/infrastructure/code/snyk_code_http_client.go @@ -539,6 +539,12 @@ func (s *SnykCodeHTTPClient) autofixRequestBody(options *AutofixOptions) ([]byte LineNum: options.issue.Range.Start.Line + 1, }, AnalysisContext: newCodeRequestContext(), + IdeExtensionDetails: AutofixIdeExtensionDetails{ + IdeName: s.c.IdeName(), + IdeVersion: s.c.IdeVersion(), + ExtensionName: s.c.IntegrationName(), + ExtensionVersion: s.c.IntegrationVersion(), + }, } if len(options.shardKey) > 0 { request.Key.Shard = options.shardKey @@ -562,13 +568,17 @@ func (s *SnykCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId st s.c.Logger().Debug().Str("method", method).Str("requestId", requestId).Msg("API: Submitting Autofix feedback") defer s.c.Logger().Debug().Str("method", method).Str("requestId", requestId).Msg("API: Submitting Autofix feedback done") - request := map[string]interface{}{ - "channel": "IDE", - "eventType": feedback, - "eventDetails": map[string]string{ - "fixId": fixId, + request := AutofixUserEvent{ + Channel: "IDE", + EventType: feedback, + EventDetails: AutofixEventDetails{FixId: fixId}, + AnalysisContext: newCodeRequestContext(), + IdeExtensionDetails: AutofixIdeExtensionDetails{ + IdeName: s.c.IdeName(), + IdeVersion: s.c.IdeVersion(), + ExtensionName: s.c.IntegrationName(), + ExtensionVersion: s.c.IntegrationVersion(), }, - "analysisContext": newCodeRequestContext(), } requestBody, err := json.Marshal(request) diff --git a/infrastructure/code/snyk_code_http_client_test.go b/infrastructure/code/snyk_code_http_client_test.go index 4e98271c0..5e3b85ef9 100644 --- a/infrastructure/code/snyk_code_http_client_test.go +++ b/infrastructure/code/snyk_code_http_client_test.go @@ -18,6 +18,7 @@ package code import ( "context" + "encoding/json" "fmt" "net/http" "os" @@ -31,6 +32,7 @@ import ( "github.com/google/uuid" "github.com/snyk/snyk-ls/application/config" + "github.com/snyk/snyk-ls/domain/snyk" "github.com/snyk/snyk-ls/internal/testutil" "github.com/snyk/snyk-ls/internal/util" ) @@ -265,3 +267,65 @@ func TestGetCodeApiUrl(t *testing.T) { assert.Equal(t, c.SnykCodeApi(), url) }) } + +func TestAutofixRequestBody(t *testing.T) { + c := testutil.UnitTest(t) + + const testBundleHash = "0123456789abcdef" + const testFilePath = "/path/to/file" + const testLineNumber0Based = 0 + const testLanguage = "language" + const testRuleId = "rule_id" + const testIdeName = "my IDE" + const testIdeVersion = "1.0.0" + const testExtensionName = "my extension" + const testExtensionVersion = "1.2.3" + + options := AutofixOptions{ + bundleHash: testBundleHash, + filePath: testFilePath, + issue: snyk.Issue{ + Range: snyk.Range{ + Start: snyk.Position{ + Line: testLineNumber0Based, + Character: 0, + }, + End: snyk.Position{ + Line: testLineNumber0Based + 5, + Character: 0, + }, + }, + AdditionalData: snyk.CodeIssueData{ + RuleId: testLanguage + "/" + testRuleId, + }, + }, + } + c.SetIdeName(testIdeName) + c.SetIdeVersion(testIdeVersion) + c.SetIntegrationName(testExtensionName) + c.SetIntegrationVersion(testExtensionVersion) + s := NewSnykCodeHTTPClient(c, NewCodeInstrumentor(), newTestCodeErrorReporter(), clientFunc) + jsonBody, _ := s.autofixRequestBody(&options) + + var body AutofixRequest + json.Unmarshal(jsonBody, &body) + + expectedBody := AutofixRequest{ + Key: AutofixRequestKey{ + Type: "file", + Hash: testBundleHash, + FilePath: testFilePath, + RuleId: testRuleId, + LineNum: testLineNumber0Based + 1, + }, + AnalysisContext: newCodeRequestContext(), + IdeExtensionDetails: AutofixIdeExtensionDetails{ + IdeName: testIdeName, + IdeVersion: testIdeVersion, + ExtensionName: testExtensionName, + ExtensionVersion: testExtensionVersion, + }, + } + + assert.Equal(t, expectedBody, body) +} diff --git a/infrastructure/code/types.go b/infrastructure/code/types.go index 37fa46ecf..82c4acb81 100644 --- a/infrastructure/code/types.go +++ b/infrastructure/code/types.go @@ -77,9 +77,17 @@ type AutofixRequestKey struct { LineNum int `json:"lineNum"` } +type AutofixIdeExtensionDetails struct { + IdeName string `json:"ideName"` + IdeVersion string `json:"ideVersion"` + ExtensionName string `json:"extensionName"` + ExtensionVersion string `json:"extensionVersion"` +} + type AutofixRequest struct { - Key AutofixRequestKey `json:"key"` - AnalysisContext codeRequestContext `json:"analysisContext"` + Key AutofixRequestKey `json:"key"` + AnalysisContext codeRequestContext `json:"analysisContext"` + IdeExtensionDetails AutofixIdeExtensionDetails `json:"ideExtensionDetails"` } // Should implement `error` interface @@ -95,8 +103,14 @@ type AutofixSuggestion struct { AutofixEdit snyk.WorkspaceEdit } -type AutofixFeedback struct { - FixId string `json:"fixId"` - Feedback string `json:"feedback"` - AnalysisContext codeRequestContext `json:"analysisContext"` +type AutofixEventDetails struct { + FixId string `json:"fixId"` +} + +type AutofixUserEvent struct { + AnalysisContext codeRequestContext `json:"analysisContext"` + Channel string `json:"channel"` + EventType string `json:"eventType"` + EventDetails AutofixEventDetails `json:"eventDetails"` + IdeExtensionDetails AutofixIdeExtensionDetails `json:"ideExtensionDetails"` }