Skip to content

Commit

Permalink
Merge branch 'main' into fix/make-server-processing-concurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ authored Oct 24, 2024
2 parents 3e0fc79 + 2dc3999 commit d7b3772
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
39 changes: 26 additions & 13 deletions infrastructure/code/autofix.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,56 @@ func (s *SnykCodeHTTPClient) GetAutofixDiffs(ctx context.Context, baseDir string
err error,
) {
method := "GetAutofixDiffs"
span := s.instrumentor.StartSpan(ctx, method)
defer s.instrumentor.Finish(span)
logger := config.CurrentConfig().Logger().With().Str("method", method).Logger()
logger.Info().Msg("Started obtaining autofix diffs")
defer logger.Info().Msg("Finished obtaining autofix diffs")

autofixResponse, status, err := s.getAutofixResponse(ctx, options)
if err != nil {
return nil, status, err
}
return autofixResponse.toUnifiedDiffSuggestions(baseDir, options.filePath), status, err
}

func (s *SnykCodeHTTPClient) getAutofixResponse(ctx context.Context, options AutofixOptions) (autofixResponse AutofixResponse, status AutofixStatus, err error) {
method := "getAutofixResponse"
span := s.instrumentor.StartSpan(ctx, method)
defer s.instrumentor.Finish(span)
logger := config.CurrentConfig().Logger().With().Str("method", method).Logger()

requestId, err := performance2.GetTraceId(ctx)
if err != nil {
logger.Err(err).Msg(failedToObtainRequestIdString + err.Error())
return nil, failed, err
return autofixResponse, failed, err
}

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

response, err := s.RunAutofix(span.Context(), options)
if err != nil {
return nil, failed, err
return response, failed, err
}

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

if response.Status == failed.message {
logger.Error().Str("responseStatus", response.Status).Msg("autofix failed")
return nil, failed, errors.New("Autofix failed")
return response, failed, errors.New("Autofix failed")
}

if response.Status == "" {
logger.Error().Str("responseStatus", response.Status).Msg("unknown response status (empty)")
return nil, failed, errors.New("Unknown response status (empty)")
return response, failed, errors.New("Unknown response status (empty)")
}

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

return response.toUnifiedDiffSuggestions(baseDir, options.filePath), AutofixStatus{message: response.Status}, err
return response, status, nil
}

func (sc *Scanner) GetAutofixDiffs(
Expand Down Expand Up @@ -132,12 +147,10 @@ func (sc *Scanner) GetAutofixDiffs(
logger.Err(autofixErr).Msg("Error getting autofix suggestions")
return nil, autofixErr
} else if fixStatus.message == completeStatus {
if len(suggestions) > 0 {
return suggestions, nil
} else {
if len(suggestions) == 0 {
logger.Info().Msg("AI fix returned successfully but no good fix could be computed.")
return suggestions, nil
}
return suggestions, nil
}
// If err == nil and fixStatus.message != completeStatus, we will keep polling.
}
Expand Down
3 changes: 2 additions & 1 deletion infrastructure/code/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,10 @@ func (s *AutofixResponse) toUnifiedDiffSuggestions(baseDir string, filePath stri
if err != nil {
logger.Err(err).Msgf("cannot decode filePath %s", filePath)
}
logger.Info().Msgf("File path %s", path)
fileContent, err := os.ReadFile(path)
if err != nil {
logger.Err(err).Msgf("cannot read fileContent %s", baseDir)
logger.Err(err).Msgf("cannot read fileContent %s", path)
return fixSuggestions
}
contentBefore := string(fileContent)
Expand Down
5 changes: 5 additions & 0 deletions infrastructure/code/fake_snyk_code_api_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ func (f *FakeSnykCodeClient) GetAutofixDiffs(_ context.Context, _ string, _ Auto
return f.UnifiedDiffSuggestions, f.AutofixStatus, nil
}

func (f *FakeSnykCodeClient) getAutofixResponse(_ context.Context, _ AutofixOptions) (autofixResponse AutofixResponse, status AutofixStatus, err error) {
f.AutofixStatus = AutofixStatus{message: completeStatus}
return autofixResponse, f.AutofixStatus, nil
}

func (f *FakeSnykCodeClient) addCall(params []any, op string) {
if f.Calls == nil {
f.Calls = make(map[string][][]any)
Expand Down
30 changes: 7 additions & 23 deletions infrastructure/code/snyk_code_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,31 +478,15 @@ func (s *SnykCodeHTTPClient) GetAutofixSuggestions(
Str("method", method).
Str("requestId", span.GetTraceId()).Logger()

var response AutofixResponse
response, err = s.RunAutofix(span.Context(), options)
if err != nil {
return autofixSuggestions, 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
}
logger.Info().Msg("Started obtaining autofix suggestions")
defer logger.Info().Msg("Finished obtaining autofix suggestions")

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
autofixResponse, status, err := s.getAutofixResponse(ctx, options)
if err != nil {
return nil, status, err
}

suggestions := response.toAutofixSuggestions(baseDir, options.filePath)
return suggestions, AutofixStatus{message: response.Status}, nil
suggestions := autofixResponse.toAutofixSuggestions(baseDir, options.filePath)
return suggestions, status, nil
}

func (s *SnykCodeHTTPClient) RunAutofix(ctx context.Context, options AutofixOptions) (AutofixResponse, error) {
Expand Down
2 changes: 2 additions & 0 deletions infrastructure/code/snyk_code_http_client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type SnykCodeClient interface {

SubmitAutofixFeedback(ctx context.Context, fixId string, result string) error

getAutofixResponse(ctx context.Context, options AutofixOptions) (autofixResponse AutofixResponse, status AutofixStatus, err error)

GetAutofixDiffs(ctx context.Context, baseDir string, options AutofixOptions) (
unifiedDiffSuggestions []AutofixUnifiedDiffSuggestion,
status AutofixStatus,
Expand Down

0 comments on commit d7b3772

Please sign in to comment.