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