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 9, 2025
1 parent 87b7182 commit d6994a2
Show file tree
Hide file tree
Showing 3 changed files with 90 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
54 changes: 54 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,55 @@ func TestGetCodeApiUrl(t *testing.T) {
assert.Equal(t, c.SnykCodeApi(), url)
})
}

func TestAutofixRequestBody(t *testing.T) {
options := AutofixOptions{
bundleHash: "0123456789abcdef",
filePath: "/path/to/file",
issue: snyk.Issue{
Range: snyk.Range{
Start: snyk.Position{
Line: 0,
Character: 0,
},
End: snyk.Position{
Line: 1,
Character: 0,
},
},
AdditionalData: snyk.CodeIssueData{
RuleId: "language/rule_id",
},
},
}

c := config.CurrentConfig()
c.SetIdeName("my IDE")
c.SetIdeVersion("1.0.0")
c.SetIntegrationName("my extension")
c.SetIntegrationVersion("1.2.3")
s := NewSnykCodeHTTPClient(c, NewCodeInstrumentor(), newTestCodeErrorReporter(), clientFunc)
jsonBody, _ := s.autofixRequestBody(&options)

var body AutofixRequest
json.Unmarshal(jsonBody, &body)

expectedBody := AutofixRequest{
Key: AutofixRequestKey{
Type: "file",
Hash: "0123456789abcdef",
FilePath: "/path/to/file",
RuleId: "rule_id",
LineNum: 1,
},
AnalysisContext: newCodeRequestContext(),
IdeExtensionDetails: AutofixIdeExtensionDetails{
IdeName: "my IDE",
IdeVersion: "1.0.0",
ExtensionName: "my extension",
ExtensionVersion: "1.2.3",
},
}

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 d6994a2

Please sign in to comment.