diff --git a/bundle/bundle.go b/bundle/bundle.go index 0c3d6f9..e8acb6a 100644 --- a/bundle/bundle.go +++ b/bundle/bundle.go @@ -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 + } + + 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") } diff --git a/bundle/bundle_manager.go b/bundle/bundle_manager.go index 3a18b9e..2e9403c 100644 --- a/bundle/bundle_manager.go +++ b/bundle/bundle_manager.go @@ -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) } } @@ -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 + 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") diff --git a/internal/deepcode/client.go b/internal/deepcode/client.go index a8c1de3..8a40b96 100644 --- a/internal/deepcode/client.go +++ b/internal/deepcode/client.go @@ -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