Skip to content

Commit 9dd78b9

Browse files
authored
Feat/ai fix feedback command [IDE-634] (#672)
1 parent 1c41d37 commit 9dd78b9

File tree

8 files changed

+76
-50
lines changed

8 files changed

+76
-50
lines changed

domain/ide/command/code_fix_feedback.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ package command
1818

1919
import (
2020
"context"
21-
21+
"github.com/snyk/snyk-ls/application/config"
2222
"github.com/snyk/snyk-ls/internal/types"
2323
)
2424

2525
type SnykCodeHttpClient interface {
26-
SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error
26+
SubmitAutofixFeedback(ctx context.Context, fixId string, positive string) error
2727
}
2828

2929
type codeFixFeedback struct {
@@ -38,11 +38,14 @@ func (cmd *codeFixFeedback) Command() types.CommandData {
3838
func (cmd *codeFixFeedback) Execute(ctx context.Context) (any, error) {
3939
args := cmd.command.Arguments
4040
fixId := args[0].(string)
41-
positive := args[1].(bool)
42-
err := cmd.apiClient.SubmitAutofixFeedback(ctx, fixId, positive)
43-
if err != nil {
44-
return nil, err
45-
}
41+
feedback := args[1].(string)
42+
43+
go func() {
44+
err := cmd.apiClient.SubmitAutofixFeedback(ctx, fixId, feedback)
45+
if err != nil {
46+
config.CurrentConfig().Logger().Err(err).Str("fixId", fixId).Str("feedback", feedback).Msg("failed to submit autofix feedback")
47+
}
48+
}()
4649

4750
return nil, nil
4851
}

domain/ide/command/code_fix_feedback_test.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,56 @@ package command
1919
import (
2020
"context"
2121
"errors"
22+
"sync"
2223
"testing"
24+
"time"
2325

2426
"github.com/stretchr/testify/assert"
2527

28+
"github.com/snyk/snyk-ls/infrastructure/code"
2629
"github.com/snyk/snyk-ls/internal/types"
2730
)
2831

2932
type fakeCodeHttpClient struct {
3033
shouldError bool
31-
feedbackSubmitted bool
34+
feedbackSubmitted string
35+
fixId string
36+
mu sync.Mutex
3237
}
3338

34-
func (c *fakeCodeHttpClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error {
39+
func (c *fakeCodeHttpClient) SubmitAutofixFeedback(ctx context.Context, fixId string, feedback string) error {
40+
c.mu.Lock()
41+
c.feedbackSubmitted = feedback
42+
c.fixId = fixId
43+
c.mu.Unlock()
44+
3545
if !c.shouldError {
36-
c.feedbackSubmitted = true
3746
return nil
3847
}
3948

4049
return errors.New("api call failed")
4150
}
4251

43-
func Test_codeFixFeedback_SubmittedSuccessfully(t *testing.T) {
44-
apiClient := fakeCodeHttpClient{}
45-
codeFixFeedbackCmd := codeFixFeedback{
46-
command: types.CommandData{
47-
Arguments: []any{"fixId", true},
48-
},
49-
apiClient: &apiClient,
50-
}
52+
func FeedbackSubmitted(c *fakeCodeHttpClient) string {
53+
c.mu.Lock()
54+
defer c.mu.Unlock()
5155

52-
_, err := codeFixFeedbackCmd.Execute(context.Background())
53-
assert.NoError(t, err)
54-
assert.True(t, apiClient.feedbackSubmitted)
56+
return c.feedbackSubmitted
5557
}
5658

57-
func Test_codeFixFeedback_SubmissionFailed(t *testing.T) {
58-
apiClient := fakeCodeHttpClient{
59-
shouldError: true,
60-
}
59+
func Test_codeFixFeedback_SubmittedSuccessfully(t *testing.T) {
60+
apiClient := fakeCodeHttpClient{}
6161
codeFixFeedbackCmd := codeFixFeedback{
6262
command: types.CommandData{
63-
Arguments: []any{"fixId", true},
63+
Arguments: []any{"fixId", code.FixPositiveFeedback},
6464
},
6565
apiClient: &apiClient,
6666
}
6767

6868
_, err := codeFixFeedbackCmd.Execute(context.Background())
69-
assert.Error(t, err)
70-
assert.False(t, apiClient.feedbackSubmitted)
69+
70+
assert.NoError(t, err)
71+
assert.Eventually(t, func() bool {
72+
return FeedbackSubmitted(&apiClient) != ""
73+
}, 2*time.Second, time.Millisecond)
7174
}

infrastructure/code/constants.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* © 2024 Snyk Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package code
18+
19+
const (
20+
FixPositiveFeedback string = "FIX_POSITIVE_FEEDBACK"
21+
FixNegativeFeedback string = "FIX_NEGATIVE_FEEDBACK"
22+
)

infrastructure/code/fake_snyk_code_api_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,6 @@ func (f *FakeSnykCodeClient) GetAutofixSuggestions(
340340
return suggestions, AutofixStatus{message: "COMPLETE"}, nil
341341
}
342342

343-
func (f *FakeSnykCodeClient) SubmitAutofixFeedback(_ context.Context, _ string, _ bool) error {
343+
func (f *FakeSnykCodeClient) SubmitAutofixFeedback(_ context.Context, _ string, _ string) error {
344344
return nil
345345
}

infrastructure/code/issue_enhancer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,16 @@ func ToEncodedNormalizedPath(rootPath string, filePath string) (string, error) {
251251
}
252252

253253
func (b *IssueEnhancer) autofixFeedbackActions(fixId string) (*data_structure.OrderedMap[types.MessageAction, types.CommandData], error) {
254-
createCommandData := func(positive bool) types.CommandData {
254+
createCommandData := func(feedback string) types.CommandData {
255255
return types.CommandData{
256256
Title: types.CodeSubmitFixFeedback,
257257
CommandId: types.CodeSubmitFixFeedback,
258-
Arguments: []any{fixId, positive},
258+
Arguments: []any{fixId, feedback},
259259
}
260260
}
261261
actionCommandMap := data_structure.NewOrderedMap[types.MessageAction, types.CommandData]()
262-
positiveFeedbackCmd := createCommandData(true)
263-
negativeFeedbackCmd := createCommandData(false)
262+
positiveFeedbackCmd := createCommandData(FixPositiveFeedback)
263+
negativeFeedbackCmd := createCommandData(FixNegativeFeedback)
264264

265265
actionCommandMap.Add("👍", positiveFeedbackCmd)
266266
actionCommandMap.Add("👎", negativeFeedbackCmd)

infrastructure/code/issue_enhancer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ func Test_autofixFunc(t *testing.T) {
108108
commandData1 := types.CommandData{
109109
Title: types.CodeSubmitFixFeedback,
110110
CommandId: types.CodeSubmitFixFeedback,
111-
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", true},
111+
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", FixPositiveFeedback},
112112
}
113113
commandData2 := types.CommandData{
114114
Title: types.CodeSubmitFixFeedback,
115115
CommandId: types.CodeSubmitFixFeedback,
116-
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", false},
116+
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", FixNegativeFeedback},
117117
}
118118
positiveFeedback := types.MessageAction("👍")
119119
negativeFeedback := types.MessageAction("👎")
@@ -156,12 +156,12 @@ func Test_autofixFunc(t *testing.T) {
156156
commandData1 := types.CommandData{
157157
Title: types.CodeSubmitFixFeedback,
158158
CommandId: types.CodeSubmitFixFeedback,
159-
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", true},
159+
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", FixPositiveFeedback},
160160
}
161161
commandData2 := types.CommandData{
162162
Title: types.CodeSubmitFixFeedback,
163163
CommandId: types.CodeSubmitFixFeedback,
164-
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", false},
164+
Arguments: []any{"123e4567-e89b-12d3-a456-426614174000/1", FixNegativeFeedback},
165165
}
166166
positiveFeedback := types.MessageAction("👍")
167167
negativeFeedback := types.MessageAction("👎")

infrastructure/code/snyk_code_http_client.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func (s *SnykCodeHTTPClient) autofixRequestBody(options *AutofixOptions) ([]byte
548548
return requestBody, err
549549
}
550550

551-
func (s *SnykCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error {
551+
func (s *SnykCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId string, feedback string) error {
552552
method := "code.SubmitAutofixFeedback"
553553
span := s.instrumentor.StartSpan(ctx, method)
554554
defer s.instrumentor.Finish(span)
@@ -562,25 +562,23 @@ func (s *SnykCodeHTTPClient) SubmitAutofixFeedback(ctx context.Context, fixId st
562562
s.c.Logger().Debug().Str("method", method).Str("requestId", requestId).Msg("API: Submitting Autofix feedback")
563563
defer s.c.Logger().Debug().Str("method", method).Str("requestId", requestId).Msg("API: Submitting Autofix feedback done")
564564

565-
var feedback string
566-
if positive {
567-
feedback = "POSITIVE"
568-
} else {
569-
feedback = "NEGATIVE"
565+
request := map[string]interface{}{
566+
"channel": "IDE",
567+
"eventType": feedback,
568+
"eventDetails": map[string]string{
569+
"fixId": fixId,
570+
},
571+
"analysisContext": newCodeRequestContext(),
570572
}
571573

572-
request := AutofixFeedback{
573-
FixId: fixId,
574-
Feedback: feedback,
575-
AnalysisContext: newCodeRequestContext(),
576-
}
577574
requestBody, err := json.Marshal(request)
575+
s.c.Logger().Err(err).Str("method", method).Str("requestBody", string(requestBody)).Msg("request body for autofix feedback")
578576
if err != nil {
579577
s.c.Logger().Err(err).Str("method", method).Str("requestBody", string(requestBody)).Msg("error creating request body for autofix feedback")
580578
return err
581579
}
582580

583-
responseBody, err := s.doCall(span.Context(), "POST", "/autofix/feedback", requestBody)
581+
responseBody, err := s.doCall(span.Context(), "POST", "/autofix/event", requestBody)
584582
if err != nil {
585583
s.c.Logger().Err(err).Str("method", method).Str("responseBody", string(responseBody)).Msg("error response for autofix feedback")
586584
return err

infrastructure/code/snyk_code_http_client_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type SnykCodeClient interface {
7171
error,
7272
)
7373

74-
SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error
74+
SubmitAutofixFeedback(ctx context.Context, fixId string, result string) error
7575

7676
GetAutoFixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error)
7777
}

0 commit comments

Comments
 (0)