-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0f1160
commit 8536453
Showing
5 changed files
with
86 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |