Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Return homeProject when filtering workflows by project id #12077

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,13 @@ export class SharedWorkflowRepository extends Repository<SharedWorkflow> {
},
});
}

async getAllRelationsForWorkflows(workflowIds: string[]) {
return await this.find({
where: {
workflowId: In(workflowIds),
},
relations: ['project'],
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
.execute();
}

async getMany(sharedWorkflowIds: string[], options?: ListQuery.Options) {
async getMany(sharedWorkflowIds: string[], originalOptions: ListQuery.Options = {}) {
const options = structuredClone(originalOptions);
if (sharedWorkflowIds.length === 0) return { workflows: [], count: 0 };

if (typeof options?.filter?.projectId === 'string' && options.filter.projectId !== '') {
Expand Down
18 changes: 16 additions & 2 deletions packages/cli/src/workflows/workflow.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ export class WorkflowService {
let { workflows, count } = await this.workflowRepository.getMany(sharedWorkflowIds, options);

if (hasSharing(workflows)) {
// Since we're filtering using project ID as part of the relation,
// we end up filtering out all the other relations, meaning that if
// it's shared to a project, it won't be able to find the home project.
// To solve this, we have to get all the relation now, even though
// we're deleting them later.
if (typeof options?.filter?.projectId === 'string' && options.filter.projectId !== '') {
const relations = await this.sharedWorkflowRepository.getAllRelationsForWorkflows(
workflows.map((c) => c.id),
);
workflows.forEach((c) => {
c.shared = relations.filter((r) => r.workflowId === c.id);
});
}
Comment on lines +69 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The facts that we're having to do two queries for this, in-memory processing and fetching things that we later discard makes me wish we could use the query builder, but that'd be a larger refactor. Maybe to improve in future.


workflows = workflows.map((w) => this.ownershipService.addOwnedByAndSharedWith(w));
}

Expand All @@ -75,8 +89,8 @@ export class WorkflowService {
}

workflows.forEach((w) => {
// @ts-expect-error: This is to emulate the old behaviour of removing the shared
// field as part of `addOwnedByAndSharedWith`. We need this field in `addScopes`
// This is to emulate the old behaviour of removing the shared field as
// part of `addOwnedByAndSharedWith`. We need this field in `addScopes`
// though. So to avoid leaking the information we just delete it.
delete w.shared;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ import { EnterpriseWorkflowService } from '@/workflows/workflow.service.ee';

import { mockInstance } from '../../shared/mocking';
import { saveCredential } from '../shared/db/credentials';
import { createTeamProject, linkUserToProject } from '../shared/db/projects';
import { createTeamProject, getPersonalProject, linkUserToProject } from '../shared/db/projects';
import { createTag } from '../shared/db/tags';
import { createManyUsers, createMember, createOwner } from '../shared/db/users';
import { createWorkflow, shareWorkflowWithProjects } from '../shared/db/workflows';
import {
createWorkflow,
shareWorkflowWithProjects,
shareWorkflowWithUsers,
} from '../shared/db/workflows';
import { randomCredentialPayload } from '../shared/random';
import * as testDb from '../shared/test-db';
import type { SuperAgentTest } from '../shared/types';
Expand Down Expand Up @@ -676,6 +680,21 @@ describe('GET /workflows', () => {

expect(response2.body.data).toHaveLength(0);
});

test('should return homeProject when filtering workflows by projectId', async () => {
const workflow = await createWorkflow({ name: 'First' }, owner);
await shareWorkflowWithUsers(workflow, [member]);
const pp = await getPersonalProject(member);

const response = await authMemberAgent
.get('/workflows')
.query(`filter={ "projectId": "${pp.id}" }`)
.expect(200);

expect(response.body.data).toHaveLength(1);
expect(response.body.data[0].id).toBe(workflow.id);
expect(response.body.data[0].homeProject).not.toBeNull();
});
});

describe('select', () => {
Expand Down
Loading