git-workspace-service 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -361,12 +361,20 @@ var WorkspaceService = class {
361
361
  provisionedAt: /* @__PURE__ */ new Date(),
362
362
  status: "provisioning",
363
363
  strategy,
364
- parentWorkspaceId: config.parentWorkspace
364
+ parentWorkspaceId: config.parentWorkspace,
365
+ onComplete: config.onComplete,
366
+ progress: {
367
+ phase: "initializing",
368
+ message: "Initializing workspace",
369
+ updatedAt: /* @__PURE__ */ new Date()
370
+ }
365
371
  };
366
372
  this.workspaces.set(workspaceId, workspace);
367
373
  try {
368
374
  if (strategy === "clone") {
375
+ this.updateProgress(workspace, "cloning", "Cloning repository");
369
376
  await this.cloneRepo(workspace, credential.token);
377
+ this.updateProgress(workspace, "creating_branch", "Creating branch");
370
378
  await this.createBranch(workspace);
371
379
  } else {
372
380
  const parent = this.workspaces.get(config.parentWorkspace);
@@ -384,9 +392,12 @@ var WorkspaceService = class {
384
392
  data: { parentWorkspaceId: parent.id }
385
393
  });
386
394
  }
395
+ this.updateProgress(workspace, "configuring", "Configuring git");
387
396
  await this.configureGit(workspace);
388
397
  workspace.status = "ready";
398
+ this.updateProgress(workspace, "ready", "Workspace ready");
389
399
  this.workspaces.set(workspaceId, workspace);
400
+ await this.executeCompletionHook(workspace, "success");
390
401
  this.log(
391
402
  "info",
392
403
  {
@@ -406,8 +417,9 @@ var WorkspaceService = class {
406
417
  return workspace;
407
418
  } catch (error) {
408
419
  workspace.status = "error";
409
- this.workspaces.set(workspaceId, workspace);
410
420
  const errorMessage = error instanceof Error ? error.message : String(error);
421
+ this.updateProgress(workspace, "error", errorMessage);
422
+ this.workspaces.set(workspaceId, workspace);
411
423
  this.log("error", { workspaceId, error: errorMessage }, "Failed to provision workspace");
412
424
  await this.emitEvent({
413
425
  type: "workspace:error",
@@ -416,6 +428,7 @@ var WorkspaceService = class {
416
428
  timestamp: /* @__PURE__ */ new Date(),
417
429
  error: errorMessage
418
430
  });
431
+ await this.executeCompletionHook(workspace, "error");
419
432
  throw error;
420
433
  }
421
434
  }
@@ -752,6 +765,72 @@ var WorkspaceService = class {
752
765
  }
753
766
  }
754
767
  }
768
+ /**
769
+ * Update workspace progress
770
+ */
771
+ updateProgress(workspace, phase, message) {
772
+ workspace.progress = {
773
+ phase,
774
+ message,
775
+ updatedAt: /* @__PURE__ */ new Date()
776
+ };
777
+ this.workspaces.set(workspace.id, workspace);
778
+ this.log(
779
+ "debug",
780
+ { workspaceId: workspace.id, phase, message },
781
+ "Progress updated"
782
+ );
783
+ }
784
+ /**
785
+ * Execute completion hook if configured
786
+ */
787
+ async executeCompletionHook(workspace, status) {
788
+ const hook = workspace.onComplete;
789
+ if (!hook) return;
790
+ if (status === "error" && hook.runOnError === false) {
791
+ return;
792
+ }
793
+ const env = {
794
+ ...process.env,
795
+ WORKSPACE_ID: workspace.id,
796
+ REPO: workspace.repo,
797
+ BRANCH: workspace.branch.name,
798
+ STATUS: status,
799
+ WORKSPACE_PATH: workspace.path
800
+ };
801
+ if (hook.command) {
802
+ try {
803
+ this.log("info", { workspaceId: workspace.id, command: hook.command }, "Executing completion hook command");
804
+ await execAsync(hook.command, { env });
805
+ } catch (error) {
806
+ const errorMessage = error instanceof Error ? error.message : String(error);
807
+ this.log("warn", { workspaceId: workspace.id, error: errorMessage }, "Completion hook command failed");
808
+ }
809
+ }
810
+ if (hook.webhook) {
811
+ try {
812
+ this.log("info", { workspaceId: workspace.id, webhook: hook.webhook }, "Calling completion webhook");
813
+ const payload = {
814
+ workspaceId: workspace.id,
815
+ repo: workspace.repo,
816
+ branch: workspace.branch.name,
817
+ status,
818
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
819
+ };
820
+ await fetch(hook.webhook, {
821
+ method: "POST",
822
+ headers: {
823
+ "Content-Type": "application/json",
824
+ ...hook.webhookHeaders
825
+ },
826
+ body: JSON.stringify(payload)
827
+ });
828
+ } catch (error) {
829
+ const errorMessage = error instanceof Error ? error.message : String(error);
830
+ this.log("warn", { workspaceId: workspace.id, error: errorMessage }, "Completion webhook failed");
831
+ }
832
+ }
833
+ }
755
834
  };
756
835
 
757
836
  // src/oauth/device-flow.ts