insomnia/packages/insomnia-app/app/sync/vcs/pull-project.ts
Opender Singh be2c347cf1
Pull a project into the correct space (#3873)
* use selector and improve types

* Loading remote projects now requires a team id; it is no longer optional

* activate with workspace or workspace id

* extract helper

* normalize graphql return type for a remote project to include the team

* pull into the space as defined by the project team id, not by the active space. If the space doesn't exist, create it

* use helper

* make testing easier by putting the active space name as the first breadcrumb

* fix tests

* add the active space into the title

* refactor and add tests

* remove todo
2021-08-05 23:30:31 +00:00

52 lines
1.7 KiB
TypeScript

import { DEFAULT_BRANCH_NAME } from '../../common/constants';
import { database } from '../../common/database';
import { RemoteSpace } from '../../models/space';
import { isWorkspace } from '../../models/workspace';
import { initializeSpaceFromTeam, initializeWorkspaceFromProject } from './initialize-model-from';
import { ProjectWithTeam } from './normalize-project-team';
import { VCS } from './vcs';
interface Options {
vcs: VCS;
project: ProjectWithTeam;
remoteSpaces: RemoteSpace[];
}
export const pullProject = async ({ vcs, project, remoteSpaces }: Options) => {
// Set project, checkout master, and pull
await vcs.setProject(project);
await vcs.checkout([], DEFAULT_BRANCH_NAME);
const remoteBranches = await vcs.getRemoteBranches();
const defaultBranchMissing = !remoteBranches.includes(DEFAULT_BRANCH_NAME);
// Find or create the remote space locally
let space = remoteSpaces.find(({ remoteId }) => remoteId === project.team.id);
if (!space) {
space = await initializeSpaceFromTeam(project.team);
await database.upsert(space);
}
// The default branch does not exist, so we create it and the workspace locally
if (defaultBranchMissing) {
const workspace = await initializeWorkspaceFromProject(project, space);
await database.upsert(workspace);
} else {
await vcs.pull([], space.remoteId); // There won't be any existing docs since it's a new pull
const flushId = await database.bufferChanges();
// @ts-expect-error -- TSCONVERSION
for (const doc of (await vcs.allDocuments() || [])) {
if (isWorkspace(doc)) {
doc.parentId = space._id;
}
await database.upsert(doc);
}
await database.flushChanges(flushId);
}
return space;
};