From 47d7a4b575907f0b022fa835af489ef7275be32e Mon Sep 17 00:00:00 2001 From: lstocchi Date: Tue, 30 Jan 2024 10:40:41 +0100 Subject: [PATCH] fix: use single ref field Signed-off-by: lstocchi --- packages/backend/src/ai.json | 6 +- .../src/managers/applicationManager.spec.ts | 38 +++++++----- .../src/managers/applicationManager.ts | 3 +- .../backend/src/managers/gitManager.spec.ts | 60 +++++++++++++++++++ packages/backend/src/managers/gitManager.ts | 11 ++-- packages/shared/src/models/IRecipe.ts | 3 +- 6 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 packages/backend/src/managers/gitManager.spec.ts diff --git a/packages/backend/src/ai.json b/packages/backend/src/ai.json index 807668be7..7b0dab948 100644 --- a/packages/backend/src/ai.json +++ b/packages/backend/src/ai.json @@ -5,8 +5,7 @@ "description" : "Chat bot application", "name" : "ChatBot", "repository": "https://github.com/redhat-et/locallm", - "branch": "main", - "sha": "bccd1c1", + "ref": "bccd1c1", "icon": "natural-language-processing", "categories": [ "natural-language-processing" @@ -22,8 +21,7 @@ "description" : "Summarizer application", "name" : "Summarizer", "repository": "https://github.com/redhat-et/locallm", - "branch": "main", - "sha": "bccd1c1", + "ref": "bccd1c1", "icon": "natural-language-processing", "categories": [ "natural-language-processing" diff --git a/packages/backend/src/managers/applicationManager.spec.ts b/packages/backend/src/managers/applicationManager.spec.ts index 0363e4a45..78090ff08 100644 --- a/packages/backend/src/managers/applicationManager.spec.ts +++ b/packages/backend/src/managers/applicationManager.spec.ts @@ -1,3 +1,20 @@ +/********************************************************************** + * Copyright (C) 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ***********************************************************************/ import { type MockInstance, describe, expect, test, vi, beforeEach } from 'vitest'; import type { ContainerAttachedInfo, DownloadModelResult, ImageInfo, PodInfo } from './applicationManager'; import { ApplicationManager } from './applicationManager'; @@ -165,8 +182,7 @@ describe('pullApplication', () => { name: 'Recipe 1', categories: [], description: '', - branch: 'branch', - sha: '000000', + ref: '000000', readme: '', repository: 'repo', }; @@ -187,9 +203,8 @@ describe('pullApplication', () => { }); await manager.pullApplication(recipe, model); const gitCloneOptions = { - branch: 'branch', repository: 'repo', - sha: '000000', + ref: '000000', targetDirectory: '\\home\\user\\aistudio\\recipe1', }; if (process.platform === 'win32') { @@ -211,8 +226,7 @@ describe('pullApplication', () => { name: 'Recipe 1', categories: [], description: '', - branch: 'branch', - sha: '000000', + ref: '000000', readme: '', repository: 'repo', }; @@ -239,8 +253,7 @@ describe('pullApplication', () => { id: 'recipe1', name: 'Recipe 1', categories: [], - branch: 'branch', - sha: '000000', + ref: '000000', description: '', readme: '', repository: 'repo', @@ -270,8 +283,7 @@ describe('pullApplication', () => { name: 'Recipe 1', categories: [], description: '', - branch: 'branch', - sha: '000000', + ref: '000000', readme: '', repository: 'repo', }; @@ -312,9 +324,8 @@ describe('doCheckout', () => { {} as unknown as ModelsManager, ); const gitCloneOptions = { - branch: 'branch', repository: 'repo', - sha: '000000', + ref: '000000', targetDirectory: 'folder', }; await manager.doCheckout(gitCloneOptions, taskUtils); @@ -348,8 +359,7 @@ describe('doCheckout', () => { await manager.doCheckout( { repository: 'repo', - branch: 'branch', - sha: '000000', + ref: '000000', targetDirectory: 'folder', }, taskUtils, diff --git a/packages/backend/src/managers/applicationManager.ts b/packages/backend/src/managers/applicationManager.ts index 7288ffcda..ecf4a0832 100644 --- a/packages/backend/src/managers/applicationManager.ts +++ b/packages/backend/src/managers/applicationManager.ts @@ -90,8 +90,7 @@ export class ApplicationManager { // clone the recipe repository on the local folder const gitCloneInfo: GitCloneInfo = { repository: recipe.repository, - branch: recipe.branch, - sha: recipe.sha, + ref: recipe.ref, targetDirectory: localFolder, }; await this.doCheckout(gitCloneInfo, taskUtil); diff --git a/packages/backend/src/managers/gitManager.spec.ts b/packages/backend/src/managers/gitManager.spec.ts new file mode 100644 index 000000000..7ed109734 --- /dev/null +++ b/packages/backend/src/managers/gitManager.spec.ts @@ -0,0 +1,60 @@ +/********************************************************************** + * Copyright (C) 2024 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ***********************************************************************/ +import { describe, expect, test, vi, beforeEach } from 'vitest'; +import { GitManager } from './gitManager'; + +const mocks = vi.hoisted(() => { + return { + cloneMock: vi.fn(), + checkoutMock: vi.fn(), + }; +}); + +vi.mock('simple-git', () => { + return { + default: () => ({ + clone: mocks.cloneMock, + checkout: mocks.checkoutMock, + }), + }; +}); + +beforeEach(() => { + vi.resetAllMocks(); +}); + +describe('cloneRepository', () => { + const gitmanager = new GitManager(); + test('clone and checkout if ref is specified', async () => { + await gitmanager.cloneRepository({ + repository: 'repo', + targetDirectory: 'target', + ref: '000', + }); + expect(mocks.cloneMock).toBeCalledWith('repo', 'target'); + expect(mocks.checkoutMock).toBeCalledWith(['000']); + }); + test('clone and checkout if ref is NOT specified', async () => { + await gitmanager.cloneRepository({ + repository: 'repo', + targetDirectory: 'target', + }); + expect(mocks.cloneMock).toBeCalledWith('repo', 'target'); + expect(mocks.checkoutMock).not.toBeCalled(); + }); +}); diff --git a/packages/backend/src/managers/gitManager.ts b/packages/backend/src/managers/gitManager.ts index 0a399a012..2b5048a67 100644 --- a/packages/backend/src/managers/gitManager.ts +++ b/packages/backend/src/managers/gitManager.ts @@ -20,16 +20,17 @@ import simpleGit from 'simple-git'; export interface GitCloneInfo { repository: string; - branch: string; - sha: string; + ref?: string; targetDirectory: string; } export class GitManager { async cloneRepository(gitCloneInfo: GitCloneInfo) { // clone repo - await simpleGit().clone(gitCloneInfo.repository, gitCloneInfo.targetDirectory, ['-b', gitCloneInfo.branch]); - // checkout to specific branch - await simpleGit(gitCloneInfo.targetDirectory).checkout([gitCloneInfo.sha]); + await simpleGit().clone(gitCloneInfo.repository, gitCloneInfo.targetDirectory); + // checkout to specific branch/commit if specified + if (gitCloneInfo.ref) { + await simpleGit(gitCloneInfo.targetDirectory).checkout([gitCloneInfo.ref]); + } } } diff --git a/packages/shared/src/models/IRecipe.ts b/packages/shared/src/models/IRecipe.ts index c8e6be210..28d9f2c67 100644 --- a/packages/shared/src/models/IRecipe.ts +++ b/packages/shared/src/models/IRecipe.ts @@ -5,8 +5,7 @@ export interface Recipe { description: string; icon?: string; repository: string; - branch: string; - sha: string; + ref?: string; readme: string; config?: string; models?: string[];