Skip to content

Commit

Permalink
fix: use single ref field
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Jan 30, 2024
1 parent 35e51d5 commit 47d7a4b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 27 deletions.
6 changes: 2 additions & 4 deletions packages/backend/src/ai.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
38 changes: 24 additions & 14 deletions packages/backend/src/managers/applicationManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -165,8 +182,7 @@ describe('pullApplication', () => {
name: 'Recipe 1',
categories: [],
description: '',
branch: 'branch',
sha: '000000',
ref: '000000',
readme: '',
repository: 'repo',
};
Expand All @@ -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') {
Expand All @@ -211,8 +226,7 @@ describe('pullApplication', () => {
name: 'Recipe 1',
categories: [],
description: '',
branch: 'branch',
sha: '000000',
ref: '000000',
readme: '',
repository: 'repo',
};
Expand All @@ -239,8 +253,7 @@ describe('pullApplication', () => {
id: 'recipe1',
name: 'Recipe 1',
categories: [],
branch: 'branch',
sha: '000000',
ref: '000000',
description: '',
readme: '',
repository: 'repo',
Expand Down Expand Up @@ -270,8 +283,7 @@ describe('pullApplication', () => {
name: 'Recipe 1',
categories: [],
description: '',
branch: 'branch',
sha: '000000',
ref: '000000',
readme: '',
repository: 'repo',
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -348,8 +359,7 @@ describe('doCheckout', () => {
await manager.doCheckout(
{
repository: 'repo',
branch: 'branch',
sha: '000000',
ref: '000000',
targetDirectory: 'folder',
},
taskUtils,
Expand Down
3 changes: 1 addition & 2 deletions packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions packages/backend/src/managers/gitManager.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
11 changes: 6 additions & 5 deletions packages/backend/src/managers/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
3 changes: 1 addition & 2 deletions packages/shared/src/models/IRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down

0 comments on commit 47d7a4b

Please sign in to comment.