| 1 | "use server"; |
| 2 | |
| 3 | import { cookies } from |
| 4 | import { redirect } |
| 5 | import { createSafeActionClient } |
| 6 | import { z } |
| 7 | |
| 8 | import { fetchClient } |
| 9 | import { collaboratorRoles } |
| 10 | |
| 11 | import { |
| 12 | createRepositorySchema, |
| 13 | forkRepositorySchema, |
| 14 | updateRepositorySchema, |
| 15 | } from "./common"; |
| 16 | |
| 17 | const actionClient = createSafeActionClient |
| 18 | handleServerError: (error) |
| 19 | }); |
| 20 | |
| 21 | export const createRepository = |
| 22 | .inputSchema( |
| 23 | createRepositorySchema.extend({ |
| 24 | ) |
| 25 | .action( |
| 26 | async ({ |
| 27 | parsedInput: { owner, |
| 28 | }) => { |
| 29 | const { data, |
| 30 | body: { name, |
| 31 | headers: { cookie: ( |
| 32 | }); |
| 33 | |
| 34 | if (error) { |
| 35 | throw new Error( |
| 36 | } |
| 37 | |
| 38 | redirect(`/${owner |
| 39 | }, |
| 40 | ); |
| 41 | |
| 42 | export const forkRepository = |
| 43 | .inputSchema( |
| 44 | forkRepositorySchema.extend({ |
| 45 | parentUsername: z.string |
| 46 | parentSlug: z.string |
| 47 | organization: z.string |
| 48 | }), |
| 49 | ) |
| 50 | .action( |
| 51 | async ({ |
| 52 | parsedInput: { |
| 53 | parentUsername, |
| 54 | parentSlug, |
| 55 | name, |
| 56 | description, |
| 57 | visibility, |
| 58 | organization, |
| 59 | }, |
| 60 | }) => { |
| 61 | const { data, |
| 62 | "/api/repositories/{username}/{slug}/fork", |
| 63 | { |
| 64 | params: { |
| 65 | path: { username: parentUsername |
| 66 | }, |
| 67 | body: { name, |
| 68 | headers: { cookie: ( |
| 69 | }, |
| 70 | ); |
| 71 | |
| 72 | if (error) { |
| 73 | throw new Error( |
| 74 | } |
| 75 | |
| 76 | redirect(`/${data |
| 77 | }, |
| 78 | ); |
| 79 | |
| 80 | const repositoryPath = z |
| 81 | |
| 82 | export const updateRepository = |
| 83 | .inputSchema(updateRepositorySchema.extend |
| 84 | .action(async ( |
| 85 | const { data, |
| 86 | "/api/repositories/{username}/{slug}", |
| 87 | { |
| 88 | params: { path: { |
| 89 | body: changes, |
| 90 | headers: { cookie: ( |
| 91 | }, |
| 92 | ); |
| 93 | |
| 94 | if (error) { |
| 95 | throw new Error( |
| 96 | } |
| 97 | |
| 98 | return { slug: |
| 99 | }); |
| 100 | |
| 101 | export const deleteRepository = |
| 102 | .inputSchema(repositoryPath) |
| 103 | .action(async ( |
| 104 | const { error |
| 105 | "/api/repositories/{username}/{slug}", |
| 106 | { |
| 107 | params: { path: { |
| 108 | headers: { cookie: ( |
| 109 | }, |
| 110 | ); |
| 111 | |
| 112 | if (error) { |
| 113 | throw new Error( |
| 114 | } |
| 115 | |
| 116 | redirect(`/${username |
| 117 | }); |
| 118 | |
| 119 | export const inviteCollaborator = |
| 120 | .inputSchema( |
| 121 | repositoryPath.extend({ |
| 122 | collaborator: z.string |
| 123 | role: z.enum |
| 124 | }), |
| 125 | ) |
| 126 | .action(async ( |
| 127 | const { error |
| 128 | "/api/repositories/{username}/{repo}/collaborators/{collaborator}", |
| 129 | { |
| 130 | params: { path: { |
| 131 | body: { role |
| 132 | headers: { cookie: ( |
| 133 | }, |
| 134 | ); |
| 135 | |
| 136 | if (error) { |
| 137 | throw new Error( |
| 138 | } |
| 139 | }); |
| 140 | |
| 141 | export const removeCollaborator = |
| 142 | .inputSchema(repositoryPath.extend |
| 143 | .action(async ( |
| 144 | const { error |
| 145 | "/api/repositories/{username}/{repo}/collaborators/{collaborator}", |
| 146 | { |
| 147 | params: { path: { |
| 148 | headers: { cookie: ( |
| 149 | }, |
| 150 | ); |
| 151 | |
| 152 | if (error) { |
| 153 | throw new Error( |
| 154 | } |
| 155 | }); |
| 156 | |
| 157 | export const setTeamRole = |
| 158 | .inputSchema( |
| 159 | repositoryPath.extend({ |
| 160 | teamId: z.string |
| 161 | role: z.enum |
| 162 | }), |
| 163 | ) |
| 164 | .action(async ( |
| 165 | const { error |
| 166 | "/api/repositories/{username}/{repo}/teams/{teamId}", |
| 167 | { |
| 168 | params: { path: { |
| 169 | body: { role |
| 170 | headers: { cookie: ( |
| 171 | }, |
| 172 | ); |
| 173 | |
| 174 | if (error) { |
| 175 | throw new Error( |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | export const removeTeamAccess = |
| 180 | .inputSchema(repositoryPath.extend |
| 181 | .action(async ( |
| 182 | const { error |
| 183 | "/api/repositories/{username}/{repo}/teams/{teamId}", |
| 184 | { |
| 185 | params: { path: { |
| 186 | headers: { cookie: ( |
| 187 | }, |
| 188 | ); |
| 189 | |
| 190 | if (error) { |
| 191 | throw new Error( |
| 192 | } |
| 193 | }); |
| 194 | |
| 195 | export const transferRepository = |
| 196 | .inputSchema(repositoryPath.extend |
| 197 | .action(async ( |
| 198 | const { data, |
| 199 | "/api/repositories/{username}/{slug}/transfer", |
| 200 | { |
| 201 | params: { path: { |
| 202 | body: { owner |
| 203 | headers: { cookie: ( |
| 204 | }, |
| 205 | ); |
| 206 | |
| 207 | if (error) { |
| 208 | throw new Error( |
| 209 | } |
| 210 | |
| 211 | // A transfer someone else has to accept leaves the repository where it is. |
| 212 | if (data.pending |
| 213 | redirect(`/${data |
| 214 | }); |