feat: Add endpoint to retrieve single workflow from GH (#17220)

Co-authored-by: Guillaume Jacquart <jacquart.guillaume@gmail.com>
This commit is contained in:
Raúl Gómez Morales
2025-07-16 14:38:13 +02:00
committed by GitHub
parent ac552e68fd
commit c4ba31ef62
7 changed files with 201 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import type {
User,
FolderRepository,
TagRepository,
WorkflowEntity,
} from '@n8n/db';
import { Container } from '@n8n/di';
import { mock } from 'jest-mock-extended';
@@ -15,7 +16,9 @@ import { SourceControlPreferencesService } from '@/environments.ee/source-contro
import { SourceControlService } from '@/environments.ee/source-control/source-control.service.ee';
import { ForbiddenError } from '@/errors/response-errors/forbidden.error';
import type { SourceControlGitService } from '../source-control-git.service.ee';
import type { SourceControlImportService } from '../source-control-import.service.ee';
import type { SourceControlScopedService } from '../source-control-scoped.service';
import type { StatusExportableCredential } from '../types/exportable-credential';
import type { SourceControlWorkflowVersionId } from '../types/source-control-workflow-version-id';
@@ -28,14 +31,17 @@ describe('SourceControlService', () => {
mock(),
);
const sourceControlImportService = mock<SourceControlImportService>();
const sourceControlScopedService = mock<SourceControlScopedService>();
const tagRepository = mock<TagRepository>();
const folderRepository = mock<FolderRepository>();
const gitService = mock<SourceControlGitService>();
const sourceControlService = new SourceControlService(
mock(),
mock(),
gitService,
preferencesService,
mock(),
sourceControlImportService,
sourceControlScopedService,
tagRepository,
folderRepository,
mock(),
@@ -375,4 +381,65 @@ describe('SourceControlService', () => {
).rejects.toThrowError(ForbiddenError);
});
});
describe('getFileContent', () => {
it.each([{ type: 'workflow' as SourceControlledFile['type'], id: '1234', content: '{}' }])(
'should return file content for $type',
async ({ type, id, content }) => {
jest.spyOn(gitService, 'getFileContent').mockResolvedValue(content);
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
const result = await sourceControlService.getRemoteFileEntity({ user, type, id });
expect(result).toEqual(JSON.parse(content));
},
);
it.each<SourceControlledFile['type']>(['folders', 'credential', 'tags', 'variables'])(
'should throw an error if the file type is not handled',
async (type) => {
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
await expect(
sourceControlService.getRemoteFileEntity({ user, type, id: 'unknown' }),
).rejects.toThrow(`Unsupported file type: ${type}`);
},
);
it('should fail if the git service fails to get the file content', async () => {
jest.spyOn(gitService, 'getFileContent').mockRejectedValue(new Error('Git service error'));
const user = mock<User>({ id: 'user-id', role: 'global:admin' });
await expect(
sourceControlService.getRemoteFileEntity({ user, type: 'workflow', id: '1234' }),
).rejects.toThrow('Git service error');
});
it('should throw an error if the user does not have access to the project', async () => {
const user = mock<User>({
id: 'user-id',
role: 'global:member',
});
jest
.spyOn(sourceControlScopedService, 'getWorkflowsInAdminProjectsFromContext')
.mockResolvedValue([]);
await expect(
sourceControlService.getRemoteFileEntity({ user, type: 'workflow', id: '1234' }),
).rejects.toThrow('You are not allowed to access workflow with id 1234');
});
it('should return content for an authorized workflow', async () => {
const user = mock<User>({ id: 'user-id', role: 'global:member' });
jest
.spyOn(sourceControlScopedService, 'getWorkflowsInAdminProjectsFromContext')
.mockResolvedValue([{ id: '1234' } as WorkflowEntity]);
jest.spyOn(gitService, 'getFileContent').mockResolvedValue('{}');
const result = await sourceControlService.getRemoteFileEntity({
user,
type: 'workflow',
id: '1234',
});
expect(result).toEqual({});
});
});
});