Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VanessaScherma committed Aug 1, 2024
1 parent f0f1160 commit 8536453
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 85 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,3 @@ runner.yml

# command scripts for runner
servers/execution/runner/lifecycle*
client/test/integration/gitlab.test.ts
42 changes: 2 additions & 40 deletions client/src/util/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface LogEntry {
class DigitalTwin {
private DTName: string;
private username: string;
private api: InstanceType<typeof Gitlab>;
public api: InstanceType<typeof Gitlab>;
private logs: LogEntry[];

constructor(DTName: string) {
Expand Down Expand Up @@ -83,42 +83,4 @@ class DigitalTwin {
}
}

interface FolderEntry {
name: string;
path: string;
}

class DigitalTwinSubfolders {
private api: InstanceType<typeof Gitlab>;
private subfolders: FolderEntry[] = [];

constructor() {
this.api = new Gitlab({
oauthToken: sessionStorage.getItem('access_token') || '',
});
}

async getDTSubfolders(projectId: number): Promise<FolderEntry[]> {
const folderPath = 'digital_twins';

try {
const files = await this.api.Repositories.allRepositoryTrees(projectId, {
path: folderPath,
recursive: true,
});

this.subfolders = files
.filter(file => file.type === 'tree' && file.path !== folderPath)
.map(file => ({
name: file.name,
path: file.path
}));

return this.subfolders;
} catch (error) {
return [];
}
}
}

export { DigitalTwin, DigitalTwinSubfolders }
export default DigitalTwin;
41 changes: 41 additions & 0 deletions client/src/util/gitlabSubfolders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Gitlab } from '@gitbeaker/rest';

interface FolderEntry {
name: string;
path: string;
}

class DigitalTwinSubfolders {
public api: InstanceType<typeof Gitlab>;
private subfolders: FolderEntry[] = [];

constructor() {
this.api = new Gitlab({
oauthToken: sessionStorage.getItem('access_token') || '',
});
}

async getDTSubfolders(projectId: number): Promise<FolderEntry[]> {
const folderPath = 'digital_twins';

try {
const files = await this.api.Repositories.allRepositoryTrees(projectId, {
path: folderPath,
recursive: true,
});

this.subfolders = files
.filter(file => file.type === 'tree' && file.path !== folderPath)
.map(file => ({
name: file.name,
path: file.path
}));

return this.subfolders;
} catch (error) {
return [];
}
}
}

export default DigitalTwinSubfolders;
47 changes: 3 additions & 44 deletions client/test/unitTests/Util/gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DigitalTwin, DigitalTwinSubfolders } from '../../../src/util/gitlab';
import DigitalTwin from '../../../src/util/gitlab';
import { Gitlab } from '@gitbeaker/rest';
import { ProjectSchema, PipelineTriggerTokenSchema } from '@gitbeaker/rest';

Expand All @@ -9,9 +9,6 @@ const mockApi = {
PipelineTriggerTokens: {
all: jest.fn(),
trigger: jest.fn()
},
Repositories: {
allRepositoryTrees: jest.fn()
}
};

Expand All @@ -20,7 +17,7 @@ describe('DigitalTwin', () => {

beforeEach(() => {
dt = new DigitalTwin('test-DTName');
(dt as any).api = mockApi as unknown as InstanceType<typeof Gitlab>;
dt.api = mockApi as unknown as InstanceType<typeof Gitlab>;
});

it('should fetch project ID successfully', async () => {
Expand Down Expand Up @@ -167,42 +164,4 @@ describe('DigitalTwin', () => {
expect(logs[0].DTName).toBe('test-DTName');
expect(logs[0].runnerTag).toBe('test-runnerTag');
});
});

describe('DigitalTwinSubfolders', () => {
let subfolders: DigitalTwinSubfolders;

const mockApi = {
Repositories: {
allRepositoryTrees: jest.fn()
}
};

beforeEach(() => {
subfolders = new DigitalTwinSubfolders();
(subfolders as any).api = mockApi as unknown as InstanceType<typeof Gitlab>;
});

it('should fetch all files and subfolders successfully', async () => {
const mockFiles = [
{ name: 'file1.txt', path: 'digital_twins/file1.txt', type: 'blob' },
{ name: 'subfolder', path: 'digital_twins/subfolder', type: 'tree' },
{ name: 'file2.txt', path: 'digital_twins/subfolder/file2.txt', type: 'blob' }
];
mockApi.Repositories.allRepositoryTrees.mockResolvedValue(mockFiles);

const folderEntries = await subfolders.getDTSubfolders(1);

expect(folderEntries).toEqual([
{ name: 'subfolder', path: 'digital_twins/subfolder' }
]);
});

it('should handle errors fetching files and subfolders', async () => {
mockApi.Repositories.allRepositoryTrees.mockRejectedValue(new Error('API error'));

const folderEntries = await subfolders.getDTSubfolders(1);

expect(folderEntries).toEqual([]);
});
});
});
40 changes: 40 additions & 0 deletions client/test/unitTests/Util/gitlabSubfolders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import DigitalTwinSubfolders from '../../../src/util/gitlabSubfolders';
import { Gitlab } from '@gitbeaker/rest';

describe('DigitalTwinSubfolders', () => {
let subfolders: DigitalTwinSubfolders;

const mockApi = {
Repositories: {
allRepositoryTrees: jest.fn()
}
};

beforeEach(() => {
subfolders = new DigitalTwinSubfolders();
subfolders.api = mockApi as unknown as InstanceType<typeof Gitlab>;
});

it('should fetch all files and subfolders successfully', async () => {
const mockFiles = [
{ name: 'file1.txt', path: 'digital_twins/file1.txt', type: 'blob' },
{ name: 'subfolder', path: 'digital_twins/subfolder', type: 'tree' },
{ name: 'file2.txt', path: 'digital_twins/subfolder/file2.txt', type: 'blob' }
];
mockApi.Repositories.allRepositoryTrees.mockResolvedValue(mockFiles);

const folderEntries = await subfolders.getDTSubfolders(1);

expect(folderEntries).toEqual([
{ name: 'subfolder', path: 'digital_twins/subfolder' }
]);
});

it('should handle errors fetching files and subfolders', async () => {
mockApi.Repositories.allRepositoryTrees.mockRejectedValue(new Error('API error'));

const folderEntries = await subfolders.getDTSubfolders(1);

expect(folderEntries).toEqual([]);
});
});

0 comments on commit 8536453

Please sign in to comment.