Skip to content
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 @@ -145,7 +145,7 @@ describe('pullBackendProject()', () => {
const request = requests[0];
expect(request).toStrictEqual(existingReq);

expect(vcs.pull).toHaveBeenCalledWith({ candidates: [], teamId: project?.parentId, teamProjectId: project?._id });
expect(vcs.pull).toHaveBeenCalledWith({ candidates: [], teamId: project?.parentId, teamProjectId: project?._id, projectId: project?._id });
});
});

Expand Down
31 changes: 16 additions & 15 deletions packages/insomnia/src/sync/vcs/pull-backend-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const pullBackendProject = async ({ vcs, backendProject, remoteProject }:

const defaultBranchMissing = !remoteBranches.includes(DEFAULT_BRANCH_NAME);

let workspaceId;

// @TODO Revisit the UX for this. What should happen if there are other branches?
// The default branch does not exist, so we create it and the workspace locally
if (defaultBranchMissing) {
const workspace = await models.initModel<Workspace>(
Expand All @@ -41,23 +40,25 @@ export const pullBackendProject = async ({ vcs, backendProject, remoteProject }:

await database.upsert(workspace);

workspaceId = workspace._id;
} else {
await vcs.pull({ candidates: [], teamId: remoteProject.parentId, teamProjectId: remoteProject._id }); // There won't be any existing docs since it's a new pull
return { project: remoteProject, workspaceId: workspace._id };
}

const flushId = await database.bufferChanges();
await vcs.pull({ candidates: [], teamId: remoteProject.parentId, teamProjectId: remoteProject._id, projectId: remoteProject._id }); // There won't be any existing docs since it's a new pull

// @ts-expect-error -- TSCONVERSION
for (const doc of (await vcs.allDocuments()) || []) {
if (isWorkspace(doc)) {
doc.parentId = remoteProject._id;
workspaceId = doc._id;
}
await database.upsert(doc);
const flushId = await database.bufferChanges();
let workspaceId;
// @ts-expect-error -- TSCONVERSION
for (const doc of (await vcs.allDocuments()) || []) {
// When we pull a BackendProject we need to update the parent ID of the workspace so that it appears inside.
// There can't be more than one workspace.
if (isWorkspace(doc)) {
doc.parentId = remoteProject._id;
workspaceId = doc._id;
}

await database.flushChanges(flushId);
await database.upsert(doc);
}

await database.flushChanges(flushId);
return { project: remoteProject, workspaceId };

};
13 changes: 11 additions & 2 deletions packages/insomnia/src/sync/vcs/vcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'path';

import * as crypt from '../../account/crypt';
import * as session from '../../account/session';
import type { Operation } from '../../common/database';
import { generateId } from '../../common/misc';
import { BaseModel } from '../../models';
import { insomniaFetch } from '../../ui/insomniaFetch';
Expand Down Expand Up @@ -577,7 +578,7 @@ export class VCS {
console.log(`[sync] Created commit ${snapshot.id} (${name})`);
}

async pull({ candidates, teamId, teamProjectId }: { candidates: StatusCandidate[]; teamId: string; teamProjectId: string }) {
async pull({ candidates, teamId, teamProjectId, projectId }: { candidates: StatusCandidate[]; teamId: string; teamProjectId: string; projectId: string }) {
await this._getOrCreateRemoteBackendProject({ teamId, teamProjectId });
const localBranch = await this._getCurrentBranch();
const tmpBranchForRemote = await this.customFetch(localBranch.name + '.hidden', localBranch.name);
Expand All @@ -589,10 +590,18 @@ export class VCS {
tmpBranchForRemote.name,
message,
true,
);
) as unknown as Operation;
// Remove tmp branch
await this._removeBranch(tmpBranchForRemote);
console.log(`[sync] Pulled branch ${localBranch.name}`);

// vcs.pull sometimes results in a delta with parentId: null, causing workspaces to be orphaned, this is a hack to restore those parentIds until we have a chance to redesign vcs
delta.upsert?.forEach(doc => {
if (!doc.parentId && doc.type === 'Workspace') {
doc.parentId = projectId;
}
});

return delta;
}

Expand Down
18 changes: 6 additions & 12 deletions packages/insomnia/src/ui/routes/remote-collections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,10 @@ export const pullFromRemoteAction: ActionFunction = async ({ params }) => {
candidates: syncItems,
teamId: project.parentId,
teamProjectId: project.remoteId,
projectId: project._id,
});

await database.batchModifyDocs(delta as unknown as Operation);
await database.batchModifyDocs(delta);
delete remoteCompareCache[workspaceId];
} catch (err) {
const errorMessage =
Expand Down Expand Up @@ -537,21 +538,14 @@ export const fetchRemoteBranchAction: ActionFunction = async ({
try {
invariant(project.remoteId, 'Project is not remote');
await vcs.checkout([], branch);
const delta = (await vcs.pull({
const delta = await vcs.pull({
candidates: [],
teamId: project.parentId,
teamProjectId: project.remoteId,
})) as unknown as Operation;
// vcs.pull sometimes results in a delta with parentId: null, causing workspaces to be orphaned, this is a hack to restore those parentIds until we have a chance to redesign vcs
await database.batchModifyDocs({
remove: delta.remove,
upsert: delta.upsert?.map(doc => ({
...doc,
...(!doc.parentId && doc.type === models.workspace.type
? { parentId: projectId }
: {}),
})),
projectId,
});

await database.batchModifyDocs(delta);
} catch (err) {
await vcs.checkout([], currentBranch);
const errorMessage =
Expand Down