Skip to content

Commit

Permalink
fix/autofix: fix the handling of empty ai fix suggestions on successf…
Browse files Browse the repository at this point in the history
…ul run
  • Loading branch information
BBerabi committed Sep 17, 2024
1 parent ecbb8cf commit b019003
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
3 changes: 2 additions & 1 deletion domain/ide/command/code_fix_diffs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (cmd *codeFixDiffs) Execute(ctx context.Context) (any, error) {

suggestions, err := cmd.codeScanner.GetAutoFixDiffs(ctx, folderPath, relPath, issue)
if err == nil && len(suggestions) == 0 {
logger.Info().Msgf("Autofix run successfully but there were no fixes")
logger.Info().Msgf("Autofix run successfully but there were no good fixes")
return suggestions, err
}
if err != nil {
// as long as the backend service doesn't support good error handling, we'll just log the error
Expand Down
48 changes: 36 additions & 12 deletions infrastructure/code/autofix.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (a AutofixUnifiedDiffSuggestion) GetUnifiedDiffForFile(filePath string) str
return a.UnifiedDiffsPerFile[filePath]
}

func (s *SnykCodeHTTPClient) GetAutoFixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error) {
func (s *SnykCodeHTTPClient) GetAutoFixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (
unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion,
status AutofixStatus,
err error,
) {
method := "GetAutoFixDiffs"
logger := config.CurrentConfig().Logger().With().Str("method", method).Logger()
span := s.instrumentor.StartSpan(ctx, method)
Expand All @@ -51,20 +55,36 @@ func (s *SnykCodeHTTPClient) GetAutoFixDiffs(ctx context.Context, baseDir string
requestId, err := performance2.GetTraceId(ctx)
if err != nil {
logger.Err(err).Msg(failedToObtainRequestIdString + err.Error())
return unifiedDiffSuggestions, err
return unifiedDiffSuggestions, status, err
}

logger.Info().Str("requestId", requestId).Msg("Started obtaining autofix diffs")
defer logger.Info().Str("requestId", requestId).Msg("Finished obtaining autofix diffs")

response, err = s.RunAutofix(span.Context(), options)
// todo(berkay): burada fixlenebilir. burasi unufied diff flow, code action flow nasil?
if err != nil || response.Status == failed.message {
logger.Err(err).Msg("error getting autofix suggestions")
return unifiedDiffSuggestions, err
if err != nil {
return unifiedDiffSuggestions, status, err
}

logger.Debug().Msgf("Status: %s", response.Status)

if response.Status == failed.message {
logger.Err(err).Str("responseStatus", response.Status).Msg("autofix failed")
return nil, failed, err
}

return response.toUnifiedDiffSuggestions(baseDir, options.filePath), err
if response.Status == "" {
s.c.Logger().Err(err).Str("responseStatus", response.Status).Msg("unknown response status (empty)")
return nil, failed, err
}

status = AutofixStatus{message: response.Status}
if response.Status != completeStatus {
return nil, status, nil
}

logger.Debug().Msgf("Running toUnifiedDiffSuggestions")
return response.toUnifiedDiffSuggestions(baseDir, options.filePath), AutofixStatus{message: response.Status}, err
}

func (sc *Scanner) GetAutoFixDiffs(
Expand Down Expand Up @@ -110,14 +130,18 @@ func (sc *Scanner) GetAutoFixDiffs(
logger.Error().Msg(msg)
return nil, errors.New(msg)
case <-ticker.C:
suggestions, err := codeClient.GetAutoFixDiffs(span.Context(), baseDir, options)
suggestions, fixStatus, err := codeClient.GetAutoFixDiffs(span.Context(), baseDir, options)
if err != nil {
logger.Err(err).Msg("Error getting autofix suggestions")
return nil, err
}
// todo(berkay): Change to status check
if len(suggestions) > 0 {
// return nil, err
return suggestions, err
} else if fixStatus.message == completeStatus {
if len(suggestions) > 0 {
return suggestions, nil
} else {
logger.Debug().Msg("Successful return but empty no good fix could be computed.")
return suggestions, nil
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions infrastructure/code/fake_snyk_code_api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ type FakeSnykCodeClient struct {
maxConcurrentScans int
NoFixSuggestions bool
UnifiedDiffSuggestions []AutofixUnifiedDiffSuggestion
AutofixStatus AutofixStatus
Options map[string]AnalysisOptions
C *config.Config
}

func (f *FakeSnykCodeClient) GetAutoFixDiffs(_ context.Context, _ string, _ AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, err error) {
return f.UnifiedDiffSuggestions, nil
func (f *FakeSnykCodeClient) GetAutoFixDiffs(_ context.Context, _ string, _ AutofixOptions) (unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion, status AutofixStatus, err error) {
// return f.UnifiedDiffSuggestions, nil, nil
return f.UnifiedDiffSuggestions, f.AutofixStatus, nil
}

func (f *FakeSnykCodeClient) addCall(params []any, op string) {
Expand Down
1 change: 0 additions & 1 deletion infrastructure/code/issue_enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func (b *IssueEnhancer) autofixFunc(ctx context.Context, issue snyk.Issue,

// Polling function just calls the endpoint and registers result, signaling `done` to the
// channel.
// todo(berkay): the code action polling, somewhat correct.
pollFunc := func() (fix *AutofixSuggestion, complete bool) {
logger.Debug().Str("requestId", b.requestId).Msg("polling")
fixSuggestions, fixStatus, err := b.SnykCode.GetAutofixSuggestions(s.Context(), autofixOptions, b.rootPath)
Expand Down
6 changes: 5 additions & 1 deletion infrastructure/code/snyk_code_http_client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,9 @@ type SnykCodeClient interface {

SubmitAutofixFeedback(ctx context.Context, fixId string, positive bool) error

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

0 comments on commit b019003

Please sign in to comment.