Skip to content

Commit

Permalink
feat: Sending IDE+extension versions to autofix
Browse files Browse the repository at this point in the history
This is for analytics purposes as different IDEs extensions might
support different features at a certain point in time.

feat: Adding test to autofixRequestBody
  • Loading branch information
antoine-snyk committed Jan 10, 2025
1 parent 71ea618 commit fb74b26
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
22 changes: 16 additions & 6 deletions infrastructure/code/snyk_code_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions infrastructure/code/snyk_code_http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package code

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
}
26 changes: 20 additions & 6 deletions infrastructure/code/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}

0 comments on commit fb74b26

Please sign in to comment.