Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions bundle/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,43 @@ func (b *deepCodeBundle) UploadBatch(ctx context.Context, requestId string, batc
func (b *deepCodeBundle) extendBundle(ctx context.Context, requestId string, uploadBatch *Batch) error {
var err error
if uploadBatch.hasContent() {
// Log batch metadata before upload to track what's being sent
filePaths := make([]string, 0, len(uploadBatch.documents))
fileHashes := make(map[string]string)
totalContentSize := 0
for filePath, bundleFile := range uploadBatch.documents {
filePaths = append(filePaths, filePath)
fileHashes[filePath] = bundleFile.Hash
totalContentSize += bundleFile.ContentSize
}
Comment thread
mihaibuzgau marked this conversation as resolved.

b.logger.Debug().
Str("requestId", requestId).
Str("bundleHash", b.bundleHash).
Int("batchFileCount", len(uploadBatch.documents)).
Int("batchTotalSize", totalContentSize).
Int("batchEstimatedSize", uploadBatch.getSize()).
Strs("filePaths", filePaths).
Interface("fileHashes", fileHashes).
Msg("Uploading partial bundle batch - metadata and content details")

b.bundleHash, b.missingFiles, err = b.SnykCode.ExtendBundle(ctx, b.bundleHash, uploadBatch.documents, []string{})

if err != nil {
b.logger.Error().
Err(err).
Str("requestId", requestId).
Str("bundleHash", b.bundleHash).
Int("batchFileCount", len(uploadBatch.documents)).
Msg("Failed to extend bundle on backend")
} else {
b.logger.Debug().
Str("requestId", requestId).
Str("newBundleHash", b.bundleHash).
Int("missingFilesCount", len(b.missingFiles)).
Interface("missingFiles", b.missingFiles).
Msg("Successfully extended bundle on backend")
}
b.logger.Debug().Str("requestId", requestId).Interface("MissingFiles", b.missingFiles).Msg("extended deepCodeBundle on backend")
}

Expand Down
66 changes: 65 additions & 1 deletion bundle/bundle_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,40 @@ func (b *bundleManager) Upload(
return bundle, nil
}

for _, batch := range batches {
for batchIndex, batch := range batches {
if err := ctx.Err(); err != nil {
return bundle, err
}

// Log batch information before enrichment and upload
b.logger.Debug().
Str("requestId", requestId).
Str("rootPath", bundle.GetRootPath()).
Str("bundleHash", bundle.GetBundleHash()).
Int("batchIndex", batchIndex+1).
Int("totalBatches", len(batches)).
Int("batchFileCount", len(batch.documents)).
Int("batchEstimatedSize", batch.getSize()).
Msg("Starting partial bundle upload batch")

b.enrichBatchWithFileContent(batch, bundle.GetRootPath())
err := bundle.UploadBatch(s.Context(), requestId, batch)
if err != nil {
b.logger.Error().
Err(err).
Str("requestId", requestId).
Int("batchIndex", batchIndex+1).
Int("totalBatches", len(batches)).
Msg("Failed to upload partial bundle batch")
return bundle, err
}

b.logger.Debug().
Str("requestId", requestId).
Int("batchIndex", batchIndex+1).
Int("totalBatches", len(batches)).
Msg("Completed partial bundle upload batch")

batch.documents = make(map[string]deepcode.BundleFile)
}
}
Expand All @@ -239,12 +264,51 @@ func (b *bundleManager) enrichBatchWithFileContent(batch *Batch, rootPath string
b.logger.Error().Err(err).Str("file", filePath).Msg("Failed to decode Path")
continue
}

// Store original hash and metadata before reading file
originalHash := bundleFile.Hash

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand the scenario. How would we already have a hash before adding the content? We should never read the same file twice, and if we do, it's a bug.

originalContentSize := bundleFile.ContentSize

// Log metadata before reading file content
b.logger.Debug().
Str("filePath", filePath).
Str("absolutePath", absPath).
Str("originalHash", originalHash).
Int("originalContentSize", originalContentSize).
Msg("Preparing to read file for partial bundle upload")

content, err := os.ReadFile(absPath)
if err != nil {
b.logger.Error().Err(err).Str("file", filePath).Msg("Failed to read bundle file")
continue
}

// Compute hash from freshly read content to detect modifications
newHash, hashErr := util.Hash(content)
if hashErr != nil {
b.logger.Warn().Err(hashErr).Str("file", filePath).Msg("Failed to compute hash for freshly read content")
} else {
// Check if file was modified in flight
fileModified := originalHash != newHash
if fileModified {
b.logger.Warn().
Str("filePath", filePath).
Str("absolutePath", absPath).
Str("originalHash", originalHash).
Str("newHash", newHash).
Int("originalContentSize", originalContentSize).
Int("newContentSize", len(content)).
Msg("File modification detected during partial bundle upload - file content changed between bundle creation and upload")
} else {
b.logger.Debug().
Str("filePath", filePath).
Str("absolutePath", absPath).
Str("hash", newHash).
Int("contentSize", len(content)).
Msg("File content verified - no modifications detected")
}
}

utf8Content, err := util.ConvertToUTF8(content)
if err != nil {
b.logger.Error().Err(err).Str("file", filePath).Msg("Failed to convert bundle file to UTF-8")
Expand Down
16 changes: 16 additions & 0 deletions internal/deepcode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ func (s *deepcodeClient) Request(

codeClientHTTP.AddDefaultHeaders(req, codeClientHTTP.NoRequestId, s.config.Organization(), method, true)

fullURL := host + path
logFields := log.Debug().
Str("method", method).
Str("url", fullURL).
Str("requestBody", string(requestBody))

headers := make(map[string]string)
for key, values := range req.Header {
if (strings.ToLower(key) != "authorization") && (strings.ToLower(key) != "session-token") {
headers[key] = strings.Join(values, ", ")
}
}
logFields = logFields.Interface("headers", headers)

logFields.Msg("HTTP request details for bundle operation")

response, err := s.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down