Skip to content

Commit

Permalink
fix: report error and reset state if checkout fails
Browse files Browse the repository at this point in the history
Fixes #428

Signed-off-by: Jeff MAURY <[email protected]>
  • Loading branch information
jeffmaury committed Feb 28, 2024
1 parent a0f3c50 commit 01ff26c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
43 changes: 43 additions & 0 deletions packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,49 @@ describe('doCheckout', () => {
},
});
});
test('doCheckout report an error if clone is errored', async () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(false);
vi.spyOn(fs, 'mkdirSync');
const cloneRepositoryMock = vi.fn().mockRejectedValue(new Error('Unknown Git reference'));
const manager = new ApplicationManager(
'/home/user/aistudio',
{
cloneRepository: cloneRepositoryMock,
} as unknown as GitManager,
taskRegistry,
{} as Webview,
{} as PodmanConnection,
{} as CatalogManager,
{} as unknown as ModelsManager,
telemetryLogger,
localRepositoryRegistry,
);
const gitCloneOptions = {
repository: 'repo',
ref: '000000',
targetDirectory: 'folder',
};
let expectedError: unknown;

try {
await manager.doCheckout(gitCloneOptions);
} catch (err: unknown) {
expectedError = err;
}

expect(cloneRepositoryMock).toBeCalledWith(gitCloneOptions);
expect(mocks.updateTaskMock).toHaveBeenLastCalledWith({
id: expect.any(String),
name: 'Checkout repository',
state: 'error',
labels: {
git: 'checkout',
},
error: 'Error: Unknown Git reference',
});
expect(expectedError).to.be.a('Error');
expect((expectedError as Error).message).equal('Unknown Git reference');
});
test('do not clone repo if already present locally', async () => {
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
const stats = {
Expand Down
41 changes: 24 additions & 17 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,24 +548,31 @@ export class ApplicationManager {
git: 'checkout',
});

// We might already have the repository cloned
if (fs.existsSync(gitCloneInfo.targetDirectory) && fs.statSync(gitCloneInfo.targetDirectory).isDirectory()) {
// Update checkout state
checkoutTask.name = 'Checkout repository (cached).';
checkoutTask.state = 'success';
} else {
// Create folder
fs.mkdirSync(gitCloneInfo.targetDirectory, { recursive: true });

// Clone the repository
console.log(`Cloning repository ${gitCloneInfo.repository} in ${gitCloneInfo.targetDirectory}.`);
await this.git.cloneRepository(gitCloneInfo);

// Update checkout state
checkoutTask.state = 'success';
try {
// We might already have the repository cloned
if (fs.existsSync(gitCloneInfo.targetDirectory) && fs.statSync(gitCloneInfo.targetDirectory).isDirectory()) {
// Update checkout state
checkoutTask.name = 'Checkout repository (cached).';
checkoutTask.state = 'success';
} else {
// Create folder
fs.mkdirSync(gitCloneInfo.targetDirectory, { recursive: true });

// Clone the repository
console.log(`Cloning repository ${gitCloneInfo.repository} in ${gitCloneInfo.targetDirectory}.`);
await this.git.cloneRepository(gitCloneInfo);

// Update checkout state
checkoutTask.state = 'success';
}
} catch (err: unknown) {
checkoutTask.state = 'error';
checkoutTask.error = String(err);
throw err;
} finally {
// Update task registry
this.taskRegistry.updateTask(checkoutTask);
}
// Update task registry
this.taskRegistry.updateTask(checkoutTask);
}

adoptRunningEnvironments() {
Expand Down

0 comments on commit 01ff26c

Please sign in to comment.