fix(repository): make the fork feature working properly
+ 64
- 35
@@ -1,5 +1,5 @@
 {
-  "_generatedAtUnix": 1664250832803,
+  "_generatedAtUnix": 1664251799514,
   "_hashAlgorithm": "sha1",
   "_version": 2,
   "islands": {

...
@@ -96,7 +96,7 @@
       "pathSource": "./app/views/repository/RepositoryCreateView.tsx"
     },
     "RepositoryDetailsView": {
-      "hash": "d8c7919f4f9923ac3c16a538f4cf3e6563ab6602",
+      "hash": "1e140eff51739a6e55dd81727b5a132050a04de0",
       "pathSource": "./app/views/repository/RepositoryDetailsView.tsx"
     },
     "RepositoryExploreView": {

app/services/repository/forkRepository.ts
@@ -62,15 +62,31 @@ const makeForkRepository: ServiceMethodFactory<
 
     const newRepo = await request.prisma.repository.create({
       data: {
-        ...source.repository,
         ...target.repoData,
+
         id: cuid(), // to generate one
+        slug: target.repoSlug,
+
         createdAt: new Date(Date.now()),
         updatedAt: new Date(Date.now()),
-        organizationId: target.parentOrg.id,
-        slug: target.repoSlug,
+
+        avatarUri: source.repository.avatarUri,
+        keywords: source.repository.keywords,
+        shortDescription: source.repository.shortDescription,
+        websiteUrl: source.repository.websiteUrl,
+
+        organization: {
+          connect: {
+            id: target.parentOrg.id,
+          },
+        },
+
         isFork: true,
-        forkedFromRepoId: source.repository.id,
+        forkedFromRepo: {
+          connect: {
+            id: source.repository.id,
+          },
+        },
       },
     });
 

app/views/repository/RepositoryDetailsView.tsx
@@ -62,27 +62,27 @@ const RepositoryDetailsView: ReactView<RepositoryDetailsViewProps> = ({
     <Layout {...commonProps}>
       <PageWrapper>
         <Grid.Col fluid>
-          <Grid.Row nowrap fluid alignItems={"center"}>
-            <h1 style={{ flex: 1, margin: 0 }}>
-              <a href={`/${parentOrg.slug}`}>
-                {parentOrg.displayName || parentOrg.slug}
-              </a>
-              {" / "}
-              <a href={`/${parentOrg.slug}/${repo.slug}`}>
-                {repo.displayName || repo.slug}
-              </a>
-              {path != null && path.trim() !== "" && path !== "/"
-                ? ` / ${path}`
-                : ""}
-              {" ∙ "}
-              <span style={{ textTransform: "capitalize" }}>
-                ({repo.visibility.toLowerCase()})
-              </span>
-            </h1>
-            <Grid.Row nowrap style={{ marginTop: 8 }}>
+          <Grid.Row fluid alignItems={"center"}>
+            <Grid.Col nowrap style={{ marginTop: 8, flex: 1 }}>
+              <h1 style={{ margin: 0 }}>
+                <a href={`/${parentOrg.slug}`}>
+                  {parentOrg.displayName || parentOrg.slug}
+                </a>
+                {" / "}
+                <a href={`/${parentOrg.slug}/${repo.slug}`}>
+                  {repo.displayName || repo.slug}
+                </a>
+                {path != null && path.trim() !== "" && path !== "/"
+                  ? ` / ${path}`
+                  : ""}
+                {" ∙ "}
+                <span style={{ textTransform: "capitalize" }}>
+                  ({repo.visibility.toLowerCase()})
+                </span>
+              </h1>
               <div style={{ flex: 1 }}>
                 {repo.isFork && repo.forkedFromRepo != null ? (
-                  <h5 style={{ margin: 0 }}>
+                  <h5 style={{ margin: 0, marginTop: 8 }}>
                     <span>Forked From</span>
                     {" ∙ "}
                     <a href={`/${repo.forkedFromRepo.organization.slug}`}>

...
@@ -101,15 +101,15 @@ const RepositoryDetailsView: ReactView<RepositoryDetailsViewProps> = ({
                   <div />
                 )}
               </div>
-              <Grid.Row nowrap style={{ minWidth: 90 }}>
-                <span>{repo.forks.length}</span>
-                <a
-                  href={`/${parentOrg.slug}/${repo.slug}/fork`}
-                  style={{ marginLeft: 8 }}
-                >
-                  Fork it!
-                </a>
-              </Grid.Row>
+            </Grid.Col>
+            <Grid.Row nowrap style={{ minWidth: 90 }}>
+              <span>{repo.forks.length}</span>
+              <a
+                href={`/${parentOrg.slug}/${repo.slug}/fork`}
+                style={{ marginLeft: 8 }}
+              >
+                Fork it!
+              </a>
             </Grid.Row>
           </Grid.Row>
         </Grid.Col>

new file
db/migrations/20220927040750_update_repository_slug_constraint_unique_by_org_id/migration.sql
@@ -0,0 +1,11 @@
+/*
+  Warnings:
+
+  - A unique constraint covering the columns `[organizationId,slug]` on the table `Repository` will be added. If there are existing duplicate values, this will fail.
+
+*/
+-- DropIndex
+DROP INDEX "Repository_slug_key";
+
+-- CreateIndex
+CREATE UNIQUE INDEX "Repository_organizationId_slug_key" ON "Repository"("organizationId", "slug");

@@ -40,7 +40,7 @@ model OrganizationMembership {
 
 model Repository {
   id               String             @id @default(cuid())
-  slug             String             @unique
+  slug             String
   createdAt        DateTime           @default(now())
   updatedAt        DateTime           @updatedAt
   avatarUri        String?

...
@@ -56,6 +56,8 @@ model Repository {
   forks            Repository[]       @relation("OneParentRepositoryToManyForkRepository")
   lastPushedAt     DateTime?
   organization     Organization       @relation("ManyRepositoriesToOneOrganization", fields: [organizationId], references: [id])
+
+  @@unique([organizationId, slug])
 }
 
 model Session {