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