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