Skip to content

Feat: Fork a Repo #312

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

Merged
merged 2 commits into from
May 19, 2023
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
65 changes: 64 additions & 1 deletion api/src/resolver_repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async function deleteEdge(_, { source, target }, { userId }) {
return true;
}

async function createRepo(_, { id, name, isPublic }, { userId }) {
async function createRepo(_, {}, { userId }) {
if (!userId) throw Error("Unauthenticated");
const repo = await prisma.repo.create({
data: {
Expand Down Expand Up @@ -497,6 +497,68 @@ async function deletePods(_, { ids }: { ids: string[] }, { userId }) {
return true;
}

async function copyRepo(_, { repoId }, { userId }) {
// Find the repo
const repo = await prisma.repo.findFirst({
where: {
id: repoId,
},
include: {
pods: {
include: {
parent: true,
},
},
},
});
if (!repo) throw new Error("Repo not found");

// Create a new repo
const { id } = await createRepo(_, {}, { userId });
// update the repo name
await prisma.repo.update({
where: {
id,
},
data: {
name: repo.name ? `Copy of ${repo.name}` : `Copy of ${repo.id}`,
},
});

// Create new id for each pod
const sourcePods = repo.pods;
const idMap = await sourcePods.reduce(async (acc, pod) => {
const map = await acc;
const newId = await nanoid();
map.set(pod.id, newId);
return map;
}, Promise.resolve(new Map()));

// Update the parent/child relationship with their new ids
const targetPods = sourcePods.map((pod) => {
return {
...pod,
id: idMap.get(pod.id),
parent: pod.parent ? { id: idMap.get(pod.parent.id) } : undefined,
repoId: id,
parentId: pod.parentId ? idMap.get(pod.parentId) : undefined,
};
});

// Add all nodes without parent/child relationship to the new repo.
// TODO: it updates the parent/child relationship automatically somehow,maybe because the parentId? Try to figure out why, then refactor addPods method.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Two comments:

  1. The parent/children relationship is a single relation (named the PARENT relation in the schema). Thus, updating one should automatically update another. We should probably only update the parent field and not touch the children field at all.
  2. Yep, providing the parentId field seems to be equivalent of using {connect: {parent: {id}}} Prisma clause.

await prisma.pod.createMany({
data: targetPods.map((pod) => ({
...pod,
id: pod.id,
index: 0,
parent: undefined,
})),
});

return id;
}

export default {
Query: {
myRepos,
Expand All @@ -509,6 +571,7 @@ export default {
createRepo,
updateRepo,
deleteRepo,
copyRepo,
updatePod,
deletePods,
addEdge,
Expand Down
1 change: 1 addition & 0 deletions api/src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export const typeDefs = gql`
createRepo: Repo
updateRepo(id: ID!, name: String!): Boolean
deleteRepo(id: ID!): Boolean
copyRepo(repoId: String!): ID!
deletePods(ids: [String]): Boolean
addPods(repoId: String!, pods: [PodInput]): Boolean
updatePod(id: String!, repoId: String!, input: PodInput): Boolean
Expand Down
12 changes: 12 additions & 0 deletions ui/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type HeaderProps = {
currentPage?: string | null;
breadcrumbItem?: React.ReactNode;
shareButton?: React.ReactNode;
forkButton?: React.ReactNode;
};

export const Header: React.FC<HeaderProps> = ({
Expand All @@ -39,6 +40,7 @@ export const Header: React.FC<HeaderProps> = ({
currentPage = null,
breadcrumbItem = null,
shareButton = null,
forkButton = null,
}) => {
const [anchorElNav, setAnchorElNav] = useState(null);
const [anchorElUser, setAnchorElUser] = useState(null);
Expand Down Expand Up @@ -96,6 +98,16 @@ export const Header: React.FC<HeaderProps> = ({
{breadcrumbItem}
</Breadcrumbs>

<Box
sx={{
display: { xs: "none", md: "flex" },
alignItems: "center",
paddingRight: "10px",
}}
>
{forkButton}
</Box>

<Box
sx={{
display: { xs: "none", md: "flex" },
Expand Down
26 changes: 26 additions & 0 deletions ui/src/pages/repo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Link from "@mui/material/Link";
import Alert from "@mui/material/Alert";
import AlertTitle from "@mui/material/AlertTitle";
import ShareIcon from "@mui/icons-material/Share";
import ContentCopyIcon from "@mui/icons-material/ContentCopy";
import Button from "@mui/material/Button";
import { gql, useApolloClient, useMutation } from "@apollo/client";

Expand Down Expand Up @@ -139,6 +140,18 @@ function RepoWrapper({ children, id }) {
if (!store) throw new Error("Missing BearContext.Provider in the tree");

const setShareOpen = useStore(store, (state) => state.setShareOpen);
const navigate = useNavigate();
const [copyRepo] = useMutation(
gql`
mutation CopyRepo($id: String!) {
copyRepo(repoId: $id)
}
`,
{ variables: { id } }
);
// if(result.data.copyRepo){
// navigate(`/repo/${result.data.copyRepo}`);
// }

const DrawerWidth = 240;

Expand Down Expand Up @@ -180,6 +193,19 @@ function RepoWrapper({ children, id }) {
Share
</Button>
}
forkButton={
<Button
endIcon={<ContentCopyIcon />}
onClick={async () => {
const result = await copyRepo();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is interesting; I didn't know we could await for the mutation function. Looks like we indeed can.

const newRepoId = result.data.copyRepo;
window.open(`/repo/${newRepoId}`);
}}
variant="contained"
>
Make a copy
</Button>
}
/>
<Box
sx={{
Expand Down