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

feat: Allow sharing to and from team projects (no-changelog) #10144

Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
remove transfer changes
valya committed Aug 8, 2024

Verified

This commit was signed with the committer’s verified signature.
valya Val
commit 8f76c37353eb948610a4dde62d9eed500decaae5
Original file line number Diff line number Diff line change
@@ -49,18 +49,12 @@ export = {
transferCredential: [
projectScope('credential:move', 'credential'),
async (req: CredentialRequest.Transfer, res: express.Response) => {
const body = z
.object({
destinationProjectId: z.string(),
shareWithOriginalProject: z.boolean().optional(),
})
.parse(req.body);
const body = z.object({ destinationProjectId: z.string() }).parse(req.body);

await Container.get(EnterpriseCredentialsService).transferOne(
req.user,
req.params.workflowId,
body.destinationProjectId,
body.shareWithOriginalProject ?? false,
);

res.status(204).send();
Original file line number Diff line number Diff line change
@@ -17,9 +17,6 @@ put:
destinationProjectId:
type: string
description: The ID of the project to transfer the credential to.
shareWithOriginalProject:
type: boolean
description: Share the transferred credential to the project it originated from.
required:
- destinationProjectId
required: true
10 changes: 1 addition & 9 deletions packages/cli/src/credentials/credentials.controller.ts
Original file line number Diff line number Diff line change
@@ -339,20 +339,12 @@ export class CredentialsController {
@Put('/:credentialId/transfer')
@ProjectScope('credential:move')
async transfer(req: CredentialRequest.Transfer) {
// TODO: make shareWithOriginalProject non-optional once the frontend
// has support
const body = z
.object({
destinationProjectId: z.string(),
shareWithOriginalProject: z.boolean().optional(),
})
.parse(req.body);
const body = z.object({ destinationProjectId: z.string() }).parse(req.body);

return await this.enterpriseCredentialsService.transferOne(
req.user,
req.params.credentialId,
body.destinationProjectId,
body.shareWithOriginalProject ?? false,
);
}
}
28 changes: 2 additions & 26 deletions packages/cli/src/credentials/credentials.service.ee.ts
Original file line number Diff line number Diff line change
@@ -116,12 +116,7 @@ export class EnterpriseCredentialsService {
return { ...rest };
}

async transferOne(
user: User,
credentialId: string,
destinationProjectId: string,
shareWithSource: boolean,
) {
async transferOne(user: User, credentialId: string, destinationProjectId: string) {
// 1. get credential
const credential = await this.sharedCredentialsRepository.findCredentialForUser(
credentialId,
@@ -171,26 +166,7 @@ export class EnterpriseCredentialsService {

await this.sharedCredentialsRepository.manager.transaction(async (trx) => {
// 6. transfer the credential

// remove original owner sharing
await trx.remove(ownerSharing);

// share it back as a user if asked to
if (shareWithSource) {
await trx.save(
trx.create(SharedCredentials, {
credentialsId: credential.id,
projectId: sourceProject.id,
role: 'credential:user',
}),
);
}

// remove any previous sharings with the new owner
await trx.delete(SharedCredentials, {
credentialsId: credential.id,
projectId: destinationProjectId,
});
await trx.remove(credential.shared);

// create new owner-sharing
await trx.save(
2 changes: 1 addition & 1 deletion packages/cli/src/requests.ts
Original file line number Diff line number Diff line change
@@ -203,7 +203,7 @@ export declare namespace CredentialRequest {
type Transfer = AuthenticatedRequest<
{ credentialId: string },
{},
{ destinationProjectId: string; shareWithOriginalProject?: boolean }
{ destinationProjectId: string }
>;

type ForWorkflow = AuthenticatedRequest<
Original file line number Diff line number Diff line change
@@ -1182,13 +1182,13 @@ describe('PUT /:credentialId/transfer', () => {
.expect(403);
});

test('transferring from a personal project to a team project should not sever all sharings', async () => {
test('transferring from a personal project to a team project severs all sharings', async () => {
//
// ARRANGE
//
const credential = await saveCredential(randomCredentialPayload(), { user: member });

// these sharings should not be deleted by the transfer
// these sharings should be deleted by the transfer
await shareCredentialWithUsers(credential, [anotherMember, owner]);

const destinationProject = await createTeamProject('Destination Project', member);
@@ -1208,72 +1208,12 @@ describe('PUT /:credentialId/transfer', () => {
expect(response.body).toEqual({});

const allSharings = await getCredentialSharings(credential);
expect(allSharings).toHaveLength(3);
expect(allSharings).toContainEqual(
expect.objectContaining({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
}),
);
for (const projectId of [anotherMemberPersonalProject.id, ownerPersonalProject.id]) {
expect(allSharings).toContainEqual(
expect.objectContaining({
credentialsId: credential.id,
projectId,
role: 'credential:user',
}),
);
}
});

test('transferring should share with original owner when asked', async () => {
//
// ARRANGE
//
const credential = await saveCredential(randomCredentialPayload(), { user: member });

// these sharings should not be deleted by the transfer
await shareCredentialWithUsers(credential, [anotherMember, owner]);

const destinationProject = await createTeamProject('Destination Project', member);

//
// ACT
//
const response = await testServer
.authAgentFor(member)
.put(`/credentials/${credential.id}/transfer`)
.send({ destinationProjectId: destinationProject.id, shareWithOriginalProject: true })
.expect(200);

//
// ASSERT
//
expect(response.body).toEqual({});

const allSharings = await getCredentialSharings(credential);
expect(allSharings).toHaveLength(4);
expect(allSharings).toContainEqual(
expect.objectContaining({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
}),
);
for (const projectId of [
anotherMemberPersonalProject.id,
ownerPersonalProject.id,
memberPersonalProject.id,
]) {
expect(allSharings).toContainEqual(
expect.objectContaining({
credentialsId: credential.id,
projectId,
role: 'credential:user',
}),
);
}
expect(allSharings).toHaveLength(1);
expect(allSharings[0]).toMatchObject({
projectId: destinationProject.id,
credentialsId: credential.id,
role: 'credential:owner',
});
});

test('can transfer from team to another team project', async () => {