Skip to content

Refactor/auto fix ccg #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 22, 2025
Merged
17 changes: 15 additions & 2 deletions llm/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -128,14 +129,20 @@ func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, requestId strin

logger := d.logger.With().Str("method", "code.RunAutofix").Str("requestId", requestId).Logger()

endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/suggestions", options.Host))
if err != nil {
logger.Err(err).Str("host", options.Host).Msg("error creating endpoint URL")
return AutofixResponse{}, failed, err
}

requestBody, err := d.autofixRequestBody(&options)
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body")
return AutofixResponse{}, failed, err
}

logger.Info().Msg("Started obtaining autofix Response")
responseBody, err := d.submitRequest(ctx, options.Endpoint, requestBody)
responseBody, err := d.submitRequest(ctx, endpoint, requestBody)
logger.Info().Msg("Finished obtaining autofix Response")

if err != nil {
Expand Down Expand Up @@ -198,14 +205,20 @@ func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, requ

logger := d.logger.With().Str("method", "code.SubmitAutofixFeedback").Str("requestId", requestId).Logger()

endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/event", options.Host))
if err != nil {
logger.Err(err).Str("host", options.Host).Msg("error creating endpoint URL")
return err
}

requestBody, err := d.autofixFeedbackRequestBody(&options)
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body")
return err
}

logger.Info().Msg("Started obtaining autofix Response")
_, err = d.submitRequest(ctx, options.Endpoint, requestBody)
_, err = d.submitRequest(ctx, endpoint, requestBody)
logger.Info().Msg("Finished obtaining autofix Response")

return err
Expand Down
68 changes: 68 additions & 0 deletions llm/api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,71 @@ func TestAddDefaultHeadersWithExistingHeaders(t *testing.T) {
t.Errorf("Expected Existing-Header to be 'existing-value', got %s", existingHeader)
}
}

func TestAutofixRequestBody(t *testing.T) {
d := &DeepCodeLLMBindingImpl{}

const testBundleHash = "0123456789abcdef"
const testBaseDir = "basedir"
const testFilePath = "/path/to/file"
const testLineNumber0Based = 0
const testRuleId = "rule_id"
const testShardKey = "shard_key"
const testHost = "http://api.test.snyk.io"
const testIdeName = "my IDE"
const testIdeVersion = "1.0.0"
const testExtensionName = "my extension"
const testExtensionVersion = "1.2.3"

options := AutofixOptions{
RuleID: testRuleId,
BundleHash: testBundleHash,
ShardKey: testShardKey,
Host: testHost,
BaseDir: testBaseDir,
FilePath: testFilePath,
LineNum: testLineNumber0Based,
CodeRequestContext: CodeRequestContext{
Initiator: "",
Flow: "",
Org: CodeRequestContextOrg{},
},
IdeExtensionDetails: AutofixIdeExtensionDetails{
IdeName: testIdeName,
IdeVersion: testIdeVersion,
ExtensionName: testExtensionName,
ExtensionVersion: testExtensionVersion,
},
}

jsonBody, err := d.autofixRequestBody(&options)
assert.NoError(t, err)

expectedBody := AutofixRequest{
Key: AutofixRequestKey{
Type: "file",
Hash: testBundleHash,
Shard: testShardKey,
FilePath: testFilePath,
RuleId: testRuleId,
LineNum: testLineNumber0Based,
},
AnalysisContext: CodeRequestContext{
Initiator: "",
Flow: "",
Org: CodeRequestContextOrg{},
},
IdeExtensionDetails: AutofixIdeExtensionDetails{
IdeName: testIdeName,
IdeVersion: testIdeVersion,
ExtensionName: testExtensionName,
ExtensionVersion: testExtensionVersion,
},
}

var body AutofixRequest
err = json.Unmarshal(jsonBody, &body)
assert.NoError(t, err)

assert.Equal(t, expectedBody, body)
}
4 changes: 2 additions & 2 deletions llm/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ type AutofixOptions struct {
FilePath string
LineNum int

Endpoint *url.URL
Host string
CodeRequestContext CodeRequestContext
IdeExtensionDetails AutofixIdeExtensionDetails
}
Expand All @@ -142,7 +142,7 @@ type AutofixFeedbackOptions struct {
FixID string
Result string

Endpoint *url.URL
Host string
CodeRequestContext CodeRequestContext
IdeExtensionDetails AutofixIdeExtensionDetails
}
Expand Down