Skip to content

Commit

Permalink
fix: autofix html decoding (#691)
Browse files Browse the repository at this point in the history
Co-authored-by: Abdelrahman Shawki Hassan <[email protected]>
  • Loading branch information
BBerabi and ShawkyZ authored Oct 9, 2024
1 parent a8e770a commit f391f56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion infrastructure/code/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,16 @@ func createAutofixWorkspaceEdit(absoluteFilePath string, fixedSourceCode string)

// toAutofixSuggestionsIssues converts the HTTP json-first payload to the domain type
func (s *AutofixResponse) toAutofixSuggestions(baseDir string, filePath string) (fixSuggestions []AutofixSuggestion) {
logger := config.CurrentConfig().Logger().With().Str("method", "toAutofixSuggestions").Logger()
for _, suggestion := range s.AutofixSuggestions {
decodedPath, err := DecodePath(ToAbsolutePath(baseDir, filePath))
if err != nil {
logger.Err(err).Msgf("cannot decode filePath %s", filePath)
continue
}
d := AutofixSuggestion{
FixId: suggestion.Id,
AutofixEdit: createAutofixWorkspaceEdit(ToAbsolutePath(baseDir, filePath), suggestion.Value),
AutofixEdit: createAutofixWorkspaceEdit(decodedPath, suggestion.Value),
}
fixSuggestions = append(fixSuggestions, d)
}
Expand Down
35 changes: 35 additions & 0 deletions infrastructure/code/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,41 @@ func Test_AutofixResponse_toAutofixSuggestion(t *testing.T) {
assert.Contains(t, editValues, "test1", "test2")
}

func Test_AutofixResponse_toAutofixSuggestion_HtmlEncodedFilePath(t *testing.T) {
response := AutofixResponse{
Status: "COMPLETE",
}
fixes := []autofixResponseSingleFix{{
Id: "123e4567-e89b-12d3-a456-426614174000/1",
Value: "test1",
}, {
Id: "123e4567-e89b-12d3-a456-426614174000/2",
Value: "test2",
}}
response.AutofixSuggestions = append(response.AutofixSuggestions, fixes...)
filePath := "file_with space.js"
baseDir := t.TempDir()
err := os.WriteFile(filepath.Join(baseDir, filePath), []byte("test test test"), 0666)
require.NoError(t, err)
// Here, we provide the HTML encoded path and expect toAutofixSuggestions to decode it.
edits := response.toAutofixSuggestions(baseDir, "file_with%20space.js")
editValues := make([]string, 0)
editFilePaths := make([]string, 0)
for _, edit := range edits {
change := edit.AutofixEdit.Changes[ToAbsolutePath(baseDir, filePath)][0]
editValues = append(editValues, change.NewText)

for key := range edit.AutofixEdit.Changes {
editFilePaths = append(editFilePaths, key)
}
}

assert.Contains(t, editValues, "test1", "test2")
for _, filePath := range editFilePaths {
assert.Contains(t, filePath, "file_with space.js")
}
}

func Test_AutofixResponse_toUnifiedDiffSuggestions(t *testing.T) {
response := AutofixResponse{
Status: "COMPLETE",
Expand Down

0 comments on commit f391f56

Please sign in to comment.