diff --git a/pkg/tasks/c1api/full_sync.go b/pkg/tasks/c1api/full_sync.go index e5dd447e..e214b769 100644 --- a/pkg/tasks/c1api/full_sync.go +++ b/pkg/tasks/c1api/full_sync.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "path/filepath" v1 "github.com/conductorone/baton-sdk/pb/c1/connectorapi/baton/v1" "github.com/conductorone/baton-sdk/pkg/annotations" @@ -130,6 +131,11 @@ func (c *fullSyncTaskHandler) HandleTask(ctx context.Context) error { return c.helpers.FinishTask(ctx, nil, nil, err) } + err = uploadDebugLogs(ctx, c.helpers) + if err != nil { + return c.helpers.FinishTask(ctx, nil, nil, err) + } + return c.helpers.FinishTask(ctx, nil, nil, nil) } @@ -140,3 +146,39 @@ func newFullSyncTaskHandler(task *v1.Task, helpers fullSyncHelpers, skipFullSync skipFullSync: skipFullSync, } } + +func uploadDebugLogs(ctx context.Context, helper fullSyncHelpers) error { + l := ctxzap.Extract(ctx) + + debugfilelocation := filepath.Join(helper.TempDir(), "debug.log") + + _, err := os.Stat(debugfilelocation) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + l.Warn("debug log file does not exists", zap.Error(err)) + return nil + } + return err + } else { + debugfile, err := os.Open(debugfilelocation) + if err != nil { + return err + } + defer debugfile.Close() + + l.Info("uploading debug logs", zap.String("file", debugfilelocation)) + err = helper.Upload(ctx, debugfile) + + if err != nil { + return err + } + defer func() { + err := os.Remove(debugfilelocation) + if err != nil { + l.Error("failed to delete file with debug logs", zap.Error(err), zap.String("file", debugfilelocation)) + } + }() + + return nil + } +}