| 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 | |
| 11 | import { |
| 12 | createIssueSchema, |
| 13 | createLabelSchema, |
| 14 | updateLabelSchema, |
| 15 | } from "./common"; |
| 16 | |
| 17 | const actionClient = createSafeActionClient |
| 18 | handleServerError: (error) |
| 19 | }); |
| 20 | |
| 21 | const target = z |
| 22 | username: z.string |
| 23 | repo: z.string |
| 24 | number: z.number |
| 25 | }); |
| 26 | |
| 27 | function issuePath(username: |
| 28 | return number === |
| 29 | ? `/${username} |
| 30 | : `/${username} |
| 31 | } |
| 32 | |
| 33 | export const createIssue = |
| 34 | .inputSchema( |
| 35 | createIssueSchema.extend({ |
| 36 | ) |
| 37 | .action( |
| 38 | async ({ |
| 39 | parsedInput: { username, |
| 40 | }) => { |
| 41 | const { data, |
| 42 | "/api/repositories/{username}/{repo}/issues", |
| 43 | { |
| 44 | params: { path: { |
| 45 | body: { title, |
| 46 | headers: { cookie: ( |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | if (error) throw |
| 51 | |
| 52 | redirect(`/${username |
| 53 | }, |
| 54 | ); |
| 55 | |
| 56 | export const updateIssue = |
| 57 | .inputSchema( |
| 58 | target.extend({ |
| 59 | title: z |
| 60 | .string() |
| 61 | .trim() |
| 62 | .min(1, " |
| 63 | .max(200, " |
| 64 | .optional(), |
| 65 | body: z |
| 66 | .string() |
| 67 | .max(20000, " |
| 68 | .nullable() |
| 69 | .optional(), |
| 70 | }), |
| 71 | ) |
| 72 | .action(async ( |
| 73 | const { error |
| 74 | "/api/repositories/{username}/{repo}/issues/{number}", |
| 75 | { |
| 76 | params: { path: { |
| 77 | body: { title, |
| 78 | headers: { cookie: ( |
| 79 | }, |
| 80 | ); |
| 81 | |
| 82 | if (error) throw |
| 83 | |
| 84 | revalidatePath(issuePath(username |
| 85 | }); |
| 86 | |
| 87 | export const closeIssue = |
| 88 | .inputSchema(target) |
| 89 | .action(async ( |
| 90 | const { error |
| 91 | "/api/repositories/{username}/{repo}/issues/{number}/close", |
| 92 | { |
| 93 | params: { path: { |
| 94 | headers: { cookie: ( |
| 95 | }, |
| 96 | ); |
| 97 | |
| 98 | if (error) throw |
| 99 | |
| 100 | revalidatePath(issuePath(username |
| 101 | revalidatePath(issuePath(username |
| 102 | }); |
| 103 | |
| 104 | export const reopenIssue = |
| 105 | .inputSchema(target) |
| 106 | .action(async ( |
| 107 | const { error |
| 108 | "/api/repositories/{username}/{repo}/issues/{number}/reopen", |
| 109 | { |
| 110 | params: { path: { |
| 111 | headers: { cookie: ( |
| 112 | }, |
| 113 | ); |
| 114 | |
| 115 | if (error) throw |
| 116 | |
| 117 | revalidatePath(issuePath(username |
| 118 | revalidatePath(issuePath(username |
| 119 | }); |
| 120 | |
| 121 | export const commentOnIssue = |
| 122 | .inputSchema( |
| 123 | target.extend({ |
| 124 | body: z |
| 125 | .string() |
| 126 | .trim() |
| 127 | .min(1, " |
| 128 | .max(20000, " |
| 129 | }), |
| 130 | ) |
| 131 | .action(async ( |
| 132 | const { data, |
| 133 | "/api/repositories/{username}/{repo}/issues/{number}/comments", |
| 134 | { |
| 135 | params: { path: { |
| 136 | body: { body |
| 137 | headers: { cookie: ( |
| 138 | }, |
| 139 | ); |
| 140 | |
| 141 | if (error) throw |
| 142 | |
| 143 | revalidatePath(issuePath(username |
| 144 | return data; |
| 145 | }); |
| 146 | |
| 147 | export const updateIssueComment = |
| 148 | .inputSchema( |
| 149 | target.extend({ |
| 150 | commentId: z.string |
| 151 | body: z |
| 152 | .string() |
| 153 | .trim() |
| 154 | .min(1, " |
| 155 | .max(20000, " |
| 156 | }), |
| 157 | ) |
| 158 | .action( |
| 159 | async ({ parsedInput |
| 160 | const { error |
| 161 | "/api/repositories/{username}/{repo}/issues/{number}/comments/{commentId}", |
| 162 | { |
| 163 | params: { path: { |
| 164 | body: { body |
| 165 | headers: { cookie: ( |
| 166 | }, |
| 167 | ); |
| 168 | |
| 169 | if (error) throw |
| 170 | |
| 171 | revalidatePath(issuePath(username |
| 172 | }, |
| 173 | ); |
| 174 | |
| 175 | export const deleteIssueComment = |
| 176 | .inputSchema(target.extend |
| 177 | .action(async ( |
| 178 | const { error |
| 179 | "/api/repositories/{username}/{repo}/issues/{number}/comments/{commentId}", |
| 180 | { |
| 181 | params: { path: { |
| 182 | headers: { cookie: ( |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | if (error) throw |
| 187 | |
| 188 | revalidatePath(issuePath(username |
| 189 | }); |
| 190 | |
| 191 | export const setIssueLabels = |
| 192 | .inputSchema(target.extend |
| 193 | .action(async ( |
| 194 | const { data, |
| 195 | "/api/repositories/{username}/{repo}/issues/{number}/labels", |
| 196 | { |
| 197 | params: { path: { |
| 198 | body: { names |
| 199 | headers: { cookie: ( |
| 200 | }, |
| 201 | ); |
| 202 | |
| 203 | if (error) throw |
| 204 | |
| 205 | revalidatePath(issuePath(username |
| 206 | return data; |
| 207 | }); |
| 208 | |
| 209 | export const setIssueAssignees = |
| 210 | .inputSchema(target.extend |
| 211 | .action(async ( |
| 212 | const { data, |
| 213 | "/api/repositories/{username}/{repo}/issues/{number}/assignees", |
| 214 | { |
| 215 | params: { path: { |
| 216 | body: { usernames |
| 217 | headers: { cookie: ( |
| 218 | }, |
| 219 | ); |
| 220 | |
| 221 | if (error) throw |
| 222 | |
| 223 | revalidatePath(issuePath(username |
| 224 | return data; |
| 225 | }); |
| 226 | |
| 227 | export const createLabel = |
| 228 | .inputSchema( |
| 229 | createLabelSchema.extend({ |
| 230 | ) |
| 231 | .action( |
| 232 | async ({ parsedInput |
| 233 | const { data, |
| 234 | "/api/repositories/{username}/{repo}/labels", |
| 235 | { |
| 236 | params: { path: { |
| 237 | body: { name, |
| 238 | headers: { cookie: ( |
| 239 | }, |
| 240 | ); |
| 241 | |
| 242 | if (error) throw |
| 243 | |
| 244 | revalidatePath(`/${username |
| 245 | // A label change can surface on any issue in the repository. |
| 246 | revalidatePath(issuePath(username |
| 247 | return data; |
| 248 | }, |
| 249 | ); |
| 250 | |
| 251 | export const updateLabel = |
| 252 | .inputSchema( |
| 253 | updateLabelSchema.extend({ |
| 254 | username: z.string |
| 255 | repo: z.string |
| 256 | labelId: z.string |
| 257 | }), |
| 258 | ) |
| 259 | .action( |
| 260 | async ({ |
| 261 | parsedInput: { username, |
| 262 | }) => { |
| 263 | const { data, |
| 264 | "/api/repositories/{username}/{repo}/labels/{labelId}", |
| 265 | { |
| 266 | params: { path: { |
| 267 | body: { name, |
| 268 | headers: { cookie: ( |
| 269 | }, |
| 270 | ); |
| 271 | |
| 272 | if (error) throw |
| 273 | |
| 274 | revalidatePath(`/${username |
| 275 | // A label change can surface on any issue in the repository. |
| 276 | revalidatePath(issuePath(username |
| 277 | return data; |
| 278 | }, |
| 279 | ); |
| 280 | |
| 281 | export const deleteLabel = |
| 282 | .inputSchema( |
| 283 | z.object({ |
| 284 | ) |
| 285 | .action(async ( |
| 286 | const { error |
| 287 | "/api/repositories/{username}/{repo}/labels/{labelId}", |
| 288 | { |
| 289 | params: { path: { |
| 290 | headers: { cookie: ( |
| 291 | }, |
| 292 | ); |
| 293 | |
| 294 | if (error) throw |
| 295 | |
| 296 | revalidatePath(`/${username |
| 297 | // A label change can surface on any issue in the repository. |
| 298 | revalidatePath(issuePath(username |
| 299 | }); |